Skip to content

Commit

Permalink
Add hooks for Volume widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
elParaguayo committed Apr 26, 2024
1 parent 5e4e868 commit f77f79d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
50 changes: 50 additions & 0 deletions qtile_extras/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,56 @@ def new_track(status):

hooks.extend(mpris_hooks)

# Volume hooks
volume_hooks = [
Hook(
"volume_change",
"""
Fired when the volume value changes.
Receives an integer volume percentage (0-100) and boolean muted status.
.. code:: python
from libqtile import qtile
from libqtile.utils import send_notification
import qtile_extras.hook
@qtile_extras.hook.subscribe.volume_change
def vol_change(volume, muted):
send_notification("Volume change", f"Volume is now {volume}%")
""",
),
Hook(
"volume_mute_change",
"""
Fired when the volume mute status changes.
Receives an integer volume percentage (0-100) and boolean muted status.
The signature is the same as ``volume_change`` to allow the same function to be
used for both hooks.
.. code:: python
from libqtile import qtile
from libqtile.utils import send_notification
import qtile_extras.hook
@qtile_extras.hook.subscribe.volume_mute_change
def mute_change(volume, muted):
if muted:
send_notification("Volume change", "Volume is now muted.")
""",
),
]

hooks.extend(volume_hooks)

# Build the registry and expose helpful entrypoints
qte = Registry("qtile-extras", hooks)

Expand Down
10 changes: 10 additions & 0 deletions qtile_extras/widget/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from libqtile.log_utils import logger
from libqtile.widget import base

from qtile_extras import hook
from qtile_extras.popup.templates.volume import VOLUME_NOTIFICATION
from qtile_extras.widget.mixins import ExtendedPopupMixin, ProgressBarMixin

Expand Down Expand Up @@ -85,6 +86,8 @@ class _Volume(base._Widget, ExtendedPopupMixin, ProgressBarMixin):
),
]

_hooks = [h.name for h in hook.volume_hooks]

def __init__(self, **config):
base._Widget.__init__(self, bar.CALCULATED, **config)
ExtendedPopupMixin.__init__(self, **config)
Expand Down Expand Up @@ -187,6 +190,13 @@ def status_change(self, vol, muted):
# Unhide bar
self.hidden = False

# Fire any hooks
if vol != self.volume:
hook.fire("volume_change", vol, muted)

if muted != self.muted:
hook.fire("volume_muted_change", vol, muted)

# Get new values
self.volume = vol
self.muted = muted
Expand Down

0 comments on commit f77f79d

Please sign in to comment.