__init__.py
Python script, ASCII text executable
1""" 2The MIT License (MIT) 3 4Copyright (c) 2025 Scott Moreau <oreaus@gmail.com> 5 6Permission is hereby granted, free of charge, to any person obtaining a copy 7of this software and associated documentation files (the "Software"), to deal 8in the Software without restriction, including without limitation the rights 9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10copies of the Software, and to permit persons to whom the Software is 11furnished to do so, subject to the following conditions: 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22SOFTWARE. 23""" 24import locale 25 26import panorama_panel 27 28import gi 29gi.require_version("Gtk", "4.0") 30 31from gi.repository import Gtk, Gdk, GLib 32 33import asyncio 34import pulsectl 35import pulsectl_asyncio 36 37class Volume(panorama_panel.Applet): 38name = "Volume" 39description = "Volume applet" 40 41def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 42super().__init__() 43if config is None: 44config = {} 45self.popdown_after_manual = config.get("popdown_after_manual", 2000) 46self.percentage_reveal = config.get("percentage_reveal", 1000) 47self.percentage_animation_time = config.get("percentage_animation_time", 250) 48self.icon_size = config.get("icon_size", 24) 49asyncio.create_task(self.main()) 50 51async def main(self): 52self.menu_button = Gtk.MenuButton() 53self.volume_step = 0.05 54self.pulse = pulsectl_asyncio.PulseAsync("panorama-panel-volume-manager") 55await self.pulse.connect() 56server_info = await self.pulse.server_info() 57self.default_sink = await self.pulse.get_sink_by_name(server_info.default_sink_name) 58self.current_volume = self.default_sink.volume.value_flat 59self.volume_adjustment = Gtk.Adjustment(value=0.5, lower=0.0, upper=1.0, step_increment=0.01, page_increment=0.1) 60self.volume_scale = Gtk.Scale.new(Gtk.Orientation.HORIZONTAL, self.volume_adjustment) 61self.volume_scale.set_hexpand(True) 62self.volume_scale.set_value(self.current_volume) 63self.volume_scale.set_size_request(200, -1) 64self.volume_scale_changed_handler = self.volume_scale.connect("value-changed", self.on_scale_changed) 65self.volume_popover = Gtk.Popover() 66self.volume_popover.set_child(self.volume_scale) 67self.menu_button.set_popover(self.volume_popover) 68self.menu_button.set_has_frame(False) 69self.button_content = Gtk.Box(orientation=self.get_orientation()) 70self.icon = Gtk.Image(pixel_size=self.icon_size) 71self.button_content.append(self.icon) 72self.menu_button.set_child(self.button_content) 73scroll_controller = Gtk.EventControllerScroll.new(Gtk.EventControllerScrollFlags.VERTICAL) 74scroll_controller.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) 75self.volume_popover.add_controller(scroll_controller) 76scroll_controller.connect("scroll", self.on_popover_scroll) 77scroll_controller = Gtk.EventControllerScroll.new(Gtk.EventControllerScrollFlags.VERTICAL) 78scroll_controller.set_propagation_phase(Gtk.PropagationPhase.CAPTURE) 79self.add_controller(scroll_controller) 80scroll_controller.connect("scroll", self.on_button_scroll) 81self.popdown_timeout = None 82self.conceal_timeout = None 83self.set_button_icon() 84gesture_click = Gtk.GestureClick() 85gesture_click.set_button(Gdk.BUTTON_MIDDLE) 86gesture_click.connect("released", self.on_middle_click_released) 87self.add_controller(gesture_click) 88self.append(self.menu_button) 89 90self.revealer = Gtk.Revealer(transition_duration=self.percentage_animation_time) 91self.button_content.append(self.revealer) 92 93if self.get_orientation() == Gtk.Orientation.HORIZONTAL: 94self.revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_RIGHT) 95elif self.get_orientation() == Gtk.Orientation.VERTICAL: 96self.revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_DOWN) 97 98self.percentage_label = Gtk.Label() 99self.percentage_label.set_width_chars(5) 100self.update_text() 101self.revealer.set_child(self.percentage_label) 102 103self.volume_popover.connect("show", self.popover_shown) 104self.volume_popover.connect("closed", self.popover_hidden) 105 106if not self.percentage_reveal: 107self.revealer.set_reveal_child(True) 108 109loop = asyncio.get_event_loop() 110loop.create_task(self.listen()) 111 112def popover_shown(self, *args): 113if self.percentage_reveal != -1: 114self.revealer.set_reveal_child(True) 115 116def popover_hidden(self, *args): 117if self.percentage_reveal: 118self.revealer.set_reveal_child(False) 119 120def set_panel_position(self, position: Gtk.PositionType): 121if not hasattr(self, "revealer"): 122return 123 124if position in {Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM}: 125self.revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_RIGHT) 126self.button_content.set_orientation(Gtk.Orientation.HORIZONTAL) 127elif position in {Gtk.PositionType.LEFT, Gtk.PositionType.RIGHT}: 128self.revealer.set_transition_type(Gtk.RevealerTransitionType.SLIDE_DOWN) 129self.button_content.set_orientation(Gtk.Orientation.VERTICAL) 130 131def on_middle_click_released(self, gesture, n_press, x, y): 132loop = asyncio.get_event_loop() 133loop.create_task(self.idle_on_middle_click_released()) 134 135async def idle_on_middle_click_released(self): 136if self.default_sink.mute: 137await self.pulse.sink_mute(self.default_sink.index, False) 138self.default_sink.mute = False 139else: 140await self.pulse.sink_mute(self.default_sink.index, True) 141self.default_sink.mute = True 142self.set_button_icon() 143 144def set_button_icon(self): 145volume = self.current_volume 146threshold = 1 / 3 147if volume == 0.0 or self.default_sink.mute: 148self.icon.set_from_icon_name("audio-volume-muted-symbolic") 149elif volume < threshold: 150self.icon.set_from_icon_name("audio-volume-low-symbolic") 151elif volume < threshold * 2: 152self.icon.set_from_icon_name("audio-volume-medium-symbolic") 153else: 154self.icon.set_from_icon_name("audio-volume-high-symbolic") 155 156def on_button_scroll(self, controller, dx, dy): 157if dy < 0 or dy > 0: 158self.menu_button.popup() 159return Gdk.EVENT_STOP 160 161def on_popover_scroll(self, controller, dx, dy): 162if dy > 0: 163new_volume = self.current_volume - self.volume_step 164elif dy < 0: 165new_volume = self.current_volume + self.volume_step 166if new_volume < 0: 167new_volume = 0.0 168elif new_volume > 1: 169new_volume = 1.0 170if self.current_volume == new_volume: 171return Gdk.EVENT_STOP 172self.volume_scale.set_value(new_volume) 173self.current_volume = new_volume 174self.set_button_icon() 175 176if self.popdown_timeout is not None: 177GLib.source_remove(self.popdown_timeout) 178if self.popdown_after_manual: 179self.popdown_timeout = GLib.timeout_add(self.popdown_after_manual, self.close_scale_popover) 180return Gdk.EVENT_STOP 181 182async def idle_volume_update(self): 183new_volume = self.volume_scale.get_value() 184await self.pulse.volume_set_all_chans(self.default_sink, new_volume) 185self.current_volume = new_volume 186self.set_button_icon() 187 188if self.popdown_timeout is not None: 189GLib.source_remove(self.popdown_timeout) 190if self.popdown_after_manual: 191self.popdown_timeout = GLib.timeout_add(self.popdown_after_manual, self.close_scale_popover) 192return False 193 194def on_scale_changed(self, scale, *args): 195loop = asyncio.get_event_loop() 196loop.create_task(self.idle_volume_update()) 197 198def update_text(self): 199self.percentage_label.set_text(f"{locale.format_string("%d", self.current_volume * 100)}%") 200 201async def idle_update_scale(self): 202server_info = await self.pulse.server_info() 203self.default_sink = await self.pulse.get_sink_by_name(server_info.default_sink_name) 204new_volume = self.default_sink.volume.value_flat 205if self.current_volume == new_volume: 206self.set_button_icon() 207return False 208self.current_volume = new_volume 209self.volume_scale.set_value(self.current_volume) 210self.set_button_icon() 211 212self.update_text() 213if self.percentage_reveal != -1: 214self.revealer.set_reveal_child(True) 215 216if self.conceal_timeout is not None: 217GLib.source_remove(self.conceal_timeout) 218if self.percentage_reveal > 0: 219self.conceal_timeout = GLib.timeout_add(self.percentage_reveal, self.conceal_percentage) 220return False 221 222def conceal_percentage(self): 223if not self.percentage_reveal: 224return 225 226if not self.volume_popover.get_visible(): 227self.revealer.set_reveal_child(False) 228self.conceal_timeout = None 229 230def close_scale_popover(self): 231self.popdown_timeout = None 232self.menu_button.popdown() 233return False 234 235async def listen(self): 236async for event in self.pulse.subscribe_events("all"): 237if event.facility == pulsectl.PulseEventFacilityEnum.sink and \ 238event.t == pulsectl.PulseEventTypeEnum.change: 239try: 240sink = await self.pulse.sink_info(event.index) 241loop = asyncio.get_event_loop() 242loop.create_task(self.idle_update_scale()) 243except pulsectl_asyncio.PulseOperationFailed: 244print(f"Could not retrieve info for sink index {event.index}") 245 246def get_config(self): 247return { 248"percentage_reveal": self.percentage_reveal, 249"popdown_after_manual": self.popdown_after_manual, 250"percentage_animation_time": self.percentage_animation_time, 251"icon_size": self.icon_size, 252} 253