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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
use std::{
    cell::RefCell,
    collections::VecDeque,
    rc::Rc,
    sync::atomic::{AtomicBool, AtomicI32, Ordering},
};

use gdk::{prelude::DisplayExtManual, WindowEdge, WindowState};
use glib::{translate::ToGlibPtr, Cast, ObjectType};
use gtk::{
    traits::{ApplicationWindowExt, ContainerExt, GtkWindowExt, SettingsExt, WidgetExt},
    Inhibit, Settings,
};
use raw_window_handle::{
    RawDisplayHandle, RawWindowHandle, WaylandDisplayHandle, WaylandWindowHandle,
    XlibDisplayHandle, XlibWindowHandle,
};

use crate::{
    dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
    error::{ExternalError, NotSupportedError, OsError as RootOsError},
    platform_impl::WindowId,
    window::{
        CursorGrabMode, CursorIcon, Icon, ImePurpose, ResizeDirection, Theme, UserAttentionType,
        WindowAttributes, WindowButtons, WindowLevel,
    },
};

use super::{
    util, EventLoopWindowTarget, Fullscreen, MonitorHandle, PlatformSpecificWindowBuilderAttributes,
};

// Currently GTK doesn't provide feature for detect theme, so we need to check theme manually.
// ref: https://github.com/WebKit/WebKit/blob/e44ffaa0d999a9807f76f1805943eea204cfdfbc/Source/WebKit/UIProcess/API/gtk/PageClientImpl.cpp#L587
const GTK_THEME_SUFFIX_LIST: [&str; 3] = ["-dark", "-Dark", "-Darker"];

pub(crate) enum WindowRequest {
    Title(String),
    Position((i32, i32)),
    Size((i32, i32)),
    SizeConstraints(Option<Size>, Option<Size>),
    Visible(bool),
    Focus,
    Resizable(bool),
    // Closable(bool),
    Minimized(bool),
    Maximized(bool),
    DragWindow,
    Fullscreen(Option<Fullscreen>),
    Decorations(bool),
    AlwaysOnBottom(bool),
    AlwaysOnTop(bool),
    WindowIcon(Option<Icon>),
    UserAttention(Option<UserAttentionType>),
    SetSkipTaskbar(bool),
    CursorIcon(Option<CursorIcon>),
    CursorPosition((i32, i32)),
    CursorIgnoreEvents(bool),
    WireUpEvents { transparent: Rc<AtomicBool> },
    // SetVisibleOnAllWorkspaces(bool),
    // ProgressBarState(ProgressBarState),
}

