From eb6b4d8ef4ac222675418e22ae11a81b9c955427 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 22 Aug 2023 13:26:18 -0400 Subject: [PATCH] buttons: Watch for ImageToggle tooltip text changes If the application changes the active or inactive tooltip text, then we want to apply that to the button depending on what state it currently has. Signed-off-by: Anna Schumaker --- emmental/buttons.py | 7 +++++++ tests/test_buttons.py | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/emmental/buttons.py b/emmental/buttons.py index 0d4aee0..f2a5da7 100644 --- a/emmental/buttons.py +++ b/emmental/buttons.py @@ -119,6 +119,13 @@ class ImageToggle(Button): inactive_tooltip_text=inactive_tooltip_text, tooltip_text=inactive_tooltip_text, active=active, **kwargs) + self.connect("notify", self.__notify) + + def __notify(self, toggle: Button, param: GObject.ParamSpec) -> None: + match (param.name, self.active): + case ("active-tooltip-text", True) | \ + ("inactive-tooltip-text", False): + self.set_tooltip_text(self.get_property(param.name)) def do_clicked(self) -> None: """Handle a click event.""" diff --git a/tests/test_buttons.py b/tests/test_buttons.py index b910810..f9c2011 100644 --- a/tests/test_buttons.py +++ b/tests/test_buttons.py @@ -211,6 +211,17 @@ class TestImageToggle(unittest.TestCase): button2.active = False self.assertEqual(button2.get_tooltip_text(), "inactive tooltip text") + def test_changing_tooltip_text(self): + """Test changing the tooltip text for the current state.""" + self.assertEqual(self.button.props.tooltip_text, None) + self.button.inactive_tooltip_text = "inactive tooltip" + self.assertEqual(self.button.props.tooltip_text, "inactive tooltip") + + self.button.active = True + self.assertEqual(self.button.props.tooltip_text, None) + self.button.active_tooltip_text = "active tooltip" + self.assertEqual(self.button.props.tooltip_text, "active tooltip") + def test_toggle(self): """Test the toggle signal.""" toggled = unittest.mock.Mock()