Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemTemperature@KopfDesDaemons: init new desklet #1308

Merged
merged 9 commits into from
Nov 9, 2024
Merged
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
23 changes: 23 additions & 0 deletions systemTemperature@KopfDesDaemons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# System Temperature Desklet

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.

## Finding the Correct Temperature File

1. **Locate Thermal Zones:**

Thermal zone files are usually located under `/sys/class/thermal/`. You can list them with the command:

```bash
ls /sys/class/thermal/
```

2. **Identify the Relevant Thermal Zone:**

Each thermal_zoneX directory represents a thermal zone. Inside, you'll find a file that contains the temperature data (in millidegrees Celsius).

3. **Set the Path in Desklet Settings:**

In the desklet settings, specify the full path to the temperature file you want to monitor, such as:

`/sys/class/thermal/thermal_zone2/temp`
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const Desklet = imports.ui.desklet;
const Lang = imports.lang;
const St = imports.gi.St;
const Mainloop = imports.mainloop;
const GLib = imports.gi.GLib;
const Settings = imports.ui.settings;
const Gettext = imports.gettext;

const UUID = "systemTemperature@KopfDesDaemons";

Gettext.bindtextdomain(UUID, GLib.get_home_dir() + "/.local/share/locale");

function _(str) {
return Gettext.dgettext(UUID, str);
}

function TemperatureDesklet(metadata, deskletId) {
this._init(metadata, deskletId);
}

TemperatureDesklet.prototype = {
__proto__: Desklet.Desklet.prototype,

_init: function (metadata, deskletId) {
Desklet.Desklet.prototype._init.call(this, metadata, deskletId);

this.setHeader(_("System Temperature"));

// Initialize settings
this.settings = new Settings.DeskletSettings(this, this.metadata["uuid"], deskletId);

// Get settings
this.initialLabelText = this.settings.getValue("labelText") || "CPU temperature:";
this.tempFilePath = this.settings.getValue("tempFilePath") || "/sys/class/thermal/thermal_zone2/temp";
this.fontSizeLabel = this.settings.getValue("fontSizeLabel") || 12;
this.fontSizeTemperature = this.settings.getValue("fontSizeTemperature") || 20;
this.dynamicColorEnabled = this.settings.getValue("dynamicColorEnabled") || true;
this.temperatureUnit = this.settings.getValue("temperatureUnit") || "C";
this.updateInterval = this.settings.getValue("updateInterval") || 1;

// Bind settings properties
const boundSettings = [
"tempFilePath",
"labelText",
"temperatureUnit",
"updateInterval",
"fontSizeLabel",
"fontSizeTemperature",
"dynamicColorEnabled"
];
boundSettings.forEach(setting => {
this.settings.bindProperty(Settings.BindingDirection.IN, setting, setting, this.on_settings_changed, null);
});

// Create label for the static text
this.label = new St.Label({ text: this.initialLabelText, y_align: St.Align.START, style_class: "label-text" });
this.label.set_style(`font-size: ${this.fontSizeLabel}px;`);

// Create label for the temperature value
this.temperatureLabel = new St.Label({ text: "Loading...", style_class: "temperature-label" });
this.temperatureLabel.set_style(`font-size: ${this.fontSizeTemperature}px;`);

// Set up the layout
this.box = new St.BoxLayout({ vertical: true });
this.box.add_child(this.label);
this.box.add_child(this.temperatureLabel);
this.setContent(this.box);

this._timeout = null;

// Start the temperature update loop
this.updateTemperature();
},

updateTemperature: function () {
try {
// Get CPU temperature
const [result, out] = GLib.spawn_command_line_sync(`cat ${this.tempFilePath}`);

if (!result || out === null) {
throw new Error("Could not retrieve CPU temperature.");
}

// Convert temperature from millidegree Celsius to degree Celsius
let temperature = parseFloat(out.toString().trim()) / 1000.0;
if (this.temperatureUnit === "F") {
temperature = (temperature * 9 / 5) + 32;
}

// Update temperature text with the chosen unit
const temperatureText = `${temperature.toFixed(1)}°${this.temperatureUnit}`;
this.temperatureLabel.set_text(temperatureText);

// Set color based on temperature if dynamic color is enabled, else set default color
if (this.dynamicColorEnabled) {
this.updateLabelColor(temperature);
} else {
this.temperatureLabel.set_style(`color: #ffffff; font-size: ${this.fontSizeTemperature}px;`);
}

} catch (e) {
this.temperatureLabel.set_text("Error");
global.logError(`Error in updateTemperature: ${e.message}`);
}

// Reset and set up the interval timeout
if (this._timeout) Mainloop.source_remove(this._timeout);
this._timeout = Mainloop.timeout_add_seconds(this.updateInterval, () => this.updateTemperature());
},

updateLabelColor: function (temperature) {
// Define min and max temperature thresholds based on the unit
let minTemp = 20, maxTemp = 90;

// Convert min and max temperature from degree Celsius to degree Fahrenheit
if (this.temperatureUnit === "F") {
minTemp = (minTemp * 9 / 5) + 32;
maxTemp = (maxTemp * 9 / 5) + 32;
}

// Calculate color based on temperature
temperature = Math.min(maxTemp, Math.max(minTemp, temperature));
const ratio = (temperature - minTemp) / (maxTemp - minTemp);
let color = `rgb(${Math.floor(ratio * 255)}, ${Math.floor((1 - ratio) * 255)}, 0)`;

// Set the color
this.temperatureLabel.set_style(`color: ${color}; font-size: ${this.fontSizeTemperature}px;`);
},

on_settings_changed: function () {
// Update the label text and styles when the settings change
if (this.label && this.labelText) {
this.label.set_text(this.labelText);
this.label.set_style(`font-size: ${this.fontSizeLabel}px;`);
}

if (this.temperatureLabel) {
this.temperatureLabel.set_style(`font-size: ${this.fontSizeTemperature}px;`);
}
},

on_desklet_removed: function () {
// Clean up the timeout when the desklet is removed
if (this._timeout) {
Mainloop.source_remove(this._timeout);
this._timeout = null;
}

if (this.label) {
this.box.remove_child(this.label);
this.label = null;
}

if (this.temperatureLabel) {
this.box.remove_child(this.temperatureLabel);
this.temperatureLabel = null;
}
}
};

