ui: Use GObject.BindingGroup

This commit is contained in:
Jamie Gravendeel
2026-01-10 01:13:04 +01:00
parent 515bafa428
commit dab108ce8b
3 changed files with 27 additions and 34 deletions

View File

@@ -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."""

View File

@@ -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

View File

@@ -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"))