Source

Kephra / lib / Kephra / App / Part / Editor.pm

use v5.10;
use warnings;
use Cwd;
use File::Find;
use File::Spec;
use Kephra::App::Panel;
use Kephra::App::Bar::Document;
use Kephra::App::Splitter;

package Kephra::App::Part::Editor;
our @ISA = 'Kephra::App::Panel';
my ($active_bar, @passive_bar, $panel, %splitter);
use Scalar::Util qw(blessed);

Kephra::API::Command::register({
 'docbar-prev' =>{sub=>'select_docbar_prev', label=>'Prev Docbar',  keys=>'alt+pageup'},
 'docbar-next' =>{sub=>'select_docbar_next', label=>'Next Docbar',  keys=>'alt+pagedown'},
 'docbar-add'  =>{sub=>'select_docbar_add',  label=>'Prev Docbar',  keys=>'ctrl+alt+pagedown'},
 'docbar-del'  =>{sub=>'select_docbar_del',  label=>'Next Docbar',  keys=>'ctrl+alt+pageup'},
 'docbar-resize-left' =>{sub=>'resize_left', label=>'',  keys=>'alt+plus'},
 'docbar-resize-right'=>{sub=>'resize_right',label=>'',  keys=>'alt+minus'},
});


sub docbar         { $active_bar }
sub passive_docbars{ @passive_bar}
sub all_docbars    { $active_bar, @passive_bar }

sub new {
	my( $class, $parent) = @_;
	my $self = $panel = $class->SUPER::new($parent);

	$active_bar = Kephra::App::Bar::Document->new($self);
	@passive_bar = Kephra::App::Bar::Document->new($self);

	my $splitter = $splitter{'main'} = Kephra::App::Splitter->new({ parent => $self,
		left => $active_bar, right => $passive_bar[-1], dominant_child => 'left' });
	$self->append( \$splitter );
	#$splitter->resize('equal');
	$self;
}

sub select_docbar_del {
	my $docbar_count = scalar @passive_bar + 1;
	return if $docbar_count < 2;
	if    ($docbar_count == 4){
		
	}
	elsif ($docbar_count == 3){
		
	}
	elsif ($docbar_count == 2){
		$splitter{'main'}->set({dominant_child => $active_bar});
		$splitter{'main'}->unsplit();
	}
	$passive_bar[-1]->Destroy();
	delete $passive_bar[-1];
	Kephra::API::focus( $active_bar->GetPage( $active_bar->GetSelection ) );
}

sub select_docbar_add {
	my $docbar_count = scalar @passive_bar + 1;
	return if $docbar_count > 3;

	if    ($docbar_count == 3){
		
	}
	elsif ($docbar_count == 2){
		
	}
	elsif ($docbar_count == 1){
		
		push @passive_bar, Kephra::App::Bar::Document->new( $splitter{'main'} );
		$splitter{'main'}->set(
			{left => $active_bar, right => $passive_bar[0], dominant_child => 'left'}
		);
		$splitter{'main'}->resize('equal');
		#$splitter{'main'}->resplit();
		#$passive_bar[0]->Show(1);
	}
	Kephra::API::focus( $active_bar->GetPage( $active_bar->GetSelection ) );
}


sub select_docbar_next {
	push @passive_bar, $active_bar;
	$active_bar = shift @passive_bar;
	$active_bar->raise_page( $active_bar->GetSelection );
}
sub select_docbar_prev {
	unshift @passive_bar, $active_bar;
	$active_bar = pop @passive_bar;
	$active_bar->raise_page( $active_bar->GetSelection );
}
sub select_docbar {
	my $bar = shift;
	return Kephra::Log::warning("need a Kephra::App::Bar::Document instance")
		unless ref $bar eq 'Kephra::App::Bar::Document';
	return if $active_bar eq $bar;
	my $i;
	for (0 ..  $#passive_bar){ $i = $_ if $bar eq $passive_bar[$_] }
	return Kephra::Log::warning("$bar is unknown") unless defined $i;

	@passive_bar = (splice(@passive_bar, $i+1), $active_bar, splice(@passive_bar, 0, $i));
	$active_bar = $bar;
	$active_bar->raise_page( $active_bar->GetSelection );
}


sub resize { $splitter{'main'}->resize('equal') }

sub resize_left { $splitter{'main'}
}
sub resize_right { $splitter{'main'}
}


sub refresh_doc_label {
	my $doc = shift // Kephra::API::document();
	return unless Kephra::API::Doc::is($doc);
	my $unsaved_mark = '*';

	my $title = $doc->title;
	$title .= $unsaved_mark if $doc->editor->{$active_bar}->GetModify;
	$_->SetPageText($_->GetPageIndex( $doc->panel->{$_} ), $title) for all_docbars();
}

1;