Replaced the underlying structure of Hypridle inhibitor to the Astal Idle Inhibitor module. (#776)

* Update hypridle module to be an all-purpose idle inhibitor module using Astal inhibiting.

* Fix types and module code.

* Update note

* Removed lingering comment.

* Remove hypridle from dependencies.
This commit is contained in:
Jas Singh
2025-02-16 12:20:29 -08:00
committed by GitHub
parent 867b2a7d68
commit 3698bfe2b3
10 changed files with 98 additions and 103 deletions

View File

@@ -127,6 +127,7 @@ export const Bar = (() => {
return (
<window
inhibit={bind(idleInhibit)}
name={`bar-${hyprlandMonitor}`}
namespace={`bar-${hyprlandMonitor}`}
className={'bar'}

View File

@@ -1,55 +0,0 @@
import { execAsync, Variable } from 'astal';
/**
* Checks if the hypridle process is active.
*
* This command checks if the hypridle process is currently running by using the `pgrep` command.
* It returns 'yes' if the process is found and 'no' otherwise.
*/
export const isActiveCommand = `bash -c "pgrep -x 'hypridle' &>/dev/null && echo 'yes' || echo 'no'"`;
/**
* A variable to track the active state of the hypridle process.
*/
export const isActive = Variable(false);
/**
* Updates the active state of the hypridle process.
*
* This function checks if the hypridle process is currently running and updates the `isActive` variable accordingly.
*
* @param isActive A Variable<boolean> that tracks the active state of the hypridle process.
*/
const updateIsActive = (isActive: Variable<boolean>): void => {
execAsync(isActiveCommand).then((res) => {
isActive.set(res === 'yes');
});
};
/**
* Toggles the hypridle process on or off based on its current state.
*
* This function checks if the hypridle process is currently running. If it is not running, it starts the process.
* If it is running, it stops the process. The active state is updated accordingly.
*
* @param isActive A Variable<boolean> that tracks the active state of the hypridle process.
*/
export const toggleIdle = (isActive: Variable<boolean>): void => {
execAsync(isActiveCommand).then((res) => {
const toggleIdleCommand =
res === 'no' ? `bash -c "nohup hypridle > /dev/null 2>&1 &"` : `bash -c "pkill hypridle"`;
execAsync(toggleIdleCommand).then(() => updateIsActive(isActive));
});
};
/**
* Checks the current status of the hypridle process and updates the active state.
*
* This function checks if the hypridle process is currently running and updates the `isActive` variable accordingly.
*/
export const checkIdleStatus = (): undefined => {
execAsync(isActiveCommand).then((res) => {
isActive.set(res === 'yes');
});
};

View File

@@ -1,37 +1,33 @@
import options from 'src/options';
import { Module } from '../../shared/Module';
import { inputHandler, throttleInput } from '../../utils/helpers';
import { checkIdleStatus, isActive, toggleIdle } from './helpers';
import { FunctionPoller } from '../../../../lib/poller/FunctionPoller';
import { inputHandler } from '../../utils/helpers';
import Variable from 'astal/variable';
import { bind } from 'astal';
import { BarBoxChild } from 'src/lib/types/bar';
import { Astal } from 'astal/gtk3';
const { label, pollingInterval, onIcon, offIcon, onLabel, offLabel, rightClick, middleClick, scrollUp, scrollDown } =
const { label, onIcon, offIcon, onLabel, offLabel, rightClick, middleClick, scrollUp, scrollDown } =
options.bar.customModules.hypridle;
const dummyVar = Variable(undefined);
checkIdleStatus();
const idleStatusPoller = new FunctionPoller<undefined, []>(dummyVar, [], bind(pollingInterval), checkIdleStatus);
idleStatusPoller.initialize('hypridle');
const throttledToggleIdle = throttleInput(() => toggleIdle(isActive), 1000);
function toggleInhibit(): void {
idleInhibit.set(!idleInhibit.get());
}
export const Hypridle = (): BarBoxChild => {
const iconBinding = Variable.derive([bind(isActive), bind(onIcon), bind(offIcon)], (active, onIcn, offIcn) => {
const iconBinding = Variable.derive([bind(idleInhibit), bind(onIcon), bind(offIcon)], (active, onIcn, offIcn) => {
return active ? onIcn : offIcn;
});
const labelBinding = Variable.derive([bind(isActive), bind(onLabel), bind(offLabel)], (active, onLbl, offLbl) => {
return active ? onLbl : offLbl;
});
const labelBinding = Variable.derive(
[bind(idleInhibit), bind(onLabel), bind(offLabel)],
(active, onLbl, offLbl) => {
return active ? onLbl : offLbl;
},
);
const hypridleModule = Module({
textIcon: iconBinding(),
tooltipText: bind(isActive).as((active) => `Hypridle ${active ? 'enabled' : 'disabled'}`),
tooltipText: bind(idleInhibit).as((active) => `Idle Inhibitor: ${active ? 'Enabled' : 'Disabled'}`),
boxClass: 'hypridle',
label: labelBinding(),
showLabelBinding: bind(label),
@@ -40,7 +36,7 @@ export const Hypridle = (): BarBoxChild => {
inputHandler(self, {
onPrimaryClick: {
fn: () => {
throttledToggleIdle();
toggleInhibit();
},
},
onSecondaryClick: {

View File

@@ -1,6 +1,6 @@
import { ResourceLabelType } from 'src/lib/types/bar';
import { GenericResourceData, Postfix, UpdateHandlers } from 'src/lib/types/customModules/generic';
import { InputHandlerEvents, RunAsyncCommand } from 'src/lib/types/customModules/utils';
import { InputHandlerEventArgs, InputHandlerEvents, RunAsyncCommand } from 'src/lib/types/customModules/utils';
import { ThrottleFn } from 'src/lib/types/utils';
import { bind, Binding, execAsync, Variable } from 'astal';
import { openMenu } from 'src/components/bar/utils/menu';
@@ -56,6 +56,15 @@ export const runAsyncCommand: RunAsyncCommand = (cmd, events, fn, postInputUpdat
.catch((err) => console.error(`Error running command "${cmd}": ${err})`));
};
/*
* NOTE: Added a throttle since spamming a button yields duplicate events
* which undo the toggle.
*/
const throttledAsyncCommand = throttleInput(
(cmd, events, fn, postInputUpdater?: Variable<boolean>) => runAsyncCommand(cmd, events, fn, postInputUpdater),
50,
);
/**
* Generic throttle function to limit the rate at which a function can be called.
*
@@ -90,7 +99,7 @@ export function throttleInput<T extends ThrottleFn>(func: T, limit: number): T {
*/
export const throttledScrollHandler = (interval: number): ThrottleFn =>
throttleInput((cmd: string, args, fn, postInputUpdater) => {
runAsyncCommand(cmd, args, fn, postInputUpdater);
throttledAsyncCommand(cmd, args, fn, postInputUpdater);
}, 200 / interval);
/**
@@ -114,7 +123,7 @@ export const inputHandler = (
}: InputHandlerEvents,
postInputUpdater?: Variable<boolean>,
): void => {
const sanitizeInput = (input: Variable<string>): string => {
const sanitizeInput = (input?: Variable<string> | Variable<string>): string => {
if (input === undefined) {
return '';
}
@@ -126,34 +135,34 @@ export const inputHandler = (
const throttledHandler = throttledScrollHandler(interval);
const disconnectPrimaryClick = onPrimaryClick(self, (clicked: GtkWidget, event: Gdk.Event) => {
runAsyncCommand(
throttledAsyncCommand(
sanitizeInput(onPrimaryClickInput?.cmd || dummyVar),
{ clicked, event },
onPrimaryClickInput.fn,
onPrimaryClickInput?.fn,
postInputUpdater,
);
});
const disconnectSecondaryClick = onSecondaryClick(self, (clicked: GtkWidget, event: Gdk.Event) => {
runAsyncCommand(
throttledAsyncCommand(
sanitizeInput(onSecondaryClickInput?.cmd || dummyVar),
{ clicked, event },
onSecondaryClickInput.fn,
onSecondaryClickInput?.fn,
postInputUpdater,
);
});
const disconnectMiddleClick = onMiddleClick(self, (clicked: GtkWidget, event: Gdk.Event) => {
runAsyncCommand(
throttledAsyncCommand(
sanitizeInput(onMiddleClickInput?.cmd || dummyVar),
{ clicked, event },
onMiddleClickInput.fn,
onMiddleClickInput?.fn,
postInputUpdater,
);
});
const id = self.connect('scroll-event', (self: GtkWidget, event: Gdk.Event) => {
const handleScroll = (input?: { cmd: Variable<string>; fn: (output: string) => void }): void => {
const handleScroll = (input?: InputHandlerEventArgs): void => {
if (input) {
throttledHandler(sanitizeInput(input.cmd), { clicked: self, event }, input.fn, postInputUpdater);
}
@@ -178,8 +187,8 @@ export const inputHandler = (
updateHandlers();
const sanitizeVariable = (someVar: Variable<string> | undefined): Binding<string> => {
if (someVar === undefined || typeof someVar.bind !== 'function') {
const sanitizeVariable = (someVar?: Variable<string>): Binding<string> => {
if (someVar === undefined) {
return bind(dummyVar);
}
return bind(someVar);
@@ -188,11 +197,11 @@ export const inputHandler = (
Variable.derive(
[
bind(scrollSpeed),
sanitizeVariable(onPrimaryClickInput),
sanitizeVariable(onSecondaryClickInput),
sanitizeVariable(onMiddleClickInput),
sanitizeVariable(onScrollUpInput),
sanitizeVariable(onScrollDownInput),
sanitizeVariable(onPrimaryClickInput?.cmd),
sanitizeVariable(onSecondaryClickInput?.cmd),
sanitizeVariable(onMiddleClickInput?.cmd),
sanitizeVariable(onScrollUpInput?.cmd),
sanitizeVariable(onScrollDownInput?.cmd),
],
() => {
const handlers = updateHandlers();