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;
}
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);
}

View File

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

View File

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

View File

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

View File

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