File: indicator.js

package info (click to toggle)
gnome-shell-extension-kimpanel 0.0~git20220902.c11f1a6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 208 kB
  • sloc: javascript: 988; sh: 38; xml: 15; makefile: 2
file content (181 lines) | stat: -rw-r--r-- 6,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const {St, GObject, Clutter, Pango} = imports.gi;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Params = imports.misc.params;
const Gettext = imports.gettext.domain('gnome-shell-extensions-kimpanel');
const _ = Gettext.gettext;

const Me = imports.misc.extensionUtils.getCurrentExtension();
const Lib = Me.imports.lib;

var KimIndicator = GObject.registerClass(
    class Indicator_KimIndicator extends PanelMenu.Button {
        _init(params) {
            super._init(0.0, 'kimpanel');
            params = Params.parse(params, {kimpanel : null});
            this._properties = {};
            this._propertySwitch = {};

            let hbox =
                new St.BoxLayout({style_class : 'panel-status-menu-box'});
            this.labelIcon =
                new St.Label({y_align : Clutter.ActorAlign.CENTER});
            this.labelIcon.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
            this.labelIcon.clutter_text.line_wrap = false;
            this.mainIcon = new St.Icon({
                gicon : Lib.createIcon('input-keyboard'),
                style_class : 'system-status-icon'
            });
            hbox.add_child(this.labelIcon);
            hbox.add_child(this.mainIcon);
            this.add_actor(hbox);
            this._deactive();

            this.kimpanel = params.kimpanel;

            let settingMenu = new PopupMenu.PopupMenuItem(_("Settings"));
            settingMenu.connect('activate',
                                () => this.kimpanel.emit('Configure'));
            let reloadMenu =
                new PopupMenu.PopupMenuItem(_("Reload Configuration"));
            reloadMenu.connect('activate',
                               () => this.kimpanel.emit('ReloadConfig'));

            this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
            this.menu.addMenuItem(reloadMenu);
            this.menu.addMenuItem(settingMenu);
            this.hide();
        }

        destroy() {
            this.kimpanel = null;
            this.labelIcon.destroy();
            this.mainIcon.destroy();
            super.destroy();
        }

        _addPropertyItem(key) {
            if (!(key in this._properties)) {
                return;
            }
            let property = this._properties[key];
            let item = Lib.createMenuItem(property);

            item._menuItemActivateId = item.connect(
                'activate', () => this.kimpanel.triggerProperty(item._key));
            item._menuItemDestroyId = item.connect('destroy', () => {
                item.disconnect(item._menuItemActivateId);
                item.disconnect(item._menuItemDestroyId);
            });
            item.setIcon(property.icon);
            item.label.text = property.label;

            this._propertySwitch[key] = item;
            this.menu.addMenuItem(this._propertySwitch[key],
                                  this.menu.numMenuItems - 3);
        }

        _updatePropertyItem(key) {
            let property = this._properties[key];
            let item = this._propertySwitch[key];
            item.setIcon(property.icon);
            item.label.text = property.label;
        }

        _updateProperty(propstr) {
            let property = Lib.parseProperty(propstr);
            if (property == null) {
                return;
            }
            let key = property.key;
            this._properties[key] = property;
            this._updateProperties();
        }

        _updateProperties(properties) {
            if (properties == undefined) {
                for (let key in this._propertySwitch) {
                    let property = this._properties[key];
                    let item = this._propertySwitch[key];
                    item.setIcon(property.icon);
                    item.label.text = property.label;
                }
                return;
            } else {
                for (let p in this._propertySwitch) {
                    if (properties[p] == undefined) {
                        this._propertySwitch[p].destroy();
                        delete this._propertySwitch[p];
                    }
                }

                let count = 0;
                for (let p in properties) {
                    let property = Lib.parseProperty(properties[p]);
                    if (property == null) {
                        continue;
                    }
                    count++;
                    let key = property.key;
                    this._properties[key] = property;
                    if (key in this._propertySwitch)
                        this._updatePropertyItem(key);
                    else
                        this._addPropertyItem(key);
                }
                if (count > 0) {
                    this.show();
                } else {
                    this.hide();
                }
            }
        }

        _setIcon(property) {
            for (let i = 0; i < property.hint.length; i++) {
                if (property.hint[i].startsWith("label=")) {
                    let label = property.hint[i].substr(6);
                    if (label.length > 0) {
                        this.labelIcon.text = Lib.extractLabelString(label);
                        this.mainIcon.visible = false;
                        this.labelIcon.visible = true;
                        return;
                    }
                }
            }

            let iconName = property.icon;
            let labelName = property.label;

            if (iconName === '') {
                this.labelIcon.text = Lib.extractLabelString(labelName);
                this.mainIcon.visible = false;
                this.labelIcon.visible = true;
            } else {
                let gicon = Lib.createIcon(iconName);
                this.mainIcon.gicon = gicon;
                this.mainIcon.visible = true;
                this.labelIcon.visible = false;
            }
        }

        _active() {
            if (this._properties['/Fcitx/im']) {
                this._setIcon(this._properties['/Fcitx/im']);
            } else {
                self._deactive()
            }
        }

        _deactive() {
            let property = {
                'icon' : 'input-keyboard',
                'label' : '',
                'text' : '',
                'key' : '',
                'hint' : [],
            };

            this._setIcon(property);
        }
    });