forked from negesti/gnome-shell-extensions-negesti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveWorkspace.js
192 lines (157 loc) · 5.73 KB
/
moveWorkspace.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const Lang = imports.lang;
const Meta = imports.gi.Meta;
const Main = imports.ui.main;
const Shell = imports.gi.Shell;
/**
* Based on code by Florain Müller and the auto-move-window-extension.
* https://git.gnome.org/browse/gnome-shell-extensions/
*
*
*/
function MoveWorkspace(utils) {
this._init(utils);
}
MoveWorkspace.prototype = {
_windowTracker: null,
_prevCheckWorkspaces: false,
_windowCreateId: 0,
_init: function(utils) {
this._utils = utils;
this._prevCheckWorkspaces = Main.wm._workspaceTracker._checkWorkspaces;
this._settingObject = this._utils.getSettingsObject();
this._settingsChangedListener = {
name: this._utils.ENABLE_MOVE_WORKSPACE,
fn: Lang.bind(this, function() {
let enabled = this._utils.getBoolean(this._utils.ENABLE_MOVE_WORKSPACE, false);
if (enabled) {
this.enable();
} else {
this.disable();
}
})
};
this._settingsChangedListener.handlerId = this._settingObject.connect(
'changed::' + this._utils.ENABLE_MOVE_WORKSPACE,
this._settingsChangedListener.fn
);
let enabled = this._utils.getBoolean(this._utils.ENABLE_MOVE_WORKSPACE, false);
if (enabled) {
this.enable();
}
},
enable : function() {
this._windowTracker = Shell.WindowTracker.get_default();
Main.wm._workspaceTracker._checkWorkspaces = this._myCheckWorkspaces;
// Connect after so the handler from ShellWindowTracker has already run
let display = global.display;
this._windowCreatedId = display.connect_after('window-created', Lang.bind(this, this._findAndMove));
},
destroy: function() {
this.disable();
},
disable: function() {
if (this._prevCheckWorkspaces) {
Main.wm._workspaceTracker._checkWorkspaces = this._prevCheckWorkspaces;
}
if (this._windowCreatedId) {
global.display.disconnect(this._windowCreatedId);
this._windowCreatedId = 0;
}
},
_myCheckWorkspaces: function() {
let i;
let emptyWorkspaces = new Array(this._workspaces.length);
if (!Meta.prefs_get_dynamic_workspaces()) {
this._checkWorkspacesId = 0;
return false;
}
for (i = 0; i < this._workspaces.length; i++) {
let lastRemoved = this._workspaces[i]._lastRemovedWindow;
if (this._workspaces[i]._keepAliveId || (lastRemoved && (
lastRemoved.get_window_type() == Meta.WindowType.SPLASHSCREEN ||
lastRemoved.get_window_type() == Meta.WindowType.DIALOG ||
lastRemoved.get_window_type() == Meta.WindowType.MODAL_DIALOG))) {
emptyWorkspaces[i] = false;
} else {
emptyWorkspaces[i] = true;
}
}
let sequences = Shell.WindowTracker.get_default().get_startup_sequences();
for (i = 0; i < sequences.length; i++) {
let index = sequences[i].get_workspace();
if (index >= 0 && index <= Utils.getWorkspaceManager().n_workspaces) {
emptyWorkspaces[index] = false;
}
}
let windows = global.get_window_actors();
for (i = 0; i < windows.length; i++) {
let winActor = windows[i];
let win = winActor.meta_window;
if (win.is_on_all_workspaces()) {
continue;
}
let workspaceIndex = win.get_workspace().index();
emptyWorkspaces[workspaceIndex] = false;
}
// If we don't have an empty workspace at the end, add one
if (!emptyWorkspaces[emptyWorkspaces.length -1]) {
Utils.getWorkspaceManager().append_new_workspace(false, global.get_current_time());
emptyWorkspaces.push(false);
}
let activeWorkspaceIndex = Utils.getWorkspaceManager().get_active_workspace_index();
emptyWorkspaces[activeWorkspaceIndex] = false;
// Delete other empty workspaces; do it from the end to avoid index changes
for (i = emptyWorkspaces.length - 2; i >= 0; i--) {
if (emptyWorkspaces[i]) {
Utils.getWorkspaceManager().remove_workspace(this._workspaces[i], global.get_current_time());
} else {
break;
}
}
this._checkWorkspacesId = 0;
return false;
},
_findAndMove: function(display, window, noRecurse) {
if (window.skip_taskbar)
return;
let app = this._windowTracker.get_window_app(window);
if (!app) {
if (!noRecurse) {
// window is not tracked yet
Mainloop.idle_add(Lang.bind(this, function() {
this._findAndMove(display, window, true);
return false;
}));
} else
global.log ('Cannot find application for window');
return;
}
app = window.get_wm_class();
// move the window if a location is configured and autoMove is set to true
let appPath = "locations." + app;
if (this._utils.getParameter(appPath, false)) {
if (this._utils.getBoolean(appPath + ".moveWorkspace", false)) {
let targetWorkspace = this._utils.getNumber(appPath + ".targetWorkspace", 1) - 1;
if (targetWorkspace >= Utils.getWorkspaceManager().n_workspaces) {
this._ensureAtLeastWorkspaces(targetWorkspace, window);
}
// wait until we focus the new window e.g. it really exists and then move it
this.moveWorkspaceListener = window.connect("focus",
Lang.bind(this, function() {
if (this.moveWorkspaceListener != null) {
window.disconnect(this.moveWorkspaceListener);
this.moveWorkspaceListener = null;
}
window.change_workspace_by_index(targetWorkspace, false);
})
);
}
}
},
_ensureAtLeastWorkspaces: function(num, window) {
for (let j = Utils.getWorkspaceManager().n_workspaces; j <= num; j++) {
window.change_workspace_by_index(j-1, false);
Utils.getWorkspaceManager().append_new_workspace(false, 0);
}
}
};