diff --git a/helpers/catalog.js b/helpers/catalog.js index af558a5f..c333a50b 100644 --- a/helpers/catalog.js +++ b/helpers/catalog.js @@ -1,10 +1,13 @@ /* Shared sensor catalog for shell and preferences. */ +// colorFormats take both "All sensors" and per-sensor scales. +// sensorColorFormats take per-sensor scales only: an "All sensors" usage +// scale (0–100 %) must not color a frequency (GHz). export const sensorCatalog = { 'temperature' : { 'icon': 'temperature-symbolic.svg', colorFormats: ['temp'] }, 'voltage' : { 'icon': 'voltage-symbolic.svg' }, 'fan' : { 'icon': 'fan-symbolic.svg', colorFormats: ['fan'] }, 'memory' : { 'icon': 'memory-symbolic.svg', 'icon-swap': 'swap-symbolic.svg', colorFormats: ['percent'] }, - 'processor' : { 'icon': 'cpu-symbolic.svg', colorFormats: ['percent'] }, + 'processor' : { 'icon': 'cpu-symbolic.svg', colorFormats: ['percent'], sensorColorFormats: ['hertz'] }, 'system' : { 'icon': 'system-symbolic.svg', colorFormats: ['load'] }, 'network' : { 'icon': 'network-symbolic.svg', 'icon-rx': 'network-download-symbolic.svg', @@ -36,12 +39,26 @@ export function colorsKeyForSensor(type, format) { const group = sensorGroupFromType(type); const formats = sensorCatalog[group]?.colorFormats; - if (formats && formats.includes(format)) + const sensorFormats = sensorCatalog[group]?.sensorColorFormats; + if (formats?.includes(format) || sensorFormats?.includes(format)) return `${group}-colors`; return null; } +// Whether "All sensors" scales apply to this sensor, or only its own scale. +export function groupColorsApply(type, format) { + if (format === 'temp') + return true; + + return sensorCatalog[sensorGroupFromType(type)]?.colorFormats?.includes(format) ?? false; +} + +// Unit of the thresholds when it is not the unit shown in the panel. +export function colorThresholdUnit(format) { + return format === 'hertz' ? 'GHz' : null; +} + export function colorPageForSensor(type, format) { let key = colorsKeyForSensor(type, format); return key ? key.slice(0, -'-colors'.length) : null; diff --git a/helpers/colors.js b/helpers/colors.js index 9ceffae8..1469482f 100644 --- a/helpers/colors.js +++ b/helpers/colors.js @@ -94,9 +94,9 @@ function styleForEntries(value, entries) { return ''; } -// Prefer rules for sensorKey; otherwise group-wide rules (sensorKey == null). -// Unknown / other sensor keys are ignored — never throws. -export function getUsageColor(value, colors, sensorKey = null) { +// Prefer rules for sensorKey; otherwise group-wide rules (sensorKey == null), +// unless useGroupWide is false. Unknown / other sensor keys are ignored — never throws. +export function getUsageColor(value, colors, sensorKey = null, useGroupWide = true) { if (!colors || colors.length === 0 || !Number.isFinite(value)) return ''; @@ -110,6 +110,9 @@ export function getUsageColor(value, colors, sensorKey = null) { return styleForEntries(value, specific); } + if (!useGroupWide) + return ''; + const groupWide = entries.filter(entry => !entry.sensorKey); return styleForEntries(value, groupWide); } diff --git a/prefs.js b/prefs.js index 0cbf6759..11c97fe0 100644 --- a/prefs.js +++ b/prefs.js @@ -6,7 +6,7 @@ import Gdk from 'gi://Gdk'; import Gtk from 'gi://Gtk'; import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js'; -import {sensorCatalog, colorPageForSensor} from './helpers/catalog.js'; +import {sensorCatalog, colorPageForSensor, colorThresholdUnit} from './helpers/catalog.js'; import { compareColorEntries, DEFAULT_THRESHOLD_RGBA, @@ -46,6 +46,8 @@ class Settings { this._removedHotSensors = {}; // pageName -> sorted list of discovered sensor keys (from Sensors.query). this._discoveredSensorsByPage = {}; + // sensor key -> value format (from Sensors.query), for threshold units. + this._discoveredSensorFormats = {}; this._sensors = null; this._sensorDiscoveryTimeoutId = 0; this._bind_sensor_page_gates(); @@ -118,14 +120,19 @@ class Settings { this._discoveredSensorsByPage[pageName] = []; if (!this._discoveredSensorsByPage[pageName].includes(key)) this._discoveredSensorsByPage[pageName].push(key); + this._discoveredSensorFormats[key] = format; } _refresh_add_colors_dropdowns() { for (let pageName of Object.keys(this._discoveredSensorsByPage)) this._discoveredSensorsByPage[pageName].sort(); - for (let pageName of Object.keys(this._thresholdColorPages)) + // Palettes built before discovery ended have titles without units or availability. + for (let [pageName, state] of Object.entries(this._thresholdColorPages)) { + for (let palette of state.palettes) + palette.group.set_title(this._palette_title(palette.sensorKey, pageName)); this._sync_add_colors_row(pageName); + } } _apply_icon_style() { @@ -593,6 +600,9 @@ class Settings { return _('All sensors colors'); let label = labelFromSensorKey(sensorKey); + let unit = colorThresholdUnit(this._discoveredSensorFormats[sensorKey]); + if (unit) + label = `${label} (${unit})`; if (this._sensor_key_is_live(pageName, sensorKey)) return _('%s colors').format(label); return _('%s colors (unavailable)').format(label); diff --git a/values.js b/values.js index c6401080..e870012e 100644 --- a/values.js +++ b/values.js @@ -26,7 +26,7 @@ import GObject from 'gi://GObject'; -import {sensorCatalog, colorsKeyForSensor} from './helpers/catalog.js'; +import {sensorCatalog, colorsKeyForSensor, groupColorsApply} from './helpers/catalog.js'; import {getUsageColor} from './helpers/colors.js'; const cbFun = (d, c) => { @@ -68,6 +68,8 @@ export const Values = GObject.registerClass({ let format = ''; let ending = ''; let exp = 0; + // Number compared to the color thresholds; null means the displayed number. + let colorValue = null; switch (sensorClass) { case 'percent': @@ -98,6 +100,8 @@ export const Values = GObject.registerClass({ ending = 'V'; break; case 'hertz': + // Thresholds are in GHz: the displayed unit changes with the value (400 MHz, 4.5 GHz). + colorValue = value / 1e9; if (value > 0) { exp = Math.max(0, Math.floor(Math.log(value) / Math.log(unit))); if (value >= Math.pow(unit, exp) * (unit - 0.05)) exp++; @@ -230,7 +234,7 @@ export const Values = GObject.registerClass({ break; } - let numeric = (typeof value === 'number' && Number.isFinite(value)) ? value : null; + let numeric = colorValue ?? ((typeof value === 'number' && Number.isFinite(value)) ? value : null); return { text: format.format(value, ending).trim(), style: this._styleFor(numeric, type, sensorClass, sensorKey), @@ -242,7 +246,8 @@ export const Values = GObject.registerClass({ if (!colorsKey || numeric === null || !Number.isFinite(numeric)) return ''; - return getUsageColor(numeric, this._settings.get_strv(colorsKey), sensorKey); + return getUsageColor(numeric, this._settings.get_strv(colorsKey), sensorKey, + groupColorsApply(type, format)); } returnIfDifferent(dwell, label, value, type, format, key) {