Coverage for src/robotide/widgets/popupmenu.py: 58%
69 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.
16import wx 1ab
19class PopupCreator(object): 1ab
21 def __init__(self): 1ab
22 self._external_hooks = []
24 def add_hook(self, hook): 1ab
25 self._external_hooks.append(hook)
27 def remove_hook(self, hook): 1ab
28 self._external_hooks.remove(hook)
30 def _get_all_actions(self, fixed_menu_items, data): 1ab
31 menu_items = fixed_menu_items
32 external_items = self._get_external_menu_items(data)
33 if external_items:
34 menu_items.add_separator()
35 for item in external_items:
36 menu_items.add_menu_item(item)
37 return menu_items
39 def _get_external_menu_items(self, data): 1ab
40 menu_items = []
41 for hook in self._external_hooks:
42 menu_items.extend(hook(data))
43 return menu_items
45 def show(self, parent, fixed_menu_items, data): 1ab
46 PopupMenu(parent, self._get_all_actions(fixed_menu_items, data))
49class PopupMenu(wx.Menu): 1ab
51 def __init__(self, parent, menu_items): 1ab
52 wx.Menu.__init__(self)
53 """
54 parent.SetBackgroundColour(Colour(200, 222, 40))
55 parent.SetOwnBackgroundColour(Colour(200, 222, 40))
56 parent.SetForegroundColour(Colour(7, 0, 70))
57 parent.SetOwnForegroundColour(Colour(7, 0, 70))
58 """
59 for item in menu_items:
60 if item.is_separator():
61 self.AppendSeparator()
62 else:
63 self._add_item(item)
64 parent.PopupMenu(self)
65 self.Destroy()
67 def _add_item(self, item): 1ab
68 id_ = wx.NewIdRef()
69 self.Append(id_, item.name)
70 self.Bind(wx.EVT_MENU, item.callable, id=id_)
73class PopupMenuItems(object): 1ab
75 def __init__(self, parent=None, menu_names=None, menu_names_nt=None): 1ab
76 self._items = [] 1cedi
77 if menu_names is None: 1cedi
78 menu_names = [] 1ei
79 if not menu_names_nt: 79 ↛ 81line 79 didn't jump to line 81 because the condition on line 79 was always true1cedi
80 menu_names_nt = menu_names 1cedi
81 for item, item_nt in zip(menu_names, menu_names_nt): 1cedi
82 # print(f"DEBUG: PopupMenuItems value of item={item} item_nt={item_nt}")
83 self.add_menu_item(PopupMenuItem(item, name_nt=item_nt, parent=parent)) 1cd
85 def __iter__(self): 1ab
86 return iter(self._items)
88 def add_menu_item(self, item): 1ab
89 self._items.append(item) 1ced
91 def add_separator(self): 1ab
92 self.add_menu_item(PopupMenuItem('---')) 1e
95class PopupMenuItem(object): 1ab
97 def __init__(self, name, name_nt=None, ccallable=None, parent=None): 1ab
98 self.name = name 1hjfgced
99 nname = name_nt if name_nt else name 1hjfgced
100 # print(f"DEBUG: PopupMenuItem value of nname={nname}")
101 self.callable = self._get_callable(nname, ccallable, parent) 1hjfgced
102 # print(f"DEBUG: PopupMenuItem value of name_nt={name_nt} callable={self.callable}")
104 @staticmethod 1ab
105 def _get_callable(name, ccallable, parent): 1ab
106 from ..action.actioninfo import get_eventhandler_name_and_parsed_name 1hjfgced
107 if ccallable: 1hjfgced
108 return ccallable 1jc
109 if name == '---': 1hfgced
110 return None 1he
111 new_name = name.split('\t')[0].lower() 1fgcd
112 handler_name, new_name = get_eventhandler_name_and_parsed_name(new_name) 1fgcd
113 return getattr(parent, handler_name) 1fgcd
115 def is_separator(self): 1ab
116 return self.name == '---'