buttons: Add a generic Button class

This Button is like a Gtk.Button, except it provides ways to set the
icon-size. I also default to large buttons, since that'll be a good
portion of the users.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-09-26 09:32:07 -04:00
parent 8ec5239acc
commit b3c2dd25fb
2 changed files with 68 additions and 0 deletions

View File

@ -4,6 +4,27 @@ from gi.repository import GObject
from gi.repository import Gtk
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):
"""Initialize a Button."""
super().__init__(focusable=False, **kwargs)
self._image = Gtk.Image(icon_name=self.icon_name,
icon_size=self.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)
class PopoverButton(Gtk.MenuButton):
"""A MenuButton with a Gtk.Popover attached."""

View File

@ -5,6 +5,53 @@ import emmental.buttons
from gi.repository import Gtk
class TestButton(unittest.TestCase):
"""Test a Button."""
def setUp(self):
"""Set up common variables."""
self.button = emmental.buttons.Button()
def test_init(self):
"""Test that the button is configured correctly."""
self.assertIsInstance(self.button, Gtk.Button)
self.assertIsInstance(self.button._image, Gtk.Image)
self.assertEqual(self.button.get_child(), self.button._image)
self.assertFalse(self.button.get_focusable())
def test_icon_name(self):
"""Test the icon-name property."""
self.assertEqual(self.button.icon_name, "")
self.button.icon_name = "icon-name"
self.assertEqual(self.button._image.get_icon_name(), "icon-name")
button2 = emmental.buttons.Button(icon_name="icon-name")
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)
self.button.icon_opacity = 0.5
self.assertAlmostEqual(self.button._image.get_opacity(),
0.5, delta=0.005)
button2 = emmental.buttons.Button(icon_opacity=0.25)
self.assertAlmostEqual(button2.icon_opacity, 0.25, delta=0.005)
self.assertAlmostEqual(button2._image.get_opacity(), 0.25, delta=0.005)
class TestPopoverButton(unittest.TestCase):
"""Test a Popover Button."""