summaryrefslogtreecommitdiffstats
path: root/extensions/44/vertical-workspaces/lib/workspaceAnimation.js
blob: 07008c609ff0228fdcd68a5ecdcdb186d584bf48 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * V-Shell (Vertical Workspaces)
 * workspacesAnimation.js
 *
 * @author     GdH <G-dH@github.com>
 * @copyright  2022 - 2023
 * @license    GPL-3.0
 *
 */

'use strict';
const Main = imports.ui.main;
const WorkspaceSwitcherPopup = imports.ui.workspaceSwitcherPopup;
const WorkspaceAnimation = imports.ui.workspaceAnimation;
const Me = imports.misc.extensionUtils.getCurrentExtension();
const _Util = Me.imports.lib.util;

// first reference to constant defined using const in other module returns undefined, the MonitorGroup const will remain empty and unused
let MonitorGroupDummy = WorkspaceAnimation.MonitorGroup;
MonitorGroupDummy = null;

let _origBaseDistance;
let _wsAnimationSwipeBeginId;
let _wsAnimationSwipeUpdateId;
let _wsAnimationSwipeEndId;

let _overrides;
let opt;
let _firstRun = true;

function update(reset = false) {
    opt = Me.imports.lib.settings.opt;
    const moduleEnabled = opt.get('workspaceAnimationModule', true);
    reset = reset || !moduleEnabled;

    // don't even touch this module if disabled
    if (_firstRun && reset)
        return;

    _firstRun = false;

    if (_overrides)
        _overrides.removeAll();

    if (reset || !moduleEnabled) {
        _connectWsAnimationSwipeTracker(true);
        _overrideMonitorGroupProperty(true);
        _overrides = null;
        opt = null;
        return;
    }

    if (opt.STATIC_WS_SWITCHER_BG) {
        _overrides = new _Util.Overrides();
        _overrideMonitorGroupProperty();
        _overrides.addOverride('WorkspaceAnimationMonitorGroup', WorkspaceAnimation.MonitorGroup.prototype, MonitorGroup);
    }

    _connectWsAnimationSwipeTracker();
}

// remove spacing between workspaces during transition to remove flashing wallpaper between workspaces with maximized windows
function _overrideMonitorGroupProperty(reset = false) {
    if (!_origBaseDistance)
        _origBaseDistance = Object.getOwnPropertyDescriptor(WorkspaceAnimation.MonitorGroup.prototype, 'baseDistance').get;

    let getter;
    if (reset) {
        if (_origBaseDistance)
            getter = { get: _origBaseDistance };
    } else {
        getter = {
            get() {
                // const spacing = 100 * imports.gi.St.ThemeContext.get_for_stage(global.stage).scale_factor;
                const spacing = 0;
                if (global.workspace_manager.layout_rows === -1)
                    return this._monitor.height + spacing + (opt.PANEL_MODE ? Main.panel.height : 0); // compensation for hidden panel
                else
                    return this._monitor.width + spacing;
            },
        };
    }

    if (getter)
        Object.defineProperty(WorkspaceAnimation.MonitorGroup.prototype, 'baseDistance', getter);
}

const MonitorGroup = {
    // injection to _init()
    after__init() {
        // we have two options to implement static bg feature
        // one is adding background to monitorGroup
        // but this one has disadvantage - sticky windows will be always on top of animated windows
        // which is bad for conky, for example, that window should be always below
        /* this._bgManager = new Background.BackgroundManager({
            container: this,
            monitorIndex: this._monitor.index,
            controlPosition: false,
        });*/

        // the second option is to make background of the monitorGroup transparent so the real desktop content will stay visible,
        // hide windows that should be animated and keep only sticky windows
        // we can keep certain sticky windows bellow and also extensions like DING (icons on desktop) will stay visible
        this.set_style('background-color: transparent;');
        // stickyGroup holds the Always on Visible Workspace windows to keep them static and above other windows during animation
        const stickyGroup = this.get_children()[1];
        stickyGroup._windowRecords.forEach(r => {
            const metaWin = r.windowActor.metaWindow;
            // conky is sticky but should never get above other windows during ws animation
            // so we hide it from the overlay group, we will see the original if not covered by other windows
            if (metaWin.wm_class === 'conky')
                r.clone.opacity = 0;
        });
        this._hiddenWindows = [];
        // remove (hide) background wallpaper from the animation, we will see the original one
        this._workspaceGroups.forEach(w => {
            w._background.opacity = 0;
        });
        // hide (scale to 0) all non-sticky windows, their clones will be animated
        global.get_window_actors().forEach(actor => {
            const metaWin = actor.metaWindow;
            if (metaWin?.get_monitor() === this._monitor.index && !(metaWin?.wm_class === 'conky' && metaWin?.is_on_all_workspaces())) { //* && !w.is_on_all_workspaces()*/) {
                // hide original window. we cannot use opacity since it also affects clones.
                // scaling them to 0 works well
                actor.scale_x = 0;
                this._hiddenWindows.push(actor);
            }
        });

        // restore all hidden windows at the end of animation
        // todo - actors removed during transition need to be removed from the list  to avoid access to destroyed actor
        this.connect('destroy', () => {
            this._hiddenWindows.forEach(actor => {
                actor.scale_x = 1;
            });
        });
    },
};

function _connectWsAnimationSwipeTracker(reset = false) {
    if (reset) {
        if (_wsAnimationSwipeBeginId) {
            Main.wm._workspaceAnimation._swipeTracker.disconnect(_wsAnimationSwipeBeginId);
            _wsAnimationSwipeBeginId = 0;
        }
        if (_wsAnimationSwipeEndId) {
            Main.wm._workspaceAnimation._swipeTracker.disconnect(_wsAnimationSwipeEndId);
            _wsAnimationSwipeEndId = 0;
        }
    } else if (!_wsAnimationSwipeBeginId) {
        // display ws switcher popup when gesture begins and connect progress
        _wsAnimationSwipeBeginId = Main.wm._workspaceAnimation._swipeTracker.connect('begin', () => _connectWsAnimationProgress(true));
        // we want to be sure that popup with the final ws index show up when gesture ends
        _wsAnimationSwipeEndId = Main.wm._workspaceAnimation._swipeTracker.connect('end', (tracker, duration, endProgress) => _connectWsAnimationProgress(false, endProgress));
    }
}

function _connectWsAnimationProgress(connect, endProgress = null) {
    if (Main.overview.visible)
        return;

    if (connect && !_wsAnimationSwipeUpdateId) {
        _wsAnimationSwipeUpdateId = Main.wm._workspaceAnimation._swipeTracker.connect('update', (tracker, progress) => _showWsSwitcherPopup(progress));
    } else if (!connect && _wsAnimationSwipeUpdateId) {
        Main.wm._workspaceAnimation._swipeTracker.disconnect(_wsAnimationSwipeUpdateId);
        _wsAnimationSwipeUpdateId = 0;
        _showWsSwitcherPopup(Math.round(endProgress));
    }
}

function _showWsSwitcherPopup(progress) {
    if (Main.overview.visible)
        return;

    const wsIndex = Math.round(progress);
    if (Main.wm._workspaceSwitcherPopup === null) {
        Main.wm._workspaceSwitcherPopup = new WorkspaceSwitcherPopup.WorkspaceSwitcherPopup();
        Main.wm._workspaceSwitcherPopup.connect('destroy', () => {
            Main.wm._workspaceSwitcherPopup = null;
        });
    }

    Main.wm._workspaceSwitcherPopup.display(wsIndex);
}