initial
This commit is contained in:
commit
451181b9fa
4 changed files with 219 additions and 0 deletions
112
playerctl_gui.py
Normal file
112
playerctl_gui.py
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#!/usr/bin/env python3
|
||||
import subprocess
|
||||
from typing import Optional
|
||||
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import GLib, Gtk
|
||||
|
||||
|
||||
METADATA_FORMAT = "{{{{status}}}} — {{{{artist}}}} — {{{{title}}}}"
|
||||
|
||||
|
||||
class PlayerctlApp(Gtk.Application):
|
||||
def __init__(self) -> None:
|
||||
super().__init__(application_id="dev.codex.PlayerctlGUI")
|
||||
self._label: Optional[Gtk.Label] = None
|
||||
|
||||
def do_activate(self, *args) -> None: # type: ignore[override]
|
||||
if self.props.active_window:
|
||||
self.props.active_window.present()
|
||||
return
|
||||
|
||||
window = Gtk.ApplicationWindow(application=self)
|
||||
window.set_title("Playerctl Controls")
|
||||
window.set_border_width(12)
|
||||
window.set_default_size(320, 120)
|
||||
|
||||
self._label = Gtk.Label(label="Connecting to player…")
|
||||
self._label.set_hexpand(True)
|
||||
self._label.set_line_wrap(True)
|
||||
self._label.set_justify(Gtk.Justification.CENTER)
|
||||
|
||||
controls = Gtk.Box(spacing=6)
|
||||
controls.set_orientation(Gtk.Orientation.HORIZONTAL)
|
||||
|
||||
prev_button = Gtk.Button.new_with_label("Prev")
|
||||
prev_button.connect("clicked", self._on_prev_clicked)
|
||||
controls.pack_start(prev_button, True, True, 0)
|
||||
|
||||
toggle_button = Gtk.Button.new_with_label("Play/Pause")
|
||||
toggle_button.connect("clicked", self._on_toggle_clicked)
|
||||
controls.pack_start(toggle_button, True, True, 0)
|
||||
|
||||
next_button = Gtk.Button.new_with_label("Next")
|
||||
next_button.connect("clicked", self._on_next_clicked)
|
||||
controls.pack_start(next_button, True, True, 0)
|
||||
|
||||
layout = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
|
||||
layout.pack_start(self._label, True, True, 0)
|
||||
layout.pack_start(controls, False, False, 0)
|
||||
|
||||
window.add(layout)
|
||||
window.show_all()
|
||||
|
||||
GLib.timeout_add_seconds(2, self._refresh_metadata)
|
||||
self._refresh_metadata()
|
||||
|
||||
def _on_toggle_clicked(self, _button: Gtk.Button) -> None:
|
||||
self._run_playerctl(["play-pause"])
|
||||
GLib.idle_add(self._refresh_metadata)
|
||||
|
||||
def _on_next_clicked(self, _button: Gtk.Button) -> None:
|
||||
self._run_playerctl(["next"])
|
||||
GLib.idle_add(self._refresh_metadata)
|
||||
|
||||
def _on_prev_clicked(self, _button: Gtk.Button) -> None:
|
||||
self._run_playerctl(["previous"])
|
||||
GLib.idle_add(self._refresh_metadata)
|
||||
|
||||
def _refresh_metadata(self) -> bool:
|
||||
if not self._label:
|
||||
return False
|
||||
|
||||
metadata = self._run_playerctl(["metadata", "--format", METADATA_FORMAT])
|
||||
if metadata is None or not metadata.strip():
|
||||
status = self._run_playerctl(["status"])
|
||||
if status is None:
|
||||
self._label.set_text("No active media player detected")
|
||||
else:
|
||||
self._label.set_text(status.strip())
|
||||
else:
|
||||
self._label.set_text(metadata.strip())
|
||||
return True
|
||||
|
||||
def _run_playerctl(self, args: list[str]) -> Optional[str]:
|
||||
cmd = ["playerctl", *args]
|
||||
try:
|
||||
completed = subprocess.run(
|
||||
cmd,
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5,
|
||||
)
|
||||
except (FileNotFoundError, subprocess.SubprocessError):
|
||||
if self._label:
|
||||
self._label.set_text("playerctl command not available")
|
||||
return None
|
||||
|
||||
if completed.returncode != 0:
|
||||
return None
|
||||
return completed.stdout
|
||||
|
||||
|
||||
def main() -> None:
|
||||
app = PlayerctlApp()
|
||||
app.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue