forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scratch.js
234 lines (200 loc) · 7.04 KB
/
scratch.js
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import Meta from 'gi://Meta';
import Mtk from 'gi://Mtk';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as WindowMenu from 'resource:///org/gnome/shell/ui/windowMenu.js';
import { Settings, Utils, Tiling, Topbar } from './imports.js';
import { Easer } from './utils.js';
let originalBuildMenu;
let float, scratchFrame; // symbols used for expando properties on metawindow
export function enable() {
originalBuildMenu = WindowMenu.WindowMenu.prototype._buildMenu;
float = Symbol();
scratchFrame = Symbol();
WindowMenu.WindowMenu.prototype._buildMenu =
function (window) {
let item;
item = this.addAction(_('Scratch'), () => {
toggle(window);
});
if (isScratchWindow(window))
item.setOrnament(PopupMenu.Ornament.CHECK);
originalBuildMenu.call(this, window);
};
}
export function disable() {
WindowMenu.WindowMenu.prototype._buildMenu = originalBuildMenu;
originalBuildMenu = null;
float = null;
scratchFrame = null;
}
/**
Tween window to "frame-coordinate" (targetX, targetY).
The frame is moved once the tween is done.
The actual window actor (not clone) is tweened to ensure it's on top of the
other windows/clones (clones if the space animates)
*/
export function easeScratch(metaWindow, targetX, targetY, params = {}) {
const complete = params?.onComplete ?? function() {};
const f = metaWindow.get_frame_rect();
const b = metaWindow.get_buffer_rect();
const dx = f.x - b.x;
const dy = f.y - b.y;
Easer.addEase(metaWindow.get_compositor_private(), {
x: targetX - dx,
y: targetY - dy,
time: Settings.prefs.animation_time,
onComplete: () => {
metaWindow.move_frame(true, targetX, targetY);
complete();
},
});
}
export function makeScratch(metaWindow) {
let fromNonScratch = !metaWindow[float];
let fromTiling = false;
// Relevant when called while navigating. Use the position the user actually sees.
let windowPositionSeen;
if (fromNonScratch) {
// Figure out some stuff before the window is removed from the tiling
let space = Tiling.spaces.spaceOfWindow(metaWindow);
fromTiling = space.indexOf(metaWindow) > -1;
if (fromTiling) {
windowPositionSeen = metaWindow.clone
.get_transformed_position()
.map(Math.round);
}
}
metaWindow[float] = true;
metaWindow.make_above();
metaWindow.stick(); // NB! Removes the window from the tiling (synchronously)
if (!metaWindow.minimized)
Tiling.showWindow(metaWindow);
if (fromTiling) {
let f = metaWindow.get_frame_rect();
let targetFrame = null;
if (metaWindow[scratchFrame]) {
let sf = metaWindow[scratchFrame];
if (Utils.monitorOfPoint(sf.x, sf.y) === Tiling.focusMonitor()) {
targetFrame = sf;
}
}
if (!targetFrame) {
// Default to moving the window slightly down and reducing the height
let vDisplacement = 30;
let [x, y] = windowPositionSeen; // The window could be non-placable so can't use frame
targetFrame = new Mtk.Rectangle({
x, y: y + vDisplacement,
width: f.width,
height: Math.min(f.height - vDisplacement, Math.floor(f.height * 0.9)),
});
}
if (!metaWindow.minimized) {
metaWindow.move_resize_frame(true, f.x, f.y,
targetFrame.width, targetFrame.height);
easeScratch(
metaWindow,
targetFrame.x,
targetFrame.y,
{
onComplete: () => {
delete metaWindow[scratchFrame];
Main.activateWindow(metaWindow);
},
});
} else {
// Can't restore the scratch geometry immediately since it distort the minimize animation
// ASSUMPTION: minimize animation is not disabled and not already done
let actor = metaWindow.get_compositor_private();
let signal = actor.connect('effects-completed', () => {
metaWindow.move_resize_frame(true, targetFrame.x, targetFrame.y,
targetFrame.width, targetFrame.height);
actor.disconnect(signal);
});
}
}
Tiling.focusMonitor()?.clickOverlay?.hide();
}
export function unmakeScratch(metaWindow) {
if (!metaWindow[scratchFrame])
metaWindow[scratchFrame] = metaWindow.get_frame_rect();
metaWindow[float] = false;
metaWindow.unmake_above();
metaWindow.unstick();
}
export function toggle(metaWindow) {
if (isScratchWindow(metaWindow)) {
unmakeScratch(metaWindow);
} else {
makeScratch(metaWindow);
if (metaWindow.has_focus) {
let space = Tiling.spaces.activeSpace;
space.setSelectionInactive();
}
}
}
export function isScratchWindow(metaWindow) {
return metaWindow && metaWindow[float];
}
/** Return scratch windows in MRU order */
export function getScratchWindows() {
return global.display.get_tab_list(Meta.TabList.NORMAL, null)
.filter(isScratchWindow);
}
export function isScratchActive() {
return getScratchWindows().some(metaWindow => !metaWindow.minimized);
}
export function toggleScratch() {
if (isScratchActive())
hide();
else
show();
}
export function toggleScratchWindow() {
let focus = global.display.focus_window;
if (isScratchWindow(focus))
hide();
else
show(true);
}
export function show(top) {
let windows = getScratchWindows();
if (windows.length === 0) {
return;
}
if (top)
windows = windows.slice(0, 1);
Topbar.fixTopBar();
windows.slice().reverse()
.map(function(meta_window) {
meta_window.unminimize();
meta_window.make_above();
meta_window.get_compositor_private().show();
});
windows[0].activate(global.get_current_time());
let monitor = Tiling.focusMonitor();
monitor.clickOverlay?.hide();
}
export function hide() {
let windows = getScratchWindows();
windows.map(function(meta_window) {
meta_window.minimize();
});
}
export function animateWindows() {
let ws = getScratchWindows().filter(w => !w.minimized);
ws = global.display.sort_windows_by_stacking(ws);
for (let w of ws) {
// let parent = w.clone.get_parent();
// parent && parent.remove_child(w.clone);
Utils.actor_remove_parent(w.clone);
Main.uiGroup.insert_child_above(w.clone, global.window_group);
let f = w.get_frame_rect();
w.clone.set_position(f.x, f.y);
Tiling.animateWindow(w);
}
}
export function showWindows() {
let ws = getScratchWindows().filter(w => !w.minimized);
ws.forEach(Tiling.showWindow);
}