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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-22 12:09:20 -04:00
parent 30bcd30328
commit f7349cd864
5 changed files with 35 additions and 34 deletions

View File

@ -21,28 +21,28 @@ box.emmental-splitbutton>menubutton>button {
padding: 6px; padding: 6px;
} }
listview > row:checked { row.emmental-active-row {
font-weight: bold; font-weight: bold;
background-color: alpha(@accent_color, 0.2); background-color: alpha(@accent_color, 0.2);
} }
listview > row:checked:hover { row.emmental-active-row:hover {
background-color: alpha(@accent_color, 0.27); background-color: alpha(@accent_color, 0.27);
} }
listview > row:checked:active { row.emmental-active-row:active {
background-color: alpha(@accent_color, 0.36); background-color: alpha(@accent_color, 0.36);
} }
listview > row:checked:selected { row.emmental-active-row:selected {
background-color: alpha(@accent_color, 0.3); 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); 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); background-color: alpha(@accent_color, 0.39);
} }

View File

@ -61,17 +61,17 @@ class ListRow(GObject.GObject):
@GObject.Property(type=bool, default=False) @GObject.Property(type=bool, default=False)
def active(self) -> bool: def active(self) -> bool:
"""Get the active state of this Row.""" """Get the active state of this Row."""
if parent := self.listitem.get_child().get_parent(): if self.listrow is not None:
return parent.get_state_flags() & Gtk.StateFlags.CHECKED return self.listrow.has_css_class("emmental-active-row")
return False return False
@active.setter @active.setter
def active(self, newval: bool) -> None: def active(self, newval: bool) -> None:
if parent := self.listitem.get_child().get_parent(): if self.listrow is not None:
if newval: if newval:
parent.set_state_flags(Gtk.StateFlags.CHECKED, False) self.listrow.add_css_class("emmental-active-row")
else: else:
parent.unset_state_flags(Gtk.StateFlags.CHECKED) self.listrow.remove_css_class("emmental-active-row")
@GObject.Property(type=Gtk.Widget) @GObject.Property(type=Gtk.Widget)
def child(self) -> Gtk.Widget | None: def child(self) -> Gtk.Widget | None:
@ -87,6 +87,11 @@ class ListRow(GObject.GObject):
"""Get the list item for this Row.""" """Get the list item for this Row."""
return self.listitem.get_item() 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): class InscriptionRow(ListRow):
"""A ListRow for displaying Gtk.Inscription widgets.""" """A ListRow for displaying Gtk.Inscription widgets."""

View File

@ -63,23 +63,6 @@ class TrackRow(factory.ListRow):
else: else:
self.bind_album(child_prop) 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) @GObject.Property(type=bool, default=True)
def online(self) -> bool: def online(self) -> bool:
"""Get the online state of this Row.""" """Get the online state of this Row."""
@ -90,6 +73,14 @@ class TrackRow(factory.ListRow):
self.listitem.set_activatable(newval) self.listitem.set_activatable(newval)
self.child.set_sensitive(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): class InscriptionRow(TrackRow):
"""Base class for Track Rows displaying a Gtk.Inscription.""" """Base class for Track Rows displaying a Gtk.Inscription."""

View File

@ -39,16 +39,21 @@ class TestListRow(unittest.TestCase):
def test_bind_active(self): def test_bind_active(self):
"""Test binding a property to the Row's active property.""" """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 = Gtk.Box()
parent.append(self.row.child) parent.append(self.row.child)
self.assertEqual(self.row.listrow, parent)
self.row.bind_active("sensitive") self.row.bind_active("sensitive")
self.assertEqual(len(self.row.bindings), 1) 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.assertTrue(self.row.active)
self.item.set_sensitive(False) 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) self.assertFalse(self.row.active)
def test_bind_and_set_property(self): def test_bind_and_set_property(self):

View File

@ -41,11 +41,13 @@ class TestTrackRowWidgets(tests.util.TestCase):
row = emmental.tracklist.row.TrackRow(self.listitem, "property") row = emmental.tracklist.row.TrackRow(self.listitem, "property")
self.assertIsInstance(row, emmental.factory.ListRow) self.assertIsInstance(row, emmental.factory.ListRow)
self.assertIsNone(row.album_binding) self.assertIsNone(row.album_binding)
self.assertIsNone(row.listrow)
self.assertEqual(row.property, "property") self.assertEqual(row.property, "property")
self.assertEqual(row.mediumid, 0) self.assertEqual(row.mediumid, 0)
row.child = Gtk.Label() row.child = Gtk.Label()
self.columnrow.set_child(row.child) self.columnrow.set_child(row.child)
self.assertEqual(row.listrow, self.listrow)
self.library.online = False self.library.online = False
self.track.active = True self.track.active = True
@ -54,8 +56,7 @@ class TestTrackRowWidgets(tests.util.TestCase):
self.assertFalse(self.listitem.get_activatable()) self.assertFalse(self.listitem.get_activatable())
self.assertFalse(row.child.get_sensitive()) self.assertFalse(row.child.get_sensitive())
self.assertTrue(row.active) self.assertTrue(row.active)
self.assertTrue(self.listrow.get_state_flags() & self.assertTrue(self.listrow.has_css_class("emmental-active-row"))
Gtk.StateFlags.CHECKED)
self.library.online = True self.library.online = True
self.track.active = False self.track.active = False
@ -63,8 +64,7 @@ class TestTrackRowWidgets(tests.util.TestCase):
self.assertTrue(self.listitem.get_activatable()) self.assertTrue(self.listitem.get_activatable())
self.assertTrue(row.child.get_sensitive()) self.assertTrue(row.child.get_sensitive())
self.assertFalse(row.active) self.assertFalse(row.active)
self.assertFalse(self.listrow.get_state_flags() & self.assertFalse(self.listrow.has_css_class("emmental-active-row"))
Gtk.StateFlags.CHECKED)
def test_inscription_row(self): def test_inscription_row(self):
"""Test the base Inscription Row.""" """Test the base Inscription Row."""