Anna Schumaker
2ff03bba18
This is a Gtk.Scale configured to be used to display track progress and seek inside the track. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""A Gtk.Scale configured for position tracking seeking."""
|
|
from gi.repository import GObject
|
|
from gi.repository import Gtk
|
|
from gi.repository import Gst
|
|
|
|
|
|
class Scale(Gtk.Scale):
|
|
"""A Gtk.Scale configured for our application."""
|
|
|
|
def __init__(self, **kwargs):
|
|
"""Initialize our Scale."""
|
|
super().__init__(margin_start=45, margin_end=45, draw_value=True,
|
|
hexpand=True, **kwargs)
|
|
self._adjustment = Gtk.Adjustment.new(value=0, lower=0, upper=1,
|
|
step_increment=5*Gst.SECOND,
|
|
page_increment=30*Gst.SECOND,
|
|
page_size=0)
|
|
self.set_adjustment(self._adjustment)
|
|
self.set_format_value_func(self.format_value)
|
|
|
|
def format_value(self, scale: Gtk.Scale, value: float) -> str:
|
|
"""Format the position and duration values."""
|
|
duration = round(self.duration * Gst.USECOND / Gst.SECOND)
|
|
position = round(value * Gst.USECOND / Gst.SECOND)
|
|
remaining = duration - position
|
|
(p_m, p_s) = divmod(position, 60)
|
|
(r_m, r_s) = divmod(remaining, 60)
|
|
return f"{p_m:02}:{p_s:02} / {r_m:02}:{r_s:02}"
|
|
|
|
@GObject.Property(type=float)
|
|
def duration(self) -> float:
|
|
"""Get the duration of the current track."""
|
|
return self._adjustment.get_upper()
|
|
|
|
@duration.setter
|
|
def duration(self, newval: float) -> None:
|
|
"""Set the duration of the current track."""
|
|
self.set_range(0, max(newval, 1))
|
|
self.emit("value-changed")
|
|
|
|
@GObject.Property(type=float)
|
|
def position(self) -> float:
|
|
"""Get the position of the current track."""
|
|
return self.get_value()
|
|
|
|
@position.setter
|
|
def position(self, newval: float) -> None:
|
|
"""Set the position of the current track."""
|
|
self.set_value(newval)
|