From a13e481754a0201ec89b8c924d341039849a461f Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sat, 10 Jun 2023 20:25:11 -0400 Subject: [PATCH] buttons: Give the ImageToggle active and inactive tooltip texts This is used to set the tooltip text to fixed strings based on the active status of the button. I also update the tooltip text on the Favorite button in the Now Playing widget at the same time. Signed-off-by: Anna Schumaker --- emmental/buttons.py | 21 +++++++++++++++++---- emmental/nowplaying/__init__.py | 4 ++-- tests/nowplaying/test_nowplaying.py | 4 +++- tests/test_buttons.py | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/emmental/buttons.py b/emmental/buttons.py index f9e80e6..fd4525c 100644 --- a/emmental/buttons.py +++ b/emmental/buttons.py @@ -96,14 +96,23 @@ class ImageToggle(Button): """Inspired by a ToggleButton, but changes image based on state.""" active_icon_name = GObject.Property(type=str) + active_tooltip_text = GObject.Property(type=str) + inactive_icon_name = GObject.Property(type=str) + inactive_tooltip_text = GObject.Property(type=str) def __init__(self, active_icon_name: str, inactive_icon_name: str, - active: bool = False, **kwargs) -> None: + active_tooltip_text: str | None = None, + inactive_tooltip_text: str | None = None, + *, active: bool = False, **kwargs) -> None: """Initialize an ImageToggle button.""" super().__init__(active_icon_name=active_icon_name, inactive_icon_name=inactive_icon_name, - icon_name=inactive_icon_name, active=active, **kwargs) + icon_name=inactive_icon_name, + active_tooltip_text=active_tooltip_text, + inactive_tooltip_text=inactive_tooltip_text, + tooltip_text=inactive_tooltip_text, + active=active, **kwargs) def do_clicked(self) -> None: """Handle a click event.""" @@ -117,8 +126,12 @@ class ImageToggle(Button): @active.setter def active(self, newval: bool) -> None: if newval != self.active: - icon = self.active_icon_name if newval else self.inactive_icon_name - self.icon_name = icon + if newval: + self.icon_name = self.active_icon_name + self.props.tooltip_text = self.active_tooltip_text + else: + self.icon_name = self.inactive_icon_name + self.props.tooltip_text = self.inactive_tooltip_text self.emit("toggled") @GObject.Signal diff --git a/emmental/nowplaying/__init__.py b/emmental/nowplaying/__init__.py index f2e647c..ddb0ff8 100644 --- a/emmental/nowplaying/__init__.py +++ b/emmental/nowplaying/__init__.py @@ -41,8 +41,8 @@ class Card(Gtk.Box): self._bottom_box = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0) self._favorite = buttons.ImageToggle("heart-filled", "heart-outline-thick-symbolic", - tooltip_text="add to " - "'Favorite Tracks'", + "remove from 'Favorite Tracks'", + "add to 'Favorite Tracks'", icon_size=Gtk.IconSize.LARGE, has_frame=False, sensitive=False, valign=Gtk.Align.CENTER) diff --git a/tests/nowplaying/test_nowplaying.py b/tests/nowplaying/test_nowplaying.py index 7804044..bdcf180 100644 --- a/tests/nowplaying/test_nowplaying.py +++ b/tests/nowplaying/test_nowplaying.py @@ -89,9 +89,11 @@ class TestNowPlaying(unittest.TestCase): self.card._favorite) self.assertEqual(self.card._favorite.active_icon_name, "heart-filled") + self.assertEqual(self.card._favorite.active_tooltip_text, + "remove from 'Favorite Tracks'") self.assertEqual(self.card._favorite.inactive_icon_name, "heart-outline-thick-symbolic") - self.assertEqual(self.card._favorite.get_tooltip_text(), + self.assertEqual(self.card._favorite.inactive_tooltip_text, "add to 'Favorite Tracks'") self.assertEqual(self.card._favorite.icon_size, Gtk.IconSize.LARGE) self.assertEqual(self.card._favorite.get_valign(), Gtk.Align.CENTER) diff --git a/tests/test_buttons.py b/tests/test_buttons.py index 3904c9b..6553528 100644 --- a/tests/test_buttons.py +++ b/tests/test_buttons.py @@ -190,6 +190,24 @@ class TestImageToggle(unittest.TestCase): active=True) self.assertTrue(button2.active) + def test_tooltip_text(self): + """Test the active and inactive tooltip text.""" + self.assertEqual(self.button.active_tooltip_text, None) + self.assertEqual(self.button.inactive_tooltip_text, None) + + button2 = emmental.buttons.ImageToggle("active", "inactive", + "active tooltip text", + "inactive tooltip text") + self.assertEqual(button2.active_tooltip_text, "active tooltip text") + self.assertEqual(button2.inactive_tooltip_text, + "inactive tooltip text") + self.assertEqual(button2.get_tooltip_text(), "inactive tooltip text") + + button2.active = True + self.assertEqual(button2.get_tooltip_text(), "active tooltip text") + button2.active = False + self.assertEqual(button2.get_tooltip_text(), "inactive tooltip text") + def test_toggle(self): """Test the toggle signal.""" toggled = unittest.mock.Mock()