180 lines
No EOL
5.6 KiB
Markdown
180 lines
No EOL
5.6 KiB
Markdown
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
|
|
1. Linux Cinnamon desktop environment
|
|
2. Python 3.x
|
|
3. `gir1.2-gtk-3.0` and `gir1.2-appindicator3-0.1` packages (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`
|
|
|
|
```javascript
|
|
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`
|
|
|
|
```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`
|
|
|
|
```css
|
|
.network-speed-applet {
|
|
padding: 2px;
|
|
}
|
|
```
|
|
|
|
## Installation
|
|
|
|
1. Create the directory structure:
|
|
```bash
|
|
mkdir -p ~/.local/share/cinnamon/applets/network-speed@yourname
|
|
```
|
|
|
|
2. Copy the three files above to this directory.
|
|
|
|
3. Restart Cinnamon (press Alt+F2, type "r" and press Enter).
|
|
|
|
4. Right-click on the panel, choose "Applets", find "Network Speed Monitor" and add it to your panel.
|
|
|
|
## Customization Options
|
|
|
|
1. You can change the update interval by modifying the timeout value in `applet.js`.
|
|
2. To change the display format, modify the `formatSpeed` function.
|
|
3. 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). |