Debian Codegraph @

envisage/ui/workbench/workbench_action_set.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ An action set in a workbench window. """
11
12
13 from traits.api import Instance, List, Str
14
15 # Enthought library imports.
16 from envisage.ui.action.api import ActionSet
17
18
19 class WorkbenchActionSet(ActionSet):
20 """An action set in a workbench window.
21
22 This class adds a 'window' trait which is the workbench window that the
23 action set is in. The trait is set by the framework when the action set is
24 added to the window.
25
26 It also adds a simple way for the action set to be enabled and/or visible
27 in specific perspectives.
28
29 """
30
31 ###########################################################################
32 # 'WorkbenchActionSet' interface.
33 ###########################################################################
34
35 # It is common for an action set to be enabled and/or visible only in a
36 # particular perspective (or group of perspectives). The following traits
37 # allow you to say which by specifiying a list of the appropriate
38 # perspective *Ids*.
39 #
40 # For finer control over the enablement/visibility simply override the
41 # 'initialize' method.
42 enabled_for_perspectives = List(Str)
43 visible_for_perspectives = List(Str)
44
45 # It is common for an action set to be enabled and/or visible only when
46 # particular view (or group of views) is visible. The following traits
47 # allow you to say which by specifiying a list of the appropriate view
48 # *Ids*.
49 #
50 # For finer control over the enablement/visibility simply override the
51 # 'initialize' method.
52 enabled_for_views = List(Str)
53 visible_for_views = List(Str)
54
55 # The workbench window that the action set is in.
56 #
57 # The framework sets this trait when the action set is first added to a
58 # window.
59 window = Instance("envisage.ui.workbench.api.WorkbenchWindow")
60
61 ###########################################################################
62 # 'ActionSet' interface.
63 ###########################################################################
64
65 def _enabled_changed(self, trait_name, old, new):
66 """Static trait change handler."""
67
68 if self.window is not None:
69 self._update_tool_bars(self.window, "enabled", new)
70 self._update_actions(self.window, "enabled", new)
71
72 def _visible_changed(self, trait_name, old, new):
73 """Static trait change handler."""
74
75 if self.window is not None:
76 self._update_tool_bars(self.window, "visible", new)
77 self._update_actions(self.window, "visible", new)
78
79 ###########################################################################
80 # 'WorkbenchActionSet' interface.
81 ###########################################################################
82
83 def initialize(self):
84 """Called when the action set has been added to a window.
85
86 Use this method to hook up any listeners that you need to control
87 the enabled and/or visible state of the action set.
88
89 By default, we listen to the window being opened and the active
90 perspective and active view being changed.
91
92 """
93
94 # We use dynamic trait handlers here instead of static handlers (or
95 # @on_trait_change) because sub-classes might have a completely
96 # different way to determine the anabled and/or visible state, hence
97 # we might want to hook up completely different events.
98 self.window.on_trait_change(self._refresh, "opened")
99 self.window.on_trait_change(self._refresh, "active_part")
100 self.window.on_trait_change(self._refresh, "active_perspective")
101
102 ###########################################################################
103 # Private interface.
104 ###########################################################################
105
106 #### Trait change handlers ################################################
107
108 def _window_changed(self):
109 """Static trait change handler."""
110
111 # fixme: We put the code into an 'initialize' method because it seems
112 # easier to explain that we expect it to be overridden. It seems a bit
113 # smelly to say that a trait change handfler needs to be overridden.
114 self.initialize()
115
116 #### Methods ##############################################################
117
118 def _refresh(self):
119 """Refresh the enabled/visible state of the action set."""
120
121 window = self.window
122
123 if len(self.enabled_for_perspectives) > 0:
124 self.enabled = (
125 window is not None
126 and window.active_perspective is not None
127 and window.active_perspective.id
128 in self.enabled_for_perspectives
129 )
130
131 if len(self.visible_for_perspectives) > 0:
132 self.visible = (
133 window is not None
134 and window.active_perspective is not None
135 and window.active_perspective.id
136 in self.visible_for_perspectives
137 )
138
139 if len(self.enabled_for_views) > 0:
140 self.enabled = (
141 window is not None
142 and window.active_part is not None
143 and window.active_part.id in self.enabled_for_views
144 )
145
146 if len(self.visible_for_views) > 0:
147 self.visible = (
148 window is not None
149 and window.active_part is not None
150 and window.active_part.id in self.visible_for_views
151 )
152
153 def _update_actions(self, window, trait_name, value):
154 """Update the state of the tool bars in the action set."""
155
156 def visitor(item):
157 """Called when we visit each item in an action manager."""
158
159 # fixme: The 'additions' group gets created by default and hence
160 # has no '_action_set_' attribute. This smells because of the fact
161 # that we 'tag' the '_action_set_' attribute onto all items to be
162 # ble to find them later. This link should be maintained externally
163 # (maybe in the action set itself?).
164 if hasattr(item, "_action_set_"):
165 if item._action_set_ is self:
166 setattr(item, trait_name, value)
167
168 # Update actions on the menu bar.
169 window.menu_bar_manager.walk(visitor)
170
171 # Update actions on the tool bars.
172 for tool_bar_manager in window.tool_bar_managers:
173 tool_bar_manager.walk(visitor)
174
175 def _update_tool_bars(self, window, trait_name, value):
176 """Update the state of the tool bars in the action set."""
177
178 for tool_bar_manager in window.tool_bar_managers:
179 if tool_bar_manager._action_set_ is self:
180 setattr(tool_bar_manager, trait_name, value)