Commits

Herbert Breunung committed b9cfdfa

parallel docbar label management

Comments (0)

Files changed (7)

lib/Kephra/API.pm

 });
 Kephra::API::Command::init();
 
-sub app                { $Kephra::App::_ref }
-sub main_window        { $Kephra::App::Frame::_ref }
-sub docbar             { Kephra::App::Part::Editor::docbar() }
-sub all_docbars        { Kephra::App::Part::Editor::all_docbars() }
-sub passive_docbars    { Kephra::App::Part::Editor::passive_docbars() }
-sub document           { Kephra::API::Docs::active() }
-sub all_documents      { Kephra::API::Docs::all() }
-sub do_on_all_documents{ }
-sub editor             { Kephra::API::Docs::active_editor() }
+sub app                 { $Kephra::App::_ref }
+sub main_window         { $Kephra::App::Frame::_ref }
+sub docbar              { Kephra::App::Part::Editor::docbar() }
+sub all_docbars         { Kephra::App::Part::Editor::all_docbars() }
+sub passive_docbars     { Kephra::App::Part::Editor::passive_docbars() }
+sub document            { Kephra::API::Docs::active() }
+sub all_documents       { Kephra::API::Docs::all() }
+sub do_with_all_editors {&Kephra::API::Docs::do_with_all_editors }
+sub editor              { Kephra::API::Docs::active_editor() }
 
-sub focus              { Kephra::App::Focus::set(@_) }
-sub focus_back         { Kephra::App::Focus::set_back(@_) }
+sub focus               { Kephra::App::Focus::set(@_) }
+sub focus_back          { Kephra::App::Focus::set_back(@_) }
 
 # IO-unit
 #sub input  { main_window()->{'part'}{'iounit'}->get_line() if ref main_window() and exists main_window()->{'part'}{'iounit'} }

lib/Kephra/API/Docs.pm

 my %document;                           # keys of subhashes: ID editor panel file
 my $active;
 my $lowest_free_ID = 1;
+my $lowest_free_anon_NR = 1;
+
 use constant DOC_CLASS => 'Kephra::Document';
 use Scalar::Util qw(looks_like_number);
 
 sub previous_doc  { App::Kephra::Focus::last( DOC_CLASS ) }
 sub all           { values %{$document{'ID'}} }
 
-
+sub is_doc { return 1 if ref $_[0] and $_[0]->isa( DOC_CLASS ); return 0; }
 
 sub add {
 	my $doc = shift;
 	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
-		unless ref $doc and $doc->isa( DOC_CLASS );
+		unless is_doc($doc);
 	$document{'ID'}  {$lowest_free_ID} = $doc;
 	$document{'panel'}{$_} = $doc for values $doc->panel;
 	$document{'editor'}{$_} = $doc for values $doc->editor;
-	$document{'file'}{$doc->file_path} = $doc if $doc->file_path;
+	if ($doc->file_path) {
+		$document{'file'}{$doc->file_path} = $doc;
+	} else {
+		$doc->anon_NR( $lowest_free_anon_NR++ );
+		$document{'anon'}{$doc->anon_NR} = $doc;
+	}
 	$lowest_free_ID++;
 }
 
 sub rename_file {
 	my $doc = find( shift );
 	Kephra::Log::warning('need a ' . DOC_CLASS . " thats in stash, not $doc!", 1)
-		unless ref $doc and $doc->isa( DOC_CLASS );
+		unless is_doc($doc);
 	for (keys %{$document{'file'}} ){
 		delete $document{'file'}{$_} if $document{'file'}{$_} eq $doc;
 	}
-	$document{'file'}{$doc->file_path} = $doc if $doc->file_path;
+	if ($doc->file_path){
+		$document{'file'}{$doc->file_path} = $doc;
+		unless ($doc->old_file_path) {
+			delete $document{'anon'}{$doc->anon_NR};
+			$doc->anon_NR(0);
+		}
+	}
 }
 
 sub remove {
 	my $doc = shift;
 #say "remove $doc ", scalar keys %{$document{'ID'}}, ' ',scalar keys %{$document{'file'}}; 
 	Kephra::Log::warning('need a ' . DOC_CLASS . ' thats in stash, not $doc!', 1)
-		unless ref $doc and $doc->isa( DOC_CLASS );
+		unless is_doc($doc);
 	delete $document{'ID'}{$doc->ID};
 #	delete $document{'panel'}{$_}    for values $doc->panel;
 #	delete $document{'editor'}{$_}   for values $doc->editor;
 	}
 }
 
-sub dump_all_ed { say $_ for keys %{$document{'editor'}} }
+sub all_editors { keys %{$document{'editor'}} }
+sub do_with_all_editors {
+}
 
-
+sub add_bar{}
+sub remove_bar{}
 1;
 

lib/Kephra/App/Bar/Document.pm

 package Kephra::App::Bar::Document;
 our @ISA = 'Wx::AuiNotebook';
 our $_ref;
-my $lowest_free_untitled_ID = 1;
 
+use Scalar::Util qw(blessed);
 
 Kephra::API::Command::register({
  'docbar-select-left'     =>{sub=>'$docbar->select_page_left',     label=>'Previous Tab',  keys=>'ctrl+pageup'},
 
 	$new_page->Reparent($self);
 	$self->InsertPage( $position, $new_page, $title, $set_active);
-	$self->set_page_title( $title, $new_page );
+	#$self->set_page_title( $title, $new_page );
 	Kephra::API::focus($new_page) if $set_active;
 	Kephra::API::main_window()->refresh_title() if $set_active;
 
 
 sub set_page_title {
 	my ($self, $label, $page) = @_;
-	return if ref $page and $page ne ref $page and $page->isa(' Wx::Window');
+	return if blessed($page) and $page->isa('Kephra::App::Panel');
 	my $position = $page ? $self->GetPageIndex($page) : $self->GetSelection;
 	return unless $self->valid_page_pos($position);
-	$label = '<untitled '.($lowest_free_untitled_ID++).'>' unless $label;
-	my $max_width = 15;
-	if ( length($label) > $max_width and $max_width > 7 ) {
-		$label = substr( $label, 0, $max_width - 2 ) . '..';
-	}
 	$self->SetPageText( $position, $label );
 }
-sub refresh_page_title {
-	my $self = shift;
-	my $doc = shift // Kephra::API::document();
-	my $unsaved_mark = '*';
-	my $position = $self->GetSelection;
-	my $label = $self->GetPageText( $position);
-	chop $label if substr($label,-1) eq $unsaved_mark;
-	$label .= $unsaved_mark if $doc->editor->{$self}->GetModify;
-	$self->SetPageText($position, $label);
-}
-
 
 
 1;

lib/Kephra/App/Editor.pm

 	#Wx::Event::EVT_RIGHT_DOWN
 	#Wx::Event::EVT_STC_UPDATEUI
 	Wx::Event::EVT_STC_SAVEPOINTREACHED
-		($self, -1, sub { Kephra::API::docbar()->refresh_page_title() });
+		($self, -1, sub { Kephra::App::Part::Editor::refresh_doc_label() });
 	Wx::Event::EVT_STC_SAVEPOINTLEFT
-		($self, -1, sub { Kephra::API::docbar()->refresh_page_title() });
+		($self, -1, sub { Kephra::App::Part::Editor::refresh_doc_label() });
 	#Wx::Event::EVT_DROP_FILES       ($ep, sub{});
 	#Wx::Event::EVT_STC_START_DRAG   ($ep, -1, sub {
 	#Wx::Event::EVT_STC_DRAG_OVER    ($ep, -1, sub { $droppos = $_[1]->GetPosition });

lib/Kephra/App/Part/Editor.pm

 
 package Kephra::App::Part::Editor;
 our @ISA = 'Kephra::App::Panel';
-my ($active, @passive, $panel);
+my ($active_bar, @passive_bar, $panel);
 
-sub docbar         { $active }
-sub passive_docbars{ @passive}
-sub all_docbars    { ($active, @passive) }
+sub docbar         { $active_bar }
+sub passive_docbars{ @passive_bar}
+sub all_docbars    { ($active_bar, @passive_bar) }
 
 sub new {
 	my( $class, $parent) = @_;
 	$self->append( \$splitter );
 	$splitter->resize('equal');
 
-	$active = $db2;
-	@passive = ($db);
+	$active_bar = $db2;
+	@passive_bar = ($db);
 
 	$self;
 }
 	
 }
 
+sub refresh_doc_label {
+	my $doc = shift // Kephra::API::document();
+	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;

lib/Kephra/Document.pm

 
 package Kephra::Document;
 use Moo; 
+my $max_title_width = 15;
+
 # export SUB_QUOTE_DEBUG=1
 
 
-has ID     => ( is  => 'rwp' );
+has ID     => ( is  => 'rwp' );                             # API::Docs ID
+has title  => ( is  => 'rwp' );                             # shown as tab label
 has editor => ( is  => 'ro' );
 has panel  => ( is  => 'ro' );
-has text   => ( is  => 'ro', );
-has notes  => ( is  => 'rw', );
+has anon_NR=> ( is  => 'rw' );
 has file_path=>( 
 	is      => 'rw',
 	coerce  =>  sub { Kephra::File::normalize_path($_[0]) },
 	trigger =>  sub {
-		return if $_[0]->old_file_path and $_[0]->old_file_path eq $_[0]->file_path;
-		my @path_parts = File::Spec->splitpath( $_[0]->file_path );
-		$_[0]->_set_file_dir( $path_parts[1] );
-		$_[0]->_set_file_name( $path_parts[2] );
-		Kephra::API::Docs::rename_file($_[0]) if $_[0]->ID;
-		$_[0]->_set_old_file_path( $_[0]->file_path );
+		my $self = shift;
+		return if $self->old_file_path and $self->old_file_path eq $self->file_path;
+		my @path_parts = File::Spec->splitpath( $self->file_path );
+		$self->_set_file_dir( $path_parts[1] );
+		$self->_set_file_name( $path_parts[2] );
+		Kephra::API::Docs::rename_file($self) if $self->ID;
+
+		if ($self->file_path) {
+			my $title = $path_parts[2];
+			$title = substr( $title, 0, $max_title_width - 2 ) . '..'
+				if length($title) > $max_title_width and $max_title_width > 7;
+			$self->_set_title($title);
+		}
+
+		$self->_set_old_file_path( $self->file_path );
 	});
 has file_dir     => ( is => 'rwp', );
 has file_name    => ( is => 'rwp', );
 has old_file_path=> ( is => 'rwp' );
 has content      => ( is => 'rw', reader=> sub{ $_[0] } );
+has notes        => ( is => 'rw', );
 
 has syntax_mode  => ( is => 'rw', default => sub { 'perl' } );
 has readonly     => ( is => 'rw', );
 	return \ %args;
 };
 
-sub BUILD    { $_[0]->_set_ID( Kephra::API::Docs::add($_[0]) ) }
+sub BUILD    {
+	my $self = shift;
+	$self->_set_ID( Kephra::API::Docs::add($self) );
+	$self->_set_title('<untitled '.$self->anon_NR.'>') unless $self->file_path;
+}
 sub DEMOLISH { Kephra::API::Docs::remove($_[0]); #say "demolish $_[0]"; 
 }
 

lib/Kephra/File.pm

 sub new  {
 	my $doc = Kephra::Document->new();
 	my $docbar = Kephra::API::docbar();
-	$_->add_page( $doc->panel->{$_},'rightmost','',1 ) for Kephra::API::all_docbars();
+	$_->add_page( $doc->panel->{$_}, 'rightmost', $doc->title, 0 )
+		for Kephra::API::all_docbars();
 	my $content_ref = $doc->editor->{$docbar}->GetDocPointer();
 	$doc->editor->{$_}->SetDocPointer( $content_ref ) for Kephra::API::passive_docbars();
 }
 			$db->set_page_title( $doc->file_name, $doc->panel->{$db} );
 		}
 		Kephra::API::app()->Yield;