function main(metadata, deskletId) {
return new TemperatureDesklet(metadata, deskletId);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"uuid": "systemTemperature@KopfDesDaemons",
"name": "System Temperature",
"description": "Displays the temperature of a thermal zone in the system.",
"version": "1.0",
"max-instances": "10"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# SYSTEM TEMPERATURE
# This file is put in the public domain.
# KopfDesDaemons, 2024
#
msgid ""
msgstr ""
"Project-Id-Version: systemTemperature@KopfDesDaemons 1.0\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-11-08 19:56+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.0.1\n"

#. metadata.json->name
#. desklet.js:27
msgid "System Temperature"
msgstr "Systemtemperatur"

#. metadata.json->description
msgid "Displays the temperature of a thermal zone in the system."
msgstr "Zeigt die Temperatur einer Thermal-Zone im System an."

#. settings-schema.json->head0->description
msgid "General"
msgstr "Allgemein"

#. settings-schema.json->tempFilePath->description
msgid "Path to file with temperature value (thermal zone)"
msgstr "Pfad zu der Datei mit dem Temperaturwert (Thermal-Zone)"

#. settings-schema.json->labelText->description
msgid "Text for the label"
msgstr "Text für das Label"

#. settings-schema.json->temperatureUnit->options
msgid "°C"
msgstr "°C"

#. settings-schema.json->temperatureUnit->options
msgid "°F"
msgstr "°F"

#. settings-schema.json->temperatureUnit->description
msgid "Temperature unit (Celsius or Fahrenheit)"
msgstr "Temperatureinheit (Celsius oder Fahrenheit)"

#. settings-schema.json->updateInterval->description
msgid "Update interval in seconds"
msgstr "Aktualisierungsintervall in Sekunden"

#. settings-schema.json->head1->description
msgid "Style"
msgstr "Stil"

#. settings-schema.json->fontSizeLabel->description
msgid "Font size for the label text"
msgstr "Schriftgöße für das Label"

#. settings-schema.json->fontSizeTemperature->description
msgid "Font size for the temperature display"
msgstr "Schriftgröße für die Temperaturanzeige"

#. settings-schema.json->dynamicColorEnabled->description
msgid "Dynamic color based on temperature"
msgstr "Dynamische Farbe basierend auf der Temperatur"
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# SYSTEM TEMPERATURE
# This file is put in the public domain.
# KopfDesDaemons, 2024
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: systemTemperature@KopfDesDaemons 1.0\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-11-08 19:56+0100\n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#. metadata.json->name
#. desklet.js:27
msgid "System Temperature"
msgstr ""

#. metadata.json->description
msgid "Displays the temperature of a thermal zone in the system."
msgstr ""

#. settings-schema.json->head0->description
msgid "General"
msgstr ""

#. settings-schema.json->tempFilePath->description
msgid "Path to file with temperature value (thermal zone)"
msgstr ""

#. settings-schema.json->labelText->description
msgid "Text for the label"
msgstr ""

#. settings-schema.json->temperatureUnit->options
msgid "°C"
msgstr ""

#. settings-schema.json->temperatureUnit->options
msgid "°F"
msgstr ""

#. settings-schema.json->temperatureUnit->description
msgid "Temperature unit (Celsius or Fahrenheit)"
msgstr ""

#. settings-schema.json->updateInterval->description
msgid "Update interval in seconds"
msgstr ""

#. settings-schema.json->head1->description
msgid "Style"
msgstr ""

#. settings-schema.json->fontSizeLabel->description
msgid "Font size for the label text"
msgstr ""

#. settings-schema.json->fontSizeTemperature->description
msgid "Font size for the temperature display"
msgstr ""

#. settings-schema.json->dynamicColorEnabled->description
msgid "Dynamic color based on temperature"
msgstr ""
Loading