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