-		#$ed->unmount_events();
 		Kephra::File::Local::read( $doc, $file );
+		$ed = Kephra::API::editor();
 		$ed->EmptyUndoBuffer;
 		#$ed->mount_events();
 	}
 
 sub save {
 	$_[0] = Kephra::API::document() unless @_;
+	my $docbar = Kephra::API::docbar();
+
 	for my $doc (@_){
 		next unless ref $doc eq 'Kephra::Document';
-		next unless $doc->editor->GetModify;
+		next unless $doc->editor->{$docbar}->GetModify;
 		if ($doc->file_path){
 			Kephra::File::Local::write($doc);
 		} else {
 
 sub close {
 	$_[0] = Kephra::API::document() unless @_;
+	my $active_bar = Kephra::API::docbar();
 
 	for my $doc (@_) {
 		next unless ref $doc eq 'Kephra::Document';
-		my $ed = $doc->editor;
+		my $ed = $doc->editor->{ $active_bar };
 		if ($ed->GetModify) {
 			my $save_answer = Kephra::App::Dialog::yes_no_cancel (
 				'close unsaved file', 'do you want to save it before?'
 			next if $save_answer == &Wx::wxCANCEL;
 			save($doc) if $save_answer == &Wx::wxYES;
 		}
-		my $bar = Kephra::API::docbar();
-		if ($bar->GetPageCount > 1){
-			$bar->remove_page( $doc->panel );
+		if ($active_bar->GetPageCount > 1){
+			$_->remove_page( $doc->panel->{$_} ) for Kephra::API::all_docbars();
 			$doc->DESTROY;
 		} else {
 			next unless $ed->GetLength;
 			$ed->ClearAll();
 			$ed->SetSavePoint;
 			$ed->EmptyUndoBuffer;
-			$bar->set_page_title('');
+			$active_bar->set_page_title('');
 		}
 		Kephra::API::main_window()->refresh_title();
 	}