buttons: Create a PopoverButton

This is a MenuButton that already has a popover attached and a property
for setting the popover child directly.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2022-08-16 15:57:52 -04:00
parent a73063a04c
commit 6ede296ba6
2 changed files with 54 additions and 0 deletions

20
emmental/buttons.py Normal file
View File

@ -0,0 +1,20 @@
# Copyright 2022 (c) Anna Schumaker.
"""Helper classes for Buttons."""
from gi.repository import GObject
from gi.repository import Gtk
class PopoverButton(Gtk.MenuButton):
"""A MenuButton with a Gtk.Popover attached."""
popover_child = GObject.Property(type=Gtk.Widget)
def __init__(self, **kwargs):
"""Initialize a popover.Button."""
super().__init__(popover=Gtk.Popover(), **kwargs)
self.bind_property("popover-child", self.get_popover(), "child")
self.get_popover().set_child(self.popover_child)
def popdown(self):
"""Close the popover."""
self.get_popover().popdown()

34
tests/test_buttons.py Normal file
View File

@ -0,0 +1,34 @@
# Copyright 2022 (c) Anna Schumaker.
"""Test our button helper classes."""
import unittest
import emmental.buttons
from gi.repository import Gtk
class TestPopoverButton(unittest.TestCase):
"""Test a Popover Button."""
def setUp(self):
"""Set up common variables."""
self.button = emmental.buttons.PopoverButton()
def test_button(self):
"""Test that the popover button is configured correctly."""
self.assertIsInstance(self.button, Gtk.MenuButton)
self.assertIsInstance(self.button.get_popover(), Gtk.Popover)
def test_popover_child(self):
"""Test setting a child widget to the popover button."""
self.assertIsNone(self.button.popover_child)
self.button.popover_child = Gtk.Label()
self.assertEqual(self.button.get_popover().get_child(),
self.button.popover_child)
button2 = emmental.buttons.PopoverButton(popover_child=Gtk.Label())
self.assertIsInstance(button2.popover_child, Gtk.Label)
self.assertEqual(button2.get_popover().get_child(),
button2.popover_child)
def test_popdown(self):
"""Test the popdown() function."""
self.button.popdown()