changeset 85:c2a7234d13d2

menu: use of garden's contextmenu for menus
author Goffi <goffi@goffi.org>
date Sat, 24 Dec 2016 14:20:49 +0100
parents 2caee196d19a
children c711be670ecd
files src/cagou/core/cagou_main.py src/cagou/core/menu.py src/cagou/kv/menu.kv src/cagou/kv/root_widget.kv
diffstat 4 files changed, 74 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/src/cagou/core/cagou_main.py	Sat Dec 24 14:20:40 2016 +0100
+++ b/src/cagou/core/cagou_main.py	Sat Dec 24 14:20:49 2016 +0100
@@ -50,6 +50,7 @@
 from kivy.clock import Clock
 from kivy.uix.label import Label
 from kivy.uix.boxlayout import BoxLayout
+from kivy.uix.floatlayout import FloatLayout
 from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition, RiseInTransition
 from kivy.uix.dropdown import DropDown
 from cagou_widget import CagouWidget
@@ -151,16 +152,23 @@
         self.manager.switch_to(screen)
 
 
-class CagouRootWidget(BoxLayout):
+class RootMenus(MenusWidget):
+    pass
+
+
+class RootBody(BoxLayout):
+    pass
+
+
+class CagouRootWidget(FloatLayout):
+    root_menus = properties.ObjectProperty()
+    root_body = properties.ObjectProperty
 
     def __init__(self, main_widget):
-        super(CagouRootWidget, self).__init__(orientation=("vertical"))
-        # general menus
-        self.menus_widget = MenusWidget()
-        self.add_widget(self.menus_widget)
+        super(CagouRootWidget, self).__init__()
         # header
         self._head_widget = RootHeadWidget()
-        self.add_widget(self._head_widget)
+        self.root_body.add_widget(self._head_widget)
         # body
         self._manager = ScreenManager()
         # main widgets
@@ -173,7 +181,7 @@
         # extra (file chooser, audio record, etc)
         extra_screen = Screen(name='extra')
         self._manager.add_widget(extra_screen)
-        self.add_widget(self._manager)
+        self.root_body.add_widget(self._manager)
 
     def changeWidget(self, widget, screen_name="main"):
         """change main widget"""
@@ -530,7 +538,7 @@
     ## menus ##
 
     def _getMenusCb(self, backend_menus):
-        main_menu = self.app.root.menus_widget
+        main_menu = self.app.root.root_menus
         self.menus.addMenus(backend_menus)
         self.menus.addMenu(C.MENU_GLOBAL, (_(u"Help"), _(u"About")), callback=main_menu.onAbout)
         main_menu.update(C.MENU_GLOBAL)
--- a/src/cagou/core/menu.py	Sat Dec 24 14:20:40 2016 +0100
+++ b/src/cagou/core/menu.py	Sat Dec 24 14:20:49 2016 +0100
@@ -22,14 +22,11 @@
 from sat.core import log as logging
 log = logging.getLogger(__name__)
 from cagou.core.constants import Const as C
-from kivy.uix.scrollview import ScrollView
-from kivy.uix.gridlayout import GridLayout
-from kivy.uix.widget import Widget
+from kivy.uix.boxlayout import BoxLayout
 from kivy.uix.label import Label
-from kivy.uix.button import Button
 from kivy.uix.popup import Popup
-from kivy.uix.dropdown import DropDown
 from kivy import properties
+from kivy.garden import contextmenu
 from sat_frontends.quick_frontend import quick_menus
 from cagou import G
 import webbrowser
@@ -59,17 +56,19 @@
         return super(AboutPopup, self).on_touch_down(touch)
 
 
-class MenuItem(Button):
+class MainMenu(contextmenu.AppMenu):
+    pass
+
+
+class MenuItem(contextmenu.ContextMenuTextItem):
     item = properties.ObjectProperty()
 
     def on_item(self, instance, item):
         self.text = item.name
 
     def on_release(self):
-        try:
-            self.parent.parent.dismiss()
-        except AttributeError:
-            pass
+        super(MenuItem, self).on_release()
+        self.parent.hide()
         selected = G.host.selected_widget
         profile = None
         if selected is not None:
@@ -89,37 +88,19 @@
         self.item.call(selected, profile)
 
 
