Added the ability to assign shortcuts to all bar modules. (#278)
This commit is contained in:
@@ -3,15 +3,14 @@ import options from 'options';
|
||||
// Module initializer
|
||||
import { module } from '../module';
|
||||
|
||||
// import { CpuData } from "lib/types/customModules/cpu";
|
||||
import Button from 'types/widgets/button';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
|
||||
// Utility Methods
|
||||
import { inputHandler } from 'customModules/utils';
|
||||
import { computeCPU } from './computeCPU';
|
||||
import { pollVariable } from 'customModules/PollVar';
|
||||
import { Module } from 'lib/types/bar';
|
||||
import { BarBoxChild } from 'lib/types/bar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
|
||||
// All the user configurable options for the cpu module that are needed
|
||||
const { label, round, leftClick, rightClick, middleClick, scrollUp, scrollDown, pollingInterval } =
|
||||
@@ -30,7 +29,7 @@ pollVariable(
|
||||
computeCPU,
|
||||
);
|
||||
|
||||
export const Cpu = (): Module => {
|
||||
export const Cpu = (): BarBoxChild => {
|
||||
const renderLabel = (cpuUsg: number, rnd: boolean): string => {
|
||||
return rnd ? `${Math.round(cpuUsg)}%` : `${cpuUsg.toFixed(2)}%`;
|
||||
};
|
||||
@@ -44,7 +43,7 @@ export const Cpu = (): Module => {
|
||||
boxClass: 'cpu',
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -8,12 +8,13 @@ import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import Button from 'types/widgets/button';
|
||||
import Label from 'types/widgets/label';
|
||||
import { getKeyboardLayout } from './getLayout';
|
||||
import { Module } from 'lib/types/bar';
|
||||
import { BarBoxChild } from 'lib/types/bar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
|
||||
const { label, labelType, icon, leftClick, rightClick, middleClick, scrollUp, scrollDown } =
|
||||
options.bar.customModules.kbLayout;
|
||||
|
||||
export const KbInput = (): Module => {
|
||||
export const KbInput = (): BarBoxChild => {
|
||||
const keyboardModule = module({
|
||||
textIcon: icon.bind('value'),
|
||||
tooltipText: '',
|
||||
@@ -46,7 +47,7 @@ export const KbInput = (): Module => {
|
||||
boxClass: 'kblayout',
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -2,13 +2,13 @@ import options from 'options';
|
||||
import { module } from '../module';
|
||||
import { inputHandler } from 'customModules/utils';
|
||||
import { computeNetwork } from './computeNetwork';
|
||||
import { Module, NetstatLabelType } from 'lib/types/bar';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import { BarBoxChild, NetstatLabelType } from 'lib/types/bar';
|
||||
import Button from 'types/widgets/button';
|
||||
import { NetworkResourceData } from 'lib/types/customModules/network';
|
||||
import { NETWORK_LABEL_TYPES } from 'lib/types/defaults/bar';
|
||||
import { GET_DEFAULT_NETSTAT_DATA } from 'lib/types/defaults/netstat';
|
||||
import { pollVariable } from 'customModules/PollVar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
|
||||
const {
|
||||
label,
|
||||
@@ -46,7 +46,7 @@ pollVariable(
|
||||
rateUnit,
|
||||
);
|
||||
|
||||
export const Netstat = (): Module => {
|
||||
export const Netstat = (): BarBoxChild => {
|
||||
const renderNetworkLabel = (lblType: NetstatLabelType, network: NetworkResourceData): string => {
|
||||
switch (lblType) {
|
||||
case 'in':
|
||||
@@ -70,7 +70,7 @@ export const Netstat = (): Module => {
|
||||
boxClass: 'netstat',
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -2,19 +2,19 @@ import options from 'options';
|
||||
import { module } from '../module';
|
||||
|
||||
import { inputHandler } from 'customModules/utils';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import Button from 'types/widgets/button';
|
||||
import { Module } from 'lib/types/bar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
import { BarBoxChild } from 'lib/types/bar';
|
||||
|
||||
const { icon, leftClick, rightClick, middleClick, scrollUp, scrollDown } = options.bar.customModules.power;
|
||||
|
||||
export const Power = (): Module => {
|
||||
export const Power = (): BarBoxChild => {
|
||||
const powerModule = module({
|
||||
tooltipText: 'Power Menu',
|
||||
textIcon: icon.bind('value'),
|
||||
boxClass: 'powermodule',
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -6,18 +6,18 @@ import { module } from '../module';
|
||||
// Types
|
||||
import { GenericResourceData } from 'lib/types/customModules/generic';
|
||||
import Button from 'types/widgets/button';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
|
||||
// Helper Methods
|
||||
import { calculateRamUsage } from './computeRam';
|
||||
|
||||
// Utility Methods
|
||||
import { formatTooltip, inputHandler, renderResourceLabel } from 'customModules/utils';
|
||||
import { Module, ResourceLabelType } from 'lib/types/bar';
|
||||
import { BarBoxChild, ResourceLabelType } from 'lib/types/bar';
|
||||
|
||||
// Global Constants
|
||||
import { LABEL_TYPES } from 'lib/types/defaults/bar';
|
||||
import { pollVariable } from 'customModules/PollVar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
|
||||
// All the user configurable options for the ram module that are needed
|
||||
const { label, labelType, round, leftClick, rightClick, middleClick, pollingInterval } = options.bar.customModules.ram;
|
||||
@@ -27,7 +27,7 @@ const ramUsage = Variable<GenericResourceData>(defaultRamData);
|
||||
|
||||
pollVariable(ramUsage, [round.bind('value')], pollingInterval.bind('value'), calculateRamUsage, round);
|
||||
|
||||
export const Ram = (): Module => {
|
||||
export const Ram = (): BarBoxChild => {
|
||||
const ramModule = module({
|
||||
textIcon: '',
|
||||
label: Utils.merge(
|
||||
@@ -44,7 +44,7 @@ export const Ram = (): Module => {
|
||||
boxClass: 'ram',
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -3,12 +3,12 @@ import { module } from '../module';
|
||||
|
||||
import { formatTooltip, inputHandler, renderResourceLabel } from 'customModules/utils';
|
||||
import { computeStorage } from './computeStorage';
|
||||
import { Module, ResourceLabelType } from 'lib/types/bar';
|
||||
import { BarBoxChild, ResourceLabelType } from 'lib/types/bar';
|
||||
import { GenericResourceData } from 'lib/types/customModules/generic';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import Button from 'types/widgets/button';
|
||||
import { LABEL_TYPES } from 'lib/types/defaults/bar';
|
||||
import { pollVariable } from 'customModules/PollVar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
|
||||
const { label, labelType, icon, round, leftClick, rightClick, middleClick, pollingInterval } =
|
||||
options.bar.customModules.storage;
|
||||
@@ -19,7 +19,7 @@ const storageUsage = Variable<GenericResourceData>(defaultStorageData);
|
||||
|
||||
pollVariable(storageUsage, [round.bind('value')], pollingInterval.bind('value'), computeStorage, round);
|
||||
|
||||
export const Storage = (): Module => {
|
||||
export const Storage = (): BarBoxChild => {
|
||||
const storageModule = module({
|
||||
textIcon: icon.bind('value'),
|
||||
label: Utils.merge(
|
||||
@@ -34,7 +34,7 @@ export const Storage = (): Module => {
|
||||
boxClass: 'storage',
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -2,11 +2,11 @@ import options from 'options';
|
||||
import { module } from '../module';
|
||||
|
||||
import { inputHandler } from 'customModules/utils';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import Button from 'types/widgets/button';
|
||||
import { Variable as VariableType } from 'types/variable';
|
||||
import { pollVariableBash } from 'customModules/PollVar';
|
||||
import { Module } from 'lib/types/bar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
import { BarBoxChild } from 'lib/types/bar';
|
||||
|
||||
const {
|
||||
updateCommand,
|
||||
@@ -36,7 +36,7 @@ pollVariableBash(
|
||||
processUpdateCount,
|
||||
);
|
||||
|
||||
export const Updates = (): Module => {
|
||||
export const Updates = (): BarBoxChild => {
|
||||
const updatesModule = module({
|
||||
textIcon: icon.bind('value'),
|
||||
tooltipText: pendingUpdates.bind('value').as((v) => `${v} updates available`),
|
||||
@@ -44,7 +44,7 @@ export const Updates = (): Module => {
|
||||
label: pendingUpdates.bind('value'),
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
import { ResourceLabelType } from 'lib/types/bar';
|
||||
import { GenericResourceData, Postfix } from 'lib/types/customModules/generic';
|
||||
import { InputHandlerEvents } from 'lib/types/customModules/utils';
|
||||
import { InputHandlerEvents, RunAsyncCommand } from 'lib/types/customModules/utils';
|
||||
import { ThrottleFn, ThrottleFnCallback } from 'lib/types/utils';
|
||||
import { GtkWidget } from 'lib/types/widget';
|
||||
import { Attribute, Child, EventArgs } from 'lib/types/widget';
|
||||
import { Binding } from 'lib/utils';
|
||||
import { openMenu } from 'modules/bar/utils';
|
||||
import options from 'options';
|
||||
import Gdk from 'types/@girs/gdk-3.0/gdk-3.0';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import { Variable as VariableType } from 'types/variable';
|
||||
import Button from 'types/widgets/button';
|
||||
|
||||
const { scrollSpeed } = options.bar.customModules;
|
||||
|
||||
export const runAsyncCommand = (
|
||||
cmd: string,
|
||||
fn: (output: string) => void,
|
||||
events: { clicked: Button<GtkWidget, GtkWidget>; event: Gdk.Event },
|
||||
): void => {
|
||||
export const runAsyncCommand: RunAsyncCommand = (cmd, events, fn): void => {
|
||||
if (cmd.startsWith('menu:')) {
|
||||
const menuName = cmd.split(':')[1].trim().toLowerCase();
|
||||
openMenu(events.clicked, events.event, `${menuName}menu`);
|
||||
@@ -48,20 +43,14 @@ export function throttle<T extends ThrottleFn>(func: T, limit: number): T {
|
||||
}
|
||||
|
||||
export const throttledScrollHandler = (interval: number): ThrottleFn =>
|
||||
throttle((cmd: string, fn: ThrottleFnCallback) => {
|
||||
Utils.execAsync(`bash -c "${cmd}"`)
|
||||
.then((output) => {
|
||||
if (fn !== undefined) {
|
||||
fn(output);
|
||||
}
|
||||
})
|
||||
.catch((err) => console.error(`Error running command "${cmd}": ${err}`));
|
||||
throttle((cmd: string, events: EventArgs, fn: ThrottleFnCallback) => {
|
||||
runAsyncCommand(cmd, events, fn);
|
||||
}, 200 / interval);
|
||||
|
||||
const dummyVar = Variable('');
|
||||
|
||||
export const inputHandler = (
|
||||
self: Button<Gtk.Widget, Gtk.Widget>,
|
||||
self: Button<Child, Attribute>,
|
||||
{ onPrimaryClick, onSecondaryClick, onMiddleClick, onScrollUp, onScrollDown }: InputHandlerEvents,
|
||||
): void => {
|
||||
const sanitizeInput = (input: VariableType<string>): string => {
|
||||
@@ -75,19 +64,20 @@ export const inputHandler = (
|
||||
const interval = scrollSpeed.value;
|
||||
const throttledHandler = throttledScrollHandler(interval);
|
||||
|
||||
self.on_primary_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onPrimaryClick?.cmd || dummyVar), onPrimaryClick.fn, { clicked, event });
|
||||
self.on_primary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onPrimaryClick?.cmd || dummyVar), { clicked, event }, onPrimaryClick.fn);
|
||||
|
||||
self.on_secondary_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onSecondaryClick?.cmd || dummyVar), onSecondaryClick.fn, { clicked, event });
|
||||
self.on_secondary_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onSecondaryClick?.cmd || dummyVar), { clicked, event }, onSecondaryClick.fn);
|
||||
|
||||
self.on_middle_click = (clicked: Button<GtkWidget, GtkWidget>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onMiddleClick?.cmd || dummyVar), onMiddleClick.fn, { clicked, event });
|
||||
self.on_middle_click = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
runAsyncCommand(sanitizeInput(onMiddleClick?.cmd || dummyVar), { clicked, event }, onMiddleClick.fn);
|
||||
|
||||
self.on_scroll_up = (): void => throttledHandler(sanitizeInput(onScrollUp?.cmd || dummyVar), onScrollUp.fn);
|
||||
self.on_scroll_up = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
throttledHandler(sanitizeInput(onScrollUp?.cmd || dummyVar), { clicked, event }, onScrollUp.fn);
|
||||
|
||||
self.on_scroll_down = (): void =>
|
||||
throttledHandler(sanitizeInput(onScrollDown?.cmd || dummyVar), onScrollDown.fn);
|
||||
self.on_scroll_down = (clicked: Button<Child, Attribute>, event: Gdk.Event): void =>
|
||||
throttledHandler(sanitizeInput(onScrollDown?.cmd || dummyVar), { clicked, event }, onScrollDown.fn);
|
||||
};
|
||||
|
||||
// Initial setup of event handlers
|
||||
|
||||
@@ -2,14 +2,14 @@ import options from 'options';
|
||||
import { module } from '../module';
|
||||
|
||||
import { inputHandler } from 'customModules/utils';
|
||||
import Gtk from 'types/@girs/gtk-3.0/gtk-3.0';
|
||||
import Button from 'types/widgets/button';
|
||||
import { getWeatherStatusTextIcon, globalWeatherVar } from 'globals/weather';
|
||||
import { Module } from 'lib/types/bar';
|
||||
import { Attribute, Child } from 'lib/types/widget';
|
||||
import { BarBoxChild } from 'lib/types/bar';
|
||||
|
||||
const { label, unit, leftClick, rightClick, middleClick, scrollUp, scrollDown } = options.bar.customModules.weather;
|
||||
|
||||
export const Weather = (): Module => {
|
||||
export const Weather = (): BarBoxChild => {
|
||||
const weatherModule = module({
|
||||
textIcon: Utils.merge([globalWeatherVar.bind('value')], (wthr) => {
|
||||
const weatherStatusIcon = getWeatherStatusTextIcon(wthr);
|
||||
@@ -26,7 +26,7 @@ export const Weather = (): Module => {
|
||||
}),
|
||||
showLabelBinding: label.bind('value'),
|
||||
props: {
|
||||
setup: (self: Button<Gtk.Widget, Gtk.Widget>) => {
|
||||
setup: (self: Button<Child, Attribute>) => {
|
||||
inputHandler(self, {
|
||||
onPrimaryClick: {
|
||||
cmd: leftClick,
|
||||
|
||||
Reference in New Issue
Block a user