|
| 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 | +} |
0 commit comments