[gnome-shell-extensions/wip/fmuellner/fix-auto-move: 3/4] auto-move-windows: Cache app configuration in map



commit d56d9637771a3ded3528240f8a0f54019195d076
Author: Florian Müllner <fmuellner gnome org>
Date:   Thu Dec 21 21:13:13 2017 +0100

    auto-move-windows: Cache app configuration in map
    
    While reading the configuration, processing it and iterating over the
    configured apps to find a match isn't terribly expensive, but caching
    the configuration in a map does save a bit of work, and makes for much
    cleaner code in findAndMove().
    
    https://gitlab.gnome.org/GNOME/gnome-shell-extensions/issues/33

 extensions/auto-move-windows/extension.js | 45 +++++++++++++++++--------------
 1 file changed, 25 insertions(+), 20 deletions(-)
---
diff --git a/extensions/auto-move-windows/extension.js b/extensions/auto-move-windows/extension.js
index 8ec9714..e193b25 100644
--- a/extensions/auto-move-windows/extension.js
+++ b/extensions/auto-move-windows/extension.js
@@ -14,18 +14,27 @@ const ExtensionUtils = imports.misc.extensionUtils;
 const Me = ExtensionUtils.getCurrentExtension();
 const Convenience = Me.imports.convenience;
 
-const SETTINGS_KEY = 'application-list';
-
-let settings;
-
 class WindowMover {
     constructor() {
-        this._settings = settings;
+        this._settings = Convenience.getSettings();
         this._windowTracker = Shell.WindowTracker.get_default();
+        this._appConfigs = new Map();
 
         let display = global.screen.get_display();
         // Connect after so the handler from ShellWindowTracker has already run
         this._windowCreatedId = display.connect_after('window-created', this._findAndMove.bind(this));
+
+        this._settings.connect('changed', this._updateAppConfigs.bind(this));
+        this._updateAppConfigs();
+    }
+
+    _updateAppConfigs() {
+        this._appConfigs.clear();
+
+        this._settings.get_strv('application-list').forEach(v => {
+            let [appId, num] = v.split(':');
+            this._appConfigs.set(appId, parseInt(num) - 1);
+        });
     }
 
     destroy() {
@@ -33,6 +42,11 @@ class WindowMover {
             global.screen.get_display().disconnect(this._windowCreatedId);
             this._windowCreatedId = 0;
         }
+
+        if (this._settings) {
+            this._settings.run_dispose();
+            this._settings = null;
+        }
     }
 
     _ensureAtLeastWorkspaces(num, window) {
@@ -46,8 +60,6 @@ class WindowMover {
         if (window.skip_taskbar)
             return;
 
-        let spaces = this._settings.get_strv(SETTINGS_KEY);
-
         let app = this._windowTracker.get_window_app(window);
         if (!app) {
             if (!noRecurse) {
@@ -60,18 +72,12 @@ class WindowMover {
                 log ('Cannot find application for window');
             return;
         }
-        let app_id = app.get_id();
-        for (let i = 0; i < spaces.length; i++) {
-            let appsToSpace = spaces[i].split(":");
-            // Match application id
-            if (appsToSpace[0] == app_id) {
-                let workspaceNum = parseInt(appsToSpace[1]) - 1;
-
-                if (workspaceNum >= global.screen.n_workspaces)
-                    this._ensureAtLeastWorkspaces(workspace_num, window);
-
-                window.change_workspace_by_index(workspaceNum, false);
-            }
+        let workspaceNum = this._appConfigs.get(app.get_id());
+        if (workspaceNum !== undefined) {
+            if (workspaceNum >= global.screen.n_workspaces)
+                this._ensureAtLeastWorkspaces(workspaceNum, window);
+
+            window.change_workspace_by_index(workspaceNum, false);
         }
     }
 };
@@ -81,7 +87,6 @@ let winMover;
 
 function init() {
     Convenience.initTranslations();
-    settings = Convenience.getSettings();
 }
 
 function myCheckWorkspaces() {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]