audio: Add a custom PlayPause button

And update the image based on the current playback state

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-09-01 14:00:18 -04:00
parent ec9ed14474
commit 33c7bdf517
2 changed files with 38 additions and 0 deletions

View File

@ -27,6 +27,19 @@ class NextButton(ControlButton):
self.player.next()
class PlayPauseButton(ControlButton):
def __init__(self, player):
ControlButton.__init__(self, player, "media-playback-start")
self.player.connect("state-changed", self.on_state_changed)
def do_clicked(self):
self.player.playpause()
def on_state_changed(self, player, old, new, pending):
icon = "pause" if new == Gst.State.PLAYING else "start"
self.set_icon_name(f"media-playback-{icon}")
class Controls(Gtk.Box):
def __init__(self):
Gtk.Box.__init__(self)

View File

@ -10,9 +10,17 @@ class FakePlayer(GObject.GObject):
GObject.GObject.__init__(self)
self.prev = False
self.nxt = False
self.play = False
def previous(self): self.prev = True
def next(self): self.nxt = True
def playpause(self):
states = { True : Gst.State.PLAYING, False : Gst.State.PAUSED }
self.play = not self.play
self.emit("state-changed", Gst.State.NULL, states[self.play], Gst.State.NULL)
@GObject.Signal(arg_types=(Gst.State, Gst.State, Gst.State))
def state_changed(self, old, new, pending): pass
class TestControlButton(unittest.TestCase):
@ -47,6 +55,23 @@ class TestNextButton(unittest.TestCase):
self.assertTrue(fake.nxt)
class TestPlayPauseButton(unittest.TestCase):
def test_play_pause_button(self):
fake = FakePlayer()
play = controls.PlayPauseButton(fake)
self.assertIsInstance(play, controls.ControlButton)
self.assertEqual(play.get_icon_name(), "media-playback-start")
play.emit("clicked")
self.assertTrue(fake.play)
self.assertEqual(play.get_icon_name(), "media-playback-pause")
play.emit("clicked")
self.assertFalse(fake.play)
self.assertEqual(play.get_icon_name(), "media-playback-start")
class TestControls(unittest.TestCase):
def test_controls_init(self):
ctrl = controls.Controls()