File: quake_window.vala

package info (click to toggle)
deepin-terminal 3.2.1.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,764 kB
  • sloc: ansic: 128; exp: 25; sh: 18; makefile: 15
file content (515 lines) | stat: -rw-r--r-- 22,750 bytes parent folder | download
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2011 ~ 2018 Deepin, Inc.
 *               2011 ~ 2018 Wang Yong
 *
 * Author:     Wang Yong <wangyong@deepin.com>
 * Maintainer: Wang Yong <wangyong@deepin.com>
 *
 * 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
 * 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/>.
 */

using Gtk;
using Utils;
using Widgets;
using XUtils;

namespace Widgets {
    public class QuakeWindow : Widgets.ConfigWindow {
        public double window_default_height_scale = 0.3;
        public double window_max_height_scale = 0.7;
        public int press_x;
        public int press_y;
        public int window_frame_margin_bottom = 60;

        public QuakeWindow() {
            quake_mode = true;

            set_app_paintable(true); // set_app_paintable is necessary step to make window transparent.
            Gdk.Screen screen = Gdk.Screen.get_default();
            set_visual(screen.get_rgba_visual());

            int monitor = config.get_terminal_monitor();

            Gdk.Rectangle rect;
            screen.get_monitor_geometry(monitor, out rect);

            set_decorated(false);
            set_keep_above(true);

            set_skip_taskbar_hint(true);
            set_skip_pager_hint(true);
            // NOTE: Don't change other type, otherwise window can't resize by user.
            set_type_hint(Gdk.WindowTypeHint.MENU);
            move(rect.x, 0);

            window_frame_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
            window_widget_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);

            add(window_frame_box);
            window_frame_box.pack_start(window_widget_box, true, true, 0);

            realize.connect((w) => {
                    update_frame();

                    try {
                        var quake_window_fullscreen = config.config_file.get_boolean("advanced", "quake_window_fullscreen");
                        if (quake_window_fullscreen) {
                            fullscreen();
                        } else {
                            // Don't make quake window too height that can't resize quake window from bottom edge.
                            var config_height = config.config_file.get_double("advanced", "quake_window_height");
                            if (config_height > window_max_height_scale) {
                                config_height = window_max_height_scale;
                            }

                            // Set default size.
                            if (config_height == 0) {
                                set_default_size(rect.width, (int) (rect.height * window_default_height_scale));
                            } else {
                                set_default_size(rect.width, (int) (rect.height * double.min(config_height, 1.0)));
                            }
                        }
                    } catch (Error e) {
                        print("QuakeWindow init: %s\n", e.message);
                    }
                });

            focus_in_event.connect((w) => {
                    update_style();

                    return false;
                });

            focus_out_event.connect((w) => {
                    update_style();

                    try {
                        // Hide quake window when lost focus, and config option 'hide_quakewindow_after_lost_focus' must be true, variable 'show_quake_menu' must be fasle.
                        // If variable 'show_quake_menu' is true, lost focus signal is cause by click right menu on quake terminal.
                        if (config.config_file.get_boolean("advanced", "hide_quakewindow_after_lost_focus")) {
                            if (show_quake_menu) {
                                show_quake_menu = false;
                            } else {
                                GLib.Timeout.add(200, () => {
                                        var window_state = get_window().get_state();
                                        // Because some desktop environment, such as DDE will grab keyboard focus when press keystroke. :(
                                        //
                                        // When press quakewindow shortcuts will make code follow order: `focus_out event -> toggle_quake_window'.
                                        // focus_out event will make quakewindow hide immediately, quakewindow will show again when execute toggle_quake_window.
                                        // At last, quakewindow will execute 'hide' and 'show' actions twice, not just simple hide window.
                                        //
                                        // So i add 200ms timeout to wait toggle_quake_window execute,
                                        // focus_out event will hide window if it find window is show state after execute toggle_quake_window.
                                        if (!(Gdk.WindowState.WITHDRAWN in window_state)) {
                                            hide();
                                        }

                                        return false;
                                    });
                                // hide();
                            }
                        }
                    } catch (Error e) {
                        print("quake_window focus_out_event: %s\n", e.message);
                    }

                    return false;
                });

