changeset 3159:30e08d904208

quick_frontend (app, widget): CagouWidget.onSelected is called when the widget is selected: A property is now used to manage `selected_widget` in QuickApp, and widget's onSelected method is called when suitable.
author Goffi <goffi@goffi.org>
date Sun, 09 Feb 2020 23:50:21 +0100
parents 6032245c927e
children 330a5f1d9eea
files sat_frontends/primitivus/chat.py sat_frontends/quick_frontend/quick_app.py sat_frontends/quick_frontend/quick_widgets.py
diffstat 3 files changed, 35 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/primitivus/chat.py	Thu Feb 06 00:01:36 2020 +0100
+++ b/sat_frontends/primitivus/chat.py	Sun Feb 09 23:50:21 2020 +0100
@@ -17,12 +17,12 @@
 # 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/>.
 
+from functools import total_ordering
+import bisect
+import urwid
+from urwid_satext import sat_widgets
 from sat.core.i18n import _
 from sat.core import log as logging
-
-log = logging.getLogger(__name__)
-import urwid
-from urwid_satext import sat_widgets
 from sat_frontends.quick_frontend import quick_widgets
 from sat_frontends.quick_frontend import quick_chat
 from sat_frontends.quick_frontend import quick_games
@@ -31,8 +31,9 @@
 from sat_frontends.primitivus.keys import action_key_map as a_key
 from sat_frontends.primitivus.widget import PrimitivusWidget
 from sat_frontends.primitivus.contact_list import ContactList
-from functools import total_ordering
-import bisect
+
+
+log = logging.getLogger(__name__)
 
 
 OCCUPANTS_FOOTER = _("{} occupants")
--- a/sat_frontends/quick_frontend/quick_app.py	Thu Feb 06 00:01:36 2020 +0100
+++ b/sat_frontends/quick_frontend/quick_app.py	Sun Feb 09 23:50:21 2020 +0100
@@ -301,8 +301,8 @@
         else:
             self.options = None
 
-        # widget currently selected (must be filled by frontend)
-        self.selected_widget = None
+        # see selected_widget setter and getter
+        self._selected_widget = None
 
         # listeners are callable watching events
         self._listeners = {}  # key: listener type ("avatar", "selected", etc),
@@ -436,6 +436,28 @@
             if isinstance(w, quick_widgets.QuickWidget):
                 return w
 
+    @property
+    def selected_widget(self):
+        """widget currently selected
+
+        This must be set by frontend using setter.
+        """
+        return self._selected_widget
+
+    @selected_widget.setter
+    def selected_widget(self, wid):
+        """Set the currently selected widget
+
+        Must be set by frontend
+        """
+        self._selected_widget = wid
+        try:
+            onSelected = wid.onSelected
+        except AttributeError:
+            pass
+        else:
+            onSelected()
+
     # backend state management
 
     @property
--- a/sat_frontends/quick_frontend/quick_widgets.py	Thu Feb 06 00:01:36 2020 +0100
+++ b/sat_frontends/quick_frontend/quick_widgets.py	Sun Feb 09 23:50:21 2020 +0100
@@ -462,3 +462,7 @@
             all other value continue deletion
         """
         return True
+
+    def onSelected(self):
+        """Called when host.selected_widget is this instance"""
+        pass