OSDN Git Service

layout: track opens & closes of all subwindows
authorAndrew Chadwick <andrewc-git@piffle.org>
Tue, 26 Jul 2011 20:38:09 +0000 (21:38 +0100)
committerBen O'Steen <bosteen@gmail.com>
Mon, 1 Aug 2011 12:53:50 +0000 (13:53 +0100)
Keep the new ToggleActions in sync with actual window states for the
prefs window, the background selection window, and the brush editor.

gui/drawwindow.py
gui/layout.py

index 2e6b584..ff1583b 100644 (file)
@@ -282,20 +282,14 @@ class Window (windowing.MainWindow, layout.MainWindow):
             visible = not lm.get_window_hidden_by_role(role)
             spec.append(visible)
             toggle_actions[i] = tuple(spec)
-
         ag.add_toggle_actions(toggle_actions)
 
         # Reflect changes from other places (like tools' close buttons) into
         # the proxys' visible states.
-        self.tool_toggle_items = {}
-        for tup in toggle_actions:
-            name = tup[0]
-            role = name[0].lower() + name[1:]
-            action = ag.get_action(name)
-            tool_item = action.create_tool_item()
-            self.tool_toggle_items[role] = tool_item
-        self.app.layout_manager.tool_visibility_observers\
-            .append(self.on_tool_visibility_changed)
+        lm.tool_visibility_observers.append(
+                self.on_toggle_item_visibility_changed)
+        lm.subwindow_visibility_observers.append(
+                self.on_subwindow_visibility_changed)
 
         # More toggle actions - ones which don't control windows.
         toggle_actions = [
@@ -382,9 +376,10 @@ class Window (windowing.MainWindow, layout.MainWindow):
         expander.set_expand(True)
         bar.insert(expander, -1)
 
-        for role in ["colorSelectionWindow", "colorSamplerWindow",
-                     "brushSelectionWindow", "layersWindow"]:
-            tool_item = self.tool_toggle_items[role]
+        for name in ["ColorSelectionWindow", "ColorSamplerWindow",
+                     "BrushSelectionWindow", "LayersWindow"]:
+            action = self.action_group.get_action(name)
+            tool_item = action.create_tool_item()
             bar.insert(tool_item, -1)
         self.toolbar = bar
 
@@ -564,7 +559,7 @@ class Window (windowing.MainWindow, layout.MainWindow):
         # If it's a tool, get it to hide/itself
         t = self.app.layout_manager.get_tool_by_role(window_name)
         if t is not None:
-            t.set_hidden(hidden=not active, reason="toggle-window-cb")
+            t.set_hidden(not active)
             return
         # Otherwise, if it's a regular subwindow hide/show+present it.
         w = self.app.layout_manager.get_subwindow_by_role(window_name)
@@ -581,13 +576,22 @@ class Window (windowing.MainWindow, layout.MainWindow):
                 return
             w.hide()
 
-    def on_tool_visibility_changed(self, role, active, reason, temporary):
-        if reason is "toggle-window-cb":
-            return
-        tool_item = self.tool_toggle_items.get(role, None)
-        if tool_item is None:
+    def on_subwindow_visibility_changed(self, window, active):
+        # Responds to non-tool subwindows being hidden and shown
+        role = window.get_role()
+        self.on_toggle_item_visibility_changed(role, active)
+
+    def on_toggle_item_visibility_changed(self, role, active, *a, **kw):
+        # Responds to any item with a role being hidden or shown by
+        # silently updating its ToggleAction to match.
+        action_name = role[0].upper() + role[1:]
+        action = self.action_group.get_action(action_name)
+        if action is None:
+            warn("Unable to find action %s" % action_name, RuntimeWarning, 1)
             return
-        tool_item.set_active(active)
+        action.block_activate()
+        action.set_active(active)
+        action.unblock_activate()
 
     def popup_cb(self, action):
         state = self.popup_states[action.get_name()]
index 730c720..ffaeab1 100644 (file)
@@ -65,6 +65,7 @@ class LayoutManager:
         self.main_window = None
         self.saved_user_tools = []
         self.tool_visibility_observers = []
+        self.subwindow_visibility_observers = []
 
     def set_main_window_title(self, title):
         """Set the title for the main window.
@@ -101,6 +102,9 @@ class LayoutManager:
                     self.widgets[role] = widget
                     widget.set_role(role)
                     widget.set_transient_for(self.main_window)
+                    cb = self.notify_subwindow_visibility_observers
+                    widget.connect("show", cb, True)
+                    widget.connect("hide", cb, False)
                     return widget
                 elif isinstance(widget, gtk.Widget):
                     title = result[1]
@@ -171,14 +175,14 @@ class LayoutManager:
             off = not on
         if on or self.saved_user_tools:
             for tool in self.saved_user_tools:
-                tool.set_hidden(False, temporary=True, reason="toggle-user-tools")
+                tool.set_hidden(False, temporary=True)
                 tool.set_floating(tool.floating)
             self.saved_user_tools = []
         elif off or not self.saved_user_tools:
             for tool in self.get_tools_in_sbindex_order():
                 if tool.hidden:
                     continue
-                tool.set_hidden(True, temporary=True, reason="toggle-user-tools")
+                tool.set_hidden(True, temporary=True)
                 self.saved_user_tools.append(tool)
 
     def show_all(self):
@@ -216,6 +220,10 @@ class LayoutManager:
         for func in self.tool_visibility_observers:
             func(*args, **kwargs)
 
+    def notify_subwindow_visibility_observers(self, *args, **kwargs):
+        for func in self.subwindow_visibility_observers:
+            func(*args, **kwargs)
+
 
 class ElasticContainer:
     """Mixin for containers which mirror certain size changes of descendents
@@ -1096,7 +1104,7 @@ class Tool (gtk.VBox, ElasticContainer):
                 lm.main_window.sidebar.hide()
             self.resize_grip_frame.set_shadow_type(gtk.SHADOW_OUT)
 
-    def set_hidden(self, hidden, reason=None, temporary=False):
+    def set_hidden(self, hidden, temporary=False):
         """Sets a tool as hidden, hiding or showing it as appropriate.
         
         Note that this does not affect whether the window is floating or not
@@ -1122,7 +1130,7 @@ class Tool (gtk.VBox, ElasticContainer):
             # Which will restore it to the correct state
         self.hidden = hidden
         lm.notify_tool_visibility_observers(role=self.role, active=not hidden,
-                                            reason=reason, temporary=temporary)
+                                            temporary=temporary)
         if not temporary:
             lm.prefs[role]["hidden"] = hidden
         if lm.main_window.sidebar.is_empty():