forked from fablevi/HideItems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
365 lines (307 loc) · 11.8 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
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import St from 'gi://St';
import Clutter from 'gi://Clutter';
import * as Config from 'resource:///org/gnome/shell/misc/config.js';
export default class HideItems extends Extension {
constructor(ext) {
super(ext);
}
enable() {
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 2000, () => {
print("This message is shown after 2 seconds.");
this.settings = this.getSettings();
this.settings.connect("changed::hideiconstate", this._changeState.bind(this))
this.settings.connect("changed::nothiddenindicator", this._settingsHiddenIndicatorStateChange.bind(this))
this.settings.connect("changed::topbarupdater", this._updateIcon.bind(this))
console.log("Hide Items Extension started...");
this._indicator = null;
this._menu = null;
//default visibility is true, but in the future i prepare it to state management
this._settingsJSON = this._importJSONFile();
this._visibility = this._setVisibiltyState();
this._iconRank = 0;
this._createButton()
this._setIconVisibility();
//Update if a new button added or old removed from the panel
const shellVersion = parseFloat(Config.PACKAGE_VERSION).toString().slice(0, 2);
console.log(shellVersion)
//let signalName = (shellVersion == 45) ? 'actor' : /* 46 */ 'child';
let signalName = 'child';
this._connectAddedHandlerID = Main.panel._rightBox.connect(signalName + "-added", this._addedIconListener.bind(this));
this._connectRemovedHandlerID = Main.panel._rightBox.connect(signalName + "-removed", this._removedIconListener.bind(this));
return GLib.SOURCE_REMOVE; // Returning false stops the timer
});
}
disable() {
console.log("Hide Items Extension stopped...");
Main.panel._rightBox.disconnect(this._connectAddedHandlerID);
Main.panel._rightBox.disconnect(this._connectRemovedHandlerID);
this._changeBackVisibility();
this._menu?.destroy();
this._menu = null;
this._showIcon?.destroy();
this._showIcon = null;
this._hideIcon?.destroy();
this._hideIcon = null;
this._indicator?.destroy();
this._indicator = null;
this._settingsJSON = null;
this._visibility = null;
this._iconRank = null;
this._connectAddedHandlerID = null;
this._connectRemovedHandlerID = null;
this.settings = null;
}
_addMenu() {
this._menu = new PopupMenu.PopupMenuItem('Settings', {
});
this._menu.connect('activate', () => {
try {
// this.#extension.openPreferences()
// Itt tudod elhelyezni a jobb gombhoz tartozó műveletet
//console.log("Jobb gombra kattintva - Menü aktiválva");
this.openPreferences();
} catch (e) {
console.log('error', e);
}
});
this._indicator.menu.addMenuItem(this._menu);
}
//call when disabled()
_changeBackVisibility() {
var rightBoxItems = Main.panel._rightBox.get_children();
rightBoxItems.forEach((item, index) => {
item.visible = true
})
}
_createButton() {
//icon size
this._iconSize = 16;
const showAdwIcon = Gio.icon_new_for_string(`${this.path}/icons/left-symbolic.svg`);
this._showIcon = this._createIcon(showAdwIcon);
const hideAdwIcon = Gio.icon_new_for_string(`${this.path}/icons/right-symbolic.svg`);
this._hideIcon = this._createIcon(hideAdwIcon);
this._indicator = new PanelMenu.Button(0.0, this.metadata.name, false);
this._indicator.connect('button-press-event', this._buttonClicked.bind(this));
this._setButtonIcon();
this._iconRank = this._getSettingsRank();
Main.panel.addToStatusArea(this.uuid, this._indicator, this._iconRank, "right");
this._addMenu()
}
_createIcon(icon) {
return new St.Icon({
gicon: icon,
style_class: 'system-status-icon',
icon_size: this._iconSize
});
}
_setButtonIcon() {
try {
if (this._indicator.first_child !== null) {
this._indicator.remove_child(this._indicator.first_child)
}
if (this._visibility) {
this._indicator.add_child(this._showIcon)
} else {
this._indicator.add_child(this._hideIcon)
}
} catch (e) {
console.log("HideItems error: ", e)
}
}
_getSettingsRank() {
/*var rightBoxItems = Main.panel._rightBox.get_children();
var rank = null;
rightBoxItems.map((item, index) => {
if (item === Main.panel.statusArea.quickSettings.get_parent()) {
rank = index;
}
})
//console.log("index: ", rank)
return rank;*/
var rightBoxItemsLength = Main.panel._rightBox.get_children().length;
return rightBoxItemsLength - 1;
//return 200;
}
//CLICKED BUTTON
_buttonClicked(actor, event) {
if (event.get_button() === Clutter.BUTTON_PRIMARY) {
// Bal egérgomb lenyomva
//console.log('Bal egérgomb lenyomva');
this._visibility = !this._visibility;
this._changeVisibilityState();
this._hideOrShowItems();
this._changeIcon();
this._indicator.menu.toggle();
} else if (event.get_button() === Clutter.BUTTON_SECONDARY) {
//console.log('Jobb egérgomb lenyomva');
}
}
_changeIcon() {
this._setButtonIcon();
}
_hideOrShowItems() {
var rightBoxItems = Main.panel._rightBox.get_children();
rightBoxItems.map((item, index) => {
if (item.child !== Main.panel.statusArea.quickSettings && item.child !== this._indicator && this._isVisibleChildrenInIt(item.child)) {
item.visible = this._visibility;
}
})
}
_isVisibleChildrenInIt(item) {
var boolean = true;
var childrenName = null;
if (item.accessible_name !== "") {
childrenName = item.accessible_name;
} else {
childrenName = item.constructor.$gtype.name
}
this._settingsJSON.visibleChildren.map((item, index) => {
item == childrenName ? boolean = false : null;
})
return boolean;
}
_setIconVisibility() {
this._visibility ?
null
:
this._hideOrShowItems();
this._changeIcon();
}
//LISTENER
_addedIconListener(container, actor) {
this._hideOrShowItems();
//change the icon index
this._sendItemsToPrefs(this._getEveryChildrenFrom_RightBox())
}
_removedIconListener(container, actor) {
this._sendItemsToPrefs(this._getEveryChildrenFrom_RightBox())
}
_updateIcon() {
this._sendItemsToPrefs(this._getEveryChildrenFrom_RightBox())
}
//state management
_importJSONFile() {
let settingsJSONpath = `${this.path}/settings.json`
try {
let file = Gio.File.new_for_path(settingsJSONpath);
let [success, content] = file.load_contents(null);
if (success) {
let json = JSON.parse(content);
return json;
} else {
return null;
}
} catch (error) {
//console.log('Hiba történt:', error.message);
return null;
}
}
_updateJSONFile(newState, newVisibility) {
let settingsJSONpath = `${this.path}/settings.json`
try {
let file = Gio.File.new_for_path(settingsJSONpath);
let [success, content] = file.load_contents(null);
if (success) {
let json = JSON.parse(content);
// Frissítsd a "position" kulcs értékét az új pozícióval
json.state = newState;
json.visibility = newVisibility;
// JSON objektumot szöveggé alakítsuk
let updatedContent = JSON.stringify(json, null, 4);
// A fájl tartalmának frissítése
file.replace_contents(
updatedContent,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null
);
} else {
}
} catch (error) {
console.log('Something wrong happened:', error.message);
}
}
_setVisibiltyState() {
if (this._settingsJSON.state === "1") {
return this._settingsJSON.visibility;
} else {
return this._settingsJSON.defaultVisibility;
}
}
_changeVisibilityState() {
if (this._settingsJSON.state === "1") {
this._updateJSONFile(this._settingsJSON.state, this._visibility);
}
}
//gsettings change
_changeState() {
//this._updateJSONFile()
//console.log("this.settings")
this._updateJSONFile(this.settings.get_string("hideiconstate"), this._visibility);
this._settingsJSON = this._importJSONFile();
}
/*
//icon index change EXPERIMENTAL!!!
_changeIndicatorIndex() {
//Main.panel.statusArea.quickSettings
var settingsRank = this._getSettingsRank();
var indicatorRank = this._getIndicatorRank();
if (settingsRank !== indicatorRank) {
this._showIcon?.destroy();
this._showIcon = null;
this._hideIcon?.destroy();
this._hideIcon = null;
this._indicator?.destroy();
this._indicator = null;
this._createButton()
}
}
_getIndicatorRank() {
var rightBoxItems = Main.panel._rightBox.get_children();
var rank = null;
rightBoxItems.map((item, index) => {
if (item.get_first_child().accessible_name == "Hide Items") {
rank = index;
}
})
//console.log("index: ", rank)
return rank;
}*/
//changeHiddenIcons
_getEveryChildrenFrom_RightBox() {
var rightBoxItems = Main.panel._rightBox.get_children();
var childrenNames = [];
rightBoxItems.map((item, index) => {
if (item.get_first_child() !== Main.panel.statusArea.quickSettings && item.get_first_child().accessible_name !== "Hide Items") {
if (item.get_first_child().accessible_name !== "") {
childrenNames.push(item.get_first_child().accessible_name);
} else {
childrenNames.push(item.get_first_child().constructor.$gtype.name);
}
}
})
return childrenNames;
}
_sendItemsToPrefs(childrenNames) {
this.settings.set_strv('allindicator', childrenNames)
}
//iconHideOrVisibleStateChange
_settingsHiddenIndicatorStateChange() {
this._settingsJSON = this._importJSONFile();
var rightBoxItems = Main.panel._rightBox.get_children();
rightBoxItems.map((item, index) => {
if (item.child !== Main.panel.statusArea.quickSettings && item.child !== this._indicator && this._isVisibleChildrenInIt(item.child)) {
item.visible = this._visibility;
} else {
item.visible = true;
}
})
}
}