sonist-gtk/usr/lib/sonist/ui/ctrl_box.py

103 lines
2.6 KiB
Python
Raw Permalink Normal View History

2023-08-18 19:04:51 +08:00
#!/usr/bin/env python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
from .image_button import ImageButton
2023-08-21 19:06:22 +08:00
from .toggle_button import ToggleButton
2023-08-24 18:21:45 +08:00
from .volume import Volume
2023-08-18 19:04:51 +08:00
2023-08-25 16:50:16 +08:00
from assets import image_dict
2023-08-18 19:04:51 +08:00
class CtrlBox(Gtk.Box):
__gsignals__ = {
2023-08-24 18:21:45 +08:00
'clicked': (GObject.SignalFlags.RUN_FIRST, None, (str,)),
'volume_changed': (GObject.SignalFlags.RUN_FIRST, None, (int,))
2023-08-18 19:04:51 +08:00
}
def __init__(self, spacing = 6):
Gtk.Box.__init__(self, spacing = spacing)
2023-08-23 20:36:52 +08:00
self.disabled = True
2023-08-21 19:06:22 +08:00
2023-08-25 16:33:32 +08:00
self.modes = [
2023-08-25 16:50:16 +08:00
image_dict['all'],
image_dict['rand'],
image_dict['single']
2023-08-25 16:33:32 +08:00
]
2023-08-18 19:04:51 +08:00
self.curr_mode = 0
2023-08-21 19:06:22 +08:00
self.mode_btn = ImageButton(self.modes[0])
2023-08-25 16:50:16 +08:00
self.prev_btn = ImageButton(image_dict['prev'])
self.play_btn = ToggleButton([image_dict['pause'], image_dict['play']], 48, 48)
self.next_btn = ImageButton(image_dict['next'])
self.vol_btn = ImageButton(image_dict['volume'])
2023-08-18 19:04:51 +08:00
self.pack_start(self.mode_btn, False, False, 0)
self.pack_start(self.prev_btn, False, False, 0)
self.pack_start(self.play_btn, False, False, 0)
self.pack_start(self.next_btn, False, False, 0)
self.pack_start(self.vol_btn, False, False, 0)
self.mode_btn.connect('clicked', self.on_btn_clicked)
self.prev_btn.connect('clicked', self.on_btn_clicked)
self.play_btn.connect('clicked', self.on_btn_clicked)
self.next_btn.connect('clicked', self.on_btn_clicked)
self.vol_btn.connect('clicked', self.on_btn_clicked)
2023-08-24 18:21:45 +08:00
self.volume_bar = Volume(self.vol_btn)
self.volume_bar.connect('volume_changed', lambda bar, vol: self.emit('volume_changed', vol))
2023-08-18 19:04:51 +08:00
def on_btn_clicked(self, btn):
2023-08-21 19:06:22 +08:00
if self.disabled:
return
2023-08-18 19:04:51 +08:00
if btn == self.play_btn:
self.emit('clicked', 'play_btn')
elif btn == self.mode_btn:
self.curr_mode += 1
if self.curr_mode > 2:
self.curr_mode = 0
self.mode_btn.set_image(self.modes[self.curr_mode])
self.emit('clicked', 'mode_btn')
elif btn == self.prev_btn:
self.emit('clicked', 'prev_btn')
elif btn == self.next_btn:
self.emit('clicked', 'next_btn')
2023-08-21 19:06:22 +08:00
2023-08-18 19:04:51 +08:00
elif btn == self.vol_btn:
2023-08-24 18:21:45 +08:00
self.volume_bar.show_all()
def set_volume(self, vol):
self.volume_bar.set_volume(vol)
2023-08-18 19:04:51 +08:00
def toggle_play_btn(self, on = True):
2023-08-21 19:06:22 +08:00
if self.disabled:
return
self.play_btn.toggle(on)
2023-08-18 19:04:51 +08:00
def toggle_mode_btn(self, mode = 'single'):
2023-08-21 19:06:22 +08:00
if self.disabled:
return
2023-08-18 19:04:51 +08:00
if mode == 'single':
self.curr_mode = 2
elif mode == 'random':
self.curr_mode = 1
else:
self.curr_mode = 0
self.mode_btn.set_image(self.modes[self.curr_mode])