            configure_event.connect((w) => {
                    // Update input shape.
                    int width, height;
                    get_size(out width, out height);

                    Cairo.RectangleInt input_shape_rect;
                    get_window().get_frame_extents(out input_shape_rect);

                    if (screen_monitor.is_composited()) {
                        input_shape_rect.x = 0;
                        input_shape_rect.y = 0;
                        input_shape_rect.width = width;
                        input_shape_rect.height = height - window_frame_box.margin_bottom + Constant.RESPONSE_RADIUS;
                    }

                    var shape = new Cairo.Region.rectangle(input_shape_rect);
                    get_window().input_shape_combine_region(shape, 0, 0);

                    // Update blur area.
                    update_blur_status();

                    window_save_before_quit();

                    return false;
                });

            window_frame_box.button_press_event.connect((w, e) => {
                    if (!screen_monitor.is_composited()) {
                        var cursor_type = get_frame_cursor_type(e.x_root, e.y_root);
                        if (cursor_type != null) {
                            e.device.get_position(null, out press_x, out press_y);

                            GLib.Timeout.add(10, () => {
                                    int pointer_x, pointer_y;
                                    e.device.get_position(null, out pointer_x, out pointer_y);

                                    if (pointer_x != press_x || pointer_y != press_y) {
                                        pointer_x *= get_scale_factor();
                                        pointer_y *= get_scale_factor();
                                        resize_window(this, pointer_x, pointer_y, (int) e.button, Gdk.CursorType.BOTTOM_SIDE);

                                        return false;
                                    } else {
                                        return true;
                                    }
                                });
                        }
                    }

                    return false;
                });

            button_press_event.connect((w, e) => {
                    var cursor_type = get_cursor_type(e.x_root, e.y_root);
                    if (cursor_type != null) {
                        e.device.get_position(null, out press_x, out press_y);

                        GLib.Timeout.add(10, () => {
                                int pointer_x, pointer_y;
                                e.device.get_position(null, out pointer_x, out pointer_y);

                                if (pointer_x != press_x || pointer_y != press_y) {
                                    pointer_x *= get_scale_factor();
                                    pointer_y *= get_scale_factor();
                                    resize_window(this, pointer_x, pointer_y, (int) e.button, Gdk.CursorType.BOTTOM_SIDE);

                                    return false;
                                } else {
                                    return true;
                                }
                            });
                    }

                    return false;
                });

            draw.connect_after((w, cr) => {
                    draw_window_widgets(cr);

                    draw_window_above(cr);

                    return true;
                });

            config.update.connect((w) => {
                    update_style();

                    update_blur_status(true);
                });
        }

        public void update_blur_status(bool force_update=false) {
            try {
                int width, height;
                get_size(out width, out height);

                if (width != resize_cache_width || height != resize_cache_height || force_update) {
                    resize_cache_width = width;
                    resize_cache_height = height;

                    unowned X.Display xdisplay = (get_window().get_display() as Gdk.X11.Display).get_xdisplay();
                    var xid = (int)((Gdk.X11.Window) get_window()).get_xid();
                    var atom_NET_WM_DEEPIN_BLUR_REGION_ROUNDED = xdisplay.intern_atom("_NET_WM_DEEPIN_BLUR_REGION_ROUNDED", false);

                    var blur_background = config.config_file.get_boolean("advanced", "blur_background");
                    if (blur_background) {
                        Cairo.RectangleInt blur_rect;
                        get_window().get_frame_extents(out blur_rect);

                        if (screen_monitor.is_composited()) {
                            blur_rect.x = 0;
                            blur_rect.y = 0;
                            blur_rect.width = width;
                            blur_rect.height = height - window_frame_box.margin_bottom;
                        }

                        blur_rect.x = (int) (blur_rect.x * Utils.get_default_monitor_scale());
                        blur_rect.y = (int) (blur_rect.y * Utils.get_default_monitor_scale());
                        blur_rect.width = (int) (blur_rect.width * Utils.get_default_monitor_scale());
                        blur_rect.height = (int) (blur_rect.height * Utils.get_default_monitor_scale());

                        ulong[] data = {(ulong) blur_rect.x, (ulong) blur_rect.y, (ulong) blur_rect.width, (ulong) blur_rect.height, 8, 8};
                        xdisplay.change_property(
                            xid,
                            atom_NET_WM_DEEPIN_BLUR_REGION_ROUNDED,
                            X.XA_CARDINAL,
                            32,
                            X.PropMode.Replace,
                            (uchar[])data,
                            ((ulong[]) data).length);
                    } else {
                        ulong[] data = {0, 0, 0, 0, 0, 0};
                        xdisplay.change_property(
                            xid,
                            atom_NET_WM_DEEPIN_BLUR_REGION_ROUNDED,
                            X.XA_CARDINAL,
                            32,
                            X.PropMode.Replace,
                            (uchar[])data,
                            ((ulong[]) data).length);
                    }
                }
            } catch (GLib.KeyFileError e) {
                print("%s\n", e.message);
            }
        }

