audio: Create a custom VolumeControlBox

I also create a shared base class at the same time so autopause controls
can use it as well.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2021-09-02 10:25:08 -04:00
parent da1211f595
commit d551e0ea13
2 changed files with 60 additions and 2 deletions

View File

@ -1,7 +1,7 @@
# Copyright 2021 (c) Anna Schumaker.
from . import menu
from gi.repository import Gtk, Gst
from . import menu
from . import scale
class ControlButton(Gtk.Button):
def __init__(self, player, icon):
@ -40,6 +40,32 @@ class PlayPauseButton(ControlButton):
self.set_icon_name(f"media-playback-{icon}")
class ControlScaleBox(scale.ScaleButtonBox):
def __init__(self, scalectrl):
icon = Gtk.Image()
icon.add_css_class("large-icons")
scalectrl.connect("value-changed", self.on_value_changed, icon)
scale.ScaleButtonBox.__init__(self, scalectrl)
self.on_value_changed(scalectrl, icon)
self.prepend(icon)
def on_value_changed(self, scale, icon):
pass
class VolumeControlBox(ControlScaleBox):
def __init__(self, player):
ControlScaleBox.__init__(self, scale.VolumeScale(player))
def on_value_changed(self, scale, icon):
value = scale.get_value()
if value == 0: name = "muted"
elif value <= 1/3: name = "low"
elif value <= 2/3: name = "medium"
else: name = "high"
icon.set_from_icon_name(f"audio-volume-{name}-symbolic")
class Controls(Gtk.Box):
def __init__(self):
Gtk.Box.__init__(self)

View File

@ -4,6 +4,7 @@ from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gst
from . import controls
from . import scale
class FakePlayer(GObject.GObject):
def __init__(self):
@ -11,9 +12,11 @@ class FakePlayer(GObject.GObject):
self.prev = False
self.nxt = False
self.play = False
self.vol = 1.0
def previous(self): self.prev = True
def next(self): self.nxt = True
def get_volume(self): return self.vol
def playpause(self):
states = { True : Gst.State.PLAYING, False : Gst.State.PAUSED }
self.play = not self.play
@ -72,6 +75,35 @@ class TestPlayPauseButton(unittest.TestCase):
self.assertEqual(play.get_icon_name(), "media-playback-start")
class TestControlScaleBox(unittest.TestCase):
def test_control_scale_box(self):
splus = scale.ScalePlus(1, 10, 1, 5)
ctrlbox = controls.ControlScaleBox(splus)
self.assertIsInstance(ctrlbox, scale.ScaleButtonBox)
self.assertIsInstance(ctrlbox.get_first_child(), Gtk.Image)
self.assertTrue(ctrlbox.get_first_child().has_css_class("large-icons"))
class TestVolumeControlbox(unittest.TestCase):
def test_volume_control_box(self):
fake = FakePlayer()
vcb = controls.VolumeControlBox(fake)
icon = vcb.get_first_child()
volume = icon.get_next_sibling().scale
self.assertIsInstance(vcb, controls.ControlScaleBox)
self.assertIsInstance(volume, scale.VolumeScale)
volume.set_value(0)
self.assertEqual(icon.get_icon_name(), "audio-volume-muted-symbolic")
volume.set_value(0.3)
self.assertEqual(icon.get_icon_name(), "audio-volume-low-symbolic")
volume.set_value(0.6)
self.assertEqual(icon.get_icon_name(), "audio-volume-medium-symbolic")
volume.set_value(0.9)
self.assertEqual(icon.get_icon_name(), "audio-volume-high-symbolic")
class TestControls(unittest.TestCase):
def test_controls_init(self):
ctrl = controls.Controls()