buttons: Replace the icon-size property with large-icon

It is much easier to pass a single boolean value instead of a Gtk
constant for specifying the icon size.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-06-10 21:25:54 -04:00
parent a13e481754
commit d807f8bd36
12 changed files with 64 additions and 59 deletions

View File

@ -8,22 +8,29 @@ class Button(Gtk.Button):
"""A Gtk.Button with extra properties and default large size."""
icon_name = GObject.Property(type=str)
icon_size = GObject.Property(type=Gtk.IconSize,
default=Gtk.IconSize.NORMAL)
icon_opacity = GObject.Property(type=float, default=1.0,
minimum=0.0, maximum=1.0)
def __init__(self, **kwargs):
def __init__(self, large_icon: bool = False, **kwargs):
"""Initialize a Button."""
super().__init__(focusable=False, **kwargs)
self._image = Gtk.Image(icon_name=self.icon_name,
icon_size=self.icon_size,
icon_size = Gtk.IconSize.LARGE if large_icon else Gtk.IconSize.NORMAL
self._image = Gtk.Image(icon_name=self.icon_name, icon_size=icon_size,
opacity=self.icon_opacity)
self.bind_property("icon-name", self._image, "icon-name")
self.bind_property("icon-size", self._image, "icon-size")
self.bind_property("icon-opacity", self._image, "opacity")
self.set_child(self._image)
@GObject.Property(type=bool, default=False)
def large_icon(self) -> bool:
"""Get if this Button has a large icon."""
return self._image.get_icon_size() == Gtk.IconSize.LARGE
@large_icon.setter
def large_icon(self, newval: bool) -> None:
size = Gtk.IconSize.LARGE if newval else Gtk.IconSize.NORMAL
self._image.set_icon_size(size)
class PopoverButton(Gtk.MenuButton):
"""A MenuButton with a Gtk.Popover attached."""
@ -45,20 +52,19 @@ class SplitButton(Gtk.Box):
"""A Button and secondary widget packed together."""
icon_name = GObject.Property(type=str)
icon_size = GObject.Property(type=Gtk.IconSize,
default=Gtk.IconSize.NORMAL)
large_icon = GObject.Property(type=bool, default=False)
def __init__(self, secondary: Gtk.Button, **kwargs):
"""Initialize a Split Button."""
super().__init__(**kwargs)
self._primary = Button(hexpand=True, icon_name=self.icon_name,
icon_size=self.icon_size)
large_icon=self.large_icon)
self._separator = Gtk.Separator(orientation=Gtk.Orientation.VERTICAL,
margin_top=12, margin_bottom=12)
self._secondary = secondary
self.bind_property("icon-name", self._primary, "icon-name")
self.bind_property("icon-size", self._primary, "icon-size")
self.bind_property("large-icon", self._primary, "large-icon")
self._primary.connect("activate", self.__activate)
self._primary.connect("clicked", self.__clicked)

View File

@ -43,13 +43,13 @@ class Card(Gtk.Box):
"heart-outline-thick-symbolic",
"remove from 'Favorite Tracks'",
"add to 'Favorite Tracks'",
icon_size=Gtk.IconSize.LARGE,
large_icon=True,
has_frame=False, sensitive=False,
valign=Gtk.Align.CENTER)
self._jump = buttons.Button(icon_name="go-jump", has_frame=False,
icon_size=Gtk.IconSize.LARGE,
tooltip_text="scroll to current track",
valign=Gtk.Align.CENTER, sensitive=False)
large_icon=True, sensitive=False,
valign=Gtk.Align.CENTER)
self._seeker = seeker.Scale(sensitive=False)
self.bind_property("artwork", self._artwork, "filepath")

View File

