# pylint: disable=C0111,R0903

import pulsectl

import core.module
import core.widget
import core.decorators
import core.input
import core.event

class Module(core.module.Module):
    def __init__(self, config, theme, type):
        super().__init__(config, theme, core.widget.Widget(self.display))
        self.background = True

        self.__type = type
        self.__volume = "n/a"
        self.__muted = False

        self.process(None)

    def display(self, _):
        return f"{int(self.__volume*100)}%"

    def process(self, _):
        with pulsectl.Pulse(self.id + "proc") as pulse:
            dev = pulse.sink_list()[0] if self.__type == "sink" else pulse.source_list()[0]
            self.__volume = dev.volume.value_flat
            self.__muted = dev.mute
        core.event.trigger("update", [self.id], redraw_only=True)
        core.event.trigger("draw")

    def update(self):
        with pulsectl.Pulse(self.id) as pulse:
            pulse.event_mask_set(self.__type)
            pulse.event_callback_set(self.process)
            pulse.event_listen()

    def state(self, _):
        if self.__muted:
            return ["warning", "muted"]
        return ["unmuted"]

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

@@ -83,31 +83,8 @@
 get%0A
-import core.decorators%0A
 impo
@@ -116,16 +116,36 @@
 .event%0A%0A
+import util.format%0A%0A
 class Mo
@@ -425,92 +425,1511 @@
 elf.
-process(None)%0A%0A    def display(self, _):%0A        return f%22%7Bint(self.__volume*100)%7D%25%22
+__change = util.format.asint(%0A            self.parameter(%22percent_change%22, %222%25%22).strip(%22%25%22), 0, 100%0A        )%0A%0A        events = %5B%0A            %7B%0A                %22type%22: %22mute%22,%0A                %22action%22: self.toggle_mute,%0A                %22button%22: core.input.LEFT_MOUSE%0A            %7D,%0A            %7B%0A                %22type%22: %22volume%22,%0A                %22action%22: self.increase_volume,%0A                %22button%22: core.input.WHEEL_UP,%0A            %7D,%0A            %7B%0A                %22type%22: %22volume%22,%0A                %22action%22: self.decrease_volume,%0A                %22button%22: core.input.WHEEL_DOWN,%0A            %7D,%0A        %5D%0A%0A        for event in events:%0A            core.input.register(self, button=event%5B%22button%22%5D, cmd=event%5B%22action%22%5D)%0A%0A        self.process(None)%0A%0A    def display(self, _):%0A        return f%22%7Bint(self.__volume*100)%7D%25%22%0A%0A    def toggle_mute(self, _):%0A        with pulsectl.Pulse(self.id + %22vol%22) as pulse:%0A            dev = pulse.sink_list()%5B0%5D if self.__type == %22sink%22 else pulse.source_list()%5B1%5D%0A            pulse.mute(dev, not self.__muted)%0A%0A    def change_volume(self, amount):%0A        with pulsectl.Pulse(self.id + %22vol%22) as pulse:%0A            dev = pulse.sink_list()%5B0%5D if self.__type == %22sink%22 else pulse.source_list()%5B1%5D%0A            vol = dev.volume%0A            vol.value_flat += amount%0A            pulse.volume_set(dev, vol)%0A%0A    def increase_volume(self, _):%0A        self.change_volume(self.__change/100.0)%0A%0A    def decrease_volume(self, _):%0A        self.change_volume(-self.__change/100.0)
 %0A%0A  
@@ -2097,17 +2097,17 @@
 _list()%5B
-0
+1
 %5D%0A      

add mouse actions