Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions helpers/catalog.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions helpers/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';

Expand All @@ -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);
}
Expand Down
14 changes: 12 additions & 2 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions values.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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),
Expand All @@ -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) {
Expand Down