From c6fca1dd739bf4089409cb8ee343345ca0e7f468 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Mon, 22 Aug 2011 21:05:38 +0100 Subject: [PATCH] Create some basic architecture to persist metadata across containers being removed/recreated as terminals are exiting. Fixes LP#711356 --- terminatorlib/container.py | 7 ++++++- terminatorlib/notebook.py | 34 +++++++++++++++++++++++++++++----- terminatorlib/paned.py | 7 +++++-- terminatorlib/window.py | 2 +- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/terminatorlib/container.py b/terminatorlib/container.py index d34c5347..35abf981 100755 --- a/terminatorlib/container.py +++ b/terminatorlib/container.py @@ -63,6 +63,11 @@ class Container(object): """Return a list of direct child widgets, if any""" return(self.children) + def get_child_metadata(self, widget): + """Return metadata that would be useful to recreate ourselves after our + child is .remove()d and .add()ed""" + return None + def split_horiz(self, widget, cwd=None): """Split this container horizontally""" return(self.split_axis(widget, True, cwd)) @@ -75,7 +80,7 @@ class Container(object): """Default axis splitter. This should be implemented by subclasses""" raise NotImplementedError('split_axis') - def add(self, widget): + def add(self, widget, metadata=None): """Add a widget to the container""" raise NotImplementedError('add') diff --git a/terminatorlib/notebook.py b/terminatorlib/notebook.py index 19824c51..bef0caf9 100755 --- a/terminatorlib/notebook.py +++ b/terminatorlib/notebook.py @@ -153,10 +153,10 @@ class Notebook(Container, gtk.Notebook): self.show_all() terminal.grab_focus() - def add(self, widget): + def add(self, widget, metadata=None): """Add a widget to the container""" dbg('adding a new tab') - self.newtab(widget=widget) + self.newtab(widget=widget, metadata=metadata) def remove(self, widget): """Remove a widget from the container""" @@ -176,6 +176,18 @@ class Notebook(Container, gtk.Notebook): self.add(newwidget) self.reorder_child(newwidget, page_num) + def get_child_metadata(self, widget): + """Fetch the relevant metadata for a widget which we'd need + to recreate it when it's readded""" + metadata = {} + metadata['tabnum'] = self.page_num(widget) + label = self.get_tab_label(widget) + if not label: + dbg('unable to find label for widget: %s' % widget) + else: + metadata['label'] = label.get_label() + return metadata + def get_children(self): """Return an ordered list of our children""" children = [] @@ -183,7 +195,7 @@ class Notebook(Container, gtk.Notebook): children.append(self.get_nth_page(page)) return(children) - def newtab(self, debugtab=False, widget=None, cwd=None): + def newtab(self, debugtab=False, widget=None, cwd=None, metadata=None): """Add a new tab, optionally supplying a child widget""" dbg('making a new tab') maker = Factory() @@ -218,20 +230,29 @@ class Notebook(Container, gtk.Notebook): handler = handler[0] self.connect_child(widget, signal, handler, *args) + if metadata and metadata.has_key('tabnum'): + tabpos = metadata['tabnum'] + else: + tabpos = -1 + label = TabLabel(self.window.get_title(), self) + if metadata and metadata.has_key('label'): + dbg('creating TabLabel with text: %s' % metadata['label']) + label.set_custom_label(metadata['label']) label.connect('close-clicked', self.closetab) label.show_all() widget.show_all() - self.append_page(widget, None) + dbg('inserting page at position: %s' % tabpos) + self.insert_page(widget, None, tabpos) self.set_tab_label(widget, label) self.set_tab_label_packing(widget, not self.config['scroll_tabbar'], not self.config['scroll_tabbar'], gtk.PACK_START) self.set_tab_reorderable(widget, True) - self.set_current_page(-1) + self.set_current_page(tabpos) self.show_all() if maker.isinstance(widget, 'Terminal'): widget.grab_focus() @@ -395,6 +416,9 @@ class TabLabel(gtk.HBox): """Update the text of our label""" self.label.set_text(text) + def get_label(self): + return self.label.get_text() + def set_custom_label(self, text): """Set a permanent label as if the user had edited it""" self.label.set_text(text) diff --git a/terminatorlib/paned.py b/terminatorlib/paned.py index 5f44b950..2349a529 100755 --- a/terminatorlib/paned.py +++ b/terminatorlib/paned.py @@ -73,7 +73,7 @@ class Paned(Container): self.show_all() - def add(self, widget): + def add(self, widget, metadata=None): """Add a widget to the container""" if len(self.children) == 0: self.pack1(widget, True, True) @@ -143,10 +143,13 @@ class Paned(Container): sibling = self.children[0] self.remove(sibling) + metadata = None parent = self.get_parent() + metadata = parent.get_child_metadata(self) + dbg('metadata obtained for %s: %s' % (self, metadata)) parent.remove(self) self.cnxids.remove_all() - parent.add(sibling) + parent.add(sibling, metadata) sibling.grab_focus() del(self) else: diff --git a/terminatorlib/window.py b/terminatorlib/window.py index b7a6f3d7..8fb4fbf7 100755 --- a/terminatorlib/window.py +++ b/terminatorlib/window.py @@ -355,7 +355,7 @@ class Window(Container, gtk.Window): self.hide() - def add(self, widget): + def add(self, widget, metadata=None): """Add a widget to the window by way of gtk.Window.add()""" maker = Factory() gtk.Window.add(self, widget)