Skip to content

Commit f57a2de

Browse files
systemTemperature@KopfDesDaemons: Initial release (#1308)
1 parent 705f394 commit f57a2de

File tree

11 files changed

+400
-0
lines changed

11 files changed

+400
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# System Temperature Desklet
2+
3+
This desklet displays the temperature of a specific thermal zone on your system. To configure it correctly, you need to specify the path to the thermal zone's temperature file in the desklet settings.
4+
5+
## Finding the Correct Temperature File
6+
7+
1. **Locate Thermal Zones:**
8+
9+
Thermal zone files are usually located under `/sys/class/thermal/`. You can list them with the command:
10+
11+
```bash
12+
ls /sys/class/thermal/
13+
```
14+
15+
2. **Identify the Relevant Thermal Zone:**
16+
17+
Each thermal_zoneX directory represents a thermal zone. Inside, you'll find a file that contains the temperature data (in millidegrees Celsius).
18+
19+
3. **Set the Path in Desklet Settings:**
20+
21+
In the desklet settings, specify the full path to the temperature file you want to monitor, such as:
22+
23+
`/sys/class/thermal/thermal_zone2/temp`
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
const Desklet = imports.ui.desklet;
2+
const Lang = imports.lang;
3+
const St = imports.gi.St;
4+
const Mainloop = imports.mainloop;
5+
const GLib = imports.gi.GLib;
6+
const Settings = imports.ui.settings;
7+
const Gettext = imports.gettext;
8+
9+
const UUID = "systemTemperature@KopfDesDaemons";
10+
11+
Gettext.bindtextdomain(UUID, GLib.get_home_dir() + "/.local/share/locale");
12+
13+
function _(str) {
14+
return Gettext.dgettext(UUID, str);
15+
}
16+
17+
function TemperatureDesklet(metadata, deskletId) {
18+
this._init(metadata, deskletId);
19+
}
20+
21+
TemperatureDesklet.prototype = {
22+
__proto__: Desklet.Desklet.prototype,
23+
24+
_init: function (metadata, deskletId) {
25+
Desklet.Desklet.prototype._init.call(this, metadata, deskletId);
26+
27+
this.setHeader(_("System Temperature"));
28+
29+
// Initialize settings
30+
this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], deskletId);
31+
32+
// Get settings
33+
this.initialLabelText = this.settings.getValue("labelText") || "CPU temperature:";
34+
this.tempFilePath = this.settings.getValue("tempFilePath") || "/sys/class/thermal/thermal_zone2/temp";
35+
this.fontSizeLabel = this.settings.getValue("fontSizeLabel") || 12;
36+
this.fontSizeTemperature = this.settings.getValue("fontSizeTemperature") || 20;
37+
this.dynamicColorEnabled = this.settings.getValue("dynamicColorEnabled") || true;
38+
this.temperatureUnit = this.settings.getValue("temperatureUnit") || "C";
39+
this.updateInterval = this.settings.getValue("updateInterval") || 1;
40+
41+
// Bind settings properties
42+
const boundSettings = [
43+
"tempFilePath",
44+
"labelText",
45+
"temperatureUnit",
46+
"updateInterval",
47+
"fontSizeLabel",
48+
"fontSizeTemperature",
49+
"dynamicColorEnabled"
50+
];
51+
boundSettings.forEach(setting => {
52+
this.settings.bindProperty(Settings.BindingDirection.IN, setting, setting, this.on_settings_changed, null);
53+
});
54+
55+
// Create label for the static text
56+
this.label = new St.Label({ text: this.initialLabelText, y_align: St.Align.START, style_class: "label-text" });
57+
this.label.set_style(`font-size: ${this.fontSizeLabel}px;`);
58+
59+
// Create label for the temperature value
60+
this.temperatureLabel = new St.Label({ text: "Loading...", style_class: "temperature-label" });
61+
this.temperatureLabel.set_style(`font-size: ${this.fontSizeTemperature}px;`);
62+
63+
// Set up the layout
64+
this.box = new St.BoxLayout({ vertical: true });
65+
this.box.add_child(this.label);
66+
this.box.add_child(this.temperatureLabel);
67+
this.setContent(this.box);
68+
69+
this._timeout = null;
70+
71+
// Start the temperature update loop
72+
this.updateTemperature();
73+
},
74+
75+
updateTemperature: function () {
76+
try {
77+
// Get CPU temperature
78+
const [result, out] = GLib.spawn_command_line_sync(`cat ${this.tempFilePath}`);
79+
80+
if (!result || out === null) {
81+
throw new Error("Could not retrieve CPU temperature.");
82+
}
83+
84+
// Convert temperature from millidegree Celsius to degree Celsius
85+
let temperature = parseFloat(out.toString().trim()) / 1000.0;
86+
if (this.temperatureUnit === "F") {
87+
temperature = (temperature * 9 / 5) + 32;
88+
}
89+
90+
// Update temperature text with the chosen unit
91+
const temperatureText = `${temperature.toFixed(1)}°${this.temperatureUnit}`;
92+
this.temperatureLabel.set_text(temperatureText);
93+
94+
// Set color based on temperature if dynamic color is enabled, else set default color
95+
if (this.dynamicColorEnabled) {
96+
this.updateLabelColor(temperature);
97+
} else {
98+
this.temperatureLabel.set_style(`color: #ffffff; font-size: ${this.fontSizeTemperature}px;`);
99+
}
100+
101+
} catch (e) {
102+
this.temperatureLabel.set_text("Error");
103+
global.logError(`Error in updateTemperature: ${e.message}`);
104+
}
105+
106+
// Reset and set up the interval timeout
107+
if (this._timeout) Mainloop.source_remove(this._timeout);
108+
this._timeout = Mainloop.timeout_add_seconds(this.updateInterval, () => this.updateTemperature());
109+
},
110+
111+
updateLabelColor: function (temperature) {
112+
// Define min and max temperature thresholds based on the unit
113+
let minTemp = 20, maxTemp = 90;
114+
115+
// Convert min and max temperature from degree Celsius to degree Fahrenheit
116+
if (this.temperatureUnit === "F") {
117+
minTemp = (minTemp * 9 / 5) + 32;
118+
maxTemp = (maxTemp * 9 / 5) + 32;
119+
}
120+
121+
// Calculate color based on temperature
122+
temperature = Math.min(maxTemp, Math.max(minTemp, temperature));
123+
const ratio = (temperature - minTemp) / (maxTemp - minTemp);
124+
let color = `rgb(${Math.floor(ratio * 255)}, ${Math.floor((1 - ratio) * 255)}, 0)`;
125+
126+
// Set the color
127+
this.temperatureLabel.set_style(`color: ${color}; font-size: ${this.fontSizeTemperature}px;`);
128+
},
129+
130+
on_settings_changed: function () {
131+
// Update the label text and styles when the settings change
132+
if (this.label && this.labelText) {
133+
this.label.set_text(this.labelText);
134+
this.label.set_style(`font-size: ${this.fontSizeLabel}px;`);
135+
}
136+
137+
if (this.temperatureLabel) {
138+
this.temperatureLabel.set_style(`font-size: ${this.fontSizeTemperature}px;`);
139+
}
140+
},
141+
142+
on_desklet_removed: function () {
143+
// Clean up the timeout when the desklet is removed
144+
if (this._timeout) {
145+
Mainloop.source_remove(this._timeout);
146+
this._timeout = null;
147+
}
148+
149+
if (this.label) {
150+
this.box.remove_child(this.label);
151+
this.label = null;
152+
}
153+
154+
if (this.temperatureLabel) {
155+
this.box.remove_child(this.temperatureLabel);
156+
this.temperatureLabel = null;
157+
}
158+
}
159+
};
160+
161+
function main(metadata, deskletId) {
162+
return new TemperatureDesklet(metadata, deskletId);
163+
}
53.7 KB
Loading
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"uuid": "systemTemperature@KopfDesDaemons",
3+
"name": "System Temperature",
4+
"description": "Displays the temperature of a thermal zone in the system.",
5+
"version": "1.0",
6+
"max-instances": "10"
7+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# SYSTEM TEMPERATURE
2+
# This file is put in the public domain.
3+
# KopfDesDaemons, 2024
4+
#
5+
msgid ""
6+
msgstr ""
7+
"Project-Id-Version: systemTemperature@KopfDesDaemons 1.0\n"
8+
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
9+
"issues\n"
10+
"POT-Creation-Date: 2024-11-08 19:56+0100\n"
11+
"PO-Revision-Date: \n"
12+
"Last-Translator: \n"
13+
"Language-Team: \n"
14+
"Language: de\n"
15+
"MIME-Version: 1.0\n"
16+
"Content-Type: text/plain; charset=UTF-8\n"
17+
"Content-Transfer-Encoding: 8bit\n"
18+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
19+
"X-Generator: Poedit 3.0.1\n"
20+
21+
#. metadata.json->name
22+
#. desklet.js:27
23+
msgid "System Temperature"
24+
msgstr "Systemtemperatur"
25+
26+
#. metadata.json->description
27+
msgid "Displays the temperature of a thermal zone in the system."
28+
msgstr "Zeigt die Temperatur einer Thermal-Zone im System an."
29+
30+
#. settings-schema.json->head0->description
31+
msgid "General"
32+
msgstr "Allgemein"
33+
34+
#. settings-schema.json->tempFilePath->description
35+
msgid "Path to file with temperature value (thermal zone)"
36+
msgstr "Pfad zu der Datei mit dem Temperaturwert (Thermal-Zone)"
37+
38+
#. settings-schema.json->labelText->description
39+
msgid "Text for the label"
40+
msgstr "Text für das Label"
41+
42+
#. settings-schema.json->temperatureUnit->options
43+
msgid "°C"
44+
msgstr "°C"
45+
46+
#. settings-schema.json->temperatureUnit->options
47+
msgid "°F"
48+
msgstr "°F"
49+
50+
#. settings-schema.json->temperatureUnit->description
51+
msgid "Temperature unit (Celsius or Fahrenheit)"
52+
msgstr "Temperatureinheit (Celsius oder Fahrenheit)"
53+
54+
#. settings-schema.json->updateInterval->description
55+
msgid "Update interval in seconds"
56+
msgstr "Aktualisierungsintervall in Sekunden"
57+
58+
#. settings-schema.json->head1->description
59+
msgid "Style"
60+
msgstr "Stil"
61+
62+
#. settings-schema.json->fontSizeLabel->description
63+
msgid "Font size for the label text"
64+
msgstr "Schriftgöße für das Label"
65+
66+
#. settings-schema.json->fontSizeTemperature->description
67+
msgid "Font size for the temperature display"
68+
msgstr "Schriftgröße für die Temperaturanzeige"
69+
70+
#. settings-schema.json->dynamicColorEnabled->description
71+
msgid "Dynamic color based on temperature"
72+
msgstr "Dynamische Farbe basierend auf der Temperatur"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# SYSTEM TEMPERATURE
2+
# This file is put in the public domain.
3+
# KopfDesDaemons, 2024
4+
#
5+
#, fuzzy
6+
msgid ""
7+
msgstr ""
8+
"Project-Id-Version: systemTemperature@KopfDesDaemons 1.0\n"
9+
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
10+
"issues\n"
11+
"POT-Creation-Date: 2024-11-08 19:56+0100\n"
12+
"PO-Revision-Date: \n"
13+
"Last-Translator: \n"
14+
"Language-Team: \n"
15+
"Language: \n"
16+
"MIME-Version: 1.0\n"
17+
"Content-Type: text/plain; charset=UTF-8\n"
18+
"Content-Transfer-Encoding: 8bit\n"
19+
20+
#. metadata.json->name
21+
#. desklet.js:27
22+
msgid "System Temperature"
23+
msgstr ""
24+
25+
#. metadata.json->description
26+
msgid "Displays the temperature of a thermal zone in the system."
27+
msgstr ""
28+
29+
#. settings-schema.json->head0->description
30+
msgid "General"
31+
msgstr ""
32+
33+
#. settings-schema.json->tempFilePath->description
34+
msgid "Path to file with temperature value (thermal zone)"
35+
msgstr ""
36+
37+
#. settings-schema.json->labelText->description
38+
msgid "Text for the label"
39+
msgstr ""
40+
41+
#. settings-schema.json->temperatureUnit->options
42+
msgid "°C"
43+
msgstr ""
44+
45+
#. settings-schema.json->temperatureUnit->options
46+
msgid "°F"
47+
msgstr ""
48+
49+
#. settings-schema.json->temperatureUnit->description
50+
msgid "Temperature unit (Celsius or Fahrenheit)"
51+
msgstr ""
52+
53+
#. settings-schema.json->updateInterval->description
54+
msgid "Update interval in seconds"
55+
msgstr ""
56+
57+
#. settings-schema.json->head1->description
58+
msgid "Style"
59+
msgstr ""
60+
61+
#. settings-schema.json->fontSizeLabel->description
62+
msgid "Font size for the label text"
63+
msgstr ""
64+
65+
#. settings-schema.json->fontSizeTemperature->description
66+
msgid "Font size for the temperature display"
67+
msgstr ""
68+
69+
#. settings-schema.json->dynamicColorEnabled->description
70+
msgid "Dynamic color based on temperature"
71+
msgstr ""

0 commit comments

Comments
 (0)