summaryrefslogtreecommitdiffstats
path: root/extensions/45/vertical-workspaces/lib/swipeTracker.js
blob: 354f1e0a1eeda136bceca0a4bbf2861d3d5e8ebd (plain)
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
/**
 * V-Shell (Vertical Workspaces)
 * swipeTracker.js
 *
 * @author     GdH <G-dH@github.com>
 * @copyright  2022 - 2023
 * @license    GPL-3.0
 *
 */

'use strict';

import Clutter from 'gi://Clutter';
import GObject from 'gi://GObject';

import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as SwipeTracker from 'resource:///org/gnome/shell/ui/swipeTracker.js';

let Me;
let opt;

export const SwipeTrackerModule = class {
    constructor(me) {
        Me = me;
        opt = Me.opt;

        this._firstActivation = true;
        this.moduleEnabled = false;
    }

    cleanGlobals() {
        Me = null;
        opt = null;
    }

    update(reset) {
        this.moduleEnabled = opt.get('swipeTrackerModule');
        const conflict = false;

        reset = reset || !this.moduleEnabled || conflict;

        // don't touch the original code if module disabled
        if (reset && !this._firstActivation) {
            this._disableModule();
        } else if (!reset) {
            this._firstActivation = false;
            this._activateModule();
        }
        if (reset && this._firstActivation)
            console.debug('  SwipeTrackerModule - Keeping untouched');
    }

    _activateModule() {
        if (opt.ORIENTATION) { // 1-VERTICAL, 0-HORIZONTAL
            this._setVertical();
        } else {
            this._setHorizontal();
        }
        console.debug('  SwipeTrackerModule - Activated');
    }

    _disableModule() {
        this._setHorizontal();

        console.debug('  SwipeTrackerModule - Disabled');
    }

    _setVertical() {
        // reverse swipe gestures for enter/leave overview and ws switching
        Main.overview._swipeTracker.orientation = Clutter.Orientation.HORIZONTAL;
        Main.wm._workspaceAnimation._swipeTracker.orientation = Clutter.Orientation.VERTICAL;
        // overview's updateGesture() function should reflect ws tmb position to match appGrid/ws animation direction
        // function in connection cannot be overridden in prototype of its class because connected is actually another copy of the original function
        if (!this._originalGestureUpdateId) {
            this._originalGestureUpdateId = GObject.signal_handler_find(Main.overview._swipeTracker._touchpadGesture, { signalId: 'update' });
            Main.overview._swipeTracker._touchpadGesture.block_signal_handler(this._originalGestureUpdateId);
            Main.overview._swipeTracker._updateGesture = SwipeTrackerVertical._updateGesture;
            this._vwGestureUpdateId = Main.overview._swipeTracker._touchpadGesture.connect('update', SwipeTrackerVertical._updateGesture.bind(Main.overview._swipeTracker));
        }
    }

    _setHorizontal() {
        // original swipeTrackers' orientation and updateGesture function
        Main.overview._swipeTracker.orientation = Clutter.Orientation.VERTICAL;
        Main.wm._workspaceAnimation._swipeTracker.orientation = Clutter.Orientation.HORIZONTAL;
        Main.overview._swipeTracker._updateGesture = SwipeTracker.SwipeTracker.prototype._updateGesture;
        if (this._vwGestureUpdateId) {
            Main.overview._swipeTracker._touchpadGesture.disconnect(this._vwGestureUpdateId);
            this._vwGestureUpdateId = 0;
        }
        if (this._originalGestureUpdateId) {
            Main.overview._swipeTracker._touchpadGesture.unblock_signal_handler(this._originalGestureUpdateId);
            this._originalGestureUpdateId = 0;
        }
    }
};

const SwipeTrackerVertical = {
    _updateGesture(gesture, time, delta, distance) {
        if (this._state !== 1) // State.SCROLLING)
            return;

        if ((this._allowedModes & Main.actionMode) === 0 || !this.enabled) {
            this._interrupt();
            return;
        }

        if (opt.WS_TMB_RIGHT)
            delta = -delta;
        this._progress += delta / distance;
        this._history.append(time, delta);

        this._progress = Math.clamp(this._progress, ...this._getBounds(this._initialProgress));
        this.emit('update', this._progress);
    },
};