-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
149 lines (129 loc) · 4.64 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
// You can do whatever you want with this code, but I hope that, if you want to
// improve it, you will talk to me so we can discuss and implement your idea into
// this very same extension. It's just to keep things neat and clean, instead of
// polluting EGO with plenty of similar extensions that do almost the same thing.
//
// If you want to debug this extension, open 'metadata.json' and set 'debug' to true.
// You can read the debugging messages in the terminal if you give the following:
// $ journalctl -f -o cat /usr/bin/gnome-shell
import Shell from 'gi://Shell';
import Meta from 'gi://Meta';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension, gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js';
// Global variables
let ignoredWindows = [];
// Debug logging function
function logDebug(message) {
console.debug(message);
}
// Populates the ignoredWindows array with windows that should not be minimized.
// It iterates through the list of open windows and adds those that meet
// the exclusion criteria to ignoredWindows.
function populateIgnoredWindows(windows) {
for (let i = 0; i < windows.length; ++i) {
let title = windows[i].title ?? '';
let window_type = windows[i].window_type ?? '';
let wm_classInitial = windows[i].wm_class ?? '';
let wm_class = wm_classInitial.toLowerCase();
logDebug(`Analyzing window ${i}:`);
logDebug(`\t Title: ${title}`);
logDebug(`\t Window Type: ${window_type}`);
logDebug(`\t wm_class: ${wm_class}`);
// Add windows to ignoredWindows based on type or title
if (window_type === Meta.WindowType.DESKTOP) {
logDebug(`\t ${title} ignored: window type is DESKTOP`);
ignoredWindows.push(windows[i]);
continue;
}
if (window_type === Meta.WindowType.DOCK) {
logDebug(`\t ${title} ignored: window type is DOCK`);
ignoredWindows.push(windows[i]);
continue;
}
if (window_type === Meta.WindowType.MODAL_DIALOG) {
logDebug(`\t ${title} ignored: window type is MODAL DIALOG`);
ignoredWindows.push(windows[i]);
continue;
}
if (title.startsWith('DING')) {
logDebug(`\t ${title} ignored: title starts with DING`);
ignoredWindows.push(windows[i]);
continue;
}
if (wm_class.endsWith('notejot')) {
logDebug(`\t ${title} ignored: wm_class ends with notejot`);
ignoredWindows.push(windows[i]);
continue;
}
if (wm_class === 'conky') {
logDebug(`\t ${title} ignored: wm_class is conky`);
ignoredWindows.push(windows[i]);
continue;
}
if (wm_class === 'gjs') {
logDebug(`\t ${title} ignored: wm_class is Gjs`);
ignoredWindows.push(windows[i]);
continue;
}
if (title.startsWith('@!') && (title.endsWith('BDH') || title.endsWith('BDHF'))) {
logDebug(`\t ${title} ignored: title starts with @! and ends with BDH or BDHF`);
ignoredWindows.push(windows[i]);
continue;
}
}
}
// Removes the windows that should be ignored (in ignoredWindows) from the
// full list of windows provided as input.
function pruneWindows(windows) {
return windows.filter(window => !ignoredWindows.includes(window));
}
// Minimizes all windows except the currently focused window.
// It first populates the ignoredWindows array and then prunes
// the windows list to remove any windows that should be ignored.
function minimizeWindowsExceptFocused() {
let metaWorkspace = global.workspace_manager.get_active_workspace();
let windows = metaWorkspace.list_windows();
populateIgnoredWindows(windows);
windows = pruneWindows(windows);
let focusedWindow = global.display.get_focus_window();
if (focusedWindow) {
logDebug(`Focusing on window: ${focusedWindow.title}`);
for (let i = 0; i < windows.length; i++) {
if (windows[i] !== focusedWindow) {
logDebug(`Minimizing window: ${windows[i].title}`);
windows[i].minimize();
}
}
}
}
// Resets the ignoredWindows array.
// This can be called when enabling or disabling the extension
// to ensure the state is clean.
function resetToggleStatus() {
ignoredWindows = [];
}
export default class extends Extension {
// Called when the extension is enabled.
// Sets up the keybinding to minimize windows except the focused one.
enable() {
resetToggleStatus();
Main.wm.addKeybinding(
'hide-unfocused-shortcut',
this.getSettings(),
Meta.KeyBindingFlags.NONE,
Shell.ActionMode.ALL,
() => {
if (Main.overview.visible) {
return; // Do nothing if the overview is open
}
minimizeWindowsExceptFocused();
}
);
}
// Called when the extension is disabled.
// Cleans up by resetting the ignored windows and removing the keybinding.
disable() {
resetToggleStatus();
Main.wm.removeKeybinding('hide-unfocused-shortcut');
}
}