changeset 260:145c29b5f2b5

core: improved getOrClone + use in chat and widgets_handler: - getOrClone is now trying to re-use others instances of a widget if they have no parent, and will create a new widget only if nothing is found - it is now used in widgets_handler's changeWidget - it is now used in Chat factory, on existing widget - new host method deleteUnusedWidgetInstances remove instances without parent. It is used by getOrClone.
author Goffi <goffi@goffi.org>
date Mon, 11 Mar 2019 08:39:43 +0100
parents 4601793b0dee
children a579eda31f4f
files cagou/core/cagou_main.py cagou/core/widgets_handler.py cagou/plugins/plugin_wid_chat.py
diffstat 3 files changed, 26 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Mon Mar 11 08:39:43 2019 +0100
+++ b/cagou/core/cagou_main.py	Mon Mar 11 08:39:43 2019 +0100
@@ -751,10 +751,33 @@
         except KeyError:
             return []
 
+    def deleteUnusedWidgetInstances(self, widget):
+        """Delete instance of this widget without parent
+
+        @param widget(quick_widgets.QuickWidget): reference widget
+            other instance of this widget will be deleted if they have no parent
+        """
+        # FIXME: unused for now
+        to_delete = []
+        for w in self.widgets.getWidgetInstances(widget):
+            if w.parent is None and w != widget:
+                to_delete.append(w)
+        for w in to_delete:
+            log.debug(u"cleaning widget: {wid}".format(wid=w))
+            self.widgets.deleteWidget(w)
+
     def getOrClone(self, widget):
-        """Get a QuickWidget if it has not parent set else clone it"""
+        """Get a QuickWidget if it has not parent set else clone it
+
+        if an other instance of this widget exist without parent, it will be used.
+        """
         if widget.parent is None:
+            self.deleteUnusedWidgetInstances(widget)
             return widget
+        for w in self.widgets.getWidgetInstances(widget):
+            if w.parent is None:
+                self.deleteUnusedWidgetInstances(w)
+                return w
         targets = list(widget.targets)
         w = self.widgets.getOrCreateWidget(widget.__class__,
                                            targets[0],
--- a/cagou/core/widgets_handler.py	Mon Mar 11 08:39:43 2019 +0100
+++ b/cagou/core/widgets_handler.py	Mon Mar 11 08:39:43 2019 +0100
@@ -318,7 +318,7 @@
             if isinstance(w, quick_widgets.QuickWidget):
                 G.host.widgets.deleteWidget(w)
         self.carousel.clear_widgets()
-        self.carousel.add_widget(new_widget)
+        self.carousel.add_widget(G.host.getOrClone(new_widget))
         self._slides_update_lock = False
         self.updateHiddenSlides()
 
--- a/cagou/plugins/plugin_wid_chat.py	Mon Mar 11 08:39:43 2019 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Mon Mar 11 08:39:43 2019 +0100
@@ -381,7 +381,7 @@
         if target is None:
             target = G.host.profiles[profiles[0]].whoami
         return G.host.widgets.getOrCreateWidget(cls, target, on_new_widget=None,
-                                                on_existing_widget=C.WIDGET_RECREATE,
+                                                on_existing_widget=G.host.getOrClone,
                                                 profiles=profiles)
 
     ## header ##