@ -14,7 +14,7 @@ class PillButton(buttons.Button):
def __init__(self, **kwargs):
"""Initialize a Pill Button."""
super().__init__(icon_size=Gtk.IconSize.LARGE, **kwargs)
super().__init__(large_icon=True, **kwargs)
self.add_css_class("pill")
@ -46,7 +46,7 @@ class Controls(Gtk.Box):
self._play = PillButton(icon_name="play-large", tooltip_text="play",
sensitive=False)
self._pause = buttons.SplitButton(icon_name="pause-large",
icon_size=Gtk.IconSize.LARGE,
large_icon=True,
tooltip_text="pause",
secondary=self._autopause,
visible=False, sensitive=False)

View File

@ -1,7 +1,6 @@
# Copyright 2022 (c) Anna Schumaker.
"""Displays our artist and album tree."""
from gi.repository import GObject
from gi.repository import Gtk
from ..buttons import ImageToggle
from .. import db
from . import row
@ -36,8 +35,7 @@ class Section(section.Section):
subtitle="0 artists, 0 albums",
icon_name="library-artists", album_table=album_table)
self.extra_widget = ImageToggle("music-artist", "music-artist2",
icon_size=Gtk.IconSize.NORMAL,
has_frame=False)
large_icon=False, has_frame=False)
self.album_table.connect("items-changed", self.__update_subtitle)
self.bind_property("show-all", self.extra_widget, "active",
GObject.BindingFlags.BIDIRECTIONAL)

View File

@ -54,7 +54,7 @@ class LoopButton(buttons.ImageToggle):
"""Initialize a Loop Button."""
super().__init__(active_icon_name="media-playlist-repeat-song",
inactive_icon_name="media-playlist-repeat",
icon_size=Gtk.IconSize.NORMAL, state="None",
large_icon=False, state="None",
has_frame=False, **kwargs)
def do_clicked(self):
@ -94,7 +94,7 @@ class ShuffleButton(buttons.ImageToggle):
"""Initialize a Shuffle Button."""
super().__init__(active_icon_name="media-playlist-shuffle",
inactive_icon_name="media-playlist-consecutive",
icon_size=Gtk.IconSize.NORMAL, icon_opacity=0.5,
large_icon=False, icon_opacity=0.5,
has_frame=False, **kwargs)
def do_toggled(self):
@ -113,8 +113,7 @@ class SortFieldWidget(Gtk.Box):
self._enabled = Gtk.Switch(valign=Gtk.Align.CENTER)
self._name = Gtk.Label(hexpand=True, sensitive=False)
self._reverse = buttons.ImageToggle("arrow1-up", "arrow1-down",
icon_size=Gtk.IconSize.NORMAL,
sensitive=False)
large_icon=False, sensitive=False)
self._box = Gtk.Box(sensitive=False)
self._move_up = Gtk.Button(icon_name="go-up-symbolic")
self._move_down = Gtk.Button(icon_name="go-down-symbolic")

View File

@ -330,9 +330,8 @@ class FavoriteButton(TrackRow):
"""Initialize a Favorite Button."""
super().__init__(listitem, property=property)
self.child = buttons.ImageToggle("heart-filled", "heart-outline-thick",
icon_size=Gtk.IconSize.NORMAL,
valign=Gtk.Align.CENTER,
has_frame=False)
large_icon=False, has_frame=False,
valign=Gtk.Align.CENTER)
def do_bind(self):
"""Bind a track property to the Toggle Button."""

View File

@ -13,7 +13,7 @@ class TestButtons(unittest.TestCase):
"""Test that the pill button is configured correctly."""
button = emmental.nowplaying.controls.PillButton()
self.assertIsInstance(button, emmental.buttons.Button)
self.assertEqual(button.icon_size, Gtk.IconSize.LARGE)
self.assertTrue(button.large_icon)
self.assertTrue(button.has_css_class("pill"))
@ -99,13 +99,13 @@ class TestControls(unittest.TestCase):
"""Test the pause button."""
self.assertIsInstance(self.controls._pause,
emmental.buttons.SplitButton)
self.assertEqual(self.controls._pause.get_tooltip_text(), "pause")
self.assertEqual(self.controls._pause.icon_name, "pause-large")
self.assertEqual(self.controls._pause.icon_size,
Gtk.IconSize.LARGE)
self.assertEqual(self.controls._play.get_next_sibling(),
self.controls._pause)
self.assertEqual(self.controls._pause.get_tooltip_text(), "pause")
self.assertEqual(self.controls._pause.icon_name, "pause-large")
self.assertTrue(self.controls._pause.large_icon)
self.assertFalse(self.controls._pause.get_visible())
self.controls.playing = True
self.assertTrue(self.controls._pause.get_visible())