pub struct Window {
    /// Window id.
    pub(crate) window_id: WindowId,
    /// Gtk application window.
    pub(crate) window: gtk::ApplicationWindow,
    pub(crate) default_vbox: Option<gtk::Box>,
    /// Window requests sender
    pub(crate) window_requests_tx: glib::Sender<(WindowId, WindowRequest)>,
    scale_factor: Rc<AtomicI32>,
    position: Rc<(AtomicI32, AtomicI32)>,
    size: Rc<(AtomicI32, AtomicI32)>,
    maximized: Rc<AtomicBool>,
    minimized: Rc<AtomicBool>,
    fullscreen: RefCell<Option<Fullscreen>>,
    min_size: RefCell<Option<Size>>,
    max_size: RefCell<Option<Size>>,
    transparent: Rc<AtomicBool>,
    /// Draw event Sender
    draw_tx: crossbeam_channel::Sender<WindowId>,
}
impl Window {
    #[inline]
    pub(crate) fn new<T>(
        window_target: &EventLoopWindowTarget<T>,
        attribs: WindowAttributes,
        pl_attribs: PlatformSpecificWindowBuilderAttributes,
    ) -> Result<Self, RootOsError> {
        let app = &window_target.app;
        let window_requests_tx = window_target.window_requests_tx.clone();
        let draw_tx = window_target.draw_tx.clone();
        let window = gtk::ApplicationWindow::builder()
            .application(app)
            .accept_focus(attribs.active)
            .build();
        let window_id = WindowId(window.id() as u64);
        window_target.windows.borrow_mut().insert(window_id);

        // Set Width/Height & Resizable
        let win_scale_factor = window.scale_factor();
        let (width, height) = attribs
            .inner_size
            .map(|size| size.to_logical::<f64>(win_scale_factor as f64).into())
            .unwrap_or((800, 600));
        window.set_default_size(1, 1);
        window.resize(width, height);

        if attribs.maximized {
            window.maximize();
        }
        window.set_resizable(attribs.resizable);
        // window.set_deletable(attribs.closable);

        // Set Min/Max Size
        util::set_size_constraints(&window, attribs.min_inner_size, attribs.max_inner_size);

        // Set Position
        if let Some(position) = attribs.position {
            let (x, y): (i32, i32) = position.to_logical::<i32>(win_scale_factor as f64).into();
            window.move_(x, y);
        }

        // Set GDK Visual
        if pl_attribs.rgba_visual || attribs.transparent {
            if let Some(screen) = GtkWindowExt::screen(&window) {
                if let Some(visual) = screen.rgba_visual() {
                    window.set_visual(Some(&visual));
                }
            }
        }

        if pl_attribs.app_paintable || attribs.transparent {
            // Set a few attributes to make the window can be painted.
            // See Gtk drawing model for more info:
            // https://docs.gtk.org/gtk3/drawing-model.html
            window.set_app_paintable(true);
        }

        if !pl_attribs.double_buffered {
            let widget = window.upcast_ref::<gtk::Widget>();
            if !window_target.is_wayland() {
                unsafe {
                    gtk::ffi::gtk_widget_set_double_buffered(widget.to_glib_none().0, 0);
                }
            }
        }

        // Add vbox for utilities like menu
        let default_vbox = if pl_attribs.default_vbox {
            let box_ = gtk::Box::new(gtk::Orientation::Vertical, 0);
            window.add(&box_);
            Some(box_)
        } else {
            None
        };

        // Rest attributes
        window.set_title(&attribs.title);
        let fullscreen = attribs.fullscreen.map(|f| f.into());
        let m = match fullscreen {
            Some(Fullscreen::Exclusive(ref m)) => Some(&m.monitor),
            Some(Fullscreen::Borderless(Some(ref m))) => Some(&m.monitor),
            _ => None,
        };
        if let Some(monitor) = m {
            let display = window.display();
            let monitors = display.n_monitors();
            for i in 0..monitors {
                let m = display.monitor(i).unwrap();
                if &m == monitor {
                    let screen = display.default_screen();
                    window.fullscreen_on_monitor(&screen, i);
                }
            }
        } else {
            window.fullscreen();
        }
        window.set_visible(attribs.visible);
        window.set_decorated(attribs.decorations);

        match attribs.window_level {
            WindowLevel::AlwaysOnBottom => window.set_keep_below(true),
            WindowLevel::Normal => (),
            WindowLevel::AlwaysOnTop => window.set_keep_above(true),
        }

        // TODO imeplement this?
        // if attribs.visible_on_all_workspaces {
        //     window.stick();
        // }

        if let Some(icon) = attribs.window_icon {
            window.set_icon(Some(&icon.inner.into()));
        }

        // Set theme
        let settings = Settings::default();

        if let Some(settings) = settings {
            if let Some(preferred_theme) = attribs.preferred_theme {
                match preferred_theme {
                    Theme::Dark => settings.set_gtk_application_prefer_dark_theme(true),
                    Theme::Light => {
                        let theme_name = settings.gtk_theme_name().map(|t| t.as_str().to_owned());
                        if let Some(theme) = theme_name {
                            // Remove dark variant.
                            if let Some(theme) = GTK_THEME_SUFFIX_LIST
                                .iter()
                                .find(|t| theme.ends_with(*t))
                                .map(|v| theme.strip_suffix(v))
                            {
                                settings.set_gtk_theme_name(theme);
                            }
                        }
                    }
                }
            }
        }

        if attribs.visible {
            window.show_all();
        } else {
            window.hide();
        }

        // TODO it's impossible to set parent window from raw handle.
        // We need a gtk variant of it.
        if let Some(parent) = pl_attribs.parent {
            window.set_transient_for(Some(&parent));
        }

        // TODO I don't understand why unfocussed window need focus
        // restore accept-focus after the window has been drawn
        // if the window was initially created without focus
        // if !attributes.focused {
        //     let signal_id = Arc::new(RefCell::new(None));
        //     let signal_id_ = signal_id.clone();
        //     let id = window.connect_draw(move |window, _| {
        //         if let Some(id) = signal_id_.take() {
        //             window.set_accept_focus(true);
        //             window.disconnect(id);
        //         }
        //         Inhibit(false)
        //     });
        //     signal_id.borrow_mut().replace(id);
        // }

        // Set window position and size callback
        let w_pos = window.position();
        let position: Rc<(AtomicI32, AtomicI32)> = Rc::new((w_pos.0.into(), w_pos.1.into()));
        let position_clone = position.clone();

        let w_size = window.size();
        let size: Rc<(AtomicI32, AtomicI32)> = Rc::new((w_size.0.into(), w_size.1.into()));
        let size_clone = size.clone();

        window.connect_configure_event(move |_, event| {
            let (x, y) = event.position();
            position_clone.0.store(x, Ordering::Release);
            position_clone.1.store(y, Ordering::Release);

            let (w, h) = event.size();
            size_clone.0.store(w as i32, Ordering::Release);
            size_clone.1.store(h as i32, Ordering::Release);

            false
        });

        // Set minimized/maximized callback
        let w_max = window.is_maximized();
        let maximized: Rc<AtomicBool> = Rc::new(w_max.into());
        let max_clone = maximized.clone();
        let minimized = Rc::new(AtomicBool::new(false));
        let minimized_clone = minimized.clone();

        window.connect_window_state_event(move |_, event| {
            let state = event.new_window_state();
            max_clone.store(state.contains(WindowState::MAXIMIZED), Ordering::Release);
            minimized_clone.store(state.contains(WindowState::ICONIFIED), Ordering::Release);
            Inhibit(false)
        });

        // Set scale factor callback
        let scale_factor: Rc<AtomicI32> = Rc::new(win_scale_factor.into());
        let scale_factor_clone = scale_factor.clone();
        window.connect_scale_factor_notify(move |window| {
            scale_factor_clone.store(window.scale_factor(), Ordering::Release);
        });

        // Check if we should paint the transparent background ourselves.
        let mut transparent = false;
        if attribs.transparent && pl_attribs.auto_transparent {
            transparent = true;
        }
        let transparent = Rc::new(AtomicBool::new(transparent));

        // Send WireUp event to let eventloop handle the rest of window setup to prevent gtk panic
        // in other thread.
        if let Err(e) = window_requests_tx.send((
            window_id,
            WindowRequest::WireUpEvents {
                transparent: transparent.clone(),
            },
        )) {
            log::warn!("Fail to send wire up events request: {}", e);
        }

        if let Err(e) = draw_tx.send(window_id) {
            log::warn!("Failed to send redraw event to event channel: {}", e);
        }

        let win = Self {
            window_id,
            window,
            default_vbox,
            window_requests_tx,
            draw_tx,
            scale_factor,
            position,
            size,
            maximized,
            minimized,
            fullscreen: RefCell::new(fullscreen),
            min_size: RefCell::new(attribs.min_inner_size),
            max_size: RefCell::new(attribs.min_inner_size),
            transparent,
        };

        win.set_skip_taskbar(pl_attribs.skip_taskbar);

        Ok(win)
    }

