diff --git a/cartridges/ui/collections.py b/cartridges/ui/collections.py index 498dd3f..a8050bb 100644 --- a/cartridges/ui/collections.py +++ b/cartridges/ui/collections.py @@ -37,19 +37,9 @@ class CollectionFilter(Gtk.Filter): class CollectionSidebarItem(Adw.SidebarItem): # pyright: ignore[reportAttributeAccessIssue] """A sidebar item representing a collection.""" - @GObject.Property(type=Collection) - def collection(self) -> Collection: - """The collection that `self` represents.""" - return self._collection + collection = GObject.Property(type=Collection) - @collection.setter - def collection(self, collection: Collection): - self._collection = collection - flags = GObject.BindingFlags.SYNC_CREATE - collection.bind_property("name", self, "title", flags) - collection.bind_property("icon-name", self, "icon-name", flags) - - def __init__(self, **kwargs: Any): + def __init__(self, collection: Collection, **kwargs: Any): super().__init__(**kwargs) self.bind_property( @@ -60,6 +50,13 @@ class CollectionSidebarItem(Adw.SidebarItem): # pyright: ignore[reportAttribute lambda _, name: GLib.markup_escape_text(name), ) + self._collection_bindings = GObject.BindingGroup() + flags = GObject.BindingFlags.DEFAULT + self._collection_bindings.bind("name", self, "title", flags) + self._collection_bindings.bind("icon-name", self, "icon-name", flags) + self.bind_property("collection", self._collection_bindings, "source") + self.collection = collection + class CollectionButton(Gtk.ToggleButton): """A toggle button representing a collection.""" diff --git a/cartridges/ui/sources.py b/cartridges/ui/sources.py index e0c11f5..031357f 100644 --- a/cartridges/ui/sources.py +++ b/cartridges/ui/sources.py @@ -13,27 +13,9 @@ from cartridges.ui import games class SourceSidebarItem(Adw.SidebarItem): # pyright: ignore[reportAttributeAccessIssue] """A sidebar item representing a source.""" + source = GObject.Property(type=Source) model = GObject.Property(type=Gio.ListModel) - @GObject.Property(type=Source) - def source(self) -> Source: - """The source that `self` represents.""" - return self._source - - @source.setter - def source(self, source: Source): - self._source = source - flags = GObject.BindingFlags.SYNC_CREATE - source.bind_property("name", self, "title", flags) - source.bind_property("icon-name", self, "icon-name", flags) - - self.model = Gtk.FilterListModel( - model=source, - filter=games.filter_, - watch_items=True, # pyright: ignore[reportCallIssue] - ) - self.props.visible = self.model.props.n_items - def __init__(self, source: Source, **kwargs: Any): super().__init__(**kwargs) @@ -41,10 +23,24 @@ class SourceSidebarItem(Adw.SidebarItem): # pyright: ignore[reportAttributeAcce self._model_signals = GObject.SignalGroup.new(Gio.ListModel) self._model_signals.connect_closure( "items-changed", - lambda model, *_: self.set_property("visible", model.props.n_items), + lambda model, *_: model.notify("n-items"), after=True, ) - self.bind_property("model", self._model_signals, "target") + + self._source_bindings = GObject.BindingGroup() + flags = GObject.BindingFlags.DEFAULT + self._source_bindings.bind("name", self, "title", flags) + self._source_bindings.bind("icon-name", self, "icon-name", flags) + self.bind_property("source", self._source_bindings, "source") + + self.model = Gtk.FilterListModel(filter=games.filter_, watch_items=True) # pyright: ignore[reportCallIssue] + self.model.bind_property( + "n-items", + self, + "visible", + GObject.BindingFlags.SYNC_CREATE, + ) + self.bind_property("source", self.model, "model") self.source = source diff --git a/cartridges/ui/window.py b/cartridges/ui/window.py index 2789c58..2a76353 100644 --- a/cartridges/ui/window.py +++ b/cartridges/ui/window.py @@ -96,7 +96,7 @@ class Window(Adw.ApplicationWindow): ) self.collections.bind_model( collections.model, - lambda collection: CollectionSidebarItem(collection=collection), + lambda collection: CollectionSidebarItem(collection), ) self.add_action(STATE_SETTINGS.create_action("show-sidebar"))