From f7349cd86496fc56e857b22e532c0b113cd86522 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 22 Aug 2023 12:09:20 -0400 Subject: [PATCH] factory: Don't change Gtk ListRowWidget state flags I was using this to set some custom styling for the active playlist and track inside a ListView. I can accomplish the same thing by adding and removing a style class from the ListRowWidget, and this doesn't break Gtk internal stuff that changed in the 4.12 release. Signed-off-by: Anna Schumaker --- emmental/emmental.css | 12 ++++++------ emmental/factory.py | 15 ++++++++++----- emmental/tracklist/row.py | 25 ++++++++----------------- tests/test_factory.py | 9 +++++++-- tests/tracklist/test_row.py | 8 ++++---- 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/emmental/emmental.css b/emmental/emmental.css index 3ed36f9..b82e3f5 100644 --- a/emmental/emmental.css +++ b/emmental/emmental.css @@ -21,28 +21,28 @@ box.emmental-splitbutton>menubutton>button { padding: 6px; } -listview > row:checked { +row.emmental-active-row { font-weight: bold; background-color: alpha(@accent_color, 0.2); } -listview > row:checked:hover { +row.emmental-active-row:hover { background-color: alpha(@accent_color, 0.27); } -listview > row:checked:active { +row.emmental-active-row:active { background-color: alpha(@accent_color, 0.36); } -listview > row:checked:selected { +row.emmental-active-row:selected { background-color: alpha(@accent_color, 0.3); } -listview > row:checked:selected:hover { +row.emmental-active-row:selected:hover { background-color: alpha(@accent_color, 0.33); } -listview > row:checked:selected:active { +row.emmental-active-row:selected:active { background-color: alpha(@accent_color, 0.39); } diff --git a/emmental/factory.py b/emmental/factory.py index 3e4d43f..da8435c 100644 --- a/emmental/factory.py +++ b/emmental/factory.py @@ -61,17 +61,17 @@ class ListRow(GObject.GObject): @GObject.Property(type=bool, default=False) def active(self) -> bool: """Get the active state of this Row.""" - if parent := self.listitem.get_child().get_parent(): - return parent.get_state_flags() & Gtk.StateFlags.CHECKED + if self.listrow is not None: + return self.listrow.has_css_class("emmental-active-row") return False @active.setter def active(self, newval: bool) -> None: - if parent := self.listitem.get_child().get_parent(): + if self.listrow is not None: if newval: - parent.set_state_flags(Gtk.StateFlags.CHECKED, False) + self.listrow.add_css_class("emmental-active-row") else: - parent.unset_state_flags(Gtk.StateFlags.CHECKED) + self.listrow.remove_css_class("emmental-active-row") @GObject.Property(type=Gtk.Widget) def child(self) -> Gtk.Widget | None: @@ -87,6 +87,11 @@ class ListRow(GObject.GObject): """Get the list item for this Row.""" return self.listitem.get_item() + @GObject.Property(type=Gtk.Widget) + def listrow(self) -> Gtk.Widget: + """Get the listrow widget that our child widget is contained in.""" + return self.listitem.props.child.props.parent + class InscriptionRow(ListRow): """A ListRow for displaying Gtk.Inscription widgets.""" diff --git a/emmental/tracklist/row.py b/emmental/tracklist/row.py index 59cec7e..7041d8e 100644 --- a/emmental/tracklist/row.py +++ b/emmental/tracklist/row.py @@ -63,23 +63,6 @@ class TrackRow(factory.ListRow): else: self.bind_album(child_prop) - @GObject.Property(type=bool, default=False) - def active(self) -> bool: - """Get the active state of this Row.""" - if parent := self.listitem.get_child().get_parent(): - if parent := parent.get_parent(): - return parent.get_state_flags() & Gtk.StateFlags.CHECKED - return False - - @active.setter - def active(self, newval: bool) -> None: - if parent := self.listitem.get_child().get_parent(): - if parent := parent.get_parent(): - if newval: - parent.set_state_flags(Gtk.StateFlags.CHECKED, False) - else: - parent.unset_state_flags(Gtk.StateFlags.CHECKED) - @GObject.Property(type=bool, default=True) def online(self) -> bool: """Get the online state of this Row.""" @@ -90,6 +73,14 @@ class TrackRow(factory.ListRow): self.listitem.set_activatable(newval) self.child.set_sensitive(newval) + @GObject.Property(type=Gtk.Widget) + def listrow(self) -> Gtk.Widget: + """Test property for active track styling.""" + if child := self.listitem.props.child: + if cell := child.props.parent: + return cell.props.parent + return None + class InscriptionRow(TrackRow): """Base class for Track Rows displaying a Gtk.Inscription.""" diff --git a/tests/test_factory.py b/tests/test_factory.py index 82c652a..e7e28c7 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -39,16 +39,21 @@ class TestListRow(unittest.TestCase): def test_bind_active(self): """Test binding a property to the Row's active property.""" + self.assertIsNone(self.row.listrow) + self.row.active = True + self.assertFalse(self.row.active) + parent = Gtk.Box() parent.append(self.row.child) + self.assertEqual(self.row.listrow, parent) self.row.bind_active("sensitive") self.assertEqual(len(self.row.bindings), 1) - self.assertTrue(parent.get_state_flags() & Gtk.StateFlags.CHECKED) + self.assertTrue(parent.has_css_class("emmental-active-row")) self.assertTrue(self.row.active) self.item.set_sensitive(False) - self.assertFalse(parent.get_state_flags() & Gtk.StateFlags.CHECKED) + self.assertFalse(parent.has_css_class("emmental-active-row")) self.assertFalse(self.row.active) def test_bind_and_set_property(self): diff --git a/tests/tracklist/test_row.py b/tests/tracklist/test_row.py index af2b945..c9b2dc4 100644 --- a/tests/tracklist/test_row.py +++ b/tests/tracklist/test_row.py @@ -41,11 +41,13 @@ class TestTrackRowWidgets(tests.util.TestCase): row = emmental.tracklist.row.TrackRow(self.listitem, "property") self.assertIsInstance(row, emmental.factory.ListRow) self.assertIsNone(row.album_binding) + self.assertIsNone(row.listrow) self.assertEqual(row.property, "property") self.assertEqual(row.mediumid, 0) row.child = Gtk.Label() self.columnrow.set_child(row.child) + self.assertEqual(row.listrow, self.listrow) self.library.online = False self.track.active = True @@ -54,8 +56,7 @@ class TestTrackRowWidgets(tests.util.TestCase): self.assertFalse(self.listitem.get_activatable()) self.assertFalse(row.child.get_sensitive()) self.assertTrue(row.active) - self.assertTrue(self.listrow.get_state_flags() & - Gtk.StateFlags.CHECKED) + self.assertTrue(self.listrow.has_css_class("emmental-active-row")) self.library.online = True self.track.active = False @@ -63,8 +64,7 @@ class TestTrackRowWidgets(tests.util.TestCase): self.assertTrue(self.listitem.get_activatable()) self.assertTrue(row.child.get_sensitive()) self.assertFalse(row.active) - self.assertFalse(self.listrow.get_state_flags() & - Gtk.StateFlags.CHECKED) + self.assertFalse(self.listrow.has_css_class("emmental-active-row")) def test_inscription_row(self): """Test the base Inscription Row."""