diff --git a/customModules/config.ts b/customModules/config.ts index 16d3f09..d3bd287 100644 --- a/customModules/config.ts +++ b/customModules/config.ts @@ -152,6 +152,89 @@ export const CustomModuleSettings = (): Scrollable => 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 * diff --git a/customModules/cputemp/helpers.ts b/customModules/cputemp/helpers.ts new file mode 100644 index 0000000..1663698 --- /dev/null +++ b/customModules/cputemp/helpers.ts @@ -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, unit: Variable): 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; + } +}; diff --git a/customModules/cputemp/index.ts b/customModules/cputemp/index.ts new file mode 100644 index 0000000..ac9d94e --- /dev/null +++ b/customModules/cputemp/index.ts @@ -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) => { + inputHandler(self, { + onPrimaryClick: { + cmd: leftClick, + }, + onSecondaryClick: { + cmd: rightClick, + }, + onMiddleClick: { + cmd: middleClick, + }, + onScrollUp: { + cmd: scrollUp, + }, + onScrollDown: { + cmd: scrollDown, + }, + }); + }, + }, + }); + + return cpuTempModule; +}; diff --git a/customModules/theme.ts b/customModules/theme.ts index 2ac92ff..09b4b84 100644 --- a/customModules/theme.ts +++ b/customModules/theme.ts @@ -48,6 +48,23 @@ export const CustomModuleTheme = (): Scrollable => { }), 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' }), diff --git a/globals/weather.ts b/globals/weather.ts index 026ce60..1e140c7 100644 --- a/globals/weather.ts +++ b/globals/weather.ts @@ -215,4 +215,8 @@ export const getWeatherStatusTextIcon = (weatherData: Weather): WeatherIcon => { } }; +export const convertCelsiusToFahrenheit = (celsiusValue: number): number => { + return (celsiusValue * 9) / 5 + 32; +}; + globalThis['globalWeatherVar'] = globalWeatherVar; diff --git a/modules/bar/Bar.ts b/modules/bar/Bar.ts index dfb4567..a9d6b8b 100644 --- a/modules/bar/Bar.ts +++ b/modules/bar/Bar.ts @@ -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 => WidgetContainer(SysTray()), ram: (): Button => WidgetContainer(Ram()), cpu: (): Button => WidgetContainer(Cpu()), + cputemp: (): Button => WidgetContainer(CpuTemp()), storage: (): Button => WidgetContainer(Storage()), netstat: (): Button => WidgetContainer(Netstat()), kbinput: (): Button => WidgetContainer(KbInput()), diff --git a/modules/bar/Exports.ts b/modules/bar/Exports.ts index 6a17b2f..0cb4547 100644 --- a/modules/bar/Exports.ts +++ b/modules/bar/Exports.ts @@ -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, diff --git a/options.ts b/options.ts index 4c2189d..1429115 100644 --- a/options.ts +++ b/options.ts @@ -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('metric'), + pollingInterval: opt(2000), + leftClick: opt(''), + rightClick: opt(''), + middleClick: opt(''), + scrollUp: opt(''), + scrollDown: opt(''), + }, storage: { label: opt(true), icon: opt('󰋊'), diff --git a/scss/style/customModules/style.scss b/scss/style/customModules/style.scss index c5e11fc..02ff384 100644 --- a/scss/style/customModules/style.scss +++ b/scss/style/customModules/style.scss @@ -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 #