-class MenuSeparator(Widget):
+class MenuSeparator(contextmenu.ContextMenuDivider):
     pass
 
 
-class MenuContainer(Button):
-
-    def __init__(self, **kwargs):
-        super(MenuContainer, self).__init__(**kwargs)
-        self.dropdown = DropDown(auto_width=False, size_hint_x=None, width=400)
-
-    def on_release(self):
-        self.dropdown.open(self)
-
-    def add_widget(self, widget):
-        widget.size_hint_y = None
-        self.dropdown.add_widget(widget)
+class RootMenuContainer(contextmenu.AppMenuTextItem):
+    pass
 
 
-class MenusWidget(ScrollView):
+class MenuContainer(contextmenu.ContextMenuTextItem):
+    pass
 
-    def __init__(self, **kwargs):
-        super(MenusWidget, self).__init__(**kwargs)
-        self._grid = GridLayout(rows=1, size_hint=(None, 1))
-        self._grid.width = self._grid.minimum_width
-        super(MenusWidget, self).add_widget(self._grid)
 
-    def add_widget(self, widget):
-        self._grid.add_widget(widget)
-
-    def clear_widgets(self, children=None):
-        self._grid.clear_widgets(children)
+class MenusWidget(BoxLayout):
 
     def update(self, type_, caller=None):
         """Method to call when menus have changed
@@ -137,10 +118,24 @@
         @param caller(Widget): instance linked to the menus
         """
         if caller is None:
-            caller = self
+            main_menu = MainMenu()
+            self.add_widget(main_menu)
+            caller = main_menu
+        else:
+            context_menu = contextmenu.ContextMenu()
+            caller.add_widget(context_menu)
+            # FIXME: next line is needed after parent is set to avoid a display bug in contextmenu
+            # TODO: fix this upstream
+            context_menu._on_visible(False)
+
+            caller = context_menu
+
         for child in container.getActiveMenus():
             if isinstance(child, quick_menus.MenuContainer):
-                menu_container = MenuContainer()
+                if isinstance(caller, MainMenu):
+                    menu_container = RootMenuContainer()
+                else:
+                    menu_container = MenuContainer()
                 menu_container.text = child.name
                 caller.add_widget(menu_container)
                 self._buildMenus(child, caller=menu_container)
--- a/src/cagou/kv/menu.kv	Sat Dec 24 14:20:40 2016 +0100
+++ b/src/cagou/kv/menu.kv	Sat Dec 24 14:20:49 2016 +0100
@@ -14,24 +14,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-<MenuItem>:
-    size_hint: None, 1
-    padding: 10, 0
-    width: self.texture_size[0]
-    height: 30
-
-<MenuSeparator>:
-    size_hint: None, None
-    width: 30
-
-<MenuContainer>:
-    size_hint: None, 1
-    width: self.texture_size[0] + dp(20)
-    background_color: 0, 0, 0, 1
-
-<MenusWidget>:
-    size_hint: 1, None
-    height: 30
 
 <AboutContent>:
     text_size: self.size
@@ -41,3 +23,13 @@
 <AboutPopup>:
     title_align: "center"
     size_hint: 0.8, 0.8
+
+<MenuItem>:
+    # following is need to fix a bug in contextmenu
+    size_hint: 1, None
+
+<MenusWidget>:
+    height: self.children[0].height if self.children else 30
+
+<MainMenu>:
+    cancel_handler_widget: self.parent
--- a/src/cagou/kv/root_widget.kv	Sat Dec 24 14:20:40 2016 +0100
+++ b/src/cagou/kv/root_widget.kv	Sat Dec 24 14:20:49 2016 +0100
@@ -67,3 +67,20 @@
         size_hint: None, 1
         width: self.norm_image_size[0]
 
+<RootMenus>:
+    size_hint: 1, None
+    pos_hint: {'top': 1}
+
+<CagouRootWidget>:
+    root_body: root_body
+    root_menus: root_menus
+    # main body
+    RootBody:
+        id: root_body
+        orientation: "vertical"
+        size_hint: 1, None
+        height: root.height - root_menus.height
+    # general menus
+    # need to be added at the end so it's drawed above other widgets
+    RootMenus:
+        id: root_menus