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()