From bf8d7fac1b700c879ed5deefabd114ef56e0cfe5 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Mon, 26 Sep 2022 16:48:35 -0400 Subject: [PATCH] nowplaying: Create an Autopause Button This is a PopoverButton that has an autopause.Entry set as the child. I also override the displayed icon to show the current autopause count. Signed-off-by: Anna Schumaker --- emmental/nowplaying/autopause.py | 27 +++++++++++++++++ tests/nowplaying/test_autopause.py | 47 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/emmental/nowplaying/autopause.py b/emmental/nowplaying/autopause.py index 975e11e..d67004d 100644 --- a/emmental/nowplaying/autopause.py +++ b/emmental/nowplaying/autopause.py @@ -4,6 +4,7 @@ import re from gi.repository import GObject from gi.repository import GLib from gi.repository import Gtk +from .. import buttons class Entry(Gtk.Entry): @@ -87,3 +88,29 @@ class Entry(Gtk.Entry): self.value > -1) self.set_icon_sensitive(Gtk.EntryIconPosition.SECONDARY, self.value < 99) + + +class Button(buttons.PopoverButton): + """A PopoverButton that displays Autopause count.""" + + value = GObject.Property(type=int, default=-1, minimum=-1, maximum=99) + + def __init__(self, **kwargs): + """Initialize an Autopause Button.""" + super().__init__(popover_child=Entry(), **kwargs) + self._arrow = Gtk.Image(icon_name="pan-down-symbolic", margin_top=6) + self._count = Gtk.Label(yalign=0, halign=Gtk.Align.CENTER, + margin_top=3) + self._overlay = Gtk.Overlay(child=self._arrow) + + self._overlay.add_overlay(self._count) + self.bind_property("value", self.popover_child, "value", + GObject.BindingFlags.BIDIRECTIONAL) + self.connect("notify::value", self.__notify_value) + + self._count.set_markup("") + self.set_child(self._overlay) + + def __notify_value(self, button: buttons.PopoverButton, param) -> None: + text = str(self.value) if self.value > -1 else "" + self._count.set_markup(f"{text}") diff --git a/tests/nowplaying/test_autopause.py b/tests/nowplaying/test_autopause.py index 956a3a0..4621310 100644 --- a/tests/nowplaying/test_autopause.py +++ b/tests/nowplaying/test_autopause.py @@ -146,3 +146,50 @@ class TestAutopauseEntry(unittest.TestCase): self.entry.set_text(text) self.entry.emit("activate") self.assertEqual(self.entry.value, value, f"text=\"{text}\"") + + +class TestAutopauseButton(unittest.TestCase): + """Test our Autopause Popover Button.""" + + def setUp(self): + """Set up common variables.""" + self.button = emmental.nowplaying.autopause.Button() + + def test_init(self): + """Test the autopause button.""" + self.assertIsInstance(self.button, emmental.buttons.PopoverButton) + self.assertIsInstance(self.button._overlay, Gtk.Overlay) + self.assertIsInstance(self.button.popover_child, + emmental.nowplaying.autopause.Entry) + self.assertEqual(self.button.get_child(), self.button._overlay) + + def test_arrow(self): + """Test that the arrow image was set up properly.""" + self.assertIsInstance(self.button._arrow, Gtk.Image) + self.assertEqual(self.button._arrow.get_icon_name(), + "pan-down-symbolic") + self.assertEqual(self.button._arrow.get_margin_top(), 6) + self.assertEqual(self.button._overlay.get_child(), self.button._arrow) + + def test_count(self): + """Test that the count label was set up properly.""" + self.assertIsInstance(self.button._count, Gtk.Label) + self.assertEqual(self.button._count.get_yalign(), 0) + self.assertEqual(self.button._count.get_halign(), Gtk.Align.CENTER) + self.assertEqual(self.button._count.get_margin_top(), 3) + self.assertEqual(self.button._count.get_text(), "") + + def test_value(self): + """Test the value property.""" + self.assertEqual(self.button.value, -1) + + self.button.value = 0 + self.assertEqual(self.button.popover_child.value, 0) + self.assertEqual(self.button._count.get_text(), "0") + + self.button.popover_child.value = 1 + self.assertEqual(self.button.value, 1) + self.assertEqual(self.button._count.get_text(), "1") + + self.button.value = -1 + self.assertEqual(self.button._count.get_text(), "")