    #[inline]
    pub fn id(&self) -> WindowId {
        self.window_id
    }

    #[inline]
    pub fn set_title(&self, title: &str) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Title(title.to_string())))
        {
            log::warn!("Fail to send title request: {}", e);
        }
    }

    #[inline]
    pub fn set_transparent(&self, transparent: bool) {
        self.transparent.store(transparent, Ordering::Relaxed);
    }

    #[inline]
    pub fn set_visible(&self, visible: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Visible(visible)))
        {
            log::warn!("Fail to send visible request: {}", e);
        }
    }

    #[inline]
    pub fn is_visible(&self) -> Option<bool> {
        Some(self.window.is_visible())
    }

    #[inline]
    pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
        let (x, y) = &*self.position;
        Ok(
            LogicalPosition::new(x.load(Ordering::Acquire), y.load(Ordering::Acquire))
                .to_physical(self.scale_factor.load(Ordering::Acquire) as f64),
        )
    }
    #[inline]
    pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
        let (x, y) = &*self.position;
        Ok(
            LogicalPosition::new(x.load(Ordering::Acquire), y.load(Ordering::Acquire))
                .to_physical(self.scale_factor.load(Ordering::Acquire) as f64),
        )
    }
    #[inline]
    pub fn set_outer_position(&self, position: Position) {
        let (x, y): (i32, i32) = position.to_logical::<i32>(self.scale_factor()).into();

        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Position((x, y))))
        {
            log::warn!("Fail to send position request: {}", e);
        }
    }
    #[inline]
    pub fn inner_size(&self) -> PhysicalSize<u32> {
        let (width, height) = &*self.size;

        LogicalSize::new(
            width.load(Ordering::Acquire) as u32,
            height.load(Ordering::Acquire) as u32,
        )
        .to_physical(self.scale_factor.load(Ordering::Acquire) as f64)
    }

    #[inline]
    pub fn outer_size(&self) -> PhysicalSize<u32> {
        let (width, height) = &*self.size;

        LogicalSize::new(
            width.load(Ordering::Acquire) as u32,
            height.load(Ordering::Acquire) as u32,
        )
        .to_physical(self.scale_factor.load(Ordering::Acquire) as f64)
    }

    #[inline]
    pub fn set_inner_size(&self, size: Size) {
        let (width, height) = size.to_logical::<i32>(self.scale_factor()).into();

        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Size((width, height))))
        {
            log::warn!("Fail to send size request: {}", e);
        }
    }

    fn set_size_constraints(&self) {
        if let Err(e) = self.window_requests_tx.send((
            self.window_id,
            WindowRequest::SizeConstraints(*self.min_size.borrow(), *self.max_size.borrow()),
        )) {
            log::warn!("Fail to send size constraint request: {}", e);
        }
    }

    #[inline]
    pub fn set_min_inner_size(&self, dimensions: Option<Size>) {
        let mut min_size = self.min_size.borrow_mut();
        *min_size = dimensions;
        self.set_size_constraints()
    }

    #[inline]
    pub fn set_max_inner_size(&self, dimensions: Option<Size>) {
        let mut max_size = self.min_size.borrow_mut();
        *max_size = dimensions;
        self.set_size_constraints()
    }

    #[inline]
    pub fn resize_increments(&self) -> Option<PhysicalSize<u32>> {
        // TODO implement this
        None
    }

    #[inline]
    pub fn set_resize_increments(&self, _increments: Option<Size>) {
        // TODO implement this
    }

    #[inline]
    pub fn set_resizable(&self, resizable: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Resizable(resizable)))
        {
            log::warn!("Fail to send resizable request: {}", e);
        }
    }

    #[inline]
    pub fn is_resizable(&self) -> bool {
        self.window.is_resizable()
    }

    #[inline]
    pub fn set_enabled_buttons(&self, _buttons: WindowButtons) {
        // TODO implement this
    }

    #[inline]
    pub fn enabled_buttons(&self) -> WindowButtons {
        // TODO implement this
        WindowButtons::empty()
    }

    #[inline]
    pub fn set_cursor_icon(&self, cursor: CursorIcon) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::CursorIcon(Some(cursor))))
        {
            log::warn!("Fail to send cursor icon request: {}", e);
        }
    }

    #[inline]
    pub fn set_cursor_grab(&self, _mode: CursorGrabMode) -> Result<(), ExternalError> {
        // TODO implement this
        Ok(())
    }

    #[inline]
    pub fn set_cursor_visible(&self, visible: bool) {
        let cursor = if visible {
            Some(CursorIcon::Default)
        } else {
            None
        };
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::CursorIcon(cursor)))
        {
            log::warn!("Fail to send cursor visibility request: {}", e);
        }
    }

    #[inline]
    pub fn drag_window(&self) -> Result<(), ExternalError> {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::DragWindow))
        {
            log::warn!("Fail to send drag window request: {}", e);
        }
        Ok(())
    }

    #[inline]
    pub fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), ExternalError> {
        // TODO implement this
        Ok(())
    }

    #[inline]
    pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::CursorIgnoreEvents(!hittest)))
        {
            log::warn!("Fail to send cursor position request: {}", e);
        }

        Ok(())
    }

    #[inline]
    pub fn scale_factor(&self) -> f64 {
        self.scale_factor.load(Ordering::Acquire) as f64
    }

    #[inline]
    pub fn set_cursor_position(&self, position: Position) -> Result<(), ExternalError> {
        let inner_pos = self.inner_position().unwrap_or_default();
        let (x, y): (i32, i32) = position.to_logical::<i32>(self.scale_factor()).into();

        if let Err(e) = self.window_requests_tx.send((
            self.window_id,
            WindowRequest::CursorPosition((x + inner_pos.x, y + inner_pos.y)),
        )) {
            log::warn!("Fail to send cursor position request: {}", e);
        }

        Ok(())
    }

    #[inline]
    pub fn set_maximized(&self, maximized: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Maximized(maximized)))
        {
            log::warn!("Fail to send maximized request: {}", e);
        }
    }

    #[inline]
    pub fn is_maximized(&self) -> bool {
        self.maximized.load(Ordering::Acquire)
    }

    #[inline]
    pub fn set_minimized(&self, minimized: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Minimized(minimized)))
        {
            log::warn!("Fail to send minimized request: {}", e);
        }
    }

    #[inline]
    pub fn is_minimized(&self) -> Option<bool> {
        Some(self.minimized.load(Ordering::Acquire))
    }

    #[inline]
    pub(crate) fn fullscreen(&self) -> Option<Fullscreen> {
        self.fullscreen.borrow().clone()
    }

    #[inline]
    pub(crate) fn set_fullscreen(&self, monitor: Option<Fullscreen>) {
        self.fullscreen.replace(monitor.clone());
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Fullscreen(monitor)))
        {
            log::warn!("Fail to send fullscreen request: {}", e);
        }
    }

    #[inline]
    pub fn set_decorations(&self, decorations: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::Decorations(decorations)))
        {
            log::warn!("Fail to send decorations request: {}", e);
        }
    }

    #[inline]
    pub fn is_decorated(&self) -> bool {
        self.window.is_decorated()
    }

    #[inline]
    pub fn set_window_level(&self, level: WindowLevel) {
        match level {
            WindowLevel::AlwaysOnBottom => {
                if let Err(e) = self
                    .window_requests_tx
                    .send((self.window_id, WindowRequest::AlwaysOnTop(true)))
                {
                    log::warn!("Fail to send always on top request: {}", e);
                }
            }
            WindowLevel::Normal => (),
            WindowLevel::AlwaysOnTop => {
                if let Err(e) = self
                    .window_requests_tx
                    .send((self.window_id, WindowRequest::AlwaysOnBottom(true)))
                {
                    log::warn!("Fail to send always on bottom request: {}", e);
                }
            }
        }
    }

    #[inline]
    pub fn set_window_icon(&self, window_icon: Option<Icon>) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::WindowIcon(window_icon)))
        {
            log::warn!("Fail to send window icon request: {}", e);
        }
    }

    #[inline]
    pub fn set_ime_position(&self, _position: Position) {
        // TODO implement this
    }

    #[inline]
    pub fn set_ime_allowed(&self, _allowed: bool) {
        // TODO implement this
    }

    #[inline]
    pub fn set_ime_purpose(&self, _purpose: ImePurpose) {
        // TODO implement this
    }

    #[inline]
    pub fn focus_window(&self) {
        if !self.minimized.load(Ordering::Acquire) && self.window.get_visible() {
            if let Err(e) = self
                .window_requests_tx
                .send((self.window_id, WindowRequest::Focus))
            {
                log::warn!("Fail to send visible request: {}", e);
            }
        }
    }

    pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::UserAttention(request_type)))
        {
            log::warn!("Fail to send user attention request: {}", e);
        }
    }

    #[inline]
    pub fn request_redraw(&self) {
        if let Err(e) = self.draw_tx.send(self.window_id) {
            log::warn!("Failed to send redraw event to event channel: {}", e);
        }
    }

    #[inline]
    pub fn current_monitor(&self) -> Option<MonitorHandle> {
        let display = self.window.display();
        // `.window()` returns `None` if the window is invisible;
        // we fallback to the primary monitor
        self.window
            .window()
            .map(|window| display.monitor_at_window(&window))
            .unwrap_or_else(|| display.primary_monitor())
            .map(|monitor| MonitorHandle { monitor })
    }

    #[inline]
    pub fn available_monitors(&self) -> VecDeque<MonitorHandle> {
        let mut handles = VecDeque::new();
        let display = self.window.display();
        let numbers = display.n_monitors();

        for i in 0..numbers {
            let monitor = MonitorHandle::new(&display, i);
            handles.push_back(monitor);
        }

        handles
    }

    #[inline]
    pub fn primary_monitor(&self) -> Option<MonitorHandle> {
        let display = self.window.display();
        display
            .primary_monitor()
            .map(|monitor| MonitorHandle { monitor })
    }

    fn is_wayland(&self) -> bool {
        self.window.display().backend().is_wayland()
    }

    #[inline]
    pub fn raw_window_handle(&self) -> RawWindowHandle {
        if self.is_wayland() {
            let mut window_handle = WaylandWindowHandle::empty();
            if let Some(window) = self.window.window() {
                window_handle.surface = unsafe {
                    gdk_wayland_sys::gdk_wayland_window_get_wl_surface(window.as_ptr() as *mut _)
                };
            }

            RawWindowHandle::Wayland(window_handle)
        } else {
            let mut window_handle = XlibWindowHandle::empty();
            unsafe {
                if let Some(window) = self.window.window() {
                    window_handle.window =
                        gdk_x11_sys::gdk_x11_window_get_xid(window.as_ptr() as *mut _);
                }
            }
            RawWindowHandle::Xlib(window_handle)
        }
    }

    #[inline]
    pub fn raw_display_handle(&self) -> RawDisplayHandle {
        if self.is_wayland() {
            let mut display_handle = WaylandDisplayHandle::empty();
            display_handle.display = unsafe {
                gdk_wayland_sys::gdk_wayland_display_get_wl_display(
                    self.window.display().as_ptr() as *mut _
                )
            };
            RawDisplayHandle::Wayland(display_handle)
        } else {
            let mut display_handle = XlibDisplayHandle::empty();
            unsafe {
                if let Ok(xlib) = x11_dl::xlib::Xlib::open() {
                    let display = (xlib.XOpenDisplay)(std::ptr::null());
                    display_handle.display = display as _;
                    display_handle.screen = (xlib.XDefaultScreen)(display) as _;
                }
            }
            RawDisplayHandle::Xlib(display_handle)
        }
    }

    #[inline]
    pub fn set_theme(&self, theme: Option<Theme>) {
        if let Some(settings) = Settings::default() {
            if let Some(preferred_theme) = theme {
                match preferred_theme {
                    Theme::Dark => settings.set_gtk_application_prefer_dark_theme(true),
                    Theme::Light => {
                        let theme_name = settings.gtk_theme_name().map(|t| t.as_str().to_owned());
                        if let Some(theme) = theme_name {
                            // Remove dark variant.
                            if let Some(theme) = GTK_THEME_SUFFIX_LIST
                                .iter()
                                .find(|t| theme.ends_with(*t))
                                .map(|v| theme.strip_suffix(v))
                            {
                                settings.set_gtk_theme_name(theme);
                            }
                        }
                    }
                }
            }
        }
    }

    #[inline]
    pub fn theme(&self) -> Option<Theme> {
        if let Some(settings) = Settings::default() {
            let theme_name = settings.gtk_theme_name().map(|s| s.as_str().to_owned());
            if let Some(theme) = theme_name {
                if GTK_THEME_SUFFIX_LIST.iter().any(|t| theme.ends_with(t)) {
                    return Some(Theme::Dark);
                }
            }
        }
        Some(Theme::Light)
    }

    #[inline]
    pub fn has_focus(&self) -> bool {
        self.window.is_active()
    }

    pub fn title(&self) -> String {
        self.window
            .title()
            .map(|t| t.as_str().to_string())
            .unwrap_or_default()
    }

    pub fn set_skip_taskbar(&self, skip: bool) {
        if let Err(e) = self
            .window_requests_tx
            .send((self.window_id, WindowRequest::SetSkipTaskbar(skip)))
        {
            log::warn!("Fail to send skip taskbar request: {}", e);
        }
    }
}

