# Copyright 2022 (c) Anna Schumaker. """A widget for selecting ReplayGain mode.""" from gi.repository import GObject from gi.repository import Gtk from gi.repository import Adw class CheckRow(Adw.ActionRow): """A custom Adw.ActionRow displaying a Check Button.""" active = GObject.Property(type=bool, default=False) group = GObject.Property(type=Adw.ActionRow) mode = GObject.Property(type=str) def __init__(self, mode: str, active: bool = False, group: Adw.ActionRow | None = None, **kwargs): """Initialize the Check Row.""" super().__init__(mode=mode, active=active, group=group, **kwargs) self._prefix = Gtk.CheckButton(active=active, group=group._prefix if group else None) self.bind_property("active", self._prefix, "active", GObject.BindingFlags.BIDIRECTIONAL) self.set_activatable_widget(self._prefix) self.add_prefix(self._prefix) def set_active(self, newval: bool) -> None: """Set the active property.""" if self.active != newval: self.active = newval class ReplayGainRow(Adw.ExpanderRow): """Build up a widget for configuring ReplayGain settings.""" enabled = GObject.Property(type=bool, default=False) mode = GObject.Property(type=str, default="auto") def __init__(self): """Initialize the ReplayGain selector.""" super().__init__(title="Volume Normalization", subtitle="Configure ReplayGain normalizing") self._switch = Gtk.Switch(valign=Gtk.Align.CENTER) self._automatic = CheckRow(title="Automatic Mode", subtitle="Emmental decides automatically", mode="auto", active=True) self._album = CheckRow(title="Album Mode", subtitle="Albums have the same volume", mode="album", group=self._automatic) self._track = CheckRow(title="Track Mode", subtitle="Tracks have the same volume", mode="track", group=self._automatic) self.add_prefix(self._switch) self.add_row(self._automatic) self.add_row(self._album) self.add_row(self._track) self.connect("notify::mode", self.__notify_mode) self._automatic.connect("notify::active", self.__row_activated) self._album.connect("notify::active", self.__row_activated) self._track.connect("notify::active", self.__row_activated) self._switch.bind_property("active", self, "expanded", GObject.BindingFlags.BIDIRECTIONAL) self.bind_property("enabled", self._switch, "active", GObject.BindingFlags.BIDIRECTIONAL) def __notify_mode(self, row: Adw.ExpanderRow, param) -> None: match self.mode: case "album": self._album.set_active(True) case "track": self._track.set_active(True) case _: self._automatic.set_active(True) def __row_activated(self, row: CheckRow, param: GObject.ParamSpec) -> None: if row.active: self.mode = row.mode