From 4be450ea666ddba90d7a677b2f85f072f42f8539 Mon Sep 17 00:00:00 2001 From: shyguyCreate <107062289+shyguyCreate@users.noreply.github.com> Date: Sun, 28 Apr 2024 07:21:12 -0600 Subject: [PATCH 1/2] feat(widgets): get_volume() return a tuple --- CHANGELOG | 1 + libqtile/widget/pulse_volume.py | 2 -- libqtile/widget/volume.py | 24 +++++++++++++----------- test/widgets/test_volume.py | 13 +++++++++++++ 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 78ecfd1baf..b6341875dc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,7 @@ Qtile x.xx.x, released XXXX-XX-XX: the `qtile cmd-obj` command line - Add `Plasma` layout. The original layout (https://github.com/numirias/qtile-plasma) appears to be unmaintained so we have added this to the main codebase. + - Add ability to specify muted and unmuted formats for `Volume` and `PulseVolume` widgets. * bugfixes Qtile 0.25.0, released 2024-04-06: diff --git a/libqtile/widget/pulse_volume.py b/libqtile/widget/pulse_volume.py index f7f7627a1f..80335cea01 100644 --- a/libqtile/widget/pulse_volume.py +++ b/libqtile/widget/pulse_volume.py @@ -128,8 +128,6 @@ def get_volume(self): if self.default_sink: mute = self.default_sink.mute - if mute: - return -1, mute base = self.default_sink.base_volume if not base: return -1, mute diff --git a/libqtile/widget/volume.py b/libqtile/widget/volume.py index a242c95199..9ac6f5328e 100644 --- a/libqtile/widget/volume.py +++ b/libqtile/widget/volume.py @@ -76,6 +76,8 @@ class Volume(base._TextBox): " List contains 4 symbols, from lowest volume to highest.", ), ("mute_command", None, "Mute command"), + ("mute_format", "M", "Format to display when volume is muted."), + ("unmute_format", "{volume}%", "Format of text to display when volume is not muted."), ("volume_app", None, "App to control volume"), ("volume_up_command", None, "Volume up command"), ("volume_down_command", None, "Volume down command"), @@ -106,6 +108,7 @@ def __init__(self, **config): self.add_defaults(Volume.defaults) self.surfaces = {} self.volume = None + self.mute = 0 self.add_callbacks( { @@ -144,9 +147,10 @@ def button_press(self, x, y, button): self.draw() def update(self): - vol = self.get_volume() - if vol != self.volume: + vol, muted = self.get_volume() + if vol != self.volume or muted != self.mute: self.volume = vol + self.mute = muted # Update the underlying canvas size before actually attempting # to figure out how big it is and draw it. self._update_drawer() @@ -183,10 +187,9 @@ def _update_drawer(self): elif self.volume >= 80: self.text = self.emoji_list[3] else: - if self.volume == -1: - self.text = "M" - else: - self.text = "{}%".format(self.volume) + self.text = ( + self.mute_format if self.mute or self.volume < 0 else self.unmute_format + ).format(volume=self.volume) def setup_images(self): from libqtile import images @@ -214,21 +217,20 @@ def get_volume(self): mixer_out = subprocess.getoutput(get_volume_cmd) except subprocess.CalledProcessError: - return -1 + return -1, 0 check_mute = mixer_out if self.check_mute_command: check_mute = subprocess.getoutput(self.check_mute_command) - if self.check_mute_string in check_mute: - return -1 + mute = 1 if self.check_mute_string in check_mute else 0 volgroups = re_vol.search(mixer_out) if volgroups: - return int(volgroups.groups()[0]) + return int(volgroups.groups()[0]), mute else: # this shouldn't happen - return -1 + return -1, mute def draw(self): if self.theme_path: diff --git a/test/widgets/test_volume.py b/test/widgets/test_volume.py index 32e888fd22..5ded4d6ea2 100644 --- a/test/widgets/test_volume.py +++ b/test/widgets/test_volume.py @@ -62,3 +62,16 @@ def test_text(): vol.volume = 50 vol._update_drawer() assert vol.text == "50%" + + +def test_formats(): + unmute_format = "Volume: {volume}%" + mute_format = "Volume: {volume}% M" + vol = Volume(unmute_format=unmute_format, mute_format=mute_format) + vol.volume = 50 + vol._update_drawer() + assert vol.text == "Volume: 50%" + + vol.mute = True + vol._update_drawer() + assert vol.text == "Volume: 50% M" From 59763c6b578fe8b1e99042e4047b43105e59331e Mon Sep 17 00:00:00 2001 From: shyguyCreate <107062289+shyguyCreate@users.noreply.github.com> Date: Sun, 28 Apr 2024 15:55:54 -0600 Subject: [PATCH 2/2] feat(widgets): self.mute True/False for Volume widget --- libqtile/widget/volume.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libqtile/widget/volume.py b/libqtile/widget/volume.py index 9ac6f5328e..d647959fb9 100644 --- a/libqtile/widget/volume.py +++ b/libqtile/widget/volume.py @@ -108,7 +108,7 @@ def __init__(self, **config): self.add_defaults(Volume.defaults) self.surfaces = {} self.volume = None - self.mute = 0 + self.mute = False self.add_callbacks( { @@ -217,13 +217,13 @@ def get_volume(self): mixer_out = subprocess.getoutput(get_volume_cmd) except subprocess.CalledProcessError: - return -1, 0 + return -1, False check_mute = mixer_out if self.check_mute_command: check_mute = subprocess.getoutput(self.check_mute_command) - mute = 1 if self.check_mute_string in check_mute else 0 + mute = self.check_mute_string in check_mute volgroups = re_vol.search(mixer_out) if volgroups: