header: Convert the volume controls into a ListBoxRow

The plan is to convert the volume control panel into a Gtk.ListBox for a
more modern appearance.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2023-05-31 09:47:24 -04:00
parent 5b0a0f54e4
commit 03e5b9ad1b
4 changed files with 35 additions and 19 deletions

View File

@ -43,7 +43,7 @@ class Header(Gtk.HeaderBar):
self._open = open.Button()
self._title = Adw.WindowTitle(title=self.title, subtitle=self.subtitle,
tooltip_text=gsetup.env_string())
self._volume = volume.Controls()
self._volume = volume.VolumeRow()
self._replaygain = replaygain.Selector()
self._box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)

View File

@ -11,29 +11,34 @@ def format_value_func(scale, value: float) -> str:
return f"{round(value*100)} %"
class Controls(Gtk.Box):
class VolumeRow(Gtk.ListBoxRow):
"""A Gtk.Box containing widgets for adjusting the volume."""
volume = GObject.Property(type=float, default=1.0)
def __init__(self):
def __init__(self, volume: float = 1.0):
"""Initialize our volume controls."""
super().__init__()
super().__init__(volume=volume)
self._box = Gtk.Box()
self._decrement = Gtk.Button(icon_name="list-remove-symbolic",
tooltip_text="reduce the volume",
valign=Gtk.Align.END, has_frame=False,
margin_bottom=6)
self._adjustment = Gtk.Adjustment.new(1.0, 0.0, 1.0, STEP_SIZE, 0, 0)
margin_bottom=5)
self._adjustment = Gtk.Adjustment.new(volume, 0.0, 1.0,
STEP_SIZE, 0, 0)
self._scale = Gtk.Scale(adjustment=self._adjustment, draw_value=True,
valign=Gtk.Align.END, hexpand=True)
self._increment = Gtk.Button(icon_name="list-add-symbolic",
tooltip_text="increase the volume",
valign=Gtk.Align.END, has_frame=False,
margin_bottom=6)
margin_bottom=5)
self._scale.set_format_value_func(format_value_func)
self.append(self._decrement)
self.append(self._scale)
self.append(self._increment)
self._box.append(self._decrement)
self._box.append(self._scale)
self._box.append(self._increment)
self.set_child(self._box)
self._decrement.connect("clicked", self.__decrement)
self._scale.connect("value-changed", self.__value_changed)

View File

@ -65,7 +65,7 @@ class TestHeader(tests.util.TestCase):
def test_volume(self):
"""Check that volume widgets work as expected."""
self.assertIsInstance(self.header._volume,
emmental.header.volume.Controls)
emmental.header.volume.VolumeRow)
self.assertEqual(self.header.volume, 1.0)
for i, vol in [(x, x/10) for x in range(11)]:

View File

@ -6,12 +6,12 @@ import tests.util
from gi.repository import Gtk
class TestControls(unittest.TestCase):
class TestVolumeRow(unittest.TestCase):
"""Test case for our custom volume controls."""
def setUp(self):
"""Set up common variables."""
self.vol = emmental.header.volume.Controls()
self.vol = emmental.header.volume.VolumeRow()
self.value = tests.util.FloatObject(value=1.0)
self.vol.bind_property("volume", self.value, "value")
@ -21,9 +21,12 @@ class TestControls(unittest.TestCase):
def test_volume(self):
"""Check that the volume Controls are set up properly."""
self.assertIsInstance(self.vol, Gtk.Box)
self.assertEqual(self.vol.get_spacing(), 0)
self.assertEqual(self.vol.get_orientation(),
self.assertIsInstance(self.vol, Gtk.ListBoxRow)
self.assertIsInstance(self.vol._box, Gtk.Box)
self.assertEqual(self.vol.get_child(), self.vol._box)
self.assertEqual(self.vol._box.get_spacing(), 0)
self.assertEqual(self.vol._box.get_orientation(),
Gtk.Orientation.HORIZONTAL)
self.assertEqual(self.vol.volume, 1.0)
@ -31,15 +34,21 @@ class TestControls(unittest.TestCase):
self.assertAlmostEqual(self.vol._scale.get_value(), 0.85)
self.assertAlmostEqual(self.value.value, 0.85)
vol2 = emmental.header.volume.VolumeRow(volume=0.5)
self.assertEqual(vol2.volume, 0.5)
self.assertEqual(vol2._adjustment.get_value(), 0.5)
def test_decrement_button(self):
"""Test the decrement button."""
self.assertIsInstance(self.vol._decrement, Gtk.Button)
self.assertEqual(self.vol.get_first_child(), self.vol._decrement)
self.assertEqual(self.vol._box.get_first_child(), self.vol._decrement)
self.assertEqual(self.vol._decrement.get_tooltip_text(),
"reduce the volume")
self.assertEqual(self.vol._decrement.get_icon_name(),
"list-remove-symbolic")
self.assertEqual(self.vol._decrement.get_valign(), Gtk.Align.END)
self.assertEqual(self.vol._decrement.get_margin_bottom(), 6)
self.assertEqual(self.vol._decrement.get_margin_bottom(), 5)
self.assertFalse(self.vol._decrement.get_has_frame())
self.vol._decrement.emit("clicked")
@ -79,10 +88,12 @@ class TestControls(unittest.TestCase):
self.assertEqual(self.vol._scale.get_next_sibling(),
self.vol._increment)
self.assertEqual(self.vol._increment.get_tooltip_text(),
"increase the volume")
self.assertEqual(self.vol._increment.get_icon_name(),
"list-add-symbolic")
self.assertEqual(self.vol._increment.get_valign(), Gtk.Align.END)
self.assertEqual(self.vol._increment.get_margin_bottom(), 6)
self.assertEqual(self.vol._increment.get_margin_bottom(), 5)
self.assertFalse(self.vol._increment.get_has_frame())
self.vol.volume = 0.9