From 710f3fba80f82c45b86d3f53e18634640f6a7593 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 17 Oct 2022 15:45:30 -0400 Subject: [PATCH] factory: Give ListRows an "active" property And style them so the row is highlighted and the text is bold. Signed-off-by: Anna Schumaker --- emmental/emmental.css | 25 +++++++++++++++++++++++++ emmental/factory.py | 19 +++++++++++++++++++ tests/test_factory.py | 15 +++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/emmental/emmental.css b/emmental/emmental.css index a1cd8a1..dac99ba 100644 --- a/emmental/emmental.css +++ b/emmental/emmental.css @@ -20,3 +20,28 @@ box.emmental-splitbutton>menubutton>button { margin-left: -1px; padding: 6px; } + +listview > row:checked { + font-weight: bold; + background-color: alpha(@accent_color, 0.2); +} + +listview > row:checked:hover { + background-color: alpha(@accent_color, 0.27); +} + +listview > row:checked:active { + background-color: alpha(@accent_color, 0.36); +} + +listview > row:checked:selected { + background-color: alpha(@accent_color, 0.3); +} + +listview > row:checked:selected:hover { + background-color: alpha(@accent_color, 0.33); +} + +listview > row:checked:selected:active { + background-color: alpha(@accent_color, 0.39); +} diff --git a/emmental/factory.py b/emmental/factory.py index a9ba319..3e4d43f 100644 --- a/emmental/factory.py +++ b/emmental/factory.py @@ -22,6 +22,10 @@ class ListRow(GObject.GObject): def do_unbind(self) -> None: """Unbind the list item from the child widget.""" + def bind_active(self, item_prop: str) -> None: + """Bind a property to the Row's active property.""" + self.bind_and_set(self.item, item_prop, self, "active") + def bind_and_set(self, src: GObject.GObject, src_prop: str, dst: GObject.GObject, dst_prop: str, bidirectional: bool = False, @@ -54,6 +58,21 @@ class ListRow(GObject.GObject): self.bindings.clear() self.do_unbind() + @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 + return False + + @active.setter + def active(self, newval: bool) -> None: + if parent := self.listitem.get_child().get_parent(): + if newval: + parent.set_state_flags(Gtk.StateFlags.CHECKED, False) + else: + parent.unset_state_flags(Gtk.StateFlags.CHECKED) + @GObject.Property(type=Gtk.Widget) def child(self) -> Gtk.Widget | None: """Get the child widget displayed by this Row.""" diff --git a/tests/test_factory.py b/tests/test_factory.py index 5db4d1e..82c652a 100644 --- a/tests/test_factory.py +++ b/tests/test_factory.py @@ -30,12 +30,27 @@ class TestListRow(unittest.TestCase): self.assertEqual(self.row.listitem, self.listitem) self.assertEqual(self.row.child, self.listitem.get_child()) self.assertEqual(self.row.item, self.item) + self.assertFalse(self.row.active) def test_bind(self): """Test calling bind() on the ListRow.""" self.row.bind() self.row.do_bind.assert_called() + def test_bind_active(self): + """Test binding a property to the Row's active property.""" + parent = Gtk.Box() + parent.append(self.row.child) + + self.row.bind_active("sensitive") + self.assertEqual(len(self.row.bindings), 1) + self.assertTrue(parent.get_state_flags() & Gtk.StateFlags.CHECKED) + self.assertTrue(self.row.active) + + self.item.set_sensitive(False) + self.assertFalse(parent.get_state_flags() & Gtk.StateFlags.CHECKED) + self.assertFalse(self.row.active) + def test_bind_and_set_property(self): """Test the ListRow bind_property() function.""" self.row.bind_and_set_property("label", "label")