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

systemUptime@KopfDesDaemons: Initial release #1334

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

Displays the current system uptime and system start time.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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 Clutter = imports.gi.Clutter;
const GdkPixbuf = imports.gi.GdkPixbuf;
const Cogl = imports.gi.Cogl;

const UUID = "systemUptime@KopfDesDaemons";

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

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

class MyDesklet extends Desklet.Desklet {
constructor(metadata, deskletId) {
super(metadata, deskletId);

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

// Bind settings properties
this.settings.bindProperty(Settings.BindingDirection.IN, "fontSize", "fontSize", this.onSettingsChanged.bind(this));
this.settings.bindProperty(Settings.BindingDirection.IN, "colorLabel", "colorLabel", this.onSettingsChanged.bind(this));

this.fontSize = this.settings.getValue("fontSize") || 20;
this.colorLabel = this.settings.getValue("colorLabel") || "rgb(51, 209, 122)";
this._timeout = null;

this.setHeader(_("System Uptime"));
this.setupLayout();
this.getStartupTime();
this.updateUptime();
}

setupLayout() {
// Create labels for uptime
this.uptimeLabel = this.createLabel(_("Uptime:") + " ", this.colorLabel);
this.uptimeValue = this.createLabel(_("Loading..."));

const uptimeRow = this.createRow([this.uptimeLabel, this.uptimeValue]);

// Create labels for startup time
this.startTimeLabel = this.createLabel(_("System start time:") + " ", this.colorLabel);
this.startupValue = this.createLabel(_("Loading..."));

const startupRow = this.createRow([this.startTimeLabel, this.startupValue]);

// Combine all into the main container
const contentBox = new St.BoxLayout({ vertical: true });
contentBox.set_style("margin-left: 0.5em;");
contentBox.add_child(startupRow);
contentBox.add_child(uptimeRow);

const clockIcon = this.getImageAtScale(`${this.metadata.path}/clock.svg`, (this.fontSize * 2), (this.fontSize * 2),);

this.container = new St.BoxLayout();
this.container.add_child(clockIcon);
this.container.add_child(contentBox);

this.setContent(this.container);
}

createLabel(text, color = "inherit") {
return new St.Label({
text,
y_align: St.Align.START,
style: `font-size: ${this.fontSize}px; color: ${color};`
});
}

createRow(children) {
const row = new St.BoxLayout();
children.forEach(child => row.add_child(child));
return row;
}

updateUptime() {
try {
const [result, out] = GLib.spawn_command_line_sync("awk '{print $1}' /proc/uptime");
if (!result || !out) throw new Error("Could not get system uptime.");

const uptimeInSeconds = parseFloat(out.toString().trim());
const hours = Math.floor(uptimeInSeconds / 3600);
const minutes = Math.floor((uptimeInSeconds % 3600) / 60);

this.uptimeValue.set_text(`${hours} ${_("hours")} ${minutes} ${_("minutes")}`);
} catch (error) {
this.uptimeValue.set_text("Error");
global.logError(`${UUID}: ${error.message}`);
}

if (this._timeout) Mainloop.source_remove(this._timeout);
this._timeout = Mainloop.timeout_add_seconds(60, () => this.updateUptime());
}

getStartupTime() {
try {
const [result, out] = GLib.spawn_command_line_sync("uptime -s");
if (!result || !out) throw new Error("Could not get system startup time.");

this.startupValue.set_text(out.toString().split(" ")[1].trim());
} catch (error) {
this.startupValue.set_text("Error");
global.logError(`${UUID}: ${error.message}`);
}
}

onSettingsChanged() {
this.setupLayout();
this.updateUptime();
this.getStartupTime();
}

on_desklet_removed() {
if (this._timeout) Mainloop.source_remove(this._timeout);
}

getImageAtScale(imageFileName, width, height) {
const pixBuf = GdkPixbuf.Pixbuf.new_from_file_at_size(imageFileName, width, height);
const image = new Clutter.Image();
image.set_data(
pixBuf.get_pixels(),
pixBuf.get_has_alpha() ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGBA_888,
width, height,
pixBuf.get_rowstride()
);

const actor = new Clutter.Actor({ width, height });
actor.set_content(image);
return actor;
}
}

function main(metadata, deskletId) {
return new MyDesklet(metadata, deskletId);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"uuid": "systemUptime@KopfDesDaemons",
"name": "System Uptime",
"description": "Displays the current system uptime.",
"version": "1.0",
"max-instances": "50"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# SYSTEM UPTIME
# This file is put in the public domain.
# KopfDesDaemons, 2024
#
msgid ""
msgstr ""
"Project-Id-Version: systemUptime@KopfDesDaemons 1.0\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-11-26 19:00+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:34
msgid "System Uptime"
msgstr "Systembetriebszeit"

#. desklet.js:42
msgid "Uptime:"
msgstr "Betriebszeit:"

#. desklet.js:43 desklet.js:49
msgid "Loading..."
msgstr "Lade..."

#. desklet.js:48
msgid "System start time:"
msgstr "Systemstartzeitpunkt:"

#. desklet.js:91
msgid "hours"
msgstr "Stunden"

#. desklet.js:91
msgid "minutes"
msgstr "Minuten"

#. metadata.json->description
msgid "Displays the current system uptime."
msgstr "Zeigt die aktuelle Systembetriebszeit."

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

#. settings-schema.json->fontSize->description
msgid "Font size"
msgstr "Schriftgröße"

#. settings-schema.json->colorLabel->description
msgid "Label color"
msgstr "Labelfarbe"
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# SYSTEM UPTIME
# This file is put in the public domain.
# KopfDesDaemons, 2024
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: systemUptime@KopfDesDaemons 1.0\n"
"Report-Msgid-Bugs-To: https://github.com/linuxmint/cinnamon-spices-desklets/"
"issues\n"
"POT-Creation-Date: 2024-11-26 19:00+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:34
msgid "System Uptime"
msgstr ""

#. desklet.js:42
msgid "Uptime:"
msgstr ""

#. desklet.js:43 desklet.js:49
msgid "Loading..."
msgstr ""

#. desklet.js:48
msgid "System start time:"
msgstr ""

#. desklet.js:91
msgid "hours"
msgstr ""

#. desklet.js:91
msgid "minutes"
msgstr ""

#. metadata.json->description
msgid "Displays the current system uptime."
msgstr ""

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

#. settings-schema.json->fontSize->description
msgid "Font size"
msgstr ""

#. settings-schema.json->colorLabel->description
msgid "Label color"
msgstr ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"head0": {
"type": "header",
"description": "Style"
},
"fontSize": {
"type": "scale",
"default": 20,
"min": 12,
"max": 60,
"step": 1,
"description": "Font size"
},
"colorLabel": {
"type": "colorchooser",
"default": "rgb(51, 209, 122)",
"description": "Label color"
}
}
3 changes: 3 additions & 0 deletions systemUptime@KopfDesDaemons/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"author": "KopfDesDaemons"
}
Binary file added systemUptime@KopfDesDaemons/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.