-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
1016 lines (902 loc) · 34 KB
/
extension.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* extension.js
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* exported init */
// const Gio = imports.gi.Gio;
// const Main = imports.ui.main;
// const Lang = imports.lang;
// const Lang = imports.lang;
// const GLib = imports.gi.GLib;
// const Gio = imports.gi.Gio;
const { GObject, Gio, Gtk, GLib, St, Clutter } = imports.gi;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const ModalDialog = imports.ui.modalDialog;
const Mainloop = imports.mainloop;
const Util = imports.misc.util;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const KEY_ICONS_TO_HIDE = "status-icons-name-to-hide";
const KEY_BLACKLISTED_ICONS = "blacklist-of-nonhidden-icons";
const KEY_DISPLAY_RAW_OBJECT_STR = "display-raw-icon-obj-name";
const KEY_DEEP_SEARCH = "deep-search-status-icons";
const KEY_IGNORE_INVISIBLE_ICON = "ignore-invisible-icons";
// const Extension = imports.misc.extensionUtils.getCurrentExtension();
// const ArgosLineView = Extension.imports.lineview.ArgosLineView;
// const ArgosMenuItem = Extension.imports.menuitem.ArgosMenuItem;
// const Utilities = Extension.imports.utilities;
const Main = imports.ui.main;
function _safe_ducktyping_apply_to_indicator(container, func) {
/* this is a measure to make it perform the safe option on both
the container and (potentially) the indicator. This is necessary as
some icon has indicator and some doesn't, where some action only works
on indicator */
try {
func(container);
} catch (ex) {}
try {
// on some icons, indicator are accessed via child property
func(container.child);
} catch (ex) {}
}
const BlacklistDialog = GObject.registerClass(
class BlacklistDialog extends ModalDialog.ModalDialog {
_init(name_container_pairs, gsetting) {
super._init({
styleClass: "extension-dialog",
shellReactive: true,
});
this._settings = gsetting;
this.switches = [];
this.blacklistedIcons = this._settings
.get_value(KEY_BLACKLISTED_ICONS)
.deep_unpack();
this.blacklistedIcons = new Set(this.blacklistedIcons);
this.setButtons([
{
label: "Cancel",
action: this._onCancel.bind(this),
key: Clutter.Escape,
},
{
label: "OK",
action: this._onOk.bind(this),
default: true,
},
]);
let box = new St.BoxLayout({ vertical: true });
this.box = box;
this.contentLayout.add(box);
let gicon = new Gio.FileIcon({
file: Gio.file_new_for_path(Me.path + "/icons/collapsed-icon.svg"),
});
let icon = new St.Icon({ gicon: gicon });
box.add(icon);
box.add(
new St.Label({
text: "Add icons name to ignore by the extension.",
x_align: Clutter.ActorAlign.CENTER,
style_class: "title-label",
})
);
// box.add(
// new St.Label({
// text: "AboutDialogTest Version " + Me.metadata.version,
// x_align: Clutter.ActorAlign.CENTER,
// style_class: "title-label",
// })
// );
// box.add(
// new St.Label({
// text: "GNOME Shell extension to display an About Dialog.",
// x_align: Clutter.ActorAlign.CENTER,
// })
// );
// box.add(
// new St.Label({
// text: "This program comes with absolutely no warranty.",
// x_align: Clutter.ActorAlign.CENTER,
// style_class: "warn-label",
// })
// );
// box.add(
// new St.Label({
// text: "Copyright © 2017-2018 BlahBlahBlah",
// x_align: Clutter.ActorAlign.CENTER,
// style_class: "copyright-label",
// })
// );
this._build(name_container_pairs);
this.open(global.get_current_time());
}
_build(name_container_pairs) {
let sortedName = [];
for (let k in name_container_pairs) sortedName[sortedName.length] = k;
// sort case insensitive
sortedName.sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
let iconsToHide = this._settings
.get_value(KEY_ICONS_TO_HIDE)
.deep_unpack();
let truncated = false;
let i = 0;
const limits = 22;
for (let statusButtonName of sortedName) {
if (
iconsToHide.indexOf(statusButtonName) >= 0 ||
this.blacklistedIcons.has(statusButtonName)
) {
continue;
}
let _switchItem = new PopupMenu.PopupSwitchMenuItem(
statusButtonName,
false
);
this.box.add(_switchItem);
this.switches.push([_switchItem, statusButtonName]);
if (i >= limits) {
truncated = true;
break;
}
i++;
}
if (truncated) {
dialog.box.add(
new St.Label({
text:
">>> " + (sortedName.length - i) + " outputs truncated...... <<<",
})
);
}
}
_onCancel(button, event) {
this.close(global.get_current_time());
}
_onOk(button, event) {
for (let s of this.switches)
if (s[0].state) this.blacklistedIcons.add(s[1]);
let tmpVairant = new GLib.Variant(
"as",
Array.from(this.blacklistedIcons)
);
this._settings.set_value(KEY_BLACKLISTED_ICONS, tmpVairant);
this.close(global.get_current_time());
}
}
);
var NoEventButton = GObject.registerClass(
class NoEventButton extends St.Button {
/* This class is to stop contained items being reactive to hover action.
The issue being:
(1) when a menu is opened, moving mouse to another panel button will
automatically open the new hovering icon's menu, and
(2) when 1 occurs in a nested menu scenario (i.e. a trying to open a
new popup menu as a menu item in another already opened popup menu)
the new one will tries to opens, then the old one will closes, and
(3) because the new popupmenu's actor is the old's popup menu, the new
popup menu will closes as well (because its parent is closing).
ALL IN ALL, this eventButton stops event passing onto contained
icons by setting XXX.reactive = false, and manually passing button-press
event to its contained item (this is to try and block Clutter.EventType.ENTER
and Clutter.EventType.MOVE) from the contained item. */
_init(container, statusIconName, params) {
this.container = container;
this.statusIconName = statusIconName;
_safe_ducktyping_apply_to_indicator(
this.container,
(c) => (c.reactive = false)
);
super._init(params);
}
vfunc_event(event) {
if (event.type() == Clutter.EventType.BUTTON_PRESS) {
// emit it back into the container's child
_safe_ducktyping_apply_to_indicator(this.container, (c) =>
c.emit("button-press-event", event)
);
}
if (event.type() == Clutter.EventType.BUTTON_RELEASE) {
// emit it back into the container's child
_safe_ducktyping_apply_to_indicator(this.container, (c) =>
c.emit("button-release-event", event)
);
}
/* check Clutter.EventType */
return Clutter.EVENT_PROPAGATE;
}
}
);
var HiddenStatusIcon = GObject.registerClass(
class HiddenStatusIcon extends PopupMenu.PopupBaseMenuItem {
_init(container_name, container, hiddenIconMenu_ptr) {
this._name = container_name;
this.container = container;
this.original_parent = container.get_parent();
this.isDestroyed = false;
this.original_reactive = container.reactive;
// this is the parent menu that stores this menuItem
// we store this reference to remove ourselves from this menu when we are destroyed
this.hiddenIconMenu_ptr = hiddenIconMenu_ptr;
try {
this.original_reactive = this.container.child.reactive;
} catch (ex) {}
// this keep track of the icon's original index
let _panel_icon = container.child.get_parent();
this.original_index = _panel_icon
.get_parent()
.get_children()
.indexOf(_panel_icon);
// listen to the container's original parent's destroy event, to set it as null
this.container.connect("destroy", () => {
this.container = null;
});
this.original_parent.connect("destroy", () => {
this.original_parent = null;
});
const STYLE1 = "width: 120px;";
const STYLE2 = "font-weight: bold;";
// var ExtensionStates =
// [_("Unknown"),
// _("Enabled"),
// _("Disabled"),
// _("Error"),
super._init({ reactive: false, can_focus: false });
// _("Out of Date"),
// _("Downloading"),
// _("Initialized")];
// if (extension.state != ExtensionSystem.ExtensionState.ERROR) {
// } else {
// super._init(params);
// }
// this._extension = extension;
// this._state = extension.state;
// this._uuid = extension.uuid;
// this._name = name;
// if (this._state > 6)
// this._state = 0;
// let box = new St.BoxLayout();
// let label1 = new St.Label({ text: "errorrrrrrrrrrrrr" });
// label1.set_style(STYLE1);
// box.add_actor(label1);
// let label2 = new St.Label({ text: "extension name" });
// // if (this._state == ExtensionSystem.ExtensionState.ERROR)
// label2.set_style(STYLE2);
// box.add_actor(label2);
// this.add_child(box);
const STYLE = "width: 120px; font-weight: bold;";
////////////////////////////
//// REMOVE ORIGINAL PARENT
_safe_ducktyping_apply_to_indicator(
container,
(c) => (c.reactive = false)
);
let parent = container.get_parent();
if (parent) parent.remove_actor(container);
////////////////////////////
let itembox = new St.BoxLayout({
// style_class: this.menu._use_alternative_theme ? "headerBar dark" : "headerBar",
vertical: false,
reactive: false,
});
let icon_name = new St.Label({
text: container_name,
});
icon_name.set_style(STYLE);
this.icon_name_button = new St.Button({ child: icon_name });
// this.container_button = new St.Button({ child: container });
this.container_button = new NoEventButton(container, container_name, {
child: container,
});
itembox.add_child(this.icon_name_button);
itembox.add_child(this.container_button);
// itembox.insert_child_at_index(container, 1);
// container.actor
this.add_child(itembox);
this.new_parent = this.container.get_parent();
// .actor.connect('button-press-event', button => {
// let _indicator = Main.panel.statusArea[button.statusButtonName];
// // Main.notify('Example Notification', "2" + Main.panel.statusArea[button.statusButtonName]);
}
restoreIcon() {
this.destroy();
}
destroy(by_external = false) {
// if it is destroyed by external, we can no longer access indicator, as it's freed by C backend.
if (!by_external) {
if (this.container) {
// remove myself
let parent = this.container.get_parent();
if (parent) {
parent.remove_actor(this.container);
}
// reset back to it's parent actor
if (this.original_parent) {
// try to restore to it's original index if it is a valid index
let restore_index = 0;
if (this.original_index >= 0) {
restore_index = this.original_index;
}
this.original_parent.insert_child_at_index(
this.container,
restore_index
);
}
// reset reactive
this.container.show();
_safe_ducktyping_apply_to_indicator(
this.container,
((c) => (c.reactive = this.original_reactive)).bind(this)
);
}
}
// this.hiddenIconMenu_ptr.menu.box.remove_child(this);
// this.hiddenIconMenu_ptr.remove_child(this);
super.destroy();
}
// hideIcon() {
// let indicator = this.indicator;
// let container = this.indicator.container;
// indicator.reactive = false;
// let parent = container.get_parent();
// if (parent)
// parent.remove_actor(container);
// }
}
);
const CollapsedIconsMenu = GObject.registerClass(
class CollapsedIconsMenu extends PanelMenu.Button {
_init(gsettings, keep_at_leftmost = true) {
super._init(0.5, "collapsed-icons-menu", false);
this._settings = gsettings;
// if set to true, this extensino will always to keep itself to be the left most icon
this.keep_at_leftmost = keep_at_leftmost;
this._icon = new St.Icon({
style_class: "popup-menu-icon",
// style_class: 'system-status-icon'
});
// this._icon.gicon = Gio.icon_new_for_string(`${Me.path}/icons/menu-down-outline.svg`);
this._icon.gicon = Gio.icon_new_for_string(
`${Me.path}/icons/collapsed-icon.svg`
);
this.add_actor(this._icon);
this._hidden_icon_menuitem = {};
// this._status_icon_to_hide = ["Caffeine"];
// load from gsettings
let icons_to_hide = this._settings
.get_value(KEY_ICONS_TO_HIDE)
.deep_unpack();
if (!icons_to_hide) {
icons_to_hide = [];
}
this._status_icon_to_hide = new Set(icons_to_hide);
// this._status_icon_to_hide.add("Caffeine");
// this set is to keep track of all hidden icon's parent under our control,
// so that we can check if the hidden status is indeed active
this._hidden_icon_parents = new Set();
// super._init()
// Main.notify('Example Notification', this.menu + "1");
// this.parent(0.0);
// let topBox = new St.BoxLayout();
// topBox.add_actor(this._icon);
// topBox.add_actor(this._panelButtonLabel);
// this.add_actor(topBox);
// this.menu = this;
this._hidden_status_icons = {};
// return;
// let label = new St.Label({ text: 'HEYO Button' });
// let menuItem = new PopupMenu.PopupMenuItem('Menu Item');
// menuItem.actor.connect('button-press-event', (a, b, c) => {
// Main.notify('Example Notification', 'Hello World !' + this) ;
// this.asf = [a, b, c];
// Main.notify('Example Notification', 'Hello World !2222222222222') ;
// });
// this.menu.addMenuItem(menuItem);
// // let section = new PopupMenu.PopupMenuSection();
// // this._item = new PopupMenu.PopupBaseMenuItem({ activate: false })
// // this.menu.addMenuItem(this._item)
// let box = new St.BoxLayout({ name: 'containerbox' });
// menuItem.add_child(box);
// let container = Main.panel.statusArea["Caffeine"].container;
// // menuItem.add_child(container);
// menuItem.add_actor(container);
// let parent = container.get_parent();
// if (parent)
// parent.remove_actor(container);
// box.insert_child_at_index(container, 0);
// Main.panel.statusArea[statusButtonName]
// menuItem.box = box;
// let preferences = new PopupMenu.PopupMenuItem("Preferences", false);
// let prefIcon = Gio.icon_new_for_string(`${Me.path}/icons/preferences.svg`);
// Main.notify("a", "a" + prefIcon);
let prefIcon = new St.Icon({
gicon: Gio.icon_new_for_string(`${Me.path}/icons/preferences.svg`),
style_class: "system-status-icon",
});
let preferences = new PopupMenu.PopupImageMenuItem(
"Preferences",
prefIcon.gicon
);
this.menu.addMenuItem(preferences);
preferences.connect("button-press-event", () => {
Util.trySpawnCommandLine(
`gnome-extensions prefs "${Me.metadata["uuid"]}"`
);
});
let blacklistIcon = new St.Icon({
gicon: Gio.icon_new_for_string(`${Me.path}/icons/blacklist.svg`),
style_class: "system-status-icon",
});
let blacklist = new PopupMenu.PopupImageMenuItem(
"Blacklist indicators",
blacklistIcon.gicon
);
this.menu.addMenuItem(blacklist);
blacklist.connect(
"button-press-event",
() => new BlacklistDialog(this._get_statusIcon_pairs(), this._settings)
);
this.submenu_nonhidden_icons = new PopupMenu.PopupSubMenuMenuItem(
"List of active icons",
true
);
this.submenu_nonhidden_icons.icon.gicon = Gio.icon_new_for_string(
`${Me.path}/icons/multiple-icon.svg`
);
this.menu.addMenuItem(this.submenu_nonhidden_icons);
this.submenu_hidden_icons = new PopupMenu.PopupSubMenuMenuItem(
"Collapsed Icons",
true
);
this.submenu_hidden_icons.icon.gicon = Gio.icon_new_for_string(
`${Me.path}/icons/file-hidden.svg`
);
this.menu.addMenuItem(this.submenu_hidden_icons);
// this.aaa = box;
// return;
this.update();
// List to myself's menu open
this.menu.connect("open-state-changed", (menu, open) => {
// Main.notify('Example Notification', "hi " + open);
// Main.notify('Example Notification', "hi " + this);
if (open) {
this.display_update();
this.update();
this.submenu_hidden_icons.menu.open();
// default open the hidden icon menu
// this.submenu_hidden_icons.menu.open();
} else {
}
});
// Bind destory callback
// this.connect("destroy", this._onDestroy.bind(this));
// Listen to multiple source's update, to automatically update the icon collapsing.
this.sessionMode_signal_id = Main.sessionMode.connect("updated", () =>
this.update()
);
}
notify(msg) {
Main.notify("Collapsed-icons-menu", "" + msg);
}
_get_statusIcon_pairs() {
let name_container_pairs = {};
/* Returns a dictionary of pairs of (String, indicator) */
if (this._settings.get_boolean(KEY_DEEP_SEARCH)) {
// create a dict to try to match unrecognised obj to a nice name; via searching if
// the indicator exists in statusArea (of which will have a nice string human-readable key)
let name_matcher = {};
for (let [key, value] of Object.entries(Main.panel.statusArea)) {
name_matcher[value.container] = key;
}
// always construct a proxy object that combines all {left, center, right} boxes.
let _canidates = [
Main.panel._leftBox,
Main.panel._centerBox,
Main.panel._rightBox,
];
for (let panel of _canidates) {
if (panel) {
for (let obj of panel.get_children()) {
try {
if (obj instanceof imports.ui.panelMenu.ButtonBox) {
/* The extension for legacy shell icon uses this! */
// if this is the case, this would give us the list of legacy gtk icons
for (let legacy_shell_icon of obj.first_child.get_children()) {
let key = "ShellTrayIcon: " + legacy_shell_icon.toString();
try {
// this can returns a readable window class name
if (legacy_shell_icon.first_child.wm_class)
key =
"ShellTrayIcon: " +
legacy_shell_icon.first_child.wm_class;
} catch (ex) {}
name_container_pairs[key] = legacy_shell_icon;
}
} else {
// I think some extensions may have mixed usage of indicators and container
// in the status area. We will use duck-typing, where if an object has .container,
// property, i will consider it as a proper indicator
if (!obj) continue;
let _indicator;
if (obj.hasOwnProperty("container")) {
_indicator = obj.container;
} else {
_indicator = obj;
}
if (!_indicator) continue;
let key = name_matcher[_indicator];
if (!key) {
// we cannot obtain a nice name for this. Use the raw obj string representation
key = _indicator.toString();
}
if (key && _indicator) {
}
name_container_pairs[key] = _indicator;
}
} catch (ex) {
this.notify("Error occurs when getting name icon pairs: " + ex);
}
}
}
}
}
// use the system simple one
for (let [key, value] of Object.entries(Main.panel.statusArea)) {
name_container_pairs[key] = value.container;
// return Main.panel.statusArea
}
return name_container_pairs;
}
display_update() {
let name_container_pairs = this._get_statusIcon_pairs();
let sortedName = [];
for (let k in name_container_pairs) sortedName[sortedName.length] = k;
// sort case insensitive
sortedName.sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
this.blacklistedIcons = this._settings
.get_value(KEY_BLACKLISTED_ICONS)
.deep_unpack();
this.blacklistedIcons = new Set(this.blacklistedIcons);
this.submenu_nonhidden_icons.menu.removeAll();
// show menu even if they are current not present in the system
for (const statusButtonName of this._status_icon_to_hide) {
if (sortedName.indexOf(statusButtonName) >= 0)
// presents!
continue;
// create switches
let menuItem = this._createSwitchMenu(statusButtonName, true);
this.submenu_nonhidden_icons.menu.addMenuItem(menuItem);
menuItem.setToggleState(true);
}
// menu for showing what icons are available to hide
try {
for (let statusButtonName of sortedName) {
if (this.blacklistedIcons.has(statusButtonName)) continue;
let _indicator = name_container_pairs[statusButtonName];
// display available icon to be hidden
if (
// statusButtonName in this._hidden_status_icons ||
(!this._settings.get_boolean(KEY_IGNORE_INVISIBLE_ICON) ||
_indicator.is_visible()) &&
(!_indicator.hasOwnProperty("child") || _indicator.child != this)
) {
// create switches
let menuItem = this._createSwitchMenu(statusButtonName);
this.submenu_nonhidden_icons.menu.addMenuItem(menuItem);
if (this._status_icon_to_hide.has(statusButtonName)) {
menuItem.setToggleState(true);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// restore any icons that should be restored
if (
statusButtonName in this._hidden_icon_menuitem &&
!this._status_icon_to_hide.has(statusButtonName)
) {
this.restoreIcon(statusButtonName);
}
}
} catch (ex) {
this.notify("Error occurs when populating icon list: " + ex);
}
}
update() {
let name_container_pairs = this._get_statusIcon_pairs();
let sortedName = [];
for (let k in name_container_pairs) sortedName[sortedName.length] = k;
// sort case insensitive
sortedName.sort((a, b) => {
return a.toLowerCase().localeCompare(b.toLowerCase());
});
// hide icons
for (let statusButtonName of sortedName) {
if (statusButtonName in this._hidden_icon_menuitem) {
// this icon should have been hidden. Ensure it.
try {
// ensure it's always in-reactive
_safe_ducktyping_apply_to_indicator(
this._hidden_icon_menuitem[statusButtonName].container,
(c) => (c.reactive = false)
);
let parentsDiffer = false;
if (
name_container_pairs[statusButtonName].get_parent() !=
this._hidden_icon_menuitem[statusButtonName].new_parent
) {
// parents differ!
parentsDiffer = true;
}
if (parentsDiffer) {
// return;
// something went wrong, this icon is NO LONGER UNDER OUR CONTROL!!!
// probably gnome-shell reset the icon's actor to topbar. Redo the hiding procedure.
this.onDestroyedExternally(statusButtonName);
}
} catch (ex) {
this.notify(
"Error occurs when re-hiding: " + statusButtonName + " - " + ex
);
}
// already hidden
continue;
}
if (
this._status_icon_to_hide.has(statusButtonName) &&
!(statusButtonName in this._hidden_icon_menuitem)
) {
try {
this.hideIcon(statusButtonName);
} catch (ex) {
this.notify(
"Error occurs when hiding: " + statusButtonName + " - " + ex
);
}
}
}
// update hidden icon menu
// let _children = this.submenu_hidden_icons.menu._getMenuItems();
// for (let i = 0; i < _children.length; i++){
// _children[i].get_parent().remove_child(_children[i]);
// }
// DETACH OLD submenu items
// let _container = this._hidden_status_icons[name].indicator.container;
// let parent = _container.get_parent();
// if (parent)
// parent.remove_actor(_container);
// }
// REMOVE OLD submenu items
// this.submenu_hidden_icons.menu.removeAll();
// // let _container = this._hidden_status_icons[name].indicator.container;
// // let parent = _container.get_parent();
// // if (parent)
// // parent.remove_actor(_container);
// let submenuItem = new HiddenStatusIcon(this._hidden_status_icons[name].indicator.container, name);
// this.submenu_hidden_icons.menu.addMenuItem(submenuItem);
// }
// this.submenu_hidden_icons.menu.removeAll();
// for (c in this._hidden_containers) {
// Main.notify("this", "hide " + c);
// let submenuItem = new HiddenStatusIcon(c);
// this.submenu_hidden_icons.menu.addMenuItem(submenuItem)
// }
if (this.keep_at_leftmost) {
// move this icon to left most
// this.get_parent() should returns the panel-menuItem
Main.panel._rightBox.set_child_at_index(this.get_parent(), 0);
}
}
update_gsettings() {
let tmpVairant = new GLib.Variant(
"as",
Array.from(this._status_icon_to_hide)
);
this._settings.set_value(KEY_ICONS_TO_HIDE, tmpVairant);
}
hideIcon(name) {
this._status_icon_to_hide.add(name);
// update gsettings
this.update_gsettings();
let name_container_pairs = this._get_statusIcon_pairs();
let _indicator = name_container_pairs[name];
if (!_indicator) {
this.notify("Icon " + name + " does not exists");
return;
}
if (name in this._hidden_icon_menuitem) {
this.notify("Icon " + name + " is already hidden");
return;
}
//////////////////////////////
// add new sub menu for the hidden icon
let submenuItem = new HiddenStatusIcon(
name,
_indicator,
this.submenu_hidden_icons
);
// let notify_destroyed = (name) => { this. [name].isDestroyed = true; }
_indicator.connect(
"destroy",
this.onDestroyedExternally.bind(this, name)
);
// add to our tray
this._hidden_icon_menuitem[name] = submenuItem;
this.submenu_hidden_icons.menu.addMenuItem(
this._hidden_icon_menuitem[name]
);
// allow user to click on tray icons
submenuItem.container_button.connect(
"button-press-event",
(button, event) => {
// this needs to first close the main menu, then open the nested sub-menu
const _open_menu = (_container) => {
if (!_container.hasOwnProperty("menu")) return;
// we first try to open a sub-menu. If after doing so, it's state is open
_container.menu.open();
// then we will close the main menu (because gnome cannot has two menu opened at once and it will
// try to close them), and perform the same action again.
// By doing this, we avoid closing the main menu if no sub-menu will opens anyway.
// E.G. useful for caffeine where it's only click action, and no menu is necessary.
if (_container.menu.isOpen) {
this.menu.close();
_container.menu.open();
}
};
_safe_ducktyping_apply_to_indicator(
this._hidden_icon_menuitem[button.statusIconName].container,
_open_menu.bind(this)
);
}
);
// // this is the icon's new parent which hides it from topbar
// let hidden_icon_parent = container.get_parent();
// this._hidden_icon_parents.add(hidden_icon_parent);
}
onDestroyedExternally(name) {
// the target indicator has been destroy. We will redo the same process to
// hide the icon aga
// try {
// if(name in this._hidden_icon_menuitem)
// this._hidden_icon_menuitem[name].destroy(true);
// } catch (ex) {}
try {
delete this._hidden_icon_menuitem[name];
} catch (ex) {}
// this.update()
}
restoreIcon(name) {
this._status_icon_to_hide.delete(name);
// update gsettings
this.update_gsettings();
if (!(name in this._hidden_icon_menuitem)) {
this.notify("Icon " + name + " is not hidden");
return;
}
this._hidden_icon_menuitem[name].destroy();
delete this._hidden_icon_menuitem[name];
}
_createSwitchMenu(name, phantom = false) {
/* phantom denotes that the icon is not currently at this system */
let state = false;
if (
name in this._hidden_icon_menuitem ||
name in this._status_icon_to_hide
)
state = true;
let display_name = name;
try {
if (this._settings.get_boolean(KEY_DISPLAY_RAW_OBJECT_STR)) {
if (!this._settings.get_boolean(KEY_DEEP_SEARCH)) {
// it is safe to use Main.panel.statusArea here because we won't display raw obj name
// in deep search (as every name would be raw anyways)
display_name += ":=> " + String(Main.panel.statusArea[name]);
}
}
} catch (ex) {}
if (phantom) display_name = "✗ " + display_name;
let switchmenuitem = new PopupMenu.PopupSwitchMenuItem(
display_name,
state
);
switchmenuitem.statusButtonName = name;
switchmenuitem.connect("toggled", (button, value) => {
if (value) {
this.hideIcon(name);
} else {
this.restoreIcon(name);
}
button.setToggleState(value);
this.update();
// this.menu.close();
// updating display within toggle will cause memory corrupts
// this.display_update();
// this.menu.open();
// this.submenu_hidden_icons.menu.open();
});
return switchmenuitem;
}
destroy() {
Main.sessionMode.disconnect(this.sessionMode_signal_id);
// this.menu.removeAll();
// this.submenu_hidden_icons = null;
// this.submenu_nonhidden_icons = null;
super.destroy();
// this.emit("destroy");
}
}
);
class CollapsedIconMenuExtension {
constructor() {
this.settings = {
updateOnOpen: false,
updateInterval: null,
position: 0,
box: "right",
};
this.cim_indicator = null;
const ExtensionUtils = imports.misc.extensionUtils;
// If `settings-schema` is defined in `metadata.json` you can simply call this
// this function without an argument.
this._settings = ExtensionUtils.getSettings();
// // You can also use this function to safely create a GSettings object for other
// // schemas without knowing whether they exist or not.
// let nautilusSettings = null;
// try {
// nautilusSettings = ExtensionUtils.getSettings('org.gnome.nautilus');
// } catch (e) {
// logError(e, 'Failed to load Nautilus settings');
// }
// always start at a delayed time so that other extensions will be loaded first.
this._delayedEnableId = null;
this._delayedEnableTime = 3000;
}
delayedEnable() {
if (this._delayedEnableId != null) {
Mainloop.source_remove(this._delayedEnableId);
this._delayedEnableId = null;
}
if (!this.cim_indicator) {
this.cim_indicator = new CollapsedIconsMenu(this._settings);
Main.panel.addToStatusArea(
"collapsed-icons-menu",
this.cim_indicator,
this.settings.position,
this.settings.box
);
}
}
enable() {
this._delayedEnableId = Mainloop.timeout_add(
this._delayedEnableTime,
this.delayedEnable.bind(this)
);
}