gnome-shell-extension-spinner/extension.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-03-09 16:39:51 +08:00
import GLib from 'gi://GLib'
import GObject from 'gi://GObject'
import St from 'gi://St'
import Clutter from 'gi://Clutter'
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js'
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js'
import * as Main from 'resource:///org/gnome/shell/ui/main.js'
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init(textInPanel) {
super._init(0.0, "spinner", true)
this.add_child(textInPanel)
}
})
export default class IndicatorExampleExtension extends Extension {
timeoutId = 0
enable() {
let textInPanel = new St.Label({
text: ":",
y_expand: true,
y_align: Clutter.ActorAlign.CENTER,
})
textInPanel.set_style("font-family: monospace")
if (this.timeoutId != 0) {
GLib.Source.remove(this.timeoutId)
this.timeoutId = 0
}
let index = 0
//const content = ['|', '/', '-', '\\']
//const content = ['⠻', '⠽', '⠾', '⠷', '⠯', '⠟']
const content = ['⢿', '⣻', '⣽', '⣾', '⣷', '⣯', '⣟', '⡿']
this.timeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT,
100,
() => {
textInPanel.text = content[index]
index ++
index %= content.length
return GLib.SOURCE_CONTINUE
}
)
this._indicator = new Indicator(textInPanel)
Main.panel.addToStatusArea(this.uuid, this._indicator, 255, "left")
}
disable() {
if (this.timeoutId != 0) {
GLib.Source.remove(this.timeoutId)
this.timeoutId = 0
}
this._indicator.destroy()
this._indicator = null
}
}