Source

Kephra / lib / Kephra / App / Focus.pm

Full commit
use v5.10;
use strict;
use warnings;
use Wx;
use Kephra::API;
use Kephra::App::Util;

package Kephra::App::Focus;
my @focus_stack = ();
my $max_size = 50;

#Kephra::CommandList::register_cmd({'app-focus-set-back'  =>{sub=>'set_back',label=>'',keys=>''},});


sub get  {                             # give widget in focus or one before
	my ($nr) = shift // 0;
	$focus_stack[$nr] unless $nr < @focus_stack or $nr < 0 and $nr > @focus_stack;
}

sub last {                             # give me last widget of a class in focus
	my $class = shift;
	return Kephra::Log::warning( "need a class name", 1 ) unless $class;
	for (@focus_stack) { return $_ if ref $_ eq $class }
}

sub set  {                              # set focus to another widget
	my ($widget) = @_;
	Kephra::Log::note("focus is on :". get()." and will be: $widget");
	return Kephra::Log::warning( "got no proper widget but $widget", 1 )
		unless Kephra::App::Util::is_widget($widget);
	return if get() and $widget eq get();
	unshift @focus_stack, $widget;
	pop @focus_stack if @focus_stack > $max_size;
	stay();
}

sub set_back {                          # go with focus n (default 1) steps back
	my ($times) = shift // 1;
	for (1 .. $times) { shift @focus_stack if @focus_stack > 1 }
	stay();
}

sub stay {Wx::Window::SetFocus(get())}  # reset focus to last entry

sub set_max_size { $max_size = shift }  # set maximum number of memorized widgets in focus

1;