# HG changeset patch # User Goffi # Date 1552289983 -3600 # Node ID 145c29b5f2b5f95b0cab7e04d1407a23eff8b92b # Parent 4601793b0dee7ef128acdcbc8ee5a2c540f7734c 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. diff -r 4601793b0dee -r 145c29b5f2b5 cagou/core/cagou_main.py --- 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], diff -r 4601793b0dee -r 145c29b5f2b5 cagou/core/widgets_handler.py --- 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() diff -r 4601793b0dee -r 145c29b5f2b5 cagou/plugins/plugin_wid_chat.py --- 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 ##