[gnome-shell/wip/exalm/gestures2: 7/9] workspaceAnimation: Use a workspace strip



commit b400167f469c0b6ae33c036c2dfce086f447f464
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Sat Jun 20 22:01:25 2020 +0500

    workspaceAnimation: Use a workspace strip
    
    Currently, the workspace swipe transition only has one workspace in each
    direction. This works until you try to do multiple swipes in quick
    succession. The second swipe would continue the existing transition, which
    only has 2 or 3 workspaces in it, and will hit a wall.
    
    To prevent this, take all workspaces and arrange them into a column or row,
    depending on the layout, and use that as a transition.
    
    For the transition that happens when focusing a window on another workspace
    (for example, via Alt+Tab), still use only two workspaces instead of all of
    them.
    
    Since we don't support layouts other than single rows/columns anymore,
    diagonal transitions aren't supported anymore, and will be shown as
    horizontal or vertical instead.
    
    Fixes https://gitlab.gnome.org/GNOME/gnome-shell/-/issues/2612
    
    https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1326

 js/ui/workspaceAnimation.js | 277 +++++++++++++++++++-------------------------
 1 file changed, 117 insertions(+), 160 deletions(-)
---
diff --git a/js/ui/workspaceAnimation.js b/js/ui/workspaceAnimation.js
index d762c3317f..496bbd58db 100644
--- a/js/ui/workspaceAnimation.js
+++ b/js/ui/workspaceAnimation.js
@@ -101,7 +101,7 @@ var WorkspaceAnimationController = class {
         Main.overview.connect('showing', () => {
             if (this._switchData) {
                 if (this._switchData.gestureActivated)
-                    this._switchWorkspaceStop();
+                    this._finishWorkspaceSwitch(this._switchData);
                 this._swipeTracker.enabled = false;
             }
         });
@@ -117,91 +117,79 @@ var WorkspaceAnimationController = class {
         this._swipeTracker = swipeTracker;
     }
 
-    _getPositionForDirection(direction, fromWs, toWs) {
-        let xDest = 0, yDest = 0;
-
-        const oldWsIsFullscreen = fromWs.list_windows().some(w => w.is_fullscreen());
-        const newWsIsFullscreen = toWs.list_windows().some(w => w.is_fullscreen());
-
-        // We have to shift windows up or down by the height of the panel to prevent having a
-        // visible gap between the windows while switching workspaces. Since fullscreen windows
-        // hide the panel, they don't need to be shifted up or down.
-        const shiftHeight = Main.panel.height;
-
-        if (direction === Meta.MotionDirection.UP ||
-            direction === Meta.MotionDirection.UP_LEFT ||
-            direction === Meta.MotionDirection.UP_RIGHT)
-            yDest = -global.screen_height + (oldWsIsFullscreen ? 0 : shiftHeight);
-        else if (direction === Meta.MotionDirection.DOWN ||
-            direction === Meta.MotionDirection.DOWN_LEFT ||
-            direction === Meta.MotionDirection.DOWN_RIGHT)
-            yDest = global.screen_height - (newWsIsFullscreen ? 0 : shiftHeight);
-
-        if (direction === Meta.MotionDirection.LEFT ||
-            direction === Meta.MotionDirection.UP_LEFT ||
-            direction === Meta.MotionDirection.DOWN_LEFT)
-            xDest = -global.screen_width;
-        else if (direction === Meta.MotionDirection.RIGHT ||
-                 direction === Meta.MotionDirection.UP_RIGHT ||
-                 direction === Meta.MotionDirection.DOWN_RIGHT)
-            xDest = global.screen_width;
-
-        return [xDest, yDest];
-    }
-
-    _prepareWorkspaceSwitch(from, to, direction) {
+    _prepareWorkspaceSwitch(workspaces) {
         if (this._switchData)
             return;
 
         const workspaceManager = global.workspace_manager;
-        const curWs = workspaceManager.get_workspace_by_index(from);
+        const vertical = workspaceManager.layout_rows === -1;
+        const nWorkspaces = workspaceManager.get_n_workspaces();
+        const activeWorkspaceIndex = workspaceManager.get_active_workspace_index();
 
         const switchData = {};
 
         this._switchData = switchData;
-        switchData.curGroup = new WorkspaceGroup(curWs, this.movingWindow);
         switchData.movingWindowBin = new Clutter.Actor();
         switchData.movingWindow = null;
-        switchData.surroundings = {};
+        switchData.workspaces = [];
         switchData.gestureActivated = false;
         switchData.inProgress = false;
 
         switchData.container = new Clutter.Actor();
-        switchData.container.add_child(switchData.curGroup);
-
         switchData.background = new Meta.BackgroundGroup();
 
         Main.uiGroup.insert_child_above(switchData.background, global.window_group);
         Main.uiGroup.insert_child_above(switchData.container, switchData.background);
         Main.uiGroup.insert_child_above(switchData.movingWindowBin, switchData.container);
 
-        for (const dir of Object.values(Meta.MotionDirection)) {
-            let ws = null;
+        let x = 0;
+        let y = 0;
 
-            if (to < 0)
-                ws = curWs.get_neighbor(dir);
-            else if (dir === direction)
-                ws = workspaceManager.get_workspace_by_index(to);
+        if (!workspaces) {
+            workspaces = [];
 
-            if (ws === null || ws === curWs) {
-                switchData.surroundings[dir] = null;
-                continue;
+            for (let i = 0; i < nWorkspaces; i++)
+                workspaces.push(i);
+        }
+
+        for (const i of workspaces) {
+            const ws = workspaceManager.get_workspace_by_index(i);
+            const fullscreen = ws.list_windows().some(w => w.is_fullscreen());
+
+            if (y > 0 && vertical && !fullscreen) {
+                // We have to shift windows up or down by the height of the panel to prevent having a
+                // visible gap between the windows while switching workspaces. Since fullscreen windows
+                // hide the panel, they don't need to be shifted up or down.
+                y -= Main.panel.height;
             }
 
-            const [x, y] = this._getPositionForDirection(dir, curWs, ws);
             const info = {
-                index: ws.index(),
+                ws,
                 actor: new WorkspaceGroup(ws, this.movingWindow),
-                xDest: x,
-                yDest: y,
+                fullscreen,
+                x,
+                y,
             };
-            switchData.surroundings[dir] = info;
+
+            switchData.workspaces.push(info);
             switchData.container.add_child(info.actor);
             switchData.container.set_child_above_sibling(info.actor, null);
-
             info.actor.set_position(x, y);
+
+            if (vertical)
+                y += global.screen_height;
+            else if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL)
+                x -= global.screen_width;
+            else
+                x += global.screen_width;
         }
 
+        const activeInfo = this._findWorkspaceInfoByIndex(activeWorkspaceIndex);
+        if (vertical)
+            switchData.container.y = -activeInfo.y;
+        else
+            switchData.container.x = -activeInfo.x;
+
         if (this.movingWindow) {
             const windowActor = this.movingWindow.get_compositor_private();
 
@@ -243,25 +231,44 @@ var WorkspaceAnimationController = class {
     }
 
     animateSwitch(from, to, direction, onComplete) {
-        this._prepareWorkspaceSwitch(from, to, direction);
-        this._switchData.inProgress = true;
+        if (this._switchData)
+            this._switchData.container.remove_all_transitions();
 
-        const workspaceManager = global.workspace_manager;
-        const fromWs = workspaceManager.get_workspace_by_index(from);
-        const toWs = workspaceManager.get_workspace_by_index(to);
+        let workspaces = [];
+
+        switch (direction) {
+        case Meta.MotionDirection.UP:
+        case Meta.MotionDirection.LEFT:
+        case Meta.MotionDirection.UP_LEFT:
+        case Meta.MotionDirection.UP_RIGHT:
+            workspaces = [to, from];
+            break;
+
+        case Meta.MotionDirection.DOWN:
+        case Meta.MotionDirection.RIGHT:
+        case Meta.MotionDirection.DOWN_LEFT:
+        case Meta.MotionDirection.DOWN_RIGHT:
+            workspaces = [from, to];
+            break;
+        }
+
+        if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL &&
+            direction !== Meta.MotionDirection.UP &&
+            direction !== Meta.MotionDirection.DOWN)
+            workspaces.reverse();
 
-        let [xDest, yDest] = this._getPositionForDirection(direction, fromWs, toWs);
+        this._prepareWorkspaceSwitch(workspaces);
+        this._switchData.inProgress = true;
+
+        const fromWs = this._findWorkspaceInfoByIndex(from);
+        const toWs = this._findWorkspaceInfoByIndex(to);
 
-        /* @direction is the direction that the "camera" moves, so the
-         * screen contents have to move one screen's worth in the
-         * opposite direction.
-         */
-        xDest = -xDest;
-        yDest = -yDest;
+        this._switchData.container.x = -fromWs.x;
+        this._switchData.container.y = -fromWs.y;
 
         this._switchData.container.ease({
-            x: xDest,
-            y: yDest,
+            x: -toWs.x,
+            y: -toWs.y,
             duration: WINDOW_ANIMATION_TIME,
             mode: Clutter.AnimationMode.EASE_OUT_CUBIC,
             onComplete: () => {
@@ -271,51 +278,27 @@ var WorkspaceAnimationController = class {
         });
     }
 
-    _directionForProgress(progress) {
-        if (global.workspace_manager.layout_rows === -1) {
-            return progress > 0
-                ? Meta.MotionDirection.DOWN
-                : Meta.MotionDirection.UP;
-        } else if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL) {
-            return progress > 0
-                ? Meta.MotionDirection.LEFT
-                : Meta.MotionDirection.RIGHT;
-        } else {
-            return progress > 0
-                ? Meta.MotionDirection.RIGHT
-                : Meta.MotionDirection.LEFT;
-        }
-    }
-
-    _getProgressRange() {
-        if (!this._switchData)
-            return [0, 0];
-
-        let lower = 0;
-        let upper = 0;
-
-        const horiz = global.workspace_manager.layout_rows !== -1;
-        let baseDistance;
-        if (horiz)
-            baseDistance = global.screen_width;
+    _getProgressForWorkspace(workspaceInfo) {
+        if (global.workspace_manager.layout_rows === -1)
+            return workspaceInfo.y / global.screen_height;
+        else if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL)
+            return -workspaceInfo.x / global.screen_width;
         else
-            baseDistance = global.screen_height;
-
-        let direction = this._directionForProgress(-1);
-        let info = this._switchData.surroundings[direction];
-        if (info !== null) {
-            const distance = horiz ? info.xDest : info.yDest;
-            lower = -Math.abs(distance) / baseDistance;
-        }
+            return workspaceInfo.x / global.screen_width;
+    }
 
-        direction = this._directionForProgress(1);
-        info = this._switchData.surroundings[direction];
-        if (info !== null) {
-            const distance = horiz ? info.xDest : info.yDest;
-            upper = Math.abs(distance) / baseDistance;
-        }
+    _findWorkspaceInfoByIndex(index) {
+        const workspace = global.workspace_manager.get_workspace_by_index(index);
+        return this._switchData.workspaces.find(ws => ws.ws == workspace);
+    }
 
-        return [lower, upper];
+    _findClosestWorkspace(progress) {
+        const distances = this._switchData.workspaces.map(ws => {
+            const workspaceProgress = this._getProgressForWorkspace(ws);
+            return Math.abs(workspaceProgress - progress);
+        });
+        const index = distances.indexOf(Math.min(...distances));
+        return this._findWorkspaceInfoByIndex(index);
     }
 
     _switchWorkspaceBegin(tracker, monitor) {
@@ -329,15 +312,10 @@ var WorkspaceAnimationController = class {
             ? Clutter.Orientation.HORIZONTAL
             : Clutter.Orientation.VERTICAL;
 
-        const activeWorkspace = workspaceManager.get_active_workspace();
-
-        let baseDistance;
-        if (horiz)
-            baseDistance = global.screen_width;
-        else
-            baseDistance = global.screen_height;
+        const baseDistance = horiz ? global.screen_width : global.screen_height;
 
         let progress;
+        let cancelProgress;
         if (this._switchData && this._switchData.gestureActivated) {
             this._switchData.container.remove_all_transitions();
             if (!horiz)
@@ -346,41 +324,36 @@ var WorkspaceAnimationController = class {
                 progress = this._switchData.container.x / baseDistance;
             else
                 progress = -this._switchData.container.x / baseDistance;
-        } else {
-            this._prepareWorkspaceSwitch(activeWorkspace.index(), -1);
-            progress = 0;
-        }
 
-        const points = [];
-        const [lower, upper] = this._getProgressRange();
+            const ws = this._findClosestWorkspace(progress);
+            cancelProgress = this._getProgressForWorkspace(ws);
+        } else {
+            this._prepareWorkspaceSwitch();
 
-        if (lower !== 0)
-            points.push(lower);
+            const activeIndex = workspaceManager.get_active_workspace_index();
+            const ws = this._findWorkspaceInfoByIndex(activeIndex);
 
-        points.push(0);
+            progress = cancelProgress = this._getProgressForWorkspace(ws);
+        }
 
-        if (upper !== 0)
-            points.push(upper);
+        const points = this._switchData.workspaces.map(this._getProgressForWorkspace);
 
-        tracker.confirmSwipe(baseDistance, points, progress, 0);
+        tracker.confirmSwipe(baseDistance, points, progress, cancelProgress);
     }
 
     _switchWorkspaceUpdate(tracker, progress) {
         if (!this._switchData)
             return;
 
-        const direction = this._directionForProgress(progress);
-        const info = this._switchData.surroundings[direction];
         let xPos = 0;
         let yPos = 0;
-        if (info) {
-            if (global.workspace_manager.layout_rows === -1)
-                yPos = -Math.round(progress * global.screen_height);
-            else if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL)
-                xPos = Math.round(progress * global.screen_width);
-            else
-                xPos = -Math.round(progress * global.screen_width);
-        }
+
+        if (global.workspace_manager.layout_rows === -1)
+            yPos = -Math.round(progress * global.screen_height);
+        else if (Clutter.get_default_text_direction() === Clutter.TextDirection.RTL)
+            xPos = Math.round(progress * global.screen_width);
+        else
+            xPos = -Math.round(progress * global.screen_width);
 
         this._switchData.container.set_position(xPos, yPos);
     }
@@ -389,40 +362,24 @@ var WorkspaceAnimationController = class {
         if (!this._switchData)
             return;
 
-        const workspaceManager = global.workspace_manager;
-        const activeWorkspace = workspaceManager.get_active_workspace();
-        let newWs = activeWorkspace;
-        let xDest = 0;
-        let yDest = 0;
-        if (endProgress !== 0) {
-            const direction = this._directionForProgress(endProgress);
-            newWs = activeWorkspace.get_neighbor(direction);
-            xDest = -this._switchData.surroundings[direction].xDest;
-            yDest = -this._switchData.surroundings[direction].yDest;
-        }
+        const newWs = this._findClosestWorkspace(endProgress);
 
         const switchData = this._switchData;
         switchData.gestureActivated = true;
 
         this._switchData.container.ease({
-            x: xDest,
-            y: yDest,
+            x: -newWs.x,
+            y: -newWs.y,
             duration,
             mode: Clutter.AnimationMode.EASE_OUT_CUBIC,
             onComplete: () => {
-                if (!newWs.active)
-                    newWs.activate(global.get_current_time());
+                if (!newWs.ws.active)
+                    newWs.ws.activate(global.get_current_time());
                 this._finishWorkspaceSwitch(switchData);
             },
         });
     }
 
-    _switchWorkspaceStop() {
-        this._switchData.container.x = 0;
-        this._switchData.container.y = 0;
-        this._finishWorkspaceSwitch(this._switchData);
-    }
-
     get gestureActive() {
         return this._switchData !== null && this._switchData.gestureActivated;
     }


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