-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
AppManager.js
167 lines (144 loc) · 3.99 KB
/
AppManager.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
import GObject from 'gi://GObject';
import Shell from 'gi://Shell';
import GLib from 'gi://GLib';
export const AppManager = GObject.registerClass(
class AppManager extends GObject.Object {
constructor(extension) {
super();
this._extension = extension;
}
leftClick(icon, event) {
let trayApp = this._getTrayApp(icon);
if (trayApp) {
let focusedApp = Shell.WindowTracker.get_default().focusApp;
let windows = trayApp.get_windows();
if (windows == "") {
return this._openApplication(trayApp, icon, event);
}
if (focusedApp != null && focusedApp.id == trayApp.id) {
return this._minimizeWindows(focusedApp.get_windows(), icon, event);
}
return this._activateWindows(windows, trayApp, event);
}
icon.click(event);
// On Windows double-click restore app
if (this.isWine(icon)) {
icon.click(event);
}
}
middleClick(icon, event) {
// When holding SHIFT
if (event.get_state_full()[1] === 1) {
let trayApp = this._getTrayApp(icon);
if (trayApp) {
const pid = this._getPid(icon);
// Kill app
if (this._isUsingQt(pid)) {
return GLib.spawn_command_line_sync(`/bin/kill ${pid}`);
}
let windows = trayApp.get_windows();
windows.forEach((window) => {
window.kill();
});
trayApp.request_quit();
}
} else {
icon.click(event);
}
}
getAppSetting(icon, setting) {
const iconApp = this._getTrayApp(icon);
if (iconApp) {
const appsSettings = JSON.parse(
this._extension._settings.get_string("applications")
);
const appSettings = appsSettings.find(
(app) => app.id == iconApp.get_id()
);
return appSettings?.[setting];
}
}
isWine(icon) {
if (
(icon.wm_class == "Wine" || icon.wm_class == "explorer.exe") &&
this._extension._settings.get_boolean("wine-behavior")
) {
return true;
}
}
_getTrayApp(icon) {
if (this.isWine(icon)) {
const wineApps = Shell.AppSystem.get_default()
.get_running()
.filter((app) => {
return app.get_windows()[0].wm_class.includes(".exe");
});
return wineApps[0];
}
const searchedApps = Shell.AppSystem.search(this._getWmClass(icon.wm_class));
if (searchedApps[0] && searchedApps[0][0]) {
var i = 1;
for (let lookup of searchedApps[0]) {
let app = Shell.AppSystem.get_default().lookup_app(lookup);
if (app && (app.get_windows() != "" || i == searchedApps[0].length)) {
return app;
}
i++;
}
}
return false;
}
_openApplication(trayApp, icon, event) {
const isFlatpak = trayApp.app_info.has_key("X-Flatpak");
const onBlacklist = this._extension.metadata["open-blacklist"].includes(icon.wm_class); // Caprine
if (this._isUsingQt(this._getPid(icon)) || isFlatpak || onBlacklist) {
return icon.click(event);
}
return trayApp.open_new_window(0);
}
_minimizeWindows(windows, icon, event) {
if (this._isUsingQt(this._getPid(icon))) {
return icon.click(event);
}
windows.forEach((window) => {
window.minimize();
});
}
_activateWindows(windows, trayApp, event) {
windows.forEach((window) => {
if (this._extension._settings.get_boolean("invoke-to-workspace")) {
window.change_workspace(
global.workspace_manager.get_active_workspace()
);
}
trayApp.activate_window(window, event.get_time());
window.unminimize();
});
}
_isUsingQt(pid) {
let [ok, out, err, exit] = GLib.spawn_command_line_sync(
`/bin/bash -c 'pmap -p ${pid} | grep Qt'`
);
if (out.length) {
return true;
}
}
_getWmClass(wmclass) {
wmclass = wmclass.replace(/[0-9]/g, ""); // skype discord
wmclass = wmclass.replace("Desktop", ""); // telegram
return wmclass;
}
_getPid(icon) {
const wmclass = this._getWmClass(icon.wm_class);
if (icon.title != "snixembed") {
return icon.pid;
}
let [ok, out, err, exit] = GLib.spawn_command_line_sync(
`/bin/bash -c "pidof -s ${wmclass}"`
);
if (out.length) {
return Number(out);
}
}
}
);