File: 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 (765 lines) | stat: -rw-r--r-- 34,309 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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/* -*- 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 Cairo;
using Config;
using Gtk;
using Utils;
using Wnck;
using XUtils;

namespace Widgets {
    public class Window : Widgets.ConfigWindow {
        public Gdk.RGBA top_line_dark_color;
        public Gdk.RGBA top_line_light_color;
        public Gtk.Box fullscreen_box;
        public Gtk.Box spacing_box;
        public bool draw_tabbar_line = true;
        public double window_default_scale = 0.618;
        public int window_frame_margin_bottom = 60;
        public int window_frame_margin_end = 50;
        public int window_frame_margin_start = 50;
        public int window_frame_margin_top = 50;
        public int window_fullscreen_monitor_height = Constant.TITLEBAR_HEIGHT * 2;
        public int window_fullscreen_monitor_timeout = 150;
        public int window_fullscreen_response_height = 5;
        public int window_height;
        public int window_widget_margin_bottom = 2;
        public int window_widget_margin_end = 2;
        public int window_widget_margin_start = 2;
        public int window_widget_margin_top = 1;
        public int window_width;

        public Window(string? window_mode) {
            transparent_window();
            init_window();

            int monitor = config.get_terminal_monitor();
            Gdk.Rectangle rect;
            screen.get_monitor_geometry(monitor, out rect);

            Gdk.Geometry geo = Gdk.Geometry();
            geo.min_width = rect.width / 3;
            geo.min_height = rect.height / 3;
            this.set_geometry_hints(null, geo, Gdk.WindowHints.MIN_SIZE);

            top_line_dark_color = Utils.hex_to_rgba("#000000", 0.2);
            top_line_light_color = Utils.hex_to_rgba("#ffffff", 0.2);

            window_frame_box.margin_top = window_frame_margin_top;
            window_frame_box.margin_bottom = window_frame_margin_bottom;
            window_frame_box.margin_start = window_frame_margin_start;
            window_frame_box.margin_end = window_frame_margin_end;

            window_widget_box.margin_top = 2;
            window_widget_box.margin_bottom = 2;
            window_widget_box.margin_start = 2;
            window_widget_box.margin_end = 2;

            realize.connect((w) => {
                    try {
                        string window_state = "";
                        string[] window_modes = {"normal", "maximize", "fullscreen"};
                        if (window_mode != null && window_mode in window_modes) {
                            window_state = window_mode;
                        } else {
                            window_state = config.config_file.get_value("advanced", "use_on_starting");
                        }

                        if (window_state == "maximize") {
                            maximize();
                            get_window().set_shadow_width(0, 0, 0, 0);
                        } else if (window_state == "fullscreen") {
                            toggle_fullscreen();
                            get_window().set_shadow_width(0, 0, 0, 0);
                        } else {
                            if (screen_monitor.is_composited()) {
                                get_window().set_shadow_width(window_frame_margin_start, window_frame_margin_end, window_frame_margin_top, window_frame_margin_bottom);
                            } else {
                                get_window().set_shadow_width(0, 0, 0, 0);
                            }
                        }

                        var width = config.config_file.get_integer("advanced", "window_width");
                        var height = config.config_file.get_integer("advanced", "window_height");
                        if (width == 0 || height == 0) {
                            if (rect.width == 0 || rect.height == 0) {
                                set_default_size(800, 600);
                            } else {
                                set_default_size((int) (rect.width * window_default_scale), (int) (rect.height * window_default_scale));
                            }
                        } else {
                            set_default_size(width, height);
                        }
                    } catch (GLib.KeyFileError e) {
                        stdout.printf(e.message);
                    }
                });

            try{
                set_icon_from_file(Utils.get_image_path("deepin-terminal.svg"));
            } catch(Error er) {
                stdout.printf(er.message);
            }
        }

        public void transparent_window() {
            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());
        }

        public void init_window() {
            set_decorated(false);

            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);

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

                    return false;
                });

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

                    return false;
                });

            configure_event.connect((w) => {
                    // Update input shape.
                    Cairo.RectangleInt rect;
                    get_window().get_frame_extents(out rect);
                    rect.x = 0;
                    rect.y = 0;
                    if (!window_is_fullscreen() && !window_is_max() && screen_monitor.is_composited()) {
                        rect.x = window_frame_box.margin_start - Constant.RESPONSE_RADIUS;
                        rect.y = window_frame_box.margin_top - Constant.RESPONSE_RADIUS;
                        rect.width += - window_frame_box.margin_start - window_frame_box.margin_end + Constant.RESPONSE_RADIUS * 2;
                        rect.height += - window_frame_box.margin_top - window_frame_box.margin_bottom + Constant.RESPONSE_RADIUS * 2;
                    }

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

                    // Update blur area.
                    update_blur_status();

                    return false;
                });

            window_state_event.connect((w, e) => {
                    update_frame();

                    return false;
                });

            window_frame_box.button_press_event.connect((w, e) => {
                    if (!screen_monitor.is_composited()) {
                        if (window_is_normal()) {
                            int pointer_x, pointer_y;
                            e.device.get_position(null, out pointer_x, out pointer_y);

                            var cursor_type = get_frame_cursor_type(e.x_root, e.y_root);
                            if (cursor_type != null) {
                                pointer_x *= get_scale_factor();
                                pointer_y *= get_scale_factor();
                                resize_window(this, pointer_x, pointer_y, (int) e.button, cursor_type);
                            }
                        }
                    }

                    return false;
                });

            button_press_event.connect((w, e) => {
                    if (window_is_normal()) {
                        int pointer_x, pointer_y;
                        e.device.get_position(null, out pointer_x, out pointer_y);

                        var cursor_type = get_cursor_type(e.x_root, e.y_root);
                        if (cursor_type != null) {
                            pointer_x *= get_scale_factor();
                            pointer_y *= get_scale_factor();
                            resize_window(this, pointer_x, pointer_y, (int) e.button, cursor_type);
                        }
                    }

                    return false;
                });

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

                    draw_window_widgets(cr);

                    draw_window_frame(cr);

                    draw_window_above(cr);

                    return true;
                });

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

                    update_blur_status(true);
                });
        }

        public void update_style() {
            clean_style();

            bool is_light_theme = is_light_theme();

            if (is_active) {
                if (window_is_normal()) {
                    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 (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_noradius_shadow_active");
                    } else {
                        window_frame_box.get_style_context().add_class("window_noradius_noshadow_active");
                    }
                }
            } else {
                if (window_is_normal()) {
                    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");
                        }
                    }
                } else {
                    if (screen_monitor.is_composited()) {
                        window_frame_box.get_style_context().add_class("window_noradius_shadow_inactive");
                    } else {
                        window_frame_box.get_style_context().add_class("window_noradius_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 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);
                        blur_rect.x = 0;
                        blur_rect.y = 0;

                        if (!window_is_fullscreen() && !window_is_max() && screen_monitor.is_composited()) {
                            blur_rect.x = window_frame_box.margin_start;
                            blur_rect.y = window_frame_box.margin_top;
                            blur_rect.width += - window_frame_box.margin_start - window_frame_box.margin_end;
                            blur_rect.height += - window_frame_box.margin_top - 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 draw_window_widgets(Cairo.Context cr) {
            Utils.propagate_draw(this, cr);
        }

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

        public bool have_terminal_at_same_workspace() {
            var screen = Wnck.Screen.get_default();
            screen.force_update();

            var active_workspace = screen.get_active_workspace();
            foreach (Wnck.Window window in screen.get_windows()) {
                var workspace = window.get_workspace();
                if (workspace != null && workspace.get_number() == active_workspace.get_number()) {
                    int pid = window.get_pid();
                    string command = Utils.get_proc_file_content("/proc/%i/comm".printf(pid)).strip();
                    if (command == "deepin-terminal") {
                        return true;
                    }
                }
            }

            return false;
        }

        public override void toggle_fullscreen() {
            if (window_is_fullscreen()) {
                unfullscreen();
            } else {
                fullscreen();
            }
        }

        public override void update_frame() {
            update_style();

            if (!screen_monitor.is_composited() || window_is_fullscreen() || window_is_max()) {
                window_widget_box.margin_top = 0;
                window_widget_box.margin_bottom = 0;
                window_widget_box.margin_start = 0;
                window_widget_box.margin_end = 0;
            } else if (window_is_tiled()) {
                window_widget_box.margin_top = 1;
                window_widget_box.margin_bottom = 1;
                window_widget_box.margin_start = 1;
                window_widget_box.margin_end = 1;
            } else {
                window_widget_box.margin_top = 2;
                window_widget_box.margin_bottom = 2;
                window_widget_box.margin_start = 2;
                window_widget_box.margin_end = 2;
            }

            if (!screen_monitor.is_composited() || window_is_fullscreen() || window_is_max()) {
                window_frame_box.margin_top = 0;
                window_frame_box.margin_bottom = 0;
                window_frame_box.margin_start = 0;
                window_frame_box.margin_end = 0;

                get_window().set_shadow_width(0, 0, 0, 0);
            } else {
                window_frame_box.margin_top = window_frame_margin_top;
                window_frame_box.margin_bottom = window_frame_margin_bottom;
                window_frame_box.margin_start = window_frame_margin_start;
                window_frame_box.margin_end = window_frame_margin_end;

                get_window().set_shadow_width(window_frame_margin_start, window_frame_margin_end, window_frame_margin_top, window_frame_margin_bottom);
            }
        }

        public void toggle_max() {
            if (window_is_max()) {
                unmaximize();
            } else {
                maximize();
            }
        }

        public virtual void draw_window_below(Cairo.Context cr) {

        }

        public void draw_window_frame(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;
            Gdk.RGBA frame_color;

            try {
                if (window_is_normal()) {
                    frame_color = Utils.hex_to_rgba(config.config_file.get_string("theme", "background"));

                    if (screen_monitor.is_composited()) {
                        // Draw line *innner* of window frame.
                        cr.save();
                        cr.set_source_rgba(frame_color.red, frame_color.green, frame_color.blue, config.config_file.get_double("general", "opacity"));
                        // Bottom.
                        Draw.draw_rectangle(cr, x + 2, y + height - 2, width - 4, 1);
                        // Left.
                        Draw.draw_rectangle(cr, x + 1, y + Constant.TITLEBAR_HEIGHT + 2, 1, height - Constant.TITLEBAR_HEIGHT - 4);
                        // Right..
                        Draw.draw_rectangle(cr, x + width - 2, y + Constant.TITLEBAR_HEIGHT + 2, 1, height - Constant.TITLEBAR_HEIGHT - 4);
                        cr.restore();
                    } else {
                        // Draw line *innner* of window frame.
                        cr.save();
                        cr.set_source_rgba(frame_color.red, frame_color.green, frame_color.blue, config.config_file.get_double("general", "opacity"));
                        Draw.draw_rectangle(cr, x, y, width, height, false);
                        cr.restore();
                    }
                }
            } catch (Error e) {
                print("Window draw_window_frame: %s\n", e.message);
            }
        }

        public void draw_window_above(Cairo.Context cr) {
            if (screen_monitor.is_composited()) {
                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;
                Gdk.RGBA frame_color = Gdk.RGBA();

                bool is_light_theme = is_light_theme();

                try {
                    frame_color = Utils.hex_to_rgba(config.config_file.get_string("theme", "background"));
                } catch (GLib.KeyFileError e) {
                    print("Window draw_window_above: %s\n", e.message);
                }

                try {
                    if (window_is_fullscreen()) {
                        int titlebar_y = y;
                        if (get_scale_factor() > 1) {
                            titlebar_y -= 1;
                        }

                        if (draw_tabbar_line) {
                            draw_titlebar_underline(cr, x, titlebar_y, width, 1);
                            draw_active_tab_underline(cr, x + active_tab_underline_x - window_frame_box.margin_start, titlebar_y + Constant.TITLEBAR_HEIGHT);
                        }
                    } else if (window_is_max() || window_is_tiled()) {
                        int titlebar_y = y;
                        if (get_scale_factor() > 1) {
                            titlebar_y -= 1;
                        }

                        draw_titlebar_underline(cr, x + 1, titlebar_y, width - 2, 1);
                        draw_active_tab_underline(cr, x + active_tab_underline_x - window_frame_box.margin_start, titlebar_y + Constant.TITLEBAR_HEIGHT + 1);
                    } else {
                        // Draw line above at titlebar.
                        cr.set_source_rgba(frame_color.red, frame_color.green, frame_color.blue, config.config_file.get_double("general", "opacity"));
                        Draw.draw_rectangle(cr, x + 2, y + 1, width - 4, 1);

                        //  if (is_light_theme) {
                        //      Utils.set_context_color(cr, top_line_light_color);
                        //  } else {
                        //      Utils.set_context_color(cr, top_line_dark_color);
                        //  }
                        Draw.draw_rectangle(cr, x + 2, y + 1, width - 4, 1);

                        cr.set_source_rgba(1, 1, 1, 0.0625 * config.config_file.get_double("general", "opacity")); // Draw top line at window.
                        Draw.draw_rectangle(cr, x + 2, y + 1, width - 4, 1);

                        // Draw line around titlebar side.
                        cr.set_source_rgba(frame_color.red, frame_color.green, frame_color.blue, config.config_file.get_double("general", "opacity"));
                        // Left.
                        Draw.draw_rectangle(cr, x + 1, y + 2, 1, Constant.TITLEBAR_HEIGHT);
                        // Right.
                        Draw.draw_rectangle(cr, x + width - 2, y + 2, 1, Constant.TITLEBAR_HEIGHT);

                        //  if (is_light_theme) {
                        //      Utils.set_context_color(cr, top_line_light_color);
                        //  } else {
                        //      Utils.set_context_color(cr, top_line_dark_color);
                        //  }

                        // Left.
                        Draw.draw_rectangle(cr, x + 1, y + 2, 1, Constant.TITLEBAR_HEIGHT);
                        // Right.
                        Draw.draw_rectangle(cr, x + width - 2, y + 2, 1, Constant.TITLEBAR_HEIGHT);

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

                        draw_titlebar_underline(cr, x + 1, titlebar_y, width - 2, 1);
                        draw_active_tab_underline(cr, x + active_tab_underline_x - window_frame_box.margin_start, titlebar_y + Constant.TITLEBAR_HEIGHT);
                    }
                } catch (Error e) {
                    print("Window draw_window_above: %s\n", e.message);
                }
            }
        }

        public void init_fullscreen_handler(Appbar appbar) {
            fullscreen_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
            spacing_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);

            spacing_box.set_size_request(-1, Constant.TITLEBAR_HEIGHT);
            fullscreen_box.pack_start(spacing_box, false, false, 0);

            configure_event.connect((w) => {
                    if (window_is_fullscreen()) {
                        Utils.remove_all_children(fullscreen_box);
                        appbar.hide();
                        appbar.hide_window_button();
                        draw_tabbar_line = false;
                    } else {
                        Gtk.Widget? parent = spacing_box.get_parent();
                        if (parent == null) {
                            fullscreen_box.pack_start(spacing_box, false, false, 0);
                            appbar.show_all();
                            appbar.show_window_button();
                            draw_tabbar_line = true;
                        }
                    }

                    return false;
                });

            motion_notify_event.connect((w, e) => {
                    if (window_is_fullscreen()) {
                        if (e.y_root < window_fullscreen_monitor_height) {
                            GLib.Timeout.add(window_fullscreen_monitor_timeout, () => {
                                    int pointer_x, pointer_y;
                                    Utils.get_pointer_position(out pointer_x, out pointer_y);

                                    if (pointer_y < window_fullscreen_response_height) {
                                        appbar.show_all();
                                        draw_tabbar_line = true;

                                        redraw_window();
                                    } else if (pointer_y > Constant.TITLEBAR_HEIGHT) {
                                        appbar.hide();
                                        draw_tabbar_line = false;

                                        redraw_window();
                                    }

                                    return false;
                                });
                        }
                    }

                    return false;
                });
        }

        public void show_window(TerminalApp app, WorkspaceManager workspace_manager, Tabbar tabbar, bool has_start=false) {
            Appbar appbar = new Appbar(app, this, tabbar, workspace_manager, has_start);

            appbar.set_valign(Gtk.Align.START);
            appbar.close_window.connect((w) => {
                    quit();
                });
            appbar.quit_fullscreen.connect((w) => {
                    toggle_fullscreen();
                });

            init(workspace_manager, tabbar);
            init_fullscreen_handler(appbar);

            window_state_event.connect((w) => {
                    appbar.update_max_button();

                    return false;
                });

            if (!have_terminal_at_same_workspace()) {
                set_position(Gtk.WindowPosition.CENTER);
            }

            var overlay = new Gtk.Overlay();
            top_box.pack_start(fullscreen_box, false, false, 0);
            box.pack_start(top_box, false, false, 0);
            box.pack_start(workspace_manager, true, true, 0);

            overlay.add(box);
            overlay.add_overlay(appbar);

            add_widget(overlay);
            show_all();
        }

        public override void window_save_before_quit() {
            Cairo.RectangleInt rect;
            get_window().get_frame_extents(out rect);

            if (window_is_normal()) {
                config.load_config();
                config.config_file.set_integer("advanced", "window_width", rect.width);
                config.config_file.set_integer("advanced", "window_height", 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 left_side_start = window_x + window_frame_margin_start - Constant.RESPONSE_RADIUS;
            var left_side_end = window_x + window_frame_margin_start;
            var right_side_start = window_x + width - window_frame_margin_end;
            var right_side_end = window_x + width - window_frame_margin_end + Constant.RESPONSE_RADIUS;
            var top_side_start = window_y + window_frame_margin_top - Constant.RESPONSE_RADIUS;;
            var top_side_end = window_y + window_frame_margin_top;
            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 (x > left_side_start && x < left_side_end) {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_LEFT_CORNER;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_LEFT_CORNER;
                }
            } else if (x > right_side_start && x < right_side_end) {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_RIGHT_CORNER;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_RIGHT_CORNER;
                }
            }

            if (x > left_side_start && x < left_side_end) {
                if (y > top_side_end && y < bottom_side_start) {
                    return Gdk.CursorType.LEFT_SIDE;
                }
            } else if (x > right_side_start && x < right_side_end) {
                if (y > top_side_end && y < bottom_side_start) {
                    return Gdk.CursorType.RIGHT_SIDE;
                }
            } else {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_SIDE;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_SIDE;
                }
            }

            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 left_side_start = window_x;
            var left_side_end = window_x + Constant.RESPONSE_RADIUS;
            var right_side_start = window_x + width - Constant.RESPONSE_RADIUS;
            var right_side_end = window_x + width;
            var top_side_start = window_y;
            var top_side_end = window_y + Constant.RESPONSE_RADIUS;
            var bottom_side_start = window_y + height - Constant.RESPONSE_RADIUS;
            var bottom_side_end = window_y + height;

            if (x > left_side_start && x < left_side_end) {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_LEFT_CORNER;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_LEFT_CORNER;
                }
            } else if (x > right_side_start && x < right_side_end) {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_RIGHT_CORNER;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_RIGHT_CORNER;
                }
            }

            if (x > left_side_start && x < left_side_end) {
                if (y > top_side_end && y < bottom_side_start) {
                    return Gdk.CursorType.LEFT_SIDE;
                }
            } else if (x > right_side_start && x < right_side_end) {
                if (y > top_side_end && y < bottom_side_start) {
                    return Gdk.CursorType.RIGHT_SIDE;
                }
            } else {
                if (y > top_side_start && y < top_side_end) {
                    return Gdk.CursorType.TOP_SIDE;
                } else if (y > bottom_side_start && y < bottom_side_end) {
                    return Gdk.CursorType.BOTTOM_SIDE;
                }
            }

            return null;
        }
    }
}