emmental/emmental/nowplaying/controls.py

96 lines
3.8 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""Our playback control widgets."""
from gi.repository import GObject
from gi.repository import Gtk
from . import autopause
from .. import buttons
from .. import window
MARGIN = 24
class PillButton(buttons.Button):
"""A Button with the pill style class."""
def __init__(self, **kwargs):
"""Initialize a Pill Button."""
super().__init__(icon_size=Gtk.IconSize.LARGE, **kwargs)
self.add_css_class("pill")
class Controls(Gtk.Box):
"""Our playback control widgets."""
autopause = GObject.Property(type=int, default=-1, minimum=-1, maximum=99)
playing = GObject.Property(type=bool, default=False)
have_next = GObject.Property(type=bool, default=False)
have_previous = GObject.Property(type=bool, default=False)
have_track = GObject.Property(type=bool, default=False)
def __init__(self):
"""Initialize the Controls."""
super().__init__(valign=Gtk.Align.START, homogeneous=True,
halign=Gtk.Align.END, hexpand=False,
margin_start=MARGIN/2, margin_end=MARGIN,
margin_top=MARGIN, margin_bottom=MARGIN)
self._autopause = autopause.Button()
self._prev = PillButton(icon_name="media-skip-backward",
tooltip_text="previous track", sensitive=False)
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,
tooltip_text="pause",
secondary=self._autopause,
visible=False, sensitive=False)
self._next = PillButton(icon_name="media-skip-forward",
tooltip_text="next track", sensitive=False)
for button in [self._prev, self._play, self._pause, self._next]:
self.append(button)
self._prev.connect("clicked", self.__on_click, "previous")
self._play.connect("clicked", self.__on_click, "play")
self._pause.connect("clicked", self.__on_click, "pause")
self._next.connect("clicked", self.__on_click, "next")
self.bind_property("autopause", self._autopause, "value",
GObject.BindingFlags.BIDIRECTIONAL)
self.bind_property("playing", self._pause, "visible")
self.bind_property("playing", self._play, "visible",
GObject.BindingFlags.INVERT_BOOLEAN)
self.bind_property("have-next", self._next, "sensitive")
self.bind_property("have-previous", self._prev, "sensitive")
self.bind_property("have-track", self._play, "sensitive")
self.bind_property("have-track", self._pause, "sensitive")
self.connect("notify::playing", self.__notify_playing)
self.add_css_class("linked")
def __on_click(self, button: Gtk.Button, signal: str) -> None:
self.emit(signal)
def __notify_playing(self, controls, param) -> None:
if not self.playing and self.autopause != -1:
if win := self.get_ancestor(window.Window):
win.post_toast("Autopause Cancelled")
self.autopause = -1
@GObject.Signal
def previous(self) -> None:
"""Signals that the Previous button has been clicked."""
@GObject.Signal
def play(self) -> None:
"""Signals that the Play button has been clicked."""
@GObject.Signal
def pause(self) -> None:
"""Signals that the Pause button has been clicked."""
@GObject.Signal
def next(self) -> None:
"""Signals that the Next button has been clicked."""