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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-06-10 20:25:11 -04:00
parent afb0ba5d68
commit a13e481754
4 changed files with 40 additions and 7 deletions

View File

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

View File

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

View File

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

View File

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