-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
WindowSwitcher.vala
472 lines (377 loc) · 16.3 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
/*
* 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_PADDING = 12;
private const string CAPTION_FONT_NAME = "Inter";
private const int MIN_OFFSET = 64;
private const int ANIMATION_DURATION = 200;
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.Text caption;
private int modifier_mask;
private WindowSwitcherIcon? _current_icon = null;
private WindowSwitcherIcon? current_icon {
get {
return _current_icon;
}
set {
if (_current_icon != null) {
_current_icon.selected = false;
}
_current_icon = value;
if (_current_icon != null) {
_current_icon.selected = true;
}
update_caption_text ();
}
}
private float scaling_factor = 1.0f;
public WindowSwitcher (Gala.WindowManager wm) {
Object (wm: wm);
}
construct {
var gtk_settings = Gtk.Settings.get_default ();
granite_settings = Granite.Settings.get_default ();
unowned var display = wm.get_display ();
scaling_factor = display.get_monitor_scale (display.get_current_monitor ());
canvas = new Clutter.Canvas ();
canvas.scale_factor = scaling_factor;
set_content (canvas);
opacity = 0;
// Carry out the initial draw
create_components ();
var effect = new ShadowEffect (40) {
shadow_opacity = 200,
css_class = "window-switcher"
};
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 ();
});
unowned var monitor_manager = wm.get_display ().get_context ().get_backend ().get_monitor_manager ();
monitor_manager.monitors_changed.connect (() => {
var cur_scale = display.get_monitor_scale (display.get_current_monitor ());
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 ();
ctx.set_operator (Cairo.Operator.CLEAR);
ctx.paint ();
ctx.clip ();
ctx.reset_clip ();
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 ((int)Math.round (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);
}
ctx.set_operator (Cairo.Operator.OVER);
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) {
destroy_all_children ();
}
var margin = InternalUtils.scale_to_int (WRAPPER_PADDING, scaling_factor);
var layout = new Clutter.FlowLayout (Clutter.FlowOrientation.HORIZONTAL);
container = new Clutter.Actor () {
reactive = true,
layout_manager = layout,
margin_left = margin,
margin_top = margin,
margin_right = margin,
margin_bottom = margin
};
container.button_release_event.connect (container_mouse_release);
container.motion_event.connect (container_motion_event);
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 (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) {
bool windows_exist;
if (binding.get_name ().has_prefix ("switch-group")) {
windows_exist = collect_current_windows (display, workspace);
} else {
windows_exist = collect_all_windows (display, workspace);
}
if (!windows_exist) {
return;
}
open_switcher ();
}
var binding_name = binding.get_name ();
var backward = binding_name.has_suffix ("-backward");
next_window (display, workspace, backward);
}
private bool collect_all_windows (Meta.Display display, Meta.Workspace? workspace) {
var windows = display.get_tab_list (Meta.TabList.NORMAL, workspace);
if (windows == null) {
return false;
}
unowned var current_window = display.get_tab_current (Meta.TabList.NORMAL, workspace);
if (current_window == null) {
current_icon = null;
}
container.width = -1;
container.destroy_all_children ();
foreach (unowned var window in windows) {
var icon = new WindowSwitcherIcon (window, ICON_SIZE, scaling_factor);
if (window == current_window) {
current_icon = icon;
}
container.add_child (icon);
}
return true;
}
private bool collect_current_windows (Meta.Display display, Meta.Workspace? workspace) {
var windows = display.get_tab_list (Meta.TabList.NORMAL, workspace);
if (windows == null) {
return false;
}
unowned var current_window = display.get_tab_current (Meta.TabList.NORMAL, workspace);
if (current_window == null) {
current_icon = null;
return false;
}
container.width = -1;
container.destroy_all_children ();
unowned var window_tracker = ((WindowManagerGala) wm).window_tracker;
var app = window_tracker.get_app_for_window (current_window);
foreach (unowned var window in windows) {
if (window_tracker.get_app_for_window (window) == app) {
var icon = new WindowSwitcherIcon (window, ICON_SIZE, scaling_factor);
if (window == current_window) {
current_icon = icon;
}
container.add_child (icon);
}
}
return true;
}
private void open_switcher () {
if (container.get_n_children () == 0) {
Clutter.get_default_backend ().get_default_seat ().bell_notify ();
return;
}
if (opened) {
return;
}
opacity = 0;
unowned var display = wm.get_display ();
var monitor = display.get_current_monitor ();
var geom = display.get_monitor_geometry (monitor);
float container_width;
container.get_preferred_width (
InternalUtils.scale_to_int (ICON_SIZE, scaling_factor) + container.margin_left + container.margin_right,
null,
out container_width
);
if (container_width + InternalUtils.scale_to_int (MIN_OFFSET, scaling_factor) * 2 > geom.width) {
container.width = geom.width - InternalUtils.scale_to_int (MIN_OFFSET, scaling_factor) * 2;
}
float nat_width, nat_height;
container.get_preferred_size (null, null, out nat_width, out nat_height);
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 ();
// container width might have changed, so we must update caption width too
update_caption_text ();
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 (wm.enable_animations ? ANIMATION_DURATION : 0);
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) => {
var action = Meta.Prefs.get_keybinding_action (binding.get_name ());
switch (action) {
case Meta.KeyBindingAction.NONE:
case Meta.KeyBindingAction.LOCATE_POINTER_KEY:
case Meta.KeyBindingAction.SWITCH_APPLICATIONS:
case Meta.KeyBindingAction.SWITCH_APPLICATIONS_BACKWARD:
case Meta.KeyBindingAction.SWITCH_WINDOWS:
case Meta.KeyBindingAction.SWITCH_WINDOWS_BACKWARD:
case Meta.KeyBindingAction.SWITCH_GROUP:
case Meta.KeyBindingAction.SWITCH_GROUP_BACKWARD:
return false;
default:
break;
}
return true;
});
}
private void close_switcher (uint32 time, bool cancel = false) {
if (!opened) {
return;
}
var window = current_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;
if (container.get_n_children () == 1 && current_icon != null) {
Clutter.get_default_backend ().get_default_seat ().bell_notify ();
return;
}
if (current_icon == null) {
actor = container.get_first_child ();
} else if (!backward) {
actor = current_icon.get_next_sibling ();
if (actor == null) {
actor = container.get_first_child ();
}
} else {
actor = current_icon.get_previous_sibling ();
if (actor == null) {
actor = container.get_last_child ();
}
}
current_icon = (WindowSwitcherIcon) actor;
}
private void update_caption_text () {
var current_window = current_icon != null ? current_icon.window : null;
var current_caption = current_window != null ? current_window.title : "n/a";
caption.set_text (current_caption);
// Make caption smaller than the wrapper, so it doesn't overflow.
caption.width = width - WRAPPER_PADDING * 2 * scaling_factor;
caption.set_position (
InternalUtils.scale_to_int (WRAPPER_PADDING, scaling_factor),
(int) (height - caption.height / 2 - InternalUtils.scale_to_int (WRAPPER_PADDING, scaling_factor) * 2)
);
}
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 WindowSwitcherIcon;
if (selected == null) {
return true;
}
if (current_icon != selected) {
current_icon = selected;
}
return true;
}
private bool container_mouse_release (Clutter.ButtonEvent event) {
if (opened && event.button == Clutter.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 inline Clutter.ModifierType get_current_modifiers () {
Clutter.ModifierType modifiers;
wm.get_display ().get_cursor_tracker ().get_pointer (null, out modifiers);
return modifiers & Clutter.ModifierType.MODIFIER_MASK;
}
}
}