diff --git a/extension.js b/extension.js index 67eb63b..094e1c2 100644 --- a/extension.js +++ b/extension.js @@ -1,222 +1,210 @@ -const Clutter = imports.gi.Clutter; -const St = imports.gi.St; -const Main = imports.ui.main; -// const Tweener = imports.ui.tweener; -const Gio = imports.gi.Gio; -const GLib = imports.gi.GLib; -const ByteArray = imports.byteArray; - -const ExtensionUtils = imports.misc.extensionUtils; -const Me = ExtensionUtils.getCurrentExtension(); - -const PREFS_SCHEMA = 'org.gnome.shell.extensions.simplenetspeed'; -const refreshTime = 3; - -let settings; -let button, timeout; -// let icon, iconDark; -let ioSpeed; +// @ts-ignore +import Clutter from 'gi://Clutter' +// @ts-ignore +import St from 'gi://St'; +// @ts-ignore +import Gio from 'gi://Gio' +// @ts-ignore +import GLib from 'gi://GLib' + +// @ts-ignore +import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js'; +// @ts-ignore +import * as Main from 'resource:///org/gnome/shell/ui/main.js'; + +const REFRESH_TIME = 3; + let lastCount = 0, lastSpeed = 0, lastCountUp = 0; -let mode; // 0: kbps 1: K/s 2: U:kbps D:kbps 3: U:K/s D:K/s 4: Total KB -let fontmode; let resetNextCount = false, resetCount = 0; -function byteArrayToString(bytes) { - if (global.TextDecoder) { - return new TextDecoder().decode(bytes); +export default class SimpleNetSpeedExtension extends Extension{ + enable() { + const PREFS_SCHEMA = 'org.gnome.shell.extensions.simplenetspeed'; + // @ts-ignore + this._settings = this.getSettings(PREFS_SCHEMA); + + // 0: kbps 1: K/s 2: U:kbps D:kbps 3: U:K/s D:K/s 4: Total KB + // default mode using bit (bps, kbps) + this.mode = this._settings.get_int('mode'); + this.fontmode = this._settings.get_int('fontmode'); + console.log(`SimpleNetSpeed.enable: mode ${this.mode}, fontmode ${this.fontmode}`); + + this.button = new St.Bin({ + style_class: 'panel-button', + reactive: true, + can_focus: true, + x_expand: true, + y_expand: false, + track_hover: true + }); + + // let icon, iconDark; + this.ioSpeed = new St.Label({ + text: '---', + y_align: Clutter.ActorAlign.CENTER, + style_class: 'simplenetspeed-label' + }); + + this.button.set_child(this.chooseLabel()); + this.button.connect('button-press-event', this.changeMode.bind(this)); + + Main.panel._rightBox.insert_child_at_index(this.button, 0); + this.timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, REFRESH_TIME, () => { + return this.parseStat(); + }); } - return imports.byteArray.toString(bytes); -} - -function init() { - -} - -function changeMode(widget, event) { - // log(event.get_button()); - if (event.get_button() == 3 && mode == 4) { // right click: reset downloaded sum - resetNextCount = true; - parseStat(); - } - else if (event.get_button() == 2) { // change font - fontmode++; - if (fontmode > 4) { - fontmode=0; + disable() { + if (this.timeout) { + GLib.source_remove(this.timeout); + this.timeout = null; } - settings.set_int('fontmode', fontmode); - chooseLabel(); - parseStat(); + Main.panel._rightBox.remove_child(this.button); + this.button.destroy(); + this.ioSpeed = null; + this.button = null; + this._settings = null; } - else if (event.get_button() == 1) { - mode++; - if (mode > 4) { - mode = 0; + + changeMode(_widget, event) { + console.log('SimpleNetSpeed.changeMode, event button:' + event.get_button()); + if (event.get_button() === 3 && this.mode === 4) { // right click: reset downloaded sum + resetNextCount = true; + this.parseStat(); + } + else if (event.get_button() === 2) { // change font + this.fontmode++; + if (this.fontmode > 4) { + this.fontmode = 0; + } + this._settings.set_int('fontmode', this.fontmode); + this.chooseLabel(); + this.parseStat(); + } + else if (event.get_button() === 1) { + this.mode++; + if (this.mode > 4) { + this.mode = 0; + } + this._settings.set_int('mode', this.mode); + this.chooseLabel(); + this.parseStat(); } - settings.set_int('mode', mode); - chooseLabel(); - parseStat(); + console.log('SimpleNetSpeed.changeMode mode: ' + this.mode + ' font: ' + this.fontmode); } - log('mode:' + mode + ' font:' + fontmode); -} -function chooseLabel() { - if (mode == 0 || mode == 1 || mode == 4) { - styleName = 'simplenetspeed-label'; - } - else { // 2 , 3 - styleName = 'simplenetspeed-label-w'; - } + chooseLabel() { + let styleName; + if (this.mode === 0 || this.mode === 1 || this.mode === 4) { + styleName = 'simplenetspeed-label'; + } + else { // 2 , 3 + styleName = 'simplenetspeed-label-w'; + } - if (fontmode > 0) { - styleName = styleName + '-' + fontmode; - } + if (this.fontmode > 0) { + styleName = styleName + '-' + this.fontmode; + } - ioSpeed.set_style_class_name(styleName); - return ioSpeed; -} + this.ioSpeed.set_style_class_name(styleName); + return this.ioSpeed; + } -function parseStat() { - try { - let input_file = Gio.file_new_for_path('/proc/net/dev'); - - let [, contents, etag] = input_file.load_contents(null); - contents = byteArrayToString(contents); - let lines = contents.split('\n'); - - let count = 0; - let countUp = 0; - let line; - - for (let i=0;i lastSpeed) { - dot = "⇅"; - } + const dot = speed > lastSpeed ? "⇅" : ""; - if (mode >= 0 && mode <= 1) { - ioSpeed.set_text(dot + speedToString(speed)); - } - else if (mode >= 2 && mode <= 3) { - ioSpeed.set_text("↓" + speedToString(speed - speedUp) + " ↑" + speedToString(speedUp)); - } - else if (mode == 4) { - if (resetNextCount == true) { - resetNextCount = false; - resetCount = count; + if (this.mode >= 0 && this.mode <= 1) { + this.ioSpeed.set_text(dot + this.speedToString(speed)); } - ioSpeed.set_text("∑ " + speedToString(count - resetCount)); + else if (this.mode >= 2 && this.mode <= 3) { + this.ioSpeed.set_text("↓" + this.speedToString(speed - speedUp) + " ↑" + this.speedToString(speedUp)); + } + else if (this.mode === 4) { + if (resetNextCount === true) { + resetNextCount = false; + resetCount = count; + } + this.ioSpeed.set_text("∑ " + this.speedToString(count - resetCount)); + } + + lastCount = count; + lastCountUp = countUp; + lastSpeed = speed; + } catch (e) { + this.ioSpeed.set_text(e.message); } - lastCount = count; - lastCountUp = countUp; - lastSpeed = speed; - } catch (e) { - ioSpeed.set_text(e.message); + return GLib.SOURCE_CONTINUE; } - return GLib.SOURCE_CONTINUE; -} - -function speedToString(amount) { - let digits; - let speed_map; - if (mode == 0 || mode == 2) { - speed_map = ["bps", "Kbps", "Mbps", "Gbps"]; - } - else if (mode == 1 || mode == 3) { - speed_map = ["B/s", "K/s", "M/s", "G/s"]; - } - else if (mode == 4) { - speed_map = ["B", "KB", "MB", "GB"]; - } + speedToString(amount) { + let digits; + let speed_map = []; + if (this.mode === 0 || this.mode === 2) { + speed_map = ["bps", "Kbps", "Mbps", "Gbps"]; + } + else if (this.mode === 1 || this.mode === 3) { + speed_map = ["B/s", "K/s", "M/s", "G/s"]; + } + else if (this.mode === 4) { + speed_map = ["B", "KB", "MB", "GB"]; + } - if (amount === 0) - return "0" + speed_map[0]; + if (amount === 0) { + return "0" + speed_map[0]; + } - if (mode==0 || mode==2) amount = amount * 8; + if (this.mode === 0 || this.mode === 2) amount = amount * 8; - let unit = 0; - while (amount >= 1000) { // 1M=1024K, 1MB/s=1000MB/s - amount /= 1000; - ++unit; - } + let unit = 0; + while (amount >= 1000) { // 1M=1024K, 1MB/s=1000MB/s + amount /= 1000; + ++unit; + } - if (amount >= 100) // 100MB 100KB 200KB + if (amount >= 100) // 100MB 100KB 200KB digits = 0; - else if (amount >= 10) // 10MB 10.2 + else if (amount >= 10) // 10MB 10.2 digits = 1; - else + else digits = 2; - return String(amount.toFixed(digits)) + speed_map[unit]; -} - -function enable() { - settings = ExtensionUtils.getSettings(PREFS_SCHEMA); - - mode = settings.get_int('mode'); // default mode using bit (bps, kbps) - fontmode = settings.get_int('fontmode'); - - button = new St.Bin({ - style_class: 'panel-button', - reactive: true, - can_focus: true, - x_expand: true, - y_expand: false, - track_hover: true - }); - - ioSpeed = new St.Label({ - text: '---', - y_align: Clutter.ActorAlign.CENTER, - style_class: 'simplenetspeed-label' - }); - - button.set_child(chooseLabel()); - button.connect('button-press-event', changeMode); - - Main.panel._rightBox.insert_child_at_index(button, 0); - timeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, refreshTime, () => { - return parseStat(); - }); -} - -function disable() { - if (timeout) { - GLib.source_remove(timeout); - timeout = null; + return String(amount.toFixed(digits)) + speed_map[unit]; } - Main.panel._rightBox.remove_child(button); - button.destroy(); - settings = button = ioSpeed = null; } diff --git a/metadata.json b/metadata.json index 1fd5d11..5ea9cb3 100644 --- a/metadata.json +++ b/metadata.json @@ -3,17 +3,9 @@ "description": "Simply showing network speed. Left click to change modes:\n\n1. Total net speed in bits per second\n2. Total net speed in Bytes per second\n3. Up & down speed in bits per second\n4. Up & down speed in Bytes per second\n5. Total of downloaded in Bytes (Right click to reset counter)\n\nMiddle click to change font size", "name": "Simple net speed", "shell-version": [ - "3.32", - "3.34", - "3.36", - "3.38", - "40", - "41", - "42", - "43", - "44" + "45" ], "url": "https://github.com/biji/simplenetspeed", "uuid": "simplenetspeed@biji.extension", - "version": 30 + "version": 31 } diff --git a/schemas/gschemas.compiled b/schemas/gschemas.compiled index e11bdb7..727b4e8 100644 Binary files a/schemas/gschemas.compiled and b/schemas/gschemas.compiled differ