#!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GObject from .slider import Slider def time_to_str(stamp = 0): m = stamp // 60 s = stamp % 60 if m < 10: m = f"0{m}" if s < 10: s = f"0{s}" return f"{m}:{s}" class Timebar(Gtk.Fixed): __gsignals__ = { 'seeked': (GObject.SignalFlags.RUN_FIRST, None, (float,)) } def __init__(self): Gtk.Fixed.__init__(self) self._duration = 0 self._time = 0 self.slider = Slider(272) self.curr = Gtk.Label() self.duration = Gtk.Label() self.curr.set_name('text') self.duration.set_justify(Gtk.Justification.RIGHT) self.duration.set_xalign(1) self.duration.set_name('text') self.slider.connect('change-value', self.on_timeupdate) self.put(self.curr, 3, 0) self.put(self.duration, 233, 0) self.put(self.slider, 0, 12) def update_time(self, curr = 0, duration = 0): self._duration = duration self._time = curr progress = curr * 100 / duration self.curr.set_text(time_to_str(curr)) self.duration.set_text(time_to_str(duration)) self.slider.set_value(progress) def on_timeupdate(self, slider, a, b): # print(slider.get_value(), a, b) p = slider.get_value() time = p * self._duration / 100 self.emit('seeked', time)