Files
custum-hyprpanel/src/components/bar/modules/cpu/index.tsx
davfsa 7fe89ac5eb Add up-to-date default workspace labels (#709)
* Add up-to-date workspace labels

* Use iterators instead of creating copies of the mappings

* Re-add guard clause

Signed-off-by: davfsa <davfsa@gmail.com>

* merge origin

* Fix duplicate icons and simplify implementation.

---------

Signed-off-by: davfsa <davfsa@gmail.com>
Co-authored-by: Jas Singh <jaskiratpal.singh@outlook.com>
2025-03-17 19:05:14 -07:00

58 lines
1.8 KiB
TypeScript

import { Module } from '../../shared/Module';
import options from 'src/options';
import { inputHandler } from 'src/components/bar/utils/helpers';
import { computeCPU } from './helpers';
import { BarBoxChild } from 'src/lib/types/bar';
import { FunctionPoller } from 'src/lib/poller/FunctionPoller';
import { bind, Variable } from 'astal';
import { Astal } from 'astal/gtk3';
const { label, round, leftClick, rightClick, middleClick, scrollUp, scrollDown, pollingInterval, icon } =
options.bar.customModules.cpu;
export const cpuUsage = Variable(0);
const cpuPoller = new FunctionPoller<number, []>(cpuUsage, [bind(round)], bind(pollingInterval), computeCPU);
cpuPoller.initialize('cpu');
export const Cpu = (): BarBoxChild => {
const labelBinding = Variable.derive([bind(cpuUsage), bind(round)], (cpuUsg: number, round: boolean) => {
return round ? `${Math.round(cpuUsg)}%` : `${cpuUsg.toFixed(2)}%`;
});
const cpuModule = Module({
textIcon: bind(icon),
label: labelBinding(),
tooltipText: 'CPU',
boxClass: 'cpu',
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,
},
});
},
onDestroy: () => {
labelBinding.drop();
},
},
});
return cpuModule;
};