comparison cagou/core/menu.py @ 373:5d994be1161b

core: removed root menus (i.e. global menu on top of window): root menus were not really useful as most actions doable there are doable through others widgets in Cagou. For actions without equivalent in widgets (like about screen), a new menu, more discreet, will be added soon. Kivy Garden's ContextMenu is not used anymore, so it has been removed from dependencies
author Goffi <goffi@goffi.org>
date Mon, 27 Jan 2020 21:17:09 +0100
parents 9c6fe392d623
children 9ef01266e3fe
comparison
equal deleted inserted replaced
372:1481f09c9175 373:5d994be1161b
25 from kivy.uix.boxlayout import BoxLayout 25 from kivy.uix.boxlayout import BoxLayout
26 from kivy.uix.label import Label 26 from kivy.uix.label import Label
27 from kivy.uix.popup import Popup 27 from kivy.uix.popup import Popup
28 from cagou.core.utils import FilterBehavior 28 from cagou.core.utils import FilterBehavior
29 from kivy import properties 29 from kivy import properties
30 from kivy_garden import contextmenu, modernmenu 30 from kivy_garden import modernmenu
31 from sat_frontends.quick_frontend import quick_menus
32 from kivy.core.window import Window 31 from kivy.core.window import Window
33 from kivy.animation import Animation 32 from kivy.animation import Animation
34 from kivy.metrics import dp 33 from kivy.metrics import dp
35 from kivy.clock import Clock 34 from kivy.clock import Clock
36 from cagou import G 35 from cagou import G
66 65
67 def on_touch_down(self, touch): 66 def on_touch_down(self, touch):
68 if self.collide_point(*touch.pos): 67 if self.collide_point(*touch.pos):
69 self.dismiss() 68 self.dismiss()
70 return super(AboutPopup, self).on_touch_down(touch) 69 return super(AboutPopup, self).on_touch_down(touch)
71
72
73 class MainMenu(contextmenu.AppMenu):
74 pass
75
76
77 class MenuItem(contextmenu.ContextMenuTextItem):
78 item = properties.ObjectProperty()
79
80 def on_item(self, instance, item):
81 self.text = item.name
82
83 def on_release(self):
84 super(MenuItem, self).on_release()
85 self.parent.hide()
86 selected = G.host.selected_widget
87 profile = None
88 if selected is not None:
89 try:
90 # FIXME: handle multi-profiles
91 profile = next(iter(selected.profiles))
92 except AttributeError:
93 pass
94
95 if profile is None:
96 try:
97 profile = list(selected.profiles)[0]
98 except (AttributeError, IndexError):
99 try:
100 profile = list(G.host.profiles)[0]
101 except IndexError:
102 log.warning("Can't find profile")
103 self.item.call(selected, profile)
104
105
106 class MenuSeparator(contextmenu.ContextMenuDivider):
107 pass
108
109
110 class RootMenuContainer(contextmenu.AppMenuTextItem):
111 pass
112
113
114 class MenuContainer(contextmenu.ContextMenuTextItem):
115 pass
116
117
118 class MenusWidget(BoxLayout):
119
120 def update(self, type_, caller=None):
121 """Method to call when menus have changed
122
123 @param type_(unicode): menu type like in sat.core.sat_main.importMenu
124 @param caller(Widget): instance linked to the menus
125 """
126 self.menus_container = G.host.menus.getMainContainer(type_)
127 self.createMenus(caller)
128
129 def _buildMenus(self, container, caller=None):
130 """Recursively build menus of the container
131
132 @param container(quick_menus.MenuContainer): menu container
133 @param caller(Widget): instance linked to the menus
134 """
135 if caller is None:
136 main_menu = MainMenu()
137 self.add_widget(main_menu)
138 caller = main_menu
139 else:
140 context_menu = contextmenu.ContextMenu()
141 caller.add_widget(context_menu)
142 # FIXME: next line is needed after parent is set to avoid
143 # a display bug in contextmenu
144 # TODO: fix this upstream
145 context_menu._on_visible(False)
146
147 caller = context_menu
148
149 for child in container.getActiveMenus():
150 if isinstance(child, quick_menus.MenuContainer):
151 if isinstance(caller, MainMenu):
152 menu_container = RootMenuContainer()
153 else:
154 menu_container = MenuContainer()
155 menu_container.text = child.name
156 caller.add_widget(menu_container)
157 self._buildMenus(child, caller=menu_container)
158 elif isinstance(child, quick_menus.MenuSeparator):
159 wid = MenuSeparator()
160 caller.add_widget(wid)
161 elif isinstance(child, quick_menus.MenuItem):
162 wid = MenuItem(item=child)
163 caller.add_widget(wid)
164 else:
165 log.error("Unknown child type: {}".format(child))
166
167 def createMenus(self, caller):
168 self.clear_widgets()
169 self._buildMenus(self.menus_container, caller)
170
171 def onAbout(self):
172 about = AboutPopup()
173 about.title = ABOUT_TITLE
174 about.content = AboutContent(
175 text=ABOUT_CONTENT.format(
176 backend_version = G.host.backend_version,
177 version=G.host.version),
178 markup=True)
179 about.open()
180 70
181 71
182 class TransferItem(BoxLayout): 72 class TransferItem(BoxLayout):
183 plug_info = properties.DictProperty() 73 plug_info = properties.DictProperty()
184 74