// We need to keep GTK window which isn't thread safe.
// We make sure all non thread safe window calls are sent to event loop to handle.
unsafe impl Send for Window {}
unsafe impl Sync for Window {}

/// A constant used to determine how much inside the window, the resize handler should appear (only used in Linux(gtk) and Windows).
/// You probably need to scale it by the scale_factor of the window.
pub const BORDERLESS_RESIZE_INSET: i32 = 5;

pub fn hit_test(window: &gdk::Window, cx: f64, cy: f64) -> WindowEdge {
    let (left, top) = window.position();
    let (w, h) = (window.width(), window.height());
    let (right, bottom) = (left + w, top + h);
    let (cx, cy) = (cx as i32, cy as i32);

    const LEFT: i32 = 0b0001;
    const RIGHT: i32 = 0b0010;
    const TOP: i32 = 0b0100;
    const BOTTOM: i32 = 0b1000;
    const TOPLEFT: i32 = TOP | LEFT;
    const TOPRIGHT: i32 = TOP | RIGHT;
    const BOTTOMLEFT: i32 = BOTTOM | LEFT;
    const BOTTOMRIGHT: i32 = BOTTOM | RIGHT;

    let inset = BORDERLESS_RESIZE_INSET * window.scale_factor();
    #[rustfmt::skip]
  let result =
      (LEFT * (if cx < (left + inset) { 1 } else { 0 }))
    | (RIGHT * (if cx >= (right - inset) { 1 } else { 0 }))
    | (TOP * (if cy < (top + inset) { 1 } else { 0 }))
    | (BOTTOM * (if cy >= (bottom - inset) { 1 } else { 0 }));

    match result {
        LEFT => WindowEdge::West,
        TOP => WindowEdge::North,
        RIGHT => WindowEdge::East,
        BOTTOM => WindowEdge::South,
        TOPLEFT => WindowEdge::NorthWest,
        TOPRIGHT => WindowEdge::NorthEast,
        BOTTOMLEFT => WindowEdge::SouthWest,
        BOTTOMRIGHT => WindowEdge::SouthEast,
        // we return `WindowEdge::__Unknown` to be ignored later.
        // we must return 8 or bigger, otherwise it will be the same as one of the other 7 variants of `WindowEdge` enum.
        _ => WindowEdge::__Unknown(8),
    }
}