Added the ability to update custom modules after a command is executed. (#476)
This commit is contained in:
@@ -21,7 +21,8 @@ const {
|
||||
scrollDown,
|
||||
} = options.bar.customModules.updates;
|
||||
|
||||
const pendingUpdates: VariableType<string> = Variable(' 0');
|
||||
const pendingUpdates: VariableType<string> = Variable('0');
|
||||
const postInputUpdater = Variable(true);
|
||||
|
||||
const processUpdateCount = (updateCount: string): string => {
|
||||
if (!padZero.value) return updateCount;
|
||||
@@ -30,7 +31,7 @@ const processUpdateCount = (updateCount: string): string => {
|
||||
|
||||
pollVariableBash(
|
||||
pendingUpdates,
|
||||
[padZero.bind('value')],
|
||||
[padZero.bind('value'), postInputUpdater.bind('value')],
|
||||
pollingInterval.bind('value'),
|
||||
updateCommand.value,
|
||||
processUpdateCount,
|
||||
@@ -45,23 +46,27 @@ export const Updates = (): BarBoxChild => {
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
inputHandler(
|
||||
self,
|
||||
{
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
},
|
||||
onSecondaryClick: {
|
||||
cmd: rightClick,
|
||||
},
|
||||
onMiddleClick: {
|
||||
cmd: middleClick,
|
||||
},
|
||||
onScrollUp: {
|
||||
cmd: scrollUp,
|
||||
},
|
||||
onScrollDown: {
|
||||
cmd: scrollDown,
|
||||
},
|
||||
},
|
||||
onSecondaryClick: {
|
||||
cmd: rightClick,
|
||||
},
|
||||
onMiddleClick: {
|
||||
cmd: middleClick,
|
||||
},
|
||||
onScrollUp: {
|
||||
cmd: scrollUp,
|
||||
},
|
||||
onScrollDown: {
|
||||
cmd: scrollDown,
|
||||
},
|
||||
});
|
||||
postInputUpdater,
|
||||
);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -12,7 +12,13 @@ import Button from 'types/widgets/button';
|
||||
|
||||
const { scrollSpeed } = options.bar.customModules;
|
||||
|
||||
export const runAsyncCommand: RunAsyncCommand = (cmd, events, fn): void => {
|
||||
const handlePostInputUpdater = (postInputUpdater?: VariableType<boolean>): void => {
|
||||
if (postInputUpdater !== undefined) {
|
||||
postInputUpdater.value = !postInputUpdater.value;
|
||||
}
|
||||
};
|
||||
|
||||
export const runAsyncCommand: RunAsyncCommand = (cmd, events, fn, postInputUpdater?: VariableType<boolean>): void => {
|
||||
if (cmd.startsWith('menu:')) {
|
||||
const menuName = cmd.split(':')[1].trim().toLowerCase();
|
||||
openMenu(events.clicked, events.event, `${menuName}menu`);
|
||||
@@ -22,6 +28,7 @@ export const runAsyncCommand: RunAsyncCommand = (cmd, events, fn): void => {
|
||||
|
||||
Utils.execAsync(`bash -c "${cmd}"`)
|
||||
.then((output) => {
|
||||
handlePostInputUpdater(postInputUpdater);
|
||||
if (fn !== undefined) {
|
||||
fn(output);
|
||||
}
|
||||
@@ -43,8 +50,8 @@ export function throttle<T extends ThrottleFn>(func: T, limit: number): T {
|
||||
}
|
||||
|
||||
export const throttledScrollHandler = (interval: number): ThrottleFn =>
|
||||
throttle((cmd: string, events: EventArgs, fn: ThrottleFnCallback) => {
|
||||
runAsyncCommand(cmd, events, fn);
|
||||
throttle((cmd: string, events: EventArgs, fn: ThrottleFnCallback, postInputUpdater?: VariableType<boolean>) => {
|
||||
runAsyncCommand(cmd, events, fn, postInputUpdater);
|
||||
}, 200 / interval);
|
||||
|
||||
const dummyVar = Variable('');
|
||||
@@ -52,6 +59,7 @@ const dummyVar = Variable('');
|
||||
export const inputHandler = (
|
||||
self: Button<Child, Attribute>,
|
||||
{ onPrimaryClick, onSecondaryClick, onMiddleClick, onScrollUp, onScrollDown }: InputHandlerEvents,
|
||||
postInputUpdater?: VariableType<boolean>,
|
||||
): void => {
|
||||
const sanitizeInput = (input: VariableType<string>): string => {
|
||||
if (input === undefined) {
|
||||
@@ -64,20 +72,50 @@ export const inputHandler = (
|
||||
const interval = scrollSpeed.value;
|
||||
const throttledHandler = throttledScrollHandler(interval);
|
||||
|
||||
self.on_primary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onPrimaryClick?.cmd || dummyVar), { clicked, event }, onPrimaryClick.fn);
|
||||
self.on_primary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void => {
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onPrimaryClick?.cmd || dummyVar),
|
||||
{ clicked, event },
|
||||
onPrimaryClick.fn,
|
||||
postInputUpdater,
|
||||
);
|
||||
};
|
||||
|
||||
self.on_secondary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onSecondaryClick?.cmd || dummyVar), { clicked, event }, onSecondaryClick.fn);
|
||||
self.on_secondary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void => {
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onSecondaryClick?.cmd || dummyVar),
|
||||
{ clicked, event },
|
||||
onSecondaryClick.fn,
|
||||
postInputUpdater,
|
||||
);
|
||||
};
|
||||
|
||||
self.on_middle_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onMiddleClick?.cmd || dummyVar), { clicked, event }, onMiddleClick.fn);
|
||||
self.on_middle_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void => {
|
||||
runAsyncCommand(
|
||||
sanitizeInput(onMiddleClick?.cmd || dummyVar),
|
||||
{ clicked, event },
|
||||
onMiddleClick.fn,
|
||||
postInputUpdater,
|
||||
);
|
||||
};
|
||||
|
||||
self.on_scroll_up = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
throttledHandler(sanitizeInput(onScrollUp?.cmd || dummyVar), { clicked, event }, onScrollUp.fn);
|
||||
self.on_scroll_up = (clicked: Button<Child, Attribute>, event: Gdk.Event): void => {
|
||||
throttledHandler(
|
||||
sanitizeInput(onScrollUp?.cmd || dummyVar),
|
||||
{ clicked, event },
|
||||
onScrollUp.fn,
|
||||
postInputUpdater,
|
||||
);
|
||||
};
|
||||
|
||||
self.on_scroll_down = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
throttledHandler(sanitizeInput(onScrollDown?.cmd || dummyVar), { clicked, event }, onScrollDown.fn);
|
||||
self.on_scroll_down = (clicked: Button<Child, Attribute>, event: Gdk.Event): void => {
|
||||
throttledHandler(
|
||||
sanitizeInput(onScrollDown?.cmd || dummyVar),
|
||||
{ clicked, event },
|
||||
onScrollDown.fn,
|
||||
postInputUpdater,
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
// Initial setup of event handlers
|
||||
|
||||
8
lib/types/customModules/utils.d.ts
vendored
8
lib/types/customModules/utils.d.ts
vendored
@@ -1,4 +1,5 @@
|
||||
import { Binding } from 'lib/utils';
|
||||
import { Variable } from 'types/variable';
|
||||
|
||||
export type InputHandlerEvents = {
|
||||
onPrimaryClick?: Binding;
|
||||
@@ -8,4 +9,9 @@ export type InputHandlerEvents = {
|
||||
onScrollDown?: Binding;
|
||||
};
|
||||
|
||||
export type RunAsyncCommand = (cmd: string, args: EventArgs, fn?: (output: string) => void) => void;
|
||||
export type RunAsyncCommand = (
|
||||
cmd: string,
|
||||
args: EventArgs,
|
||||
fn?: (output: string) => void,
|
||||
postInputUpdater?: Variable<boolean>,
|
||||
) => void;
|
||||
|
||||
8
lib/types/utils.d.ts
vendored
8
lib/types/utils.d.ts
vendored
@@ -1,7 +1,13 @@
|
||||
import { substitutes } from 'lib/icons';
|
||||
import { EventArgs } from './widget';
|
||||
import { Variable } from 'types/variable';
|
||||
|
||||
type SubstituteKeys = keyof typeof substitutes;
|
||||
|
||||
export type ThrottleFn = (cmd: string, args: EventArgs, fn?: (output: string) => void) => void;
|
||||
export type ThrottleFn = (
|
||||
cmd: string,
|
||||
args: EventArgs,
|
||||
fn?: (output: string) => void,
|
||||
postInputUpdated?: Variable<boolean>,
|
||||
) => void;
|
||||
export type ThrottleFnCallback = ((output: string) => void) | undefined;
|
||||
|
||||
Reference in New Issue
Block a user