View File

@ -95,9 +95,9 @@ class TestNowPlaying(unittest.TestCase):
"heart-outline-thick-symbolic")
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)
self.assertFalse(self.card._favorite.get_has_frame())
self.assertTrue(self.card._favorite.large_icon)
self.assertFalse(self.card._favorite.get_sensitive())
self.card.have_db_track = True
@ -118,9 +118,9 @@ class TestNowPlaying(unittest.TestCase):
self.assertEqual(self.card._jump.icon_name, "go-jump")
self.assertEqual(self.card._jump.get_tooltip_text(),
"scroll to current track")
self.assertEqual(self.card._jump.icon_size, Gtk.IconSize.LARGE)
self.assertEqual(self.card._jump.get_valign(), Gtk.Align.CENTER)
self.assertFalse(self.card._jump.get_has_frame())
self.assertTrue(self.card._jump.large_icon)
self.assertFalse(self.card._jump.get_sensitive())
self.card.have_db_track = True

View File

@ -35,8 +35,7 @@ class TestArtist(tests.util.TestCase):
"music-artist")
self.assertEqual(self.artists.extra_widget.inactive_icon_name,
"music-artist2")
self.assertEqual(self.artists.extra_widget.icon_size,
Gtk.IconSize.NORMAL)
self.assertFalse(self.artists.extra_widget.large_icon)
self.assertFalse(self.artists.extra_widget.get_has_frame())
def test_subtitle(self):

View File

