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' }),

View File

@@ -215,4 +215,8 @@ export const getWeatherStatusTextIcon = (weatherData: Weather): WeatherIcon => {
}
};
export const convertCelsiusToFahrenheit = (celsiusValue: number): number => {
return (celsiusValue * 9) / 5 + 32;
};
globalThis['globalWeatherVar'] = globalWeatherVar;

View File

@@ -16,6 +16,7 @@ import {
// Custom Modules
Ram,
Cpu,
CpuTemp,
Storage,
Netstat,
KbInput,
@@ -55,6 +56,7 @@ type Section =
| 'clock'
| 'ram'
| 'cpu'
| 'cputemp'
| 'storage'
| 'netstat'
| 'kbinput'
@@ -107,6 +109,7 @@ const widget = {
systray: (): Button<Child, Attribute> => WidgetContainer(SysTray()),
ram: (): Button<Child, Attribute> => WidgetContainer(Ram()),
cpu: (): Button<Child, Attribute> => WidgetContainer(Cpu()),
cputemp: (): Button<Child, Attribute> => WidgetContainer(CpuTemp()),
storage: (): Button<Child, Attribute> => WidgetContainer(Storage()),
netstat: (): Button<Child, Attribute> => WidgetContainer(Netstat()),
kbinput: (): Button<Child, Attribute> => WidgetContainer(KbInput()),

View File

@@ -13,6 +13,7 @@ import { SysTray } from './systray/index';
// Custom Modules
import { Ram } from '../../customModules/ram/index';
import { Cpu } from '../../customModules/cpu/index';
import { CpuTemp } from 'customModules/cputemp/index';
import { Storage } from 'customModules/storage/index';
import { Netstat } from 'customModules/netstat/index';
import { KbInput } from 'customModules/kblayout/index';
@@ -37,6 +38,7 @@ export {
// Custom Modules
Ram,
Cpu,
CpuTemp,
Storage,
Netstat,
KbInput,

View File

@@ -303,6 +303,15 @@ const options = mkOptions(OPTIONS, {
icon_background: opt(colors.base2),
spacing: opt('0.5em'),
},
cpuTemp: {
enableBorder: opt(false),
border: opt(colors.peach),
background: opt(colors.base2),
text: opt(colors.peach),
icon: opt(colors.peach),
icon_background: opt(colors.base2),
spacing: opt('0.5em'),
},
storage: {
enableBorder: opt(false),
border: opt(colors.pink),
@@ -987,6 +996,20 @@ const options = mkOptions(OPTIONS, {
scrollUp: opt(''),
scrollDown: opt(''),
},
cpuTemp: {
icon: opt(''),
sensor: opt(''),
label: opt(true),
round: opt(true),
showUnit: opt(true),
unit: opt<UnitType>('metric'),
pollingInterval: opt(2000),
leftClick: opt(''),
rightClick: opt(''),
middleClick: opt(''),
scrollUp: opt(''),
scrollDown: opt(''),
},
storage: {
label: opt(true),
icon: opt('󰋊'),

View File

@@ -179,6 +179,33 @@
1.05em //
);
/*
* #################################
* # Cpu Temp Module Styling #
* #################################
*/
@include styleModule(
//
// class name
'cpu-temp',
// label color
$bar-buttons-modules-cpuTemp-text,
// icon color
$bar-buttons-modules-cpuTemp-icon,
// icon background if split style is used
$bar-buttons-modules-cpuTemp-icon_background,
// label background
$bar-buttons-modules-cpuTemp-background,
// inner spacing
$bar-buttons-modules-cpuTemp-spacing,
// if border enabled
$bar-buttons-modules-cpuTemp-enableBorder,
// border color
$bar-buttons-modules-cpuTemp-border,
// custom font size
1.05em //
);
/*
* #################################
* # Storage Module Styling #