diff --git a/nix/module.nix b/nix/module.nix index ca12dfa..1f1eaf5 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -466,6 +466,8 @@ in theme.bar.buttons.modules.kbLayout.enableBorder = mkBoolOption false; theme.bar.buttons.modules.kbLayout.spacing = mkStrOption "0.45em"; theme.bar.buttons.modules.netstat.enableBorder = mkBoolOption false; + theme.bar.buttons.modules.microphone.enableBorder = mkBoolOption false, + theme.bar.buttons.modules.microphone.spacing = mkStrOption "0.45em", theme.bar.buttons.modules.netstat.spacing = mkStrOption "0.45em"; theme.bar.buttons.modules.power.enableBorder = mkBoolOption false; theme.bar.buttons.modules.power.spacing = mkStrOption "0.45em"; diff --git a/src/components/bar/exports.ts b/src/components/bar/exports.ts index fc4f9b5..dbc3648 100644 --- a/src/components/bar/exports.ts +++ b/src/components/bar/exports.ts @@ -11,6 +11,7 @@ import { Clock } from '../../components/bar/modules/clock/index'; import { SysTray } from '../../components/bar/modules/systray/index'; // Custom Modules +import { Microphone } from '../../components/bar/modules/microphone/index'; import { Ram } from '../../components/bar/modules/ram/index'; import { Cpu } from '../../components/bar/modules/cpu/index'; import { CpuTemp } from '../../components/bar/modules/cputemp/index'; @@ -39,6 +40,7 @@ export { SysTray, // Custom Modules + Microphone, Ram, Cpu, CpuTemp, diff --git a/src/components/bar/index.tsx b/src/components/bar/index.tsx index 5246f98..115fd43 100644 --- a/src/components/bar/index.tsx +++ b/src/components/bar/index.tsx @@ -12,6 +12,7 @@ import { SysTray, // Custom Modules + Microphone, Ram, Cpu, CpuTemp, @@ -51,6 +52,7 @@ const widget = { bluetooth: (): JSX.Element => WidgetContainer(Bluetooth()), clock: (): JSX.Element => WidgetContainer(Clock()), systray: (): JSX.Element => WidgetContainer(SysTray()), + microphone: (): JSX.Element => WidgetContainer(Microphone()), ram: (): JSX.Element => WidgetContainer(Ram()), cpu: (): JSX.Element => WidgetContainer(Cpu()), cputemp: (): JSX.Element => WidgetContainer(CpuTemp()), diff --git a/src/components/bar/modules/microphone/index.tsx b/src/components/bar/modules/microphone/index.tsx new file mode 100644 index 0000000..6a3a8e8 --- /dev/null +++ b/src/components/bar/modules/microphone/index.tsx @@ -0,0 +1,76 @@ +import options from 'src/options'; +import { Module } from '../../shared/Module'; +import { bind, Variable } from 'astal'; +import { BarBoxChild } from 'src/lib/types/bar'; +import { Astal } from 'astal/gtk3'; +import { inputHandler } from '../../utils/helpers'; +import AstalWp from 'gi://AstalWp?version=0.1'; + +const wireplumber = AstalWp.get_default() as AstalWp.Wp; +const audioService = wireplumber.audio; + +const { label, mutedIcon, unmutedIcon, leftClick, rightClick, middleClick, scrollUp, scrollDown } = + options.bar.customModules.microphone; + +export const Microphone = (): BarBoxChild => { + const iconBinding = Variable.derive( + [ + bind(mutedIcon), + bind(unmutedIcon), + bind(audioService.defaultMicrophone, 'volume'), + bind(audioService.defaultMicrophone, 'mute'), + ], + (iconMuted, iconUnmuted, volume, isMuted) => { + if (isMuted || volume === 0) { + return iconMuted; + } + + return iconUnmuted; + }, + ); + + const tooltipBinding = Variable.derive( + [ + bind(mutedIcon), + bind(unmutedIcon), + bind(audioService.defaultMicrophone, 'description'), + bind(audioService.defaultMicrophone, 'volume'), + bind(audioService.defaultMicrophone, 'mute'), + ], + (iconMuted, iconUnmuted, description, volume, isMuted) => { + const icon = isMuted || !volume ? iconMuted : iconUnmuted; + + return `${icon} ${description}`; + }, + ); + const microphoneModule = Module({ + textIcon: iconBinding(), + label: bind(audioService.defaultMicrophone, 'volume').as((vol) => `${Math.round(vol * 100)}%`), + tooltipText: tooltipBinding(), + boxClass: 'mic', + showLabelBinding: bind(label), + props: { + setup: (self: Astal.Button) => { + inputHandler(self, { + onPrimaryClick: { + cmd: leftClick, + }, + onSecondaryClick: { + cmd: rightClick, + }, + onMiddleClick: { + cmd: middleClick, + }, + onScrollUp: { + cmd: scrollUp, + }, + onScrollDown: { + cmd: scrollDown, + }, + }); + }, + }, + }); + + return microphoneModule; +}; diff --git a/src/components/bar/settings/config.tsx b/src/components/bar/settings/config.tsx index 925dfa0..363255a 100644 --- a/src/components/bar/settings/config.tsx +++ b/src/components/bar/settings/config.tsx @@ -16,6 +16,23 @@ export const CustomModuleSettings = (): JSX.Element => {