Coverage for src/robotide/action/action.py: 75%
82 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
1# Copyright 2008-2015 Nokia Networks
2# Copyright 2016- Robot Framework Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
17def action_factory(action_info): 1ac
18 if action_info.is_separator(): 1abd
19 return _MenuSeparator(action_info) 1abd
20 return Action(action_info) 1ab
23class _Registrable(object): 1ac
25 def __init__(self, action_info): 1ac
26 self._registered_to = [] 1abd
27 self.action = None 1abd
28 self.shortcut = None 1abd
29 self.icon = None 1abd
30 self._insertion_point = action_info.insertion_point 1abd
31 self._enabled = True 1abd
32 self._enabled_status_listeners = [] 1abd
34 disable = enable = lambda event: None 1ac
36 def is_separator(self): 1ac
37 return False 1abd
39 def get_insertion_index(self, menu): 1ac
40 return self._insertion_point.get_index(menu) 1abd
42 def register(self, registerer): 1ac
43 self._registered_to.append(registerer) 1abd
45 def unregister(self): 1ac
46 for registerer in self._registered_to:
47 registerer.unregister(self)
48 self._registered_to = []
50 def has_action(self): 1ac
51 return self.action is not None 1ab
53 def has_shortcut(self): 1ac
54 return bool(self.shortcut) 1ab
56 def has_icon(self): 1ac
57 return self.icon is not None 1ab
59 def inform_changes_in_enabled_status(self, listener): 1ac
60 self._enabled_status_listeners.append(listener)
63class Action(_Registrable): 1ac
64 """Acts based on user actions if action is enabled. Created from `ActionInfo`.
66 If `ActionInfo` contains container, acts and allows to select related UI
67 widgets (menu item, toolbar button) and shortcuts only if the focus is in the given
68 container or its children.
69 Action can be set enabled or disabled which enables/disables also related UI
70 widgets and shortcut.
71 """
73 def __init__(self, action_info): 1ac
74 _Registrable.__init__(self, action_info) 1ab
75 self.menu_name = action_info.menu_name 1ab
76 self.name = action_info.name 1ab
77 self.action = action_info.action 1ab
78 self.container = action_info.container 1ab
79 self.shortcut = action_info.shortcut 1ab
80 self.icon = action_info.icon 1ab
81 self.doc = action_info.doc 1ab
82 # print("DEBUG: Action: %s::%s" % (self.menu_name,self.name))
84 def get_shortcut(self): 1ac
85 return self.shortcut.printable 1abd
87 def act(self, event): 1ac
88 if self.is_active():
89 self.action(event)
91 def disable(self): 1ac
92 """Disables action and related menu item, toolbar button and shortcut"""
93 self._enabled = False
94 self._inform_changes_in_enabled_status()
96 def enable(self): 1ac
97 """Enables action and related menu item, toolbar button and shortcut"""
98 self._enabled = True
99 self._inform_changes_in_enabled_status()
101 def _inform_changes_in_enabled_status(self): 1ac
102 for listener in self._enabled_status_listeners:
103 listener.enabled_status_changed(self)
105 def is_active(self): 1ac
106 if self._is_always_inactive() or not self._enabled:
107 return False
108 if self._is_always_active(): 108 ↛ 110line 108 didn't jump to line 110 because the condition on line 108 was always true
109 return True
110 return self._container_is_active()
112 def _is_always_inactive(self): 1ac
113 return self.action is None
115 def _is_always_active(self): 1ac
116 return self.container is None
118 def _container_is_active(self): 1ac
119 if not self.container.IsShownOnScreen():
120 return False
121 widget = self.container.FindFocus()
122 while widget:
123 if widget == self.container.Parent:
124 return True
125 widget = widget.GetParent()
126 return False
129class _MenuSeparator(_Registrable): 1ac
131 def __init__(self, action_info): 1ac
132 _Registrable.__init__(self, action_info) 1abd
133 self.menu_name = action_info.menu_name 1abd
134 self.name = '---' 1abd
136 def is_separator(self): 1ac
137 return True 1abd