From 3f106cb55953a30eb787e1ca24fe724f000f511e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Hedin=20Br=C3=B8nner?= Date: Tue, 24 Oct 2017 21:23:41 +0200 Subject: [PATCH] Make spaces an object which can handle eg. adding/removing spaces --- extension.js | 7 +--- tiling.js | 100 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 62 insertions(+), 45 deletions(-) diff --git a/extension.js b/extension.js index 61ea299..1dcb42d 100644 --- a/extension.js +++ b/extension.js @@ -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); } diff --git a/tiling.js b/tiling.js index 7a88a7a..7dffeb2 100644 --- a/tiling.js +++ b/tiling.js @@ -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;