Make spaces an object which can handle eg. adding/removing spaces

This commit is contained in:
Tor Hedin Brønner
2017-10-25 17:00:39 +02:00
parent 0af2f199d3
commit 3f106cb559
2 changed files with 62 additions and 45 deletions
+1 -6
View File
@@ -76,17 +76,12 @@ function enable() {
// windows are not accessible yet for instance.
isDuringGnomeShellStartup = Main.actionMode === Shell.ActionMode.NONE;
let updateWorkspacesId =
global.screen.connect('notify::n-workspaces',
dynamic_function_ref('workspacesChanged'));
global.display.connect('window-created', dynamic_function_ref('window_created'));
function initWorkspaces() {
// Hook up existing workspaces
for (let i=0; i < global.screen.n_workspaces; i++) {
let workspace = global.screen.get_workspace_by_index(i)
Tiling.spaces[i] = Space(workspace);
Tiling.spaces.addSpace(workspace);
debug("workspace", workspace)
add_all_from_workspace(workspace);
}
+61 -39
View File
@@ -103,6 +103,67 @@ Space = (workspace) => {
return space;
}
// Singleton spaces object, shouldn't be reused
var spaces = (function () {
let spaces = [];
spaces.nWorkspacesSignal =
global.screen.connect('notify::n-workspaces',
Lang.bind(spaces, dynamic_function_ref('workspacesChanged', spaces)));
spaces.windowCreatedSignal =
global.display.connect('window-created',
dynamic_function_ref('window_created'));
spaces.workspacesChanged = function () {
let nWorkspaces = global.screen.n_workspaces;
// Identifying destroyed workspaces is rather bothersome,
// as it will for example report having windows,
// but will crash when looking at the workspace index
// Gather all indexed workspaces for easy comparison
let workspaces = {};
for (let i=0; i < nWorkspaces; i++) {
let workspace = global.screen.get_workspace_by_index(i);
workspaces[workspace] = true;
let index = workspace.index();
if (this[index] === undefined) {
debug('workspace added', workspace);
this.addSpace(workspace, index);
}
}
this.filter(space => {
// find the workspaces that aren't indexed
return workspaces[space.workspace] !== true;
}).forEach(space => {
debug('workspace removed', space.workspace);
this.removeSpace(space);
});
};
spaces.addSpace = function(workspace, index) {
index = index || this.length;
this.splice(index, 0, Space(workspace));
};
spaces.removeSpace = function(space) {
let workspace = space.workspace;
workspace.disconnect(space.addSignal);
workspace.disconnect(space.removeSignal);
this.splice(this.indexOf(space), 1);
};
return spaces;
})();
window.spaces = spaces;
spaceOf = (meta_window) => {
return spaces[meta_window.get_workspace().workspace_index];
}
panelBox.connect('hide', () => {
let space = spaces[global.screen.get_active_workspace_index()];
if (space.selectedWindow.fullscreen) {
@@ -112,12 +173,6 @@ panelBox.connect('hide', () => {
}
});
const spaces = []
spaceOf = (meta_window) => {
return spaces[meta_window.get_workspace().workspace_index];
}
window.spaces = spaces;
focus = () => {
let meta_window = global.display.focus_window;
if (!meta_window)
@@ -676,39 +731,6 @@ add_all_from_workspace = (workspace) => {
}
}
workspacesChanged = function () {
let nWorkspaces = global.screen.n_workspaces;
if (spaces.length < nWorkspaces) {
debug('workspace added', nWorkspaces);
let index = nWorkspaces - 1;
let workspace = global.screen.get_workspace_by_index(index);
spaces[index] = Space(workspace);
} else {
debug('workspace removed', nWorkspaces);
// Identifying destroyed workspaces is rather bothersome,
// as it will for example report having windows,
// but will crash when looking at the workspace index
// Gather all indexed workspaces for easy comparison
let workspaces = {};
for (let i=0; i < nWorkspaces; i++) {
let workspace = global.screen.get_workspace_by_index(i);
workspaces[workspace] = true;
}
spaces.filter(space => {
// find the workspaces that aren't indexed
return workspaces[space.workspace] !== true;
}).forEach(space => {
let workspace = space.workspace;
workspace.disconnect(space.addSignal);
workspace.disconnect(space.removeSignal);
spaces.splice(spaces.indexOf(space), 1);
});
}
}
toggle_maximize_horizontally = (meta_window) => {
meta_window = meta_window || global.display.focus_window;