Debian Codegraph @

extensions/auto-move-windows/extension.js

raw
1 // SPDX-FileCopyrightText: 2011 Giovanni Campagna <[email protected]>
2 // SPDX-FileCopyrightText: 2011 Alessandro Crismani <[email protected]>
3 // SPDX-FileCopyrightText: 2014 Florian Müllner <[email protected]>
4 //
5 // SPDX-License-Identifier: GPL-2.0-or-later
6
7 import Shell from 'gi://Shell';
8
9 import {Extension, InjectionManager} from 'resource:///org/gnome/shell/extensions/extension.js';
10 import * as Main from 'resource:///org/gnome/shell/ui/main.js';
11
12 class WindowMover {
13 constructor(settings) {
14 this._settings = settings;
15 this._appSystem = Shell.AppSystem.get_default();
16 this._appConfigs = new Map();
17 this._appData = new Map();
18
19 this._appSystem.connectObject('installed-changed',
20 () => this._updateAppData(), this);
21
22 this._settings.connectObject('changed',
23 this._updateAppConfigs.bind(this), this);
24 this._updateAppConfigs();
25 }
26
27 _updateAppConfigs() {
28 this._appConfigs.clear();
29
30 this._settings.get_strv('application-list').forEach(v => {
31 const [appId, num] = v.split(':');
32 this._appConfigs.set(appId, parseInt(num) - 1);
33 });
34
35 this._updateAppData();
36 }
37
38 _updateAppData() {
39 const ids = [...this._appConfigs.keys()];
40 const removedApps = [...this._appData.keys()]
41 .filter(a => !ids.includes(a.id));
42 removedApps.forEach(app => {
43 app.disconnectObject(this);
44 this._appData.delete(app);
45 });
46
47 const addedApps = ids
48 .map(id => this._appSystem.lookup_app(id))
49 .filter(app => app && !this._appData.has(app));
50 addedApps.forEach(app => {
51 app.connectObject('windows-changed',
52 this._appWindowsChanged.bind(this), this);
53 this._appData.set(app, {windows: app.get_windows()});
54 });
55 }
56
57 destroy() {
58 this._appSystem.disconnectObject(this);
59 this._appSystem = null;
60
61 this._settings.disconnectObject(this);
62 this._settings = null;
63
64 this._appConfigs.clear();
65 this._updateAppData();
66 }
67
68 _moveWindow(window, workspaceNum) {
69 if (window.skip_taskbar || window.is_on_all_workspaces())
70 return;
71
72 // ensure we have the required number of workspaces
73 const workspaceManager = global.workspace_manager;
74 for (let i = workspaceManager.n_workspaces; i <= workspaceNum; i++) {
75 window.change_workspace_by_index(i - 1, false);
76 workspaceManager.append_new_workspace(false, 0);
77 }
78
79 window.change_workspace_by_index(workspaceNum, false);
80 }
81
82 _appWindowsChanged(app) {
83 const data = this._appData.get(app);
84 const windows = app.get_windows();
85
86 // If get_compositor_private() returns non-NULL on a removed windows,
87 // the window still exists and is just moved to a different workspace
88 // or something; assume it'll be added back immediately, so keep it
89 // to avoid moving it again
90 windows.push(...data.windows.filter(w => {
91 return !windows.includes(w) && w.get_compositor_private() !== null;
92 }));
93
94 const workspaceNum = this._appConfigs.get(app.id);
95 windows.filter(w => !data.windows.includes(w)).forEach(window => {
96 this._moveWindow(window, workspaceNum);
97 });
98 data.windows = windows;
99 }
100 }
101
102 export default class AutoMoveExtension extends Extension {
103 enable() {
104 this._injectionManager = new InjectionManager();
105 this._injectionManager.overrideMethod(Main.wm._workspaceTracker, '_checkWorkspaces',
106 originalMethod => this._getCheckWorkspaceOverride(originalMethod));
107
108 this._windowMover = new WindowMover(this.getSettings());
109 }
110
111 disable() {
112 this._injectionManager.clear();
113 this._injectionManager = null;
114
115 this._windowMover.destroy();
116 this._windowMover = null;
117 }
118
119 _getCheckWorkspaceOverride(originalMethod) {
120 /* eslint-disable no-invalid-this */
121 return function () {
122 const keepAliveWorkspaces = [];
123 let foundNonEmpty = false;
124 for (let i = this._workspaces.length - 1; i >= 0; i--) {
125 if (!foundNonEmpty) {
126 foundNonEmpty = this._workspaces[i].list_windows().some(
127 w => !w.is_on_all_workspaces());
128 } else if (!this._workspaces[i]._keepAliveId) {
129 keepAliveWorkspaces.push(this._workspaces[i]);
130 }
131 }
132
133 // make sure the original method only removes empty workspaces at the end
134 keepAliveWorkspaces.forEach(ws => (ws._keepAliveId = 1));
135 originalMethod.call(this);
136 keepAliveWorkspaces.forEach(ws => delete ws._keepAliveId);
137
138 return false;
139 };
140 /* eslint-enable no-invalid-this */
141 }
142 }