forked from Valent-in/Show-Desktop-Applet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
163 lines (125 loc) · 3.73 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
import St from 'gi://St';
import Meta from 'gi://Meta';
import Gio from 'gi://Gio';
import Shell from 'gi://Shell';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
let ExtensionName;
let settings, extSettings;
let settingsSignal;
let panelButton;
let storedWindows = [];
let isHotkeySet = false;
function toggleDesktop() {
let workspace = global.workspace_manager.get_active_workspace();
let windows = workspace.list_windows();
let tmpStoredWindows = [];
// Try to minimize all windows
for (let i = 0; i < windows.length; ++i) {
if (!shouldBeIgnored(windows[i])) {
windows[i].minimize();
tmpStoredWindows.push(windows[i])
}
}
if (tmpStoredWindows.length > 0) {
storedWindows = tmpStoredWindows;
} else {
// If nothing were minimized (desktop is alredy shown)
// try to restore previously minimized
let topWindow;
let stackedWindows = global.display.sort_windows_by_stacking(storedWindows);
for (let i = 0; i < stackedWindows.length; i++) {
if (stackedWindows[i] && stackedWindows[i].located_on_workspace(workspace)) {
stackedWindows[i].unminimize();
topWindow = stackedWindows[i];
}
}
if (topWindow)
topWindow.activate(global.get_current_time());
storedWindows = [];
}
if (Main.overview.visible) {
Main.overview.hide();
}
}
function shouldBeIgnored(window) {
if (!window)
return true;
if (window.minimized ||
window.skip_taskbar ||
window.window_type == Meta.WindowType.DESKTOP ||
window.window_type == Meta.WindowType.DOCK) {
return true;
}
return false;
}
function createPanelButton() {
panelButton = new PanelMenu.Button(0.0, `${ExtensionName}`, false);
let icon = new St.Icon({
icon_name: 'computer-symbolic',
style_class: 'system-status-icon',
});
panelButton.add_child(icon);
panelButton.connect('button-press-event', toggleDesktop);
panelButton.connect_after('key-release-event', (actor, event) => {
let symbol = event.get_key_symbol();
if (symbol == Clutter.KEY_Return || symbol == Clutter.KEY_space) {
toggleDesktop();
}
return Clutter.EVENT_PROPAGATE;
});
}
function addButton() {
let role = `${ExtensionName} Indicator`;
let index = settings.get_enum('button-position');
let positions = ['left', 'left', 'center', 'right', 'right'];
let modifiers = [0, 1, 0, 1, -1];
createPanelButton();
if (index == 0 || index == 4)
panelButton.style = '-natural-hpadding:4px;-minimum-hpadding:4px;'
Main.panel.addToStatusArea(role, panelButton, modifiers[index], positions[index]);
}
function removeButton() {
if (panelButton)
panelButton.destroy();
panelButton = null;
}
function onEnable() {
addButton();
let mode = Shell.ActionMode.NORMAL | Shell.ActionMode.OVERVIEW | Shell.ActionMode.POPUP;
let flag = Meta.KeyBindingFlags.IGNORE_AUTOREPEAT;
if (settings.get_boolean('enable-hotkey')) {
extSettings.set_strv('show-desktop', []);
Main.wm.addKeybinding('show-desktop-hotkey', settings, flag, mode, toggleDesktop);
isHotkeySet = true;
}
}
function onDisable() {
if (isHotkeySet) {
Main.wm.removeKeybinding('show-desktop-hotkey');
extSettings.reset('show-desktop');
isHotkeySet = false;
}
removeButton();
}
export default class extends Extension {
enable() {
ExtensionName = this.metadata.name;
settings = this.getSettings();
extSettings = new Gio.Settings({ schema: 'org.gnome.desktop.wm.keybindings' });
storedWindows = [];
onEnable();
settingsSignal = settings.connect('changed', (s) => {
onDisable();
onEnable();
});
}
disable() {
settings.disconnect(settingsSignal);
onDisable();
storedWindows = null;
settings = null;
extSettings = null;
}
}