-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
WindowCloneContainer.vala
361 lines (301 loc) · 12.9 KB
/
WindowCloneContainer.vala
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// Copyright (C) 2014 Tom Beckmann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
namespace Gala {
/**
* Container which controls the layout of a set of WindowClones.
*/
public class WindowCloneContainer : Clutter.Actor {
public signal void window_selected (Meta.Window window);
public int padding_top { get; set; default = 12; }
public int padding_left { get; set; default = 12; }
public int padding_right { get; set; default = 12; }
public int padding_bottom { get; set; default = 12; }
public GestureTracker? gesture_tracker { get; construct; }
public bool overview_mode { get; construct; }
private bool opened;
/**
* The window that is currently selected via keyboard shortcuts. It is not
* necessarily the same as the active window.
*/
private WindowClone? current_window;
public WindowCloneContainer (GestureTracker? gesture_tracker, bool overview_mode = false) {
Object (gesture_tracker: gesture_tracker, overview_mode: overview_mode);
}
construct {
opened = false;
current_window = null;
}
/**
* Create a WindowClone for a MetaWindow and add it to the group
*
* @param window The window for which to create the WindowClone for
*/
public void add_window (Meta.Window window) {
unowned Meta.Display display = window.get_display ();
var children = get_children ();
GLib.SList<Meta.Window> windows = new GLib.SList<Meta.Window> ();
foreach (unowned Clutter.Actor child in children) {
unowned WindowClone tw = (WindowClone) child;
windows.prepend (tw.window);
}
windows.prepend (window);
windows.reverse ();
var windows_ordered = display.sort_windows_by_stacking (windows);
var new_window = new WindowClone (window, gesture_tracker, overview_mode);
new_window.selected.connect (window_selected_cb);
new_window.destroy.connect (window_destroyed);
new_window.request_reposition.connect (() => reflow ());
var added = false;
unowned Meta.Window? target = null;
foreach (unowned Meta.Window w in windows_ordered) {
if (w != window) {
target = w;
continue;
}
break;
}
foreach (unowned Clutter.Actor child in children) {
unowned WindowClone tw = (WindowClone) child;
if (target == tw.window) {
insert_child_above (new_window, tw);
added = true;
break;
}
}
// top most or no other children
if (!added)
add_child (new_window);
reflow ();
}
/**
* Find and remove the WindowClone for a MetaWindow
*/
public void remove_window (Meta.Window window) {
foreach (var child in get_children ()) {
if (((WindowClone) child).window == window) {
remove_child (child);
reflow ();
break;
}
}
}
private void window_selected_cb (WindowClone tiled) {
window_selected (tiled.window);
}
private void window_destroyed (Clutter.Actor actor) {
var window = actor as WindowClone;
if (window == null)
return;
window.destroy.disconnect (window_destroyed);
window.selected.disconnect (window_selected_cb);
Idle.add (() => {
reflow ();
return false;
});
}
/**
* Sort the windows z-order by their actual stacking to make intersections
* during animations correct.
*/
public void restack_windows (Meta.Display display) {
var children = get_children ();
GLib.SList<Meta.Window> windows = new GLib.SList<Meta.Window> ();
foreach (unowned Clutter.Actor child in children) {
unowned WindowClone tw = (WindowClone) child;
windows.prepend (tw.window);
}
var windows_ordered = display.sort_windows_by_stacking (windows);
windows_ordered.reverse ();
foreach (unowned Meta.Window window in windows_ordered) {
var i = 0;
foreach (unowned Clutter.Actor child in children) {
if (((WindowClone) child).window == window) {
set_child_at_index (child, i);
children.remove (child);
i++;
break;
}
}
}
}
/**
* Recalculate the tiling positions of the windows and animate them to
* the resulting spots.
*/
public void reflow (bool with_gesture = false, bool is_cancel_animation = false) {
if (!opened)
return;
var windows = new List<InternalUtils.TilableWindow?> ();
foreach (var child in get_children ()) {
unowned WindowClone window = (WindowClone) child;
windows.prepend ({ window.window.get_frame_rect (), window });
}
if (windows.length () < 1)
return;
// make sure the windows are always in the same order so the algorithm
// doesn't give us different slots based on stacking order, which can lead
// to windows flying around weirdly
windows.sort ((a, b) => {
var seq_a = ((WindowClone) a.id).window.get_stable_sequence ();
var seq_b = ((WindowClone) b.id).window.get_stable_sequence ();
return (int) (seq_b - seq_a);
});
Meta.Rectangle area = {
padding_left,
padding_top,
(int)width - padding_left - padding_right,
(int)height - padding_top - padding_bottom
};
var window_positions = InternalUtils.calculate_grid_placement (area, windows);
foreach (var tilable in window_positions) {
unowned WindowClone window = (WindowClone) tilable.id;
window.take_slot (tilable.rect, with_gesture, is_cancel_animation);
window.place_widgets (tilable.rect.width, tilable.rect.height);
}
}
/**
* Look for the next window in a direction and make this window the
* new current_window. Used for keyboard navigation.
*
* @param direction The MetaMotionDirection in which to search for windows for.
*/
public void select_next_window (Meta.MotionDirection direction) {
if (get_n_children () < 1)
return;
if (current_window == null) {
current_window = (WindowClone) get_child_at_index (0);
return;
}
var current_rect = current_window.slot;
WindowClone? closest = null;
foreach (var window in get_children ()) {
if (window == current_window)
continue;
var window_rect = ((WindowClone) window).slot;
switch (direction) {
case Meta.MotionDirection.LEFT:
if (window_rect.x > current_rect.x)
continue;
// test for vertical intersection
if (window_rect.y + window_rect.height > current_rect.y
&& window_rect.y < current_rect.y + current_rect.height) {
if (closest == null
|| closest.slot.x < window_rect.x)
closest = (WindowClone) window;
}
break;
case Meta.MotionDirection.RIGHT:
if (window_rect.x < current_rect.x)
continue;
// test for vertical intersection
if (window_rect.y + window_rect.height > current_rect.y
&& window_rect.y < current_rect.y + current_rect.height) {
if (closest == null
|| closest.slot.x > window_rect.x)
closest = (WindowClone) window;
}
break;
case Meta.MotionDirection.UP:
if (window_rect.y > current_rect.y)
continue;
// test for horizontal intersection
if (window_rect.x + window_rect.width > current_rect.x
&& window_rect.x < current_rect.x + current_rect.width) {
if (closest == null
|| closest.slot.y < window_rect.y)
closest = (WindowClone) window;
}
break;
case Meta.MotionDirection.DOWN:
if (window_rect.y < current_rect.y)
continue;
// test for horizontal intersection
if (window_rect.x + window_rect.width > current_rect.x
&& window_rect.x < current_rect.x + current_rect.width) {
if (closest == null
|| closest.slot.y > window_rect.y)
closest = (WindowClone) window;
}
break;
default:
break;
}
}
if (closest == null)
return;
if (current_window != null)
current_window.active = false;
closest.active = true;
current_window = closest;
}
/**
* Emit the selected signal for the current_window.
*/
public bool activate_selected_window () {
if (current_window != null) {
current_window.selected ();
return true;
}
return false;
}
/**
* When opened the WindowClones are animated to a tiled layout
*/
public void open (Meta.Window? selected_window = null, bool with_gesture = false, bool is_cancel_animation = false) {
if (opened) {
return;
}
opened = true;
// hide the highlight when opened
if (selected_window != null) {
foreach (var child in get_children ()) {
unowned WindowClone tiled_window = (WindowClone) child;
if (tiled_window.window == selected_window) {
current_window = tiled_window;
break;
}
}
if (current_window != null) {
current_window.active = false;
}
} else {
current_window = null;
}
// make sure our windows are where they belong in case they were moved
// while were closed.
if (gesture_tracker == null || !is_cancel_animation) {
foreach (var window in get_children ()) {
((WindowClone) window).transition_to_original_state (false, with_gesture, is_cancel_animation);
}
}
reflow (with_gesture, is_cancel_animation);
}
/**
* Calls the transition_to_original_state() function on each child
* to make them take their original locations again.
*/
public void close (bool with_gesture = false, bool is_cancel_animation = false) {
if (!opened) {
return;
}
opened = false;
foreach (var window in get_children ()) {
((WindowClone) window).transition_to_original_state (true, with_gesture, is_cancel_animation);
}
}
}
}