Skip to content

Commit ad7805b

Browse files
committed
Cleanup WindowCloneContainer
1 parent bf8bca2 commit ad7805b

File tree

1 file changed

+47
-79
lines changed

1 file changed

+47
-79
lines changed

src/Widgets/MultitaskingView/WindowCloneContainer.vala

Lines changed: 47 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ public class Gala.WindowCloneContainer : ActorTarget {
1818
public int padding_bottom { get; set; default = 12; }
1919

2020
public WindowManager wm { get; construct; }
21-
public bool overview_mode { get; construct; }
22-
2321
public float monitor_scale { get; construct set; }
22+
public bool overview_mode { get; construct; }
2423

2524
private bool opened = false;
2625

2726
/**
28-
* The window that is currently selected via keyboard shortcuts. It is not
29-
* necessarily the same as the active window.
27+
* The window that is currently selected via keyboard shortcuts.
28+
* It is not necessarily the same as the active window.
3029
*/
3130
private unowned WindowClone? current_window = null;
3231

@@ -35,24 +34,20 @@ public class Gala.WindowCloneContainer : ActorTarget {
3534
}
3635

3736
/**
38-
* Create a WindowClone for a MetaWindow and add it to the group
37+
* Create a WindowClone for a Meta.Window and add it to the group
3938
*
4039
* @param window The window for which to create the WindowClone for
4140
*/
4241
public void add_window (Meta.Window window) {
4342
var windows = new List<Meta.Window> ();
44-
foreach (unowned var child in get_children ()) {
45-
unowned var clone = (WindowClone) child;
43+
windows.append (window);
44+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
4645
windows.append (clone.window);
4746
}
48-
windows.append (window);
49-
50-
unowned var display = wm.get_display ();
51-
var windows_ordered = InternalUtils.sort_windows (display, windows);
5247

5348
var new_window = new WindowClone (wm, window, monitor_scale, overview_mode);
54-
55-
new_window.selected.connect ((clone) => window_selected (clone.window));
49+
new_window.selected.connect ((_new_window) => window_selected (_new_window.window));
50+
new_window.request_reposition.connect (() => reflow (false));
5651
new_window.destroy.connect ((_new_window) => {
5752
// make sure to release reference if the window is selected
5853
if (_new_window == current_window) {
@@ -66,12 +61,10 @@ public class Gala.WindowCloneContainer : ActorTarget {
6661

6762
reflow (false);
6863
});
69-
new_window.request_reposition.connect (() => reflow (false));
70-
7164
bind_property ("monitor-scale", new_window, "monitor-scale");
7265

7366
unowned Meta.Window? target = null;
74-
foreach (unowned var w in windows_ordered) {
67+
foreach (unowned var w in InternalUtils.sort_windows (wm.get_display (), windows)) {
7568
if (w != window) {
7669
target = w;
7770
continue;
@@ -84,8 +77,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
8477
add_child (new_window);
8578
}
8679

87-
foreach (unowned var child in get_children ()) {
88-
unowned var clone = (WindowClone) child;
80+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
8981
if (target == clone.window) {
9082
insert_child_below (new_window, clone);
9183
break;
@@ -99,9 +91,9 @@ public class Gala.WindowCloneContainer : ActorTarget {
9991
* Find and remove the WindowClone for a MetaWindow
10092
*/
10193
public void remove_window (Meta.Window window) {
102-
foreach (unowned var child in get_children ()) {
103-
if (((WindowClone) child).window == window) {
104-
remove_child (child);
94+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
95+
if (clone.window == window) {
96+
remove_child (clone);
10597
reflow (false);
10698
break;
10799
}
@@ -117,24 +109,22 @@ public class Gala.WindowCloneContainer : ActorTarget {
117109
* during animations correct.
118110
*/
119111
private void restack_windows () {
120-
unowned var display = wm.get_display ();
121-
122-
var children = get_children ();
112+
var children = (GLib.List<weak WindowClone>) get_children ();
123113

124114
var windows = new List<Meta.Window> ();
125-
foreach (unowned Clutter.Actor child in children) {
126-
windows.prepend (((WindowClone) child).window);
115+
foreach (unowned var clone in children) {
116+
windows.prepend (clone.window);
127117
}
128118

129-
var windows_ordered = InternalUtils.sort_windows (display, windows);
119+
var windows_ordered = InternalUtils.sort_windows (wm.get_display (), windows);
130120
windows_ordered.reverse ();
131121

132122
var i = 0;
133123
foreach (unowned var window in windows_ordered) {
134-
foreach (unowned var child in children) {
135-
if (((WindowClone) child).window == window) {
136-
set_child_at_index (child, i);
137-
children.remove (child);
124+
foreach (unowned var clone in children) {
125+
if (clone.window == window) {
126+
set_child_at_index (clone, i);
127+
children.remove (clone);
138128
i++;
139129
break;
140130
}
@@ -143,24 +133,18 @@ public class Gala.WindowCloneContainer : ActorTarget {
143133
}
144134

145135
/**
146-
* Recalculate the tiling positions of the windows and animate them to
147-
* the resulting spots.
136+
* Recalculate the tiling positions of the windows and animate them to the resulting spots.
148137
*/
149138
private void reflow (bool view_toggle) {
150-
if (!opened) {
139+
if (!opened || get_n_children () == 0) {
151140
return;
152141
}
153142

154143
var windows = new List<InternalUtils.TilableWindow?> ();
155-
foreach (unowned var child in get_children ()) {
156-
unowned var clone = (WindowClone) child;
144+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
157145
windows.prepend ({ clone.window.get_frame_rect (), clone });
158146
}
159147

160-
if (windows.is_empty ()) {
161-
return;
162-
}
163-
164148
// make sure the windows are always in the same order so the algorithm
165149
// doesn't give us different slots based on stacking order, which can lead
166150
// to windows flying around weirdly
@@ -173,15 +157,12 @@ public class Gala.WindowCloneContainer : ActorTarget {
173157
Mtk.Rectangle area = {
174158
padding_left,
175159
padding_top,
176-
(int)width - padding_left - padding_right,
177-
(int)height - padding_top - padding_bottom
160+
(int) width - padding_left - padding_right,
161+
(int) height - padding_top - padding_bottom
178162
};
179163

180-
var window_positions = InternalUtils.calculate_grid_placement (area, windows);
181-
182-
foreach (var tilable in window_positions) {
183-
unowned var clone = (WindowClone) tilable.id;
184-
clone.take_slot (tilable.rect, !view_toggle);
164+
foreach (var tilable in InternalUtils.calculate_grid_placement (area, windows)) {
165+
tilable.id.take_slot (tilable.rect, !view_toggle);
185166
}
186167
}
187168

@@ -212,8 +193,10 @@ public class Gala.WindowCloneContainer : ActorTarget {
212193
break;
213194
case Clutter.Key.Return:
214195
case Clutter.Key.KP_Enter:
215-
if (!activate_selected_window ()) {
196+
if (current_window == null) {
216197
requested_close ();
198+
} else {
199+
window_selected (current_window.window);
217200
}
218201
break;
219202
}
@@ -222,31 +205,30 @@ public class Gala.WindowCloneContainer : ActorTarget {
222205
}
223206

224207
/**
225-
* Look for the next window in a direction and make this window the
226-
* new current_window. Used for keyboard navigation.
208+
* Look for the next window in a direction and make this window the new current_window.
209+
* Used for keyboard navigation.
227210
*
228-
* @param direction The MetaMotionDirection in which to search for windows for.
211+
* @param direction The MetaMotionDirection in which to search for windows for.
212+
* @param user_action Whether we must select a window and, if failed, play a bell sound.
229213
*/
230214
public void select_next_window (Meta.MotionDirection direction, bool user_action) {
231-
if (get_n_children () < 1) {
215+
if (get_n_children () == 0) {
232216
return;
233217
}
234218

235-
unowned var display = wm.get_display ();
236-
237219
WindowClone? closest = null;
238220

239221
if (current_window == null) {
240222
closest = (WindowClone) get_child_at_index (0);
241223
} else {
242224
var current_rect = current_window.slot;
243225

244-
foreach (unowned var child in get_children ()) {
245-
if (child == current_window) {
226+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
227+
if (clone == current_window) {
246228
continue;
247229
}
248230

249-
var window_rect = ((WindowClone) child).slot;
231+
var window_rect = clone.slot;
250232

251233
if (window_rect == null) {
252234
continue;
@@ -262,7 +244,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
262244
&& window_rect.y < current_rect.y + current_rect.height) {
263245

264246
if (closest == null || closest.slot.x < window_rect.x) {
265-
closest = (WindowClone) child;
247+
closest = clone;
266248
}
267249
}
268250
} else if (direction == RIGHT) {
@@ -275,7 +257,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
275257
&& window_rect.y < current_rect.y + current_rect.height) {
276258

277259
if (closest == null || closest.slot.x > window_rect.x) {
278-
closest = (WindowClone) child;
260+
closest = clone;
279261
}
280262
}
281263
} else if (direction == UP) {
@@ -288,7 +270,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
288270
&& window_rect.x < current_rect.x + current_rect.width) {
289271

290272
if (closest == null || closest.slot.y < window_rect.y) {
291-
closest = (WindowClone) child;
273+
closest = clone;
292274
}
293275
}
294276
} else if (direction == DOWN) {
@@ -301,7 +283,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
301283
&& window_rect.x < current_rect.x + current_rect.width) {
302284

303285
if (closest == null || closest.slot.y > window_rect.y) {
304-
closest = (WindowClone) child;
286+
closest = clone;
305287
}
306288
}
307289
} else {
@@ -313,7 +295,7 @@ public class Gala.WindowCloneContainer : ActorTarget {
313295

314296
if (closest == null) {
315297
if (current_window != null && user_action) {
316-
InternalUtils.bell_notify (display);
298+
InternalUtils.bell_notify (wm.get_display ());
317299
current_window.active = true;
318300
}
319301
return;
@@ -330,31 +312,17 @@ public class Gala.WindowCloneContainer : ActorTarget {
330312
current_window = closest;
331313
}
332314

333-
/**
334-
* Emit the selected signal for the current_window.
335-
*/
336-
public bool activate_selected_window () {
337-
if (current_window != null) {
338-
window_selected (current_window.window);
339-
return true;
340-
}
341-
342-
return false;
343-
}
344-
345315
public override void start_progress (GestureAction action) {
346316
if (!opened) {
347317
opened = true;
348318

349-
unowned var display = wm.get_display ();
350-
351319
if (current_window != null) {
352320
current_window.active = false;
353321
}
354322

355-
for (var child = get_first_child (); child != null; child = child.get_next_sibling ()) {
356-
unowned var clone = (WindowClone) child;
357-
if (clone.window == display.focus_window) {
323+
unowned var focus_window = wm.get_display ().focus_window;
324+
foreach (unowned var clone in (GLib.List<weak WindowClone>) get_children ()) {
325+
if (clone.window == focus_window) {
358326
current_window = clone;
359327
break;
360328
}

0 commit comments

Comments
 (0)