-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
WindowSwitcher.vala
497 lines (396 loc) · 17.4 KB
/
WindowSwitcher.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
* Copyright 2021 Aral Balkan <mail@ar.al>
* Copyright 2020 Mark Story <mark@mark-story.com>
* Copyright 2017 Popye <sailor3101@gmail.com>
* Copyright 2014 Tom Beckmann
* SPDX-License-Identifier: GPL-3.0-or-later
*/
namespace Gala {
public class WindowSwitcher : Clutter.Actor {
public const int ICON_SIZE = 64;
public const int WRAPPER_BORDER_RADIUS = 3;
public const int WRAPPER_PADDING = 12;
public const string CAPTION_FONT_NAME = "Inter";
private const int MIN_OFFSET = 64;
private const int FIX_TIMEOUT_INTERVAL = 100;
public bool opened { get; private set; default = false; }
public Gala.WindowManager? wm { get; construct; }
private Gala.ModalProxy modal_proxy = null;
private Granite.Settings granite_settings;
private Clutter.Canvas canvas;
private Clutter.Actor container;
private Clutter.Actor indicator;
private Clutter.Text caption;
private int modifier_mask;
private WindowIcon? cur_icon = null;
private int scaling_factor = 1;
// For some reason, on Odin, the height of the caption loses
// its padding after the first time the switcher displays. As a
// workaround, I store the initial value here once we have it.
private float caption_height = -1.0f;
public WindowSwitcher (Gala.WindowManager wm) {
Object (wm: wm);
}
construct {
var gtk_settings = Gtk.Settings.get_default ();
granite_settings = Granite.Settings.get_default ();
scaling_factor = InternalUtils.get_ui_scaling_factor ();
canvas = new Clutter.Canvas ();
canvas.scale_factor = scaling_factor;
set_content (canvas);
// Carry out the initial draw
create_components ();
// FIXME: Kind of abusing the style class here for a smaller shadow
var effect = new ShadowEffect (30) {
shadow_opacity = 200,
css_class = "workspace"
};
add_effect (effect);
// Redraw the components if the colour scheme changes.
granite_settings.notify["prefers-color-scheme"].connect (() => {
canvas.invalidate ();
create_components ();
});
gtk_settings.notify["gtk-theme-name"].connect (() => {
canvas.invalidate ();
create_components ();
});
Meta.MonitorManager.@get ().monitors_changed.connect (() => {
var cur_scale = InternalUtils.get_ui_scaling_factor ();
if (cur_scale != scaling_factor) {
scaling_factor = cur_scale;
canvas.scale_factor = scaling_factor;
create_components ();
}
});
canvas.draw.connect (draw);
}
private bool draw (Cairo.Context ctx, int width, int height) {
ctx.save ();
var widget_path = new Gtk.WidgetPath ();
widget_path.append_type (typeof (Gtk.Window));
widget_path.iter_set_object_name (-1, "window");
var style_context = new Gtk.StyleContext ();
style_context.set_scale (scaling_factor);
style_context.set_path (widget_path);
style_context.add_class ("background");
style_context.add_class ("csd");
style_context.add_class ("unified");
if (granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK) {
var gtksettings = Gtk.Settings.get_default ();
var css_provider = Gtk.CssProvider.get_named (gtksettings.gtk_theme_name, "dark");
style_context.add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
}
style_context.render_background (ctx, 0, 0, width, height);
style_context.render_frame (ctx, 0, 0, width, height);
ctx.restore ();
return true;
}
private void create_components () {
// We've already been constructed once, start again
if (container != null) {
caption_height = -1.0f;
destroy_all_children ();
}
var layout = new Clutter.FlowLayout (Clutter.FlowOrientation.HORIZONTAL);
container = new Clutter.Actor ();
container.layout_manager = layout;
container.reactive = true;
container.button_press_event.connect (container_mouse_press);
container.motion_event.connect (container_motion_event);
var rgba = InternalUtils.get_theme_accent_color ();
var accent_color = Clutter.Color ();
accent_color.init (
(uint8) (rgba.red * 255),
(uint8) (rgba.green * 255),
(uint8) (rgba.blue * 255),
(uint8) (rgba.alpha * 255)
);
var rect_radius = WRAPPER_BORDER_RADIUS * scaling_factor;
indicator = new Clutter.Actor ();
indicator.margin_left = indicator.margin_top =
indicator.margin_right = indicator.margin_bottom = 0;
indicator.set_pivot_point (0.5f, 0.5f);
var indicator_canvas = new Clutter.Canvas ();
indicator.set_content (indicator_canvas);
indicator_canvas.scale_factor = scaling_factor;
indicator_canvas.draw.connect ((ctx, width, height) => {
ctx.save ();
ctx.set_operator (Cairo.Operator.CLEAR);
ctx.paint ();
ctx.clip ();
ctx.reset_clip ();
// draw rect
Clutter.cairo_set_source_color (ctx, accent_color);
Drawing.Utilities.cairo_rounded_rectangle (ctx, 0, 0, width, height, rect_radius);
ctx.set_operator (Cairo.Operator.SOURCE);
ctx.fill ();
ctx.restore ();
return true;
});
var caption_color = "#2e2e31";
if (granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK) {
caption_color = "#fafafa";
}
caption = new Clutter.Text.full (CAPTION_FONT_NAME, "", Clutter.Color.from_string (caption_color));
caption.set_pivot_point (0.5f, 0.5f);
caption.set_ellipsize (Pango.EllipsizeMode.END);
caption.set_line_alignment (Pango.Alignment.CENTER);
add_child (indicator);
add_child (container);
add_child (caption);
}
[CCode (instance_pos = -1)]
public void handle_switch_windows (
Meta.Display display, Meta.Window? window,
Clutter.KeyEvent event, Meta.KeyBinding binding
) {
var workspace = display.get_workspace_manager ().get_active_workspace ();
// copied from gnome-shell, finds the primary modifier in the mask
var mask = binding.get_mask ();
if (mask == 0) {
modifier_mask = 0;
} else {
modifier_mask = 1;
while (mask > 1) {
mask >>= 1;
modifier_mask <<= 1;
}
}
if (!opened) {
var windows_exist = collect_windows (display, workspace);
if (!windows_exist) {
return;
}
open_switcher ();
update_indicator_position (true);
}
var binding_name = binding.get_name ();
var backward = binding_name.has_suffix ("-backward");
next_window (display, workspace, backward);
}
private bool collect_windows (Meta.Display display, Meta.Workspace? workspace) {
var windows = display.get_tab_list (Meta.TabList.NORMAL, workspace);
if (windows == null) {
return false;
}
var current_window = display.get_tab_current (Meta.TabList.NORMAL, workspace);
container.width = -1;
container.destroy_all_children ();
foreach (var window in windows) {
var icon = new WindowIcon (window, ICON_SIZE * scaling_factor);
if (window == current_window) {
cur_icon = icon;
}
icon.set_pivot_point (0.5f, 0.5f);
container.add_child (icon);
}
return true;
}
private void open_switcher () {
var display = wm.get_display ();
if (container.get_n_children () == 0) {
Utils.bell (display);
return;
}
if (opened) {
return;
}
container.margin_left = container.margin_top =
container.margin_right = container.margin_bottom = (WRAPPER_PADDING * 2 * scaling_factor);
var l = container.layout_manager as Clutter.FlowLayout;
l.column_spacing = l.row_spacing = WRAPPER_PADDING * scaling_factor;
indicator.visible = false;
var indicator_size = (ICON_SIZE + WRAPPER_PADDING * 2) * scaling_factor;
indicator.set_size (indicator_size, indicator_size);
((Clutter.Canvas) indicator.content).set_size (indicator_size, indicator_size);
caption.visible = false;
caption.margin_bottom = caption.margin_top = WRAPPER_PADDING * scaling_factor;
var monitor = display.get_current_monitor ();
var geom = display.get_monitor_geometry (monitor);
float container_width;
container.get_preferred_width (
ICON_SIZE * scaling_factor + container.margin_left + container.margin_right,
null,
out container_width
);
if (container_width + MIN_OFFSET * scaling_factor * 2 > geom.width) {
container.width = geom.width - MIN_OFFSET * scaling_factor * 2;
}
float nat_width, nat_height;
container.get_preferred_size (null, null, out nat_width, null);
nat_width -= (WRAPPER_PADDING * scaling_factor) / container.get_n_children ();
container.get_preferred_size (null, null, null, out nat_height);
// For some reason, on Odin, the height of the caption loses
// its padding after the first time the switcher displays. As a
// workaround, I store the initial value here once we have it
// and use that correct value on subsequent attempts.
if (caption_height == -1.0f) {
caption_height = caption.height;
}
opacity = 0;
var switcher_height = (int) (nat_height + caption_height / 2 - container.margin_bottom + WRAPPER_PADDING * 3 * scaling_factor);
set_size ((int) nat_width, switcher_height);
canvas.set_size ((int) nat_width, switcher_height);
canvas.invalidate ();
set_position (
(int) (geom.x + (geom.width - width) / 2),
(int) (geom.y + (geom.height - height) / 2)
);
toggle_display (true);
// if we did not have the grab before the key was released, close immediately
if ((get_current_modifiers () & modifier_mask) == 0) {
close_switcher (wm.get_display ().get_current_time ());
}
}
private void toggle_display (bool show) {
if (opened == show) {
return;
}
opened = show;
if (show) {
push_modal ();
} else {
wm.pop_modal (modal_proxy);
}
save_easing_state ();
set_easing_duration (200);
opacity = show ? 255 : 0;
restore_easing_state ();
container.reactive = show;
}
private void push_modal () {
modal_proxy = wm.push_modal (this);
modal_proxy.set_keybinding_filter ((binding) => {
// if it's not built-in, we can block it right away
if (!binding.is_builtin ())
return true;
// otherwise we determine by name if it's meant for us
var name = binding.get_name ();
return !(name == "switch-applications" || name == "switch-applications-backward"
|| name == "switch-windows" || name == "switch-windows-backward");
});
}
private void close_switcher (uint32 time, bool cancel = false) {
if (!opened) {
return;
}
var window = cur_icon.window;
if (window == null) {
return;
}
if (!cancel) {
var workspace = window.get_workspace ();
if (workspace != wm.get_display ().get_workspace_manager ().get_active_workspace ()) {
workspace.activate_with_focus (window, time);
} else {
window.activate (time);
}
}
toggle_display (false);
}
private void next_window (Meta.Display display, Meta.Workspace? workspace, bool backward) {
Clutter.Actor actor;
var current = cur_icon;
if (container.get_n_children () == 1) {
Utils.bell (display);
return;
}
if (!backward) {
actor = current.get_next_sibling ();
if (actor == null) {
actor = container.get_first_child ();
}
} else {
actor = current.get_previous_sibling ();
if (actor == null) {
actor = container.get_last_child ();
}
}
cur_icon = (WindowIcon) actor;
update_indicator_position ();
}
private void update_caption_text () {
var current_window = cur_icon.window;
var current_caption = "n/a";
if (current_window != null) {
current_caption = current_window.get_title ();
}
caption.set_text (current_caption);
caption.visible = true;
// Make caption smaller than the wrapper, so it doesn't overflow.
caption.width = width - WRAPPER_PADDING * 2 * scaling_factor;
caption.set_position (
WRAPPER_PADDING * scaling_factor,
(int) (height - caption_height / 2 - (WRAPPER_PADDING * scaling_factor * 2))
);
}
private void update_indicator_position (bool initial = false) {
// FIXME there are some troubles with layouting, in some cases we
// are here too early, in which case all the children are at
// (0|0), so we can easily check for that and come back later
if (container.get_n_children () > 1
&& container.get_child_at_index (1).x < 1) {
GLib.Timeout.add (FIX_TIMEOUT_INTERVAL, () => {
update_indicator_position (initial);
return false;
}, GLib.Priority.DEFAULT);
return;
}
float x = cur_icon.x;
float y = cur_icon.y;
if (initial) {
indicator.visible = true;
}
// Move the indicator without animating it.
indicator.x = container.margin_left + (container.get_n_children () > 1 ? x : 0) - (WRAPPER_PADDING * scaling_factor);
indicator.y = container.margin_top + y - (WRAPPER_PADDING * scaling_factor);
update_caption_text ();
}
public override void key_focus_out () {
close_switcher (wm.get_display ().get_current_time ());
}
private bool container_motion_event (Clutter.MotionEvent event) {
var actor = event.stage.get_actor_at_pos (Clutter.PickMode.ALL, (int)event.x, (int)event.y);
if (actor == null) {
return true;
}
var selected = actor as WindowIcon;
if (selected == null) {
return true;
}
if (cur_icon != selected) {
cur_icon = selected;
update_indicator_position ();
}
return true;
}
private bool container_mouse_press (Clutter.ButtonEvent event) {
if (opened && event.button == Gdk.BUTTON_PRIMARY) {
close_switcher (event.time);
}
return true;
}
public override bool key_release_event (Clutter.KeyEvent event) {
if ((get_current_modifiers () & modifier_mask) == 0) {
close_switcher (event.time);
return true;
}
switch (event.keyval) {
case Clutter.Key.Escape:
close_switcher (event.time, true);
return true;
}
return false;
}
private Gdk.ModifierType get_current_modifiers () {
Gdk.ModifierType modifiers;
double[] axes = {};
Gdk.Display.get_default ()
.get_device_manager ()
.get_client_pointer ()
.get_state (Gdk.get_default_root_window (), axes, out modifiers);
return modifiers;
}
}
}