sonist-gtk/ui/volume.py

54 lines
1.1 KiB
Python
Raw Normal View History

2023-08-24 18:21:45 +08:00
#!/usr/bin/env python3
import gi, threading
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class Volume(Gtk.Popover):
__gsignals__ = {
'volume_changed': (GObject.SignalFlags.RUN_FIRST, None, (int,))
}
timer = None
2023-08-24 19:36:37 +08:00
volume = 0
2023-08-24 18:21:45 +08:00
def __init__(self, btn = None):
Gtk.Popover.__init__(self)
self.set_relative_to(btn)
slider = Gtk.Scale.new(Gtk.Orientation.VERTICAL)
slider.set_name('Slider')
slider.set_range(0, 100)
slider.set_size_request(5, 100)
slider.set_draw_value(False)
slider.set_inverted(True)
self.slider = slider
self.slider.connect('change-value', self.debounce)
self.set_volume(100)
self.add(slider)
def set_volume(self, vol = 0):
if self.volume == int(vol):
return
self.volume = int(vol)
self.slider.set_value(self.volume)
def on_volume_changed(self):
vol = int(self.slider.get_value())
self.emit('volume_changed', vol)
def debounce(self, slider, a, b):
if self.timer is not None:
self.timer.cancel()
self.timer = threading.Timer(0.2, self.on_volume_changed)
self.timer.start()