Added a cpu temperature custom module. (#446)

* Added a CPU Temperature module.

* Update defauls and add wiki link.

* Move celsius to fahr conversion to method.
This commit is contained in:
Jas Singh
2024-11-06 02:52:22 -08:00
committed by GitHub
parent ec3be1cad8
commit 22055b727a
9 changed files with 281 additions and 0 deletions

View File

@@ -152,6 +152,89 @@ export const CustomModuleSettings = (): Scrollable<GtkWidget, Attribute> =>
type: 'string',
}),
/*
************************************
* CPU TEMP *
************************************
*/
Header('CPU Temperature'),
Option({
opt: options.theme.bar.buttons.modules.cpuTemp.enableBorder,
title: 'Button Border',
type: 'boolean',
}),
Option({
opt: options.bar.customModules.cpuTemp.sensor,
title: 'CPU Temperature Sensor',
subtitle: 'Wiki: https://hyprpanel.com/configuration/panel.html#custom-modules',
subtitleLink: 'https://hyprpanel.com/configuration/panel.html#custom-modules',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.unit,
title: 'CPU Temperature Unit',
type: 'enum',
enums: ['imperial', 'metric'],
}),
Option({
opt: options.bar.customModules.cpuTemp.showUnit,
title: 'Show Unit',
type: 'boolean',
}),
Option({
opt: options.bar.customModules.cpuTemp.icon,
title: 'Cpu Temperature Icon',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.label,
title: 'Show Label',
type: 'boolean',
}),
Option({
opt: options.theme.bar.buttons.modules.cpuTemp.spacing,
title: 'Spacing',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.round,
title: 'Round',
type: 'boolean',
}),
Option({
opt: options.bar.customModules.cpuTemp.pollingInterval,
title: 'Polling Interval',
type: 'number',
min: 100,
max: 60 * 24 * 1000,
increment: 1000,
}),
Option({
opt: options.bar.customModules.cpuTemp.leftClick,
title: 'Left Click',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.rightClick,
title: 'Right Click',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.middleClick,
title: 'Middle Click',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.scrollUp,
title: 'Scroll Up',
type: 'string',
}),
Option({
opt: options.bar.customModules.cpuTemp.scrollDown,
title: 'Scroll Down',
type: 'string',
}),
/*
************************************
* STORAGE *

View File

@@ -0,0 +1,37 @@
import GLib from 'gi://GLib?version=2.0';
import { convertCelsiusToFahrenheit } from 'globals/weather';
import { UnitType } from 'lib/types/weather';
import options from 'options';
import { Variable } from 'types/variable';
const { sensor } = options.bar.customModules.cpuTemp;
/**
* Retrieves the current CPU temperature.
* @returns CPU temperature in degrees Celsius
*/
export const getCPUTemperature = (round: Variable<boolean>, unit: Variable<UnitType>): number => {
try {
if (sensor.value.length === 0) {
return 0;
}
const [success, tempInfoBytes] = GLib.file_get_contents(sensor.value);
const tempInfo = new TextDecoder('utf-8').decode(tempInfoBytes);
if (!success || !tempInfoBytes) {
console.error(`Failed to read ${sensor.value} or file content is null.`);
return 0;
}
let decimalTemp = parseInt(tempInfo) / 1000;
if (unit.value === 'imperial') {
decimalTemp = convertCelsiusToFahrenheit(decimalTemp);
}
return round ? Math.round(decimalTemp) : parseFloat(decimalTemp.toFixed(2));
} catch (error) {
console.error('Error calculating CPU Temp:', error);
return 0;
}
};

View File

@@ -0,0 +1,85 @@
import options from 'options';
// Module initializer
import { module } from '../module';
import Button from 'types/widgets/button';
// Utility Methods
import { inputHandler } from 'customModules/utils';
import { getCPUTemperature } from './helpers';
import { pollVariable } from 'customModules/PollVar';
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,
sensor,
round,
showUnit,
unit,
leftClick,
rightClick,
middleClick,
scrollUp,
scrollDown,
pollingInterval,
icon,
} = options.bar.customModules.cpuTemp;
export const cpuTemp = Variable(0);
pollVariable(
// Variable to poll and update with the result of the function passed in
cpuTemp,
// Variables that should trigger the polling function to update when they change
[sensor.bind('value'), round.bind('value'), unit.bind('value')],
// Interval at which to poll
pollingInterval.bind('value'),
// Function to execute to get the network data
getCPUTemperature,
round,
unit,
);
export const CpuTemp = (): BarBoxChild => {
const cpuTempModule = module({
textIcon: icon.bind('value'),
label: Utils.merge(
[cpuTemp.bind('value'), unit.bind('value'), showUnit.bind('value'), round.bind('value')],
(cpuTmp, tempUnit, shwUnit) => {
const unitLabel = tempUnit === 'imperial' ? 'F' : 'C';
const unit = shwUnit ? ` ${unitLabel}` : '';
return `${cpuTmp.toString()}°${unit}`;
},
),
tooltipText: 'CPU Temperature',
boxClass: 'cpu-temp',
showLabelBinding: label.bind('value'),
props: {
setup: (self: Button<Child, Attribute>) => {
inputHandler(self, {
onPrimaryClick: {
cmd: leftClick,
},
onSecondaryClick: {
cmd: rightClick,
},
onMiddleClick: {
cmd: middleClick,
},
onScrollUp: {
cmd: scrollUp,
},
onScrollDown: {
cmd: scrollDown,
},
});
},
},
});
return cpuTempModule;
};

View File

@@ -48,6 +48,23 @@ export const CustomModuleTheme = (): Scrollable<GtkWidget, Attribute> => {
}),
Option({ opt: options.theme.bar.buttons.modules.cpu.border, title: 'Border', type: 'color' }),
Header('CPU Temperature'),
Option({ opt: options.theme.bar.buttons.modules.cpuTemp.text, title: 'Text', type: 'color' }),
Option({ opt: options.theme.bar.buttons.modules.cpuTemp.icon, title: 'Icon', type: 'color' }),
Option({
opt: options.theme.bar.buttons.modules.cpuTemp.background,
title: 'Label Background',
type: 'color',
}),
Option({
opt: options.theme.bar.buttons.modules.cpuTemp.icon_background,
title: 'Icon Background',
subtitle:
"Applies a background color to the icon section of the button.\nRequires 'split' button styling.",
type: 'color',
}),
Option({ opt: options.theme.bar.buttons.modules.cpuTemp.border, title: 'Border', type: 'color' }),
Header('Storage'),
Option({ opt: options.theme.bar.buttons.modules.storage.text, title: 'Text', type: 'color' }),
Option({ opt: options.theme.bar.buttons.modules.storage.icon, title: 'Icon', type: 'color' }),