5.6 KiB
applets
to create applet for linux mint cinnamon DE
Applet for Linux Cinnamon to Display Download/Upload Speed
Here's a simple applet code for Linux Cinnamon that displays your current network download and upload speeds. This creates a panel applet similar to what you might see in Windows task manager or other system monitors.
Prerequisites
- Linux Cinnamon desktop environment
- Python 3.x
gir1.2-gtk-3.0andgir1.2-appindicator3-0.1packages (usually installed by default)
Applet Code
Create a new directory for your applet and save these files:
1. network-speed@yourname/files/network-speed@yourname/applet.js
const Applet = imports.ui.applet;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
const St = imports.gi.St;
const Mainloop = imports.mainloop;
const Gettext = imports.gettext;
function MyApplet(metadata, orientation, panel_height, instance_id) {
this._init(metadata, orientation, panel_height, instance_id);
}
MyApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype,
_init: function(metadata, orientation, panel_height, instance_id) {
Applet.TextIconApplet.prototype._init.call(this, orientation, panel_height, instance_id);
try {
this.metadata = metadata;
this.uuid = this.metadata.uuid;
this.instance_id = instance_id;
this.setAllowedLayout(Applet.AllowedLayout.BOTH);
// Initial values
this.downSpeed = 0;
this.upSpeed = 0;
this.prevDownBytes = 0;
this.prevUpBytes = 0;
// Get initial network stats
this.getNetworkStats();
// Update every second
this.timeout = Mainloop.timeout_add_seconds(1, () => {
this.getNetworkStats();
return true;
});
this.on_orientation_changed(orientation);
} catch (e) {
global.logError(e);
}
},
getNetworkStats: function() {
try {
let [res, out, err, status] = GLib.spawn_command_line_sync('cat /proc/net/dev');
if (status === 0) {
let lines = out.toString().split('\n');
let downBytes = 0;
let upBytes = 0;
// Skip first 2 lines (header)
for (let i = 2; i < lines.length; i++) {
let line = lines[i].trim();
if (line === '') continue;
let iface = line.split(':')[0].trim();
// Skip lo (loopback) interface
if (iface === 'lo') continue;
let stats = line.split(':')[1].trim().split(/\s+/);
downBytes += parseInt(stats[0]);
upBytes += parseInt(stats[8]);
}
// Calculate speeds (bytes per second)
if (this.prevDownBytes > 0 && this.prevUpBytes > 0) {
this.downSpeed = downBytes - this.prevDownBytes;
this.upSpeed = upBytes - this.prevUpBytes;
}
this.prevDownBytes = downBytes;
this.prevUpBytes = upBytes;
this.updateUI();
}
} catch (e) {
global.logError(e);
}
},
updateUI: function() {
try {
let down = this.formatSpeed(this.downSpeed);
let up = this.formatSpeed(this.upSpeed);
this.set_applet_label(`↓${down} ↑${up}`);
} catch (e) {
global.logError(e);
}
},
formatSpeed: function(bytes) {
if (bytes < 1024) {
return bytes + 'B';
} else if (bytes < 1024 * 1024) {
return Math.round(bytes / 1024) + 'K';
} else {
return Math.round(bytes / (1024 * 1024)) + 'M';
}
},
on_orientation_changed: function(orientation) {
this.orientation = orientation;
},
on_applet_removed_from_panel: function() {
if (this.timeout) {
Mainloop.source_remove(this.timeout);
}
}
};
function main(metadata, orientation, panel_height, instance_id) {
return new MyApplet(metadata, orientation, panel_height, instance_id);
}
2. network-speed@yourname/files/network-speed@yourname/metadata.json
{
"uuid": "network-speed@yourname",
"name": "Network Speed Monitor",
"description": "Displays current download and upload speeds",
"icon": "network-wired",
"max-instances": 1
}
3. network-speed@yourname/files/network-speed@yourname/stylesheet.css
.network-speed-applet {
padding: 2px;
}
Installation
-
Create the directory structure:
mkdir -p ~/.local/share/cinnamon/applets/network-speed@yourname -
Copy the three files above to this directory.
-
Restart Cinnamon (press Alt+F2, type "r" and press Enter).
-
Right-click on the panel, choose "Applets", find "Network Speed Monitor" and add it to your panel.
Customization Options
- You can change the update interval by modifying the timeout value in
applet.js. - To change the display format, modify the
formatSpeedfunction. - You can add tooltips or more detailed information by expanding the code.
This applet reads from /proc/net/dev to get network statistics, which is the most reliable method on Linux. It shows download (↓) and upload (↑) speeds in a compact format (B for bytes, K for kilobytes, M for megabytes per second).