-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
124 lines (100 loc) · 3.59 KB
/
main.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
/*
KWin Script Application Switcher
(C) 2022 Natalie Clarius <[email protected]>
GNU General Public License v3.0
*/
///////////////////////
// initialization
///////////////////////
const debugMode = readConfig("debugMode", true);
function debug(...args) {
if (debugMode) { console.debug("applicationswitcher:", ...args); }
}
debug("initializing");
///////////////////////
// special applications to ignore
///////////////////////
const ignoredApps = ["plasmashell", "org.kde.plasmashell", // desktop shell
"krunner", "org.kde.krunner", // KRunner
"kwin_wayland", // lock screen
"org.kde.ksmserver-logout-greeter", // logout screen
"ksplashqml" // login splash screen
];
///////////////////////
// get application a window belongs to
///////////////////////
// "dolphin"
function getApp(current) {
if (!current) return "";
return String(current.resourceClass);
}
///////////////////////
// keep track of active application (to check whether app has been switched)
///////////////////////
// "dolphin"
var prevActiveApp = ""
// set previously active application for initially active window
setPrevActiveApp(workspace.activeClient);
// set previously active application for recently activated window
function setPrevActiveApp(current) {
if (!current) return;
prevActiveApp = getApp(current);
}
// get previously active application
function getPrevActiveApp() {
return prevActiveApp;
}
///////////////////////
// keep track of windows belonging to same application in order of activation
///////////////////////
// {"dolphin": [oldest window, ..., most recent window], "konsole": ...}
var appGroups = {};
// compute app groups for initially present windows
workspace.clientList().forEach(window => updateAppGroups(window));
// update app groups with given window
function updateAppGroups(current) {
if (!current) return;
let app = getApp(current);
if (!appGroups[app]) appGroups[app] = [];
appGroups[app] = appGroups[app].filter(window => window &&
window != current);
appGroups[app].push(current);
debug("updating app group", appGroups[app].map(window => window.caption));
}
// return other visible windows of same application as given window
function getAppGroup(current) {
if (!current) return;
let appGroup = appGroups[getApp(current)].filter(window => window &&
!window.minimized &&
(window.x11DesktopIds.includes(workspace.currentDesktop) || window.x11DesktopIds.length == 0) &&
(window.activities.includes(workspace.currentActivity) || window.activities.length == 0));
debug("getting app group", appGroup.map(window => window.caption));
return appGroup;
}
///////////////////////
// main
///////////////////////
// when client is activated, auto-raise other windows of the same applicaiton
workspace.clientActivated.connect(active => {
if (!active) return;
debug("---------");
debug("activated", active.caption);
debug("app", getApp(active));
// abort if application is ignored
if (ignoredApps.includes(getApp(active))) {
debug("ignored");
return;
}
updateAppGroups(active);
// if application was switched
debug("previous app", getPrevActiveApp());
if (getApp(active) != getPrevActiveApp()) {
debug("app switched");
setPrevActiveApp(active);
// auto-raise other windows of same application
for (let window of getAppGroup(active)) {
debug("auto-raising", window.caption);
workspace.activeClient = window;
}
}
});