Debian Codegraph @

extensions/status-icons/extension.js

raw
1 // SPDX-FileCopyrightText: 2018 Adel Gadllah <[email protected]>
2 // SPDX-FileCopyrightText: 2018 Florian Müllner <[email protected]>
3 //
4 // SPDX-License-Identifier: GPL-2.0-or-later
5
6 import Clutter from 'gi://Clutter';
7 import Shell from 'gi://Shell';
8 import St from 'gi://St';
9
10 import * as Main from 'resource:///org/gnome/shell/ui/main.js';
11 import {Button as PanelButton} from 'resource:///org/gnome/shell/ui/panelMenu.js';
12
13 const PANEL_ICON_SIZE = 16;
14
15 const STANDARD_TRAY_ICON_IMPLEMENTATIONS = [
16 'bluetooth-applet',
17 'gnome-sound-applet',
18 'nm-applet',
19 'gnome-power-manager',
20 'keyboard',
21 'a11y-keyboard',
22 'kbd-scrolllock',
23 'kbd-numlock',
24 'kbd-capslock',
25 'ibus-ui-gtk',
26 ];
27
28 export default class SysTray {
29 constructor() {
30 this._icons = new Map();
31 this._tray = null;
32 }
33
34 _onTrayIconAdded(o, icon) {
35 const wmClass = icon.wm_class ? icon.wm_class.toLowerCase() : '';
36 if (STANDARD_TRAY_ICON_IMPLEMENTATIONS.includes(wmClass))
37 return;
38
39 const button = new PanelButton(0.5, null, true);
40
41 const scaleFactor = St.ThemeContext.get_for_stage(global.stage).scale_factor;
42 const iconSize = PANEL_ICON_SIZE * scaleFactor;
43
44 icon.set({
45 width: iconSize,
46 height: iconSize,
47 x_align: Clutter.ActorAlign.CENTER,
48 y_align: Clutter.ActorAlign.CENTER,
49 });
50
51 const iconBin = new St.Widget({
52 layout_manager: new Clutter.BinLayout(),
53 style_class: 'system-status-icon',
54 });
55 iconBin.add_child(icon);
56 button.add_child(iconBin);
57
58 this._icons.set(icon, button);
59
60 button.connect('button-release-event',
61 (actor, event) => icon.click(event));
62 button.connect('key-press-event',
63 (actor, event) => icon.click(event));
64
65 const role = `${icon}`;
66 Main.panel.addToStatusArea(role, button);
67 }
68
69 _onTrayIconRemoved(o, icon) {
70 const button = this._icons.get(icon);
71 button?.destroy();
72 this._icons.delete(icon);
73 }
74
75 enable() {
76 this._tray = new Shell.TrayManager();
77 this._tray.connectObject(
78 'tray-icon-added', this._onTrayIconAdded.bind(this),
79 'tray-icon-removed', this._onTrayIconRemoved.bind(this),
80 this);
81 this._tray.manage_screen(Main.panel);
82 }
83
84 disable() {
85 this._icons.forEach(button => button.destroy());
86 this._icons.clear();
87
88 this._tray.unmanage_screen();
89 this._tray.disconnectObject(this);
90 this._tray = null;
91 }
92 }