@ -30,17 +30,6 @@ class TestButton(unittest.TestCase):
self.assertEqual(button2.icon_name, "icon-name")
self.assertEqual(button2._image.get_icon_name(), "icon-name")
def test_icon_size(self):
"""Test the icon-size property."""
self.assertEqual(self.button.icon_size, Gtk.IconSize.NORMAL)
self.button.icon_size = Gtk.IconSize.LARGE
self.assertEqual(self.button._image.get_icon_size(),
Gtk.IconSize.LARGE)
button2 = emmental.buttons.Button(icon_size=Gtk.IconSize.LARGE)
self.assertEqual(button2.icon_size, Gtk.IconSize.LARGE)
self.assertEqual(button2._image.get_icon_size(), Gtk.IconSize.LARGE)
def test_icon_opacity(self):
"""Test the icon-opacity property."""
self.assertEqual(self.button.icon_opacity, 1.0)
@ -52,6 +41,20 @@ class TestButton(unittest.TestCase):
self.assertAlmostEqual(button2.icon_opacity, 0.25, delta=0.005)
self.assertAlmostEqual(button2._image.get_opacity(), 0.25, delta=0.005)
def test_large_icon(self):
"""Test the large-icon property."""
self.assertFalse(self.button.large_icon)
self.assertEqual(self.button._image.get_icon_size(),
Gtk.IconSize.NORMAL)
self.button.large_icon = True
self.assertEqual(self.button._image.get_icon_size(),
Gtk.IconSize.LARGE)
button2 = emmental.buttons.Button(large_icon=True)
self.assertTrue(button2.large_icon)
self.assertEqual(button2._image.get_icon_size(), Gtk.IconSize.LARGE)
class TestPopoverButton(unittest.TestCase):
"""Test a Popover Button."""
@ -123,16 +126,16 @@ class TestSplitButton(unittest.TestCase):
self.assertEqual(button2.icon_name, "icon-name")
self.assertEqual(button2._primary.icon_name, "icon-name")
def test_icon_size(self):
"""Test the icon size property."""
self.assertEqual(self.button.icon_size, Gtk.IconSize.NORMAL)
self.button.icon_size = Gtk.IconSize.LARGE
self.assertEqual(self.button._primary.icon_size, Gtk.IconSize.LARGE)
def test_large_icon(self):
"""Test the large icon property."""
self.assertFalse(self.button.large_icon)
self.button.large_icon = True
self.assertTrue(self.button._primary.large_icon)
button2 = emmental.buttons.SplitButton(icon_size=Gtk.IconSize.LARGE,
button2 = emmental.buttons.SplitButton(large_icon=True,
secondary=Gtk.Button())
self.assertEqual(button2.icon_size, Gtk.IconSize.LARGE)
self.assertEqual(button2._primary.icon_size, Gtk.IconSize.LARGE)
self.assertTrue(button2.large_icon)
self.assertTrue(button2._primary.large_icon)
def test_secondary(self):
"""Test the secondary property."""

View File

@ -84,7 +84,7 @@ class TestLoopButton(unittest.TestCase):
self.assertEqual(self.loop.active_icon_name,
"media-playlist-repeat-song")
self.assertEqual(self.loop.inactive_icon_name, "media-playlist-repeat")
self.assertEqual(self.loop.icon_size, Gtk.IconSize.NORMAL)
self.assertFalse(self.loop.large_icon)
self.assertFalse(self.loop.get_has_frame())
def test_state(self):
@ -143,12 +143,12 @@ class TestShuffleButtons(unittest.TestCase):
def test_init(self):
"""Test that the shuffle button is configured correctly."""
self.assertIsInstance(self.shuffle, emmental.buttons.ImageToggle)
self.assertEqual(self.shuffle.icon_size, Gtk.IconSize.NORMAL)
self.assertEqual(self.shuffle.active_icon_name,
"media-playlist-shuffle")
self.assertEqual(self.shuffle.inactive_icon_name,
"media-playlist-consecutive")
self.assertAlmostEqual(self.shuffle.icon_opacity, 0.5, delta=0.005)
self.assertFalse(self.shuffle.large_icon)
self.assertFalse(self.shuffle.get_has_frame())
def test_opacity(self):
@ -262,12 +262,13 @@ class TestSortFieldWidget(unittest.TestCase):
def test_reverse(self):
"""Test reversing a sort field."""
self.assertIsInstance(self.sort._reverse, emmental.buttons.ImageToggle)
self.assertEqual(self.sort._reverse.active_icon_name, "arrow1-up")
self.assertEqual(self.sort._reverse.inactive_icon_name, "arrow1-down")
self.assertEqual(self.sort._reverse.icon_size, Gtk.IconSize.NORMAL)
self.assertEqual(self.sort._name.get_next_sibling(),
self.sort._reverse)
self.assertEqual(self.sort._reverse.active_icon_name, "arrow1-up")
self.assertEqual(self.sort._reverse.inactive_icon_name, "arrow1-down")
self.assertFalse(self.sort._reverse.large_icon)
self.sort._reverse.emit("clicked")
self.sort.set_sort_field(self.model[0])

View File

@ -232,9 +232,9 @@ class TestTrackRowWidgets(tests.util.TestCase):
self.assertEqual(row.property, "favorite")
self.assertEqual(row.child.active_icon_name, "heart-filled")
self.assertEqual(row.child.inactive_icon_name, "heart-outline-thick")
self.assertEqual(row.child.icon_size, Gtk.IconSize.NORMAL)
self.assertEqual(row.child.get_valign(), Gtk.Align.CENTER)
self.assertFalse(row.child.get_has_frame())
self.assertFalse(row.child.large_icon)
self.track.favorite = True
row.bind()