        public void add_widget(Gtk.Widget widget) {
            window_widget_box.pack_start(widget, true, true, 0);
        }

        public void toggle_quake_window() {
            Gdk.Screen screen = Gdk.Screen.get_default();
            int monitor = config.get_terminal_monitor();
            int window_monitor = screen.get_monitor_at_window(get_window());

            Gdk.Rectangle rect;
            screen.get_monitor_geometry(monitor, out rect);

            if (monitor == window_monitor) {
                var window_state = get_window().get_state();
                if (Gdk.WindowState.WITHDRAWN in window_state) {
                    show_quake_window(rect);
                } else {

                    try {
                        // When option hide_quakewindow_after_lost_focus enable.
                        // Focus terminal if terminal is not active, only hide temrinal after terminal focus.
                        if (config.config_file.get_boolean("advanced", "hide_quakewindow_when_active")) {
                            // Because some desktop environment, such as DDE will grab keyboard focus when press keystroke. :(
                            // So i add 200ms timeout to wait desktop environment release keyboard focus and then get window active state.
                            // Otherwise, window is always un-active state that quake terminal can't toggle to hide.
                            GLib.Timeout.add(200, () => {
                                    if (is_active) {
                                        hide();
                                    } else {
                                        present();
                                    }

                                    return false;
                                });
                        }
                        // Hide terminal immediately if option hide_quakewindow_when_active is false.
                        else {
                            hide();
                        }
                    } catch (Error e) {
                        print("quake_window toggle_quake_window: %s\n", e.message);
                    }
                }
            } else {
                show_quake_window(rect);
            }
        }

        public void show_quake_window(Gdk.Rectangle rect) {
            // Init.
            int width, height;
            get_size(out width, out height);
            show_all();

            // Resize quake terminal window's width along with monitor's width.
            get_window().move_resize(rect.x, 0, rect.width, height);

            // Present window.
            present();
        }

        public void update_style() {
            clean_style();

            bool is_light_theme = is_light_theme();

            if (is_active) {
                if (is_light_theme) {
                    if (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_light_shadow_active");
                    } else {
                        window_frame_box.get_style_context().add_class("window_light_noshadow_active");
                    }
                } else {
                    if (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_dark_shadow_active");
                    } else {
                        window_frame_box.get_style_context().add_class("window_dark_noshadow_active");
                    }
                }
            } else {
                if (is_light_theme) {
                    if (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_light_shadow_inactive");
                    } else {
                        window_frame_box.get_style_context().add_class("window_light_noshadow_inactive");
                    }
                } else {
                    if (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_dark_shadow_inactive");
                    } else {
                        window_frame_box.get_style_context().add_class("window_dark_noshadow_inactive");
                    }
                }
            }
        }

        public void clean_style() {
            window_frame_box.get_style_context().remove_class("window_light_shadow_inactive");
            window_frame_box.get_style_context().remove_class("window_dark_shadow_inactive");
            window_frame_box.get_style_context().remove_class("window_light_shadow_active");
            window_frame_box.get_style_context().remove_class("window_dark_shadow_active");
            window_frame_box.get_style_context().remove_class("window_noradius_shadow_inactive");
            window_frame_box.get_style_context().remove_class("window_noradius_shadow_active");
            window_frame_box.get_style_context().remove_class("window_light_noshadow_inactive");
            window_frame_box.get_style_context().remove_class("window_dark_noshadow_inactive");
            window_frame_box.get_style_context().remove_class("window_light_noshadow_active");
            window_frame_box.get_style_context().remove_class("window_dark_noshadow_active");
            window_frame_box.get_style_context().remove_class("window_noradius_noshadow_inactive");
            window_frame_box.get_style_context().remove_class("window_noradius_noshadow_active");
        }

        public void draw_window_widgets(Cairo.Context cr) {
            Utils.propagate_draw(this, cr);
        }

        public void draw_window_above(Cairo.Context cr) {
            Gtk.Allocation window_frame_rect;
            window_frame_box.get_allocation(out window_frame_rect);

            int x = window_frame_box.margin_start;
            int y = window_frame_box.margin_top;
            int width = window_frame_rect.width;
            int height = window_frame_rect.height;

            int titlebar_y = y;
            if (get_scale_factor() > 1) {
                titlebar_y += 1;
            }

            draw_titlebar_underline(cr, x, titlebar_y + height - Constant.TITLEBAR_HEIGHT - 1, width, -1);
            draw_active_tab_underline(cr, x + active_tab_underline_x, titlebar_y + height - Constant.TITLEBAR_HEIGHT - 1);
        }

        public void show_window(WorkspaceManager workspace_manager, Tabbar tabbar) {
            Gdk.RGBA background_color = Gdk.RGBA();

            init(workspace_manager, tabbar);
            // First focus terminal after show quake terminal.
            // Sometimes, some popup window (like wine program's popup notify window) will grab focus,
            // so call window.present to make terminal get focus.
            show.connect((t) => {
                    present();
                });

            top_box.draw.connect((w, cr) => {
                    Gtk.Allocation rect;
                    w.get_allocation(out rect);

                    try {
                        background_color = Utils.hex_to_rgba(config.config_file.get_string("theme", "background"));
                        cr.set_source_rgba(background_color.red, background_color.green, background_color.blue, config.config_file.get_double("general", "opacity"));
                        Draw.draw_rectangle(cr, 0, 0, rect.width, Constant.TITLEBAR_HEIGHT);
                    } catch (Error e) {
                        print("Main quake mode: %s\n", e.message);
                    }

                    Utils.propagate_draw(top_box, cr);

                    return true;
                });

            top_box.pack_start(tabbar, true, true, 0);
            box.pack_start(workspace_manager, true, true, 0);

            try {
                if (config.config_file.get_boolean("advanced", "show_quakewindow_tab")) {
                    box.pack_start(top_box, false, false, 0);
                }
            } catch (Error e) {
                print("Main quake mode: %s\n", e.message);
            }

            add_widget(box);
            show_all();
        }

        public override void window_save_before_quit() {
            int monitor = config.get_terminal_monitor();
            Gdk.Rectangle rect;
            screen.get_monitor_geometry(monitor, out rect);

            int width, height;
            get_size(out width, out height);

            config.load_config();
            config.config_file.set_double("advanced", "quake_window_height", height * 1.0 / rect.height);
            config.save();
        }

        public override Gdk.CursorType? get_cursor_type(double x, double y) {
            int window_x, window_y;
            get_window().get_origin(out window_x, out window_y);

            int width, height;
            get_size(out width, out height);

            var bottom_side_start = window_y + height - window_frame_margin_bottom;
            var bottom_side_end = window_y + height - window_frame_margin_bottom + Constant.RESPONSE_RADIUS;;

            if (y > bottom_side_start && y < bottom_side_end) {
                return Gdk.CursorType.BOTTOM_SIDE;
            } else {
                return null;
            }
        }

        public override Gdk.CursorType? get_frame_cursor_type(double x, double y) {
            int window_x, window_y;
            get_window().get_origin(out window_x, out window_y);

            int width, height;
            get_size(out width, out height);

            var bottom_side_start = window_y + height - Constant.RESPONSE_RADIUS;
            var bottom_side_end = window_y + height;

            if (y > bottom_side_start && y < bottom_side_end) {
                return Gdk.CursorType.BOTTOM_SIDE;
            } else {
                return null;
            }
        }

        public override void update_frame() {
            update_style();

            if (screen_monitor.is_composited()) {
                window_frame_box.margin_bottom = window_frame_margin_bottom;
                get_window().set_shadow_width(0, 0, 0, window_frame_margin_bottom);
            } else {
                window_frame_box.margin_bottom = 0;
                get_window().set_shadow_width(0, 0, 0, 0);
            }
        }
    }
}