83 lines
3.3 KiB
Python
83 lines
3.3 KiB
Python
# Copyright 2022 (c) Anna Schumaker.
|
|
"""A custom Gtk.Box with controls for adjusting the volume."""
|
|
from gi.repository import GObject
|
|
from gi.repository import Gtk
|
|
from gi.repository import Adw
|
|
|
|
STEP_SIZE = 0.05
|
|
|
|
|
|
def format_value_func(scale, value: float) -> str:
|
|
"""Format the volume value to a percentage."""
|
|
return f"{round(value*100)} %"
|
|
|
|
|
|
class VolumeRow(Gtk.ListBoxRow):
|
|
"""A Gtk.Box containing widgets for adjusting the volume."""
|
|
|
|
volume = GObject.Property(type=float, default=1.0)
|
|
|
|
def __init__(self, volume: float = 1.0):
|
|
"""Initialize our volume controls."""
|
|
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=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=5)
|
|
|
|
self._scale.set_format_value_func(format_value_func)
|
|
|
|
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)
|
|
self._increment.connect("clicked", self.__increment)
|
|
|
|
self.bind_property("volume", self._adjustment, "value",
|
|
GObject.BindingFlags.BIDIRECTIONAL)
|
|
|
|
def __decrement(self, button: Gtk.Button) -> None:
|
|
self._scale.set_value(self._scale.get_value() - STEP_SIZE)
|
|
|
|
def __increment(self, button: Gtk.Button) -> None:
|
|
self._scale.set_value(self._scale.get_value() + STEP_SIZE)
|
|
|
|
def __value_changed(self, range: Gtk.Range) -> None:
|
|
self.volume = range.get_value()
|
|
|
|
|
|
class BackgroundRow(Adw.ExpanderRow):
|
|
"""A VolumeRow for setting Background Listening volume."""
|
|
|
|
enabled = GObject.Property(type=bool, default=False)
|
|
volume = GObject.Property(type=float, default=0.5)
|
|
|
|
def __init__(self):
|
|
"""Initialize the BackgroundRow."""
|
|
super().__init__(title="Background Listening",
|
|
subtitle="Decrease the volume to help focus")
|
|
self._switch = Gtk.Switch(valign=Gtk.Align.CENTER)
|
|
self._volume = VolumeRow(volume=self.volume)
|
|
|
|
self.add_prefix(self._switch)
|
|
self.add_row(self._volume)
|
|
|
|
self._switch.bind_property("active", self, "expanded",
|
|
GObject.BindingFlags.BIDIRECTIONAL)
|
|
self.bind_property("enabled", self._switch, "active",
|
|
GObject.BindingFlags.BIDIRECTIONAL)
|
|
self.bind_property("volume", self._volume, "volume",
|
|
GObject.BindingFlags.BIDIRECTIONAL)
|