diff cagou/core/menu.py @ 404:f7476818f9fb

core (common): JidSelector + behaviors various improvments: - renamed *Behaviour => *Behavior to be consistent with Kivy + moved to new "core.behaviors" modules - use a dedicated property in ContactItem for notification counter (which is now named "badge") - in JidSelector, well-known strings now create use a dedicated layout, add separator (except if new `add_separators` property is set to False), and are added to attribute of the same name - a new `item_class` property is now used to indicate the class to instanciate for items (by default it's a ContactItem) - FilterBahavior.do_filter now expect the parent layout instead of directly the children, this is to allow a FilterBahavior to manage several children layout at once (used with JidSelector) - core.utils has been removed, as the behavior there has been moved to core.behaviors
author Goffi <goffi@goffi.org>
date Wed, 12 Feb 2020 20:02:58 +0100
parents 71f51198478c
children 5761b5f03c0c
line wrap: on
line diff
--- a/cagou/core/menu.py	Wed Feb 12 20:02:58 2020 +0100
+++ b/cagou/core/menu.py	Wed Feb 12 20:02:58 2020 +0100
@@ -26,13 +26,11 @@
 from kivy.uix.label import Label
 from kivy.uix.button import Button
 from kivy.uix.popup import Popup
-from cagou.core.utils import FilterBehavior
+from .behaviors import FilterBehavior
 from kivy import properties
-from kivy_garden import modernmenu
 from kivy.core.window import Window
 from kivy.animation import Animation
 from kivy.metrics import dp
-from kivy.clock import Clock
 from cagou import G
 from functools import partial
 import webbrowser
@@ -295,105 +293,8 @@
 
     def do_filter_input(self, filter_input, text):
         self.layout.spacing = 0 if text else dp(5)
-        self.do_filter(self.layout.children,
+        self.do_filter(self.layout,
                        text,
                        lambda c: c.jid,
                        width_cb=lambda c: c.width,
                        height_cb=lambda c: dp(70))
-
-
-class TouchMenu(modernmenu.ModernMenu):
-    pass
-
-
-class TouchMenuItemBehaviour(object):
-    """Class to use on every item where a menu may appear
-
-    main_wid attribute must be set to the class inheriting from TouchMenuBehaviour
-    do_item_action is the method called on simple click
-    getMenuChoices must return a list of menus for long press
-        menus there are dict as expected by ModernMenu
-        (translated text, index and callback)
-    """
-    main_wid = properties.ObjectProperty()
-    click_timeout = properties.NumericProperty(0.4)
-
-    def on_touch_down(self, touch):
-        if not self.collide_point(*touch.pos):
-            return
-        t = partial(self.open_menu, touch)
-        touch.ud['menu_timeout'] = t
-        Clock.schedule_once(t, self.click_timeout)
-        return super(TouchMenuItemBehaviour, self).on_touch_down(touch)
-
-    def do_item_action(self, touch):
-        pass
-
-    def on_touch_up(self, touch):
-        if touch.ud.get('menu_timeout'):
-            Clock.unschedule(touch.ud['menu_timeout'])
-            if self.collide_point(*touch.pos) and self.main_wid.menu is None:
-                self.do_item_action(touch)
-        return super(TouchMenuItemBehaviour, self).on_touch_up(touch)
-
-    def open_menu(self, touch, dt):
-        self.main_wid.open_menu(self, touch)
-        del touch.ud['menu_timeout']
-
-    def getMenuChoices(self):
-        """return choice adapted to selected item
-
-        @return (list[dict]): choices ad expected by ModernMenu
-        """
-        return []
-
-
-class TouchMenuBehaviour(object):
-    """Class to handle a menu appearing on long press on items
-
-    classes using this behaviour need to have a float_layout property
-    pointing the main FloatLayout.
-    """
-    float_layout = properties.ObjectProperty()
-
-    def __init__(self, *args, **kwargs):
-        super(TouchMenuBehaviour, self).__init__(*args, **kwargs)
-        self.menu = None
-        self.menu_item = None
-
-    ## menu methods ##
-
-    def clean_fl_children(self, layout, children):
-        """insure that self.menu and self.menu_item are None when menu is dimissed"""
-        if self.menu is not None and self.menu not in children:
-            self.menu = self.menu_item = None
-
-    def clear_menu(self):
-        """remove menu if there is one"""
-        if self.menu is not None:
-            self.menu.dismiss()
-            self.menu = None
-            self.menu_item = None
-
-    def open_menu(self, item, touch):
-        """open menu for item
-
-        @param item(PathWidget): item when the menu has been requested
-        @param touch(kivy.input.MotionEvent): touch data
-        """
-        if self.menu_item == item:
-            return
-        self.clear_menu()
-        pos = self.to_widget(*touch.pos)
-        choices = item.getMenuChoices()
-        if not choices:
-            return
-        self.menu = TouchMenu(choices=choices,
-                              center=pos,
-                              size_hint=(None, None))
-        self.float_layout.add_widget(self.menu)
-        self.menu.start_display(touch)
-        self.menu_item = item
-
-    def on_float_layout(self, wid, float_layout):
-        float_layout.bind(children=self.clean_fl_children)