-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
125 lines (107 loc) · 3.79 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
import GObject from 'gi://GObject';
import St from 'gi://St';
import Gio from 'gi://Gio';
import {Extension} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
export default class PanelOrganizerExtension extends Extension {
constructor(metadata) {
super(metadata);
this._settings = null;
this._settingsChanged = null;
this._originalPanelOrder = null;
}
enable() {
this._settings = this.getSettings();
this._settingsChanged = this._settings.connect('changed', this._onSettingsChanged.bind(this));
this._storePanelOrder();
this._applySettings();
}
disable() {
if (this._settingsChanged) {
this._settings.disconnect(this._settingsChanged);
this._settingsChanged = null;
}
this._settings = null;
this._restoreDefaultLayout();
}
_onSettingsChanged() {
this._applySettings();
}
_storePanelOrder() {
const panel = Main.panel;
this._originalPanelOrder = {
left: [],
center: [],
right: []
};
['_leftBox', '_centerBox', '_rightBox'].forEach(boxName => {
const box = panel[boxName];
const boxPosition = boxName.slice(1, -3); // Remove leading underscore and trailing 'Box'
box.get_children().forEach(child => {
if (child.constructor.name) {
this._originalPanelOrder[boxPosition].push(child.constructor.name);
}
});
});
}
_applySettings() {
const leftElements = this._settings.get_strv('left-elements');
const rightElements = this._settings.get_strv('right-elements');
const panel = Main.panel;
const allChildren = [
...panel._leftBox.get_children(),
...panel._centerBox.get_children(),
...panel._rightBox.get_children()
];
// Remove all children from the panel
panel._leftBox.remove_all_children();
panel._centerBox.remove_all_children();
panel._rightBox.remove_all_children();
// Add left elements
leftElements.forEach(element => {
const child = this._findChildByName(allChildren, element);
if (child) {
panel._leftBox.add_child(child);
}
});
// Add right elements
rightElements.forEach(element => {
const child = this._findChildByName(allChildren, element);
if (child) {
panel._rightBox.add_child(child);
}
});
// Add remaining elements to the center
allChildren.forEach(child => {
if (child.get_parent() === null) {
panel._centerBox.add_child(child);
}
});
}
_findChildByName(children, name) {
return children.find(child => child.constructor.name === name);
}
_restoreDefaultLayout() {
if (!this._originalPanelOrder) {
return;
}
const panel = Main.panel;
const allChildren = [
...panel._leftBox.get_children(),
...panel._centerBox.get_children(),
...panel._rightBox.get_children()
];
panel._leftBox.remove_all_children();
panel._centerBox.remove_all_children();
panel._rightBox.remove_all_children();
['left', 'center', 'right'].forEach(position => {
const box = panel[`_${position}Box`];
this._originalPanelOrder[position].forEach(elementName => {
const child = this._findChildByName(allChildren, elementName);
if (child) {
box.add_child(child);
}
});
});
}
}