versoview/
window.rs

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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
use std::{cell::Cell, collections::HashMap};

use base::id::WebViewId;
use constellation_traits::EmbedderToConstellationMessage;
use crossbeam_channel::Sender;
use embedder_traits::{
    AlertResponse, AllowOrDeny, ConfirmResponse, Cursor, EmbedderMsg, ImeEvent, InputEvent,
    MouseButton, MouseButtonAction, MouseButtonEvent, MouseMoveEvent, Notification, PromptResponse,
    TouchEventType, ViewportDetails, WebDriverJSValue, WebResourceResponseMsg, WheelMode,
};
use euclid::{Point2D, Scale, Size2D};
use glutin::{
    config::{ConfigTemplateBuilder, GlConfig},
    surface::{Surface, WindowSurface},
};
use glutin_winit::DisplayBuilder;
use ipc_channel::ipc::IpcSender;
use keyboard_types::{
    Code, CompositionEvent, CompositionState, KeyState, KeyboardEvent, Modifiers,
};
#[cfg(any(target_os = "macos", target_os = "windows"))]
use muda::{MenuEvent, MenuEventReceiver};
#[cfg(linux)]
use notify_rust::Image;
#[cfg(target_os = "macos")]
use raw_window_handle::HasWindowHandle;
use reqwest::Client;
use servo_url::ServoUrl;
use versoview_messages::ToControllerMessage;
use webrender_api::{
    ScrollLocation,
    units::{DeviceIntPoint, DevicePoint, DeviceRect, DeviceSize, LayoutVector2D},
};
#[cfg(any(linux, target_os = "windows"))]
use winit::window::ResizeDirection;
use winit::{
    dpi::{LogicalPosition, LogicalSize, PhysicalPosition},
    event::{ElementState, Ime, TouchPhase, WindowEvent},
    event_loop::ActiveEventLoop,
    keyboard::ModifiersState,
    window::{CursorIcon, Window as WinitWindow, WindowAttributes, WindowId},
};

use crate::{
    bookmark::BookmarkManager,
    compositor::IOCompositor,
    keyboard::keyboard_event_from_winit,
    rendering::{RenderingContext, gl_config_picker},
    tab::TabManager,
    verso::{VersoInternalMsg, send_to_constellation},
    webview::{Panel, WebView, execute_script, prompt::PromptSender, webview_menu::WebViewMenu},
};

use arboard::Clipboard;

const PANEL_HEIGHT: f64 = 50.0;
const TAB_HEIGHT: f64 = 30.0;
const BOOKMARK_HEIGHT: f64 = 30.0;
const PANEL_PADDING: f64 = 4.0;

#[derive(Default)]
pub(crate) struct EventListeners {
    /// This is `true` if the controller wants to get and handle OnNavigationStarting/AllowNavigationRequest
    pub(crate) on_navigation_starting: bool,
    /// An id to request response sender map if the controller wants to get and handle web resource requests
    pub(crate) on_web_resource_requested:
        Option<HashMap<uuid::Uuid, (url::Url, IpcSender<WebResourceResponseMsg>)>>,
    /// This is `true` if the controller wants to get and handle WindowEvent::CloseRequested
    pub(crate) on_close_requested: bool,
}

#[derive(Debug, Default)]
struct CursorState {
    current_cursor: CursorIcon,
    #[cfg(any(linux, target_os = "windows"))]
    cursor_resizing: bool,
}

/// A Verso window is a Winit window containing several web views.
pub struct Window {
    /// Access to Winit window
    pub(crate) window: WinitWindow,
    cursor_state: CursorState,
    /// GL surface of the window
    pub(crate) surface: Surface<WindowSurface>,
    /// The main panel of this window.
    pub(crate) panel: Option<Panel>,
    /// The WebView of this window.
    // pub(crate) webview: Option<WebView>,
    /// Event listeners registered from the webview controller
    pub(crate) event_listeners: EventListeners,
    /// The mouse physical position in the web view.
    pub(crate) mouse_position: Cell<Option<PhysicalPosition<f64>>>,
    /// Modifiers state of the keyboard.
    modifiers_state: Cell<ModifiersState>,
    /// State to indicate if the window is resizing.
    pub(crate) resizing: bool,
    /// Global menu event receiver for muda crate
    #[cfg(any(target_os = "macos", target_os = "windows"))]
    pub(crate) menu_event_receiver: MenuEventReceiver,
    /// Window tabs manager
    pub(crate) tab_manager: TabManager,
    pub(crate) focused_webview_id: Option<WebViewId>,
    /// Window-wide menu. e.g. context menu(Wayland) and browsing history menu.
    pub(crate) webview_menu: Option<Box<dyn WebViewMenu>>,
    /// Show the bookmark bar or not
    pub show_bookmark: bool,
    /// The reqwest client
    pub(crate) reqwest_client: Client,
    /// The sender for the Verso internal channel
    pub(crate) verso_internal_sender: IpcSender<VersoInternalMsg>,
}

impl Window {
    /// Create a Verso window from Winit window and return the rendering context.
    pub fn new(
        evl: &ActiveEventLoop,
        window_attributes: WindowAttributes,
        verso_internal_sender: IpcSender<VersoInternalMsg>,
    ) -> (Self, RenderingContext) {
        let template = ConfigTemplateBuilder::new()
            .with_alpha_size(8)
            .with_transparency(cfg!(macos));

        let (window, gl_config) = DisplayBuilder::new()
            .with_window_attributes(Some(window_attributes))
            .build(evl, template, gl_config_picker)
            .expect("Failed to create window and gl config");

        let window = window.ok_or("Failed to create window").unwrap();

        log::debug!("Picked a config with {} samples", gl_config.num_samples());

        #[cfg(macos)]
        unsafe {
            let rwh = window.window_handle().expect("Failed to get window handle");
            if let RawWindowHandle::AppKit(AppKitWindowHandle { ns_view, .. }) = rwh.as_ref() {
                decorate_window(
                    ns_view.as_ptr() as *mut AnyObject,
                    LogicalPosition::new(8.0, 40.0),
                );
            }
        }
        let (rendering_context, surface) =
            RenderingContext::create(&window, &gl_config, window.inner_size())
                .expect("Failed to create rendering context");
        log::trace!("Created rendering context for window {:?}", window);

        (
            Self {
                window,
                cursor_state: CursorState::default(),
                surface,
                panel: None,
                event_listeners: Default::default(),
                mouse_position: Default::default(),
                modifiers_state: Cell::new(ModifiersState::default()),
                resizing: false,
                #[cfg(any(target_os = "macos", target_os = "windows"))]
                menu_event_receiver: MenuEvent::receiver().clone(),
                tab_manager: TabManager::new(),
                focused_webview_id: None,
                webview_menu: None,
                show_bookmark: false,
                reqwest_client: Client::new(),
                verso_internal_sender,
            },
            rendering_context,
        )
    }

    /// Create a Verso window with the rendering context.
    pub fn new_with_compositor(
        evl: &ActiveEventLoop,
        window_attributes: WindowAttributes,
        compositor: &mut IOCompositor,
        verso_internal_sender: IpcSender<VersoInternalMsg>,
    ) -> Self {
        let window = evl
            .create_window(window_attributes)
            .expect("Failed to create window.");

        #[cfg(macos)]
        unsafe {
            let rwh = window.window_handle().expect("Failed to get window handle");
            if let RawWindowHandle::AppKit(AppKitWindowHandle { ns_view, .. }) = rwh.as_ref() {
                decorate_window(
                    ns_view.as_ptr() as *mut AnyObject,
                    LogicalPosition::new(8.0, 40.0),
                );
            }
        }
        let surface = compositor
            .rendering_context
            .create_surface(&window)
            .unwrap();

        let mut window = Self {
            window,
            cursor_state: CursorState::default(),
            surface,
            panel: None,
            // webview: None,
            event_listeners: Default::default(),
            mouse_position: Default::default(),
            modifiers_state: Cell::new(ModifiersState::default()),
            resizing: false,
            #[cfg(any(target_os = "macos", target_os = "windows"))]
            menu_event_receiver: MenuEvent::receiver().clone(),
            tab_manager: TabManager::new(),
            focused_webview_id: None,
            webview_menu: None,
            show_bookmark: false,
            reqwest_client: Client::new(),
            verso_internal_sender,
        };
        compositor.swap_current_window(&mut window);
        window
    }

    /// Get the content area size for the webview to draw on
    pub fn get_content_size(
        &self,
        mut size: DeviceRect,
        include_tab: bool,
        include_bookmark: bool,
    ) -> DeviceRect {
        if self.panel.is_some() {
            let mut height: f64 = PANEL_HEIGHT + PANEL_PADDING;
            if include_tab {
                height += TAB_HEIGHT;
            }
            if include_bookmark {
                height += BOOKMARK_HEIGHT;
            }
            height *= self.scale_factor();
            size.min.y = size.max.y.min(height as f32);
            size.min.x += 10.0;
            size.max.y -= 10.0;
            size.max.x -= 10.0;
        }
        size
    }

    /// Send the constellation message to start Panel UI
    pub fn create_panel(
        &mut self,
        constellation_sender: &Sender<EmbedderToConstellationMessage>,
        initial_url: url::Url,
    ) {
        let hidpi_scale_factor = Scale::new(self.scale_factor() as f32);
        let size = self.window.inner_size();
        let size = Size2D::new(size.width as f32, size.height as f32);
        let size = size.to_f32() / hidpi_scale_factor;
        let viewport_details = ViewportDetails {
            size,
            hidpi_scale_factor,
        };

        let panel_id = WebViewId::new();
        self.panel = Some(Panel {
            webview: WebView::new(panel_id, viewport_details),
            initial_url: ServoUrl::from_url(initial_url),
        });

        let url = ServoUrl::parse("verso://resources/components/panel.html").unwrap();

        send_to_constellation(
            constellation_sender,
            EmbedderToConstellationMessage::NewWebView(url, panel_id, viewport_details),
        );
    }

    /// Create a new webview and send the constellation message to load the initial URL
    pub fn create_tab(
        &mut self,
        constellation_sender: &Sender<EmbedderToConstellationMessage>,
        initial_url: ServoUrl,
    ) {
        let webview_id = WebViewId::new();
        let size = self.size().to_f32();
        let rect = DeviceRect::from_size(size);

        let show_tab = self.tab_manager.count() >= 1;
        let content_size = self.get_content_size(rect, show_tab, self.show_bookmark);

        let hidpi_scale_factor = Scale::new(self.scale_factor() as f32);
        let size = content_size.size().to_f32() / hidpi_scale_factor;
        let viewport_details = ViewportDetails {
            size,
            hidpi_scale_factor,
        };

        let mut webview = WebView::new(webview_id, viewport_details);
        webview.set_size(content_size);

        if let Some(panel) = &self.panel {
            let cmd: String = format!(
                "window.navbar.addTab('{}', {})",
                serde_json::to_string(&webview.webview_id).unwrap(),
                true,
            );

            let _ = execute_script(constellation_sender, &panel.webview.webview_id, cmd);
        }

        self.tab_manager.append_tab(webview, true);

        send_to_constellation(
            constellation_sender,
            EmbedderToConstellationMessage::NewWebView(initial_url, webview_id, viewport_details),
        );
        log::debug!("Verso Window {:?} adds webview {}", self.id(), webview_id);
    }

    /// Close a tab
    pub fn close_tab(&mut self, compositor: &mut IOCompositor, tab_id: WebViewId) {
        // if there are more than 2 tabs, we need to ask for the new active tab after tab is closed
        if self.tab_manager.count() > 1 {
            if let Some(panel) = &self.panel {
                let cmd: String = format!(
                    "window.navbar.closeTab('{}')",
                    serde_json::to_string(&tab_id).unwrap()
                );

                let active_tab_id = execute_script(
                    &compositor.constellation_chan,
                    &panel.webview.webview_id,
                    cmd,
                )
                .unwrap();

                if let WebDriverJSValue::String(resp) = active_tab_id {
                    let active_id: WebViewId = serde_json::from_str(&resp).unwrap();
                    self.activate_tab(compositor, active_id, self.tab_manager.count() > 2);
                }
            }
        }
        send_to_constellation(
            &compositor.constellation_chan,
            EmbedderToConstellationMessage::CloseWebView(tab_id),
        );
    }

    /// Activate a tab
    pub fn activate_tab(
        &mut self,
        compositor: &mut IOCompositor,
        tab_id: WebViewId,
        show_tab: bool,
    ) {
        let size = self.size().to_f32();
        let rect = DeviceRect::from_size(size);
        let content_size = self.get_content_size(rect, show_tab, self.show_bookmark);
        let (tab_id, prompt_id) = self.tab_manager.set_size(tab_id, content_size);

        if let Some(prompt_id) = prompt_id {
            compositor.on_resize_webview_event(prompt_id, content_size);
        }
        if let Some(tab_id) = tab_id {
            compositor.on_resize_webview_event(tab_id, content_size);

            let old_tab_id = self.tab_manager.current_tab_id();
            if self.tab_manager.activate_tab(tab_id).is_some() {
                // throttle the old tab to avoid unnecessary animation caclulations
                if let Some(old_tab_id) = old_tab_id {
                    let _ = compositor.constellation_chan.send(
                        EmbedderToConstellationMessage::SetWebViewThrottled(old_tab_id, true),
                    );
                }
                let _ = compositor.constellation_chan.send(
                    EmbedderToConstellationMessage::SetWebViewThrottled(tab_id, false),
                );

                self.focused_webview_id = Some(tab_id);
                let _ = compositor
                    .constellation_chan
                    .send(EmbedderToConstellationMessage::FocusWebView(tab_id));

                // Set navigation button enabled state
                let history = self.tab_manager.history(tab_id).unwrap();
                let prev_btn_enabled = history.current_idx > 0;
                let next_btn_enabled = history.current_idx < history.list.len() - 1;
                let _ = execute_script(
                    &compositor.constellation_chan,
                    &self.panel.as_ref().unwrap().webview.webview_id,
                    format!(
                        "window.navbar.setNavBtnEnabled({}, {})",
                        prev_btn_enabled, next_btn_enabled
                    ),
                );

                // update painting order immediately to draw the active tab
                compositor.send_root_pipeline_display_list(self);
            }
        }
    }

    /// Handle Winit window event and return a boolean to indicate if the compositor should repaint immediately.
    pub fn handle_winit_window_event(
        &mut self,
        sender: &Sender<EmbedderToConstellationMessage>,
        compositor: &mut IOCompositor,
        event: &winit::event::WindowEvent,
    ) {
        match event {
            WindowEvent::RedrawRequested => {
                if compositor.ready_to_present {
                    self.window.pre_present_notify();
                    if let Err(err) = compositor.rendering_context.present(&self.surface) {
                        log::warn!("Failed to present surface: {:?}", err);
                    }
                    compositor.ready_to_present = false;
                }
            }
            WindowEvent::Focused(focused) => {
                if *focused {
                    compositor.swap_current_window(self);
                }
            }
            WindowEvent::Resized(size) => {
                if self.window.has_focus() {
                    self.resizing = true;
                }
                let size = Size2D::new(size.width, size.height);
                compositor.resize(size.to_f32(), self);
            }
            WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
                compositor.on_scale_factor_event(*scale_factor as f32, self);
            }
            WindowEvent::CursorEntered { .. } => {
                compositor.swap_current_window(self);
            }
            WindowEvent::CursorLeft { .. } => {
                self.mouse_position.set(None);
            }
            WindowEvent::CursorMoved { position, .. } => {
                let point: DevicePoint = DevicePoint::new(position.x as f32, position.y as f32);
                self.mouse_position.set(Some(*position));
                let webview_id = match self.focused_webview_id {
                    Some(webview_id) => webview_id,
                    None => {
                        log::trace!("No focused webview, skipping MouseInput event.");
                        return;
                    }
                };

                forward_input_event(
                    compositor,
                    webview_id,
                    sender,
                    InputEvent::MouseMove(MouseMoveEvent { point }),
                );

                // handle Windows and Linux non-decoration window resize cursor
                #[cfg(any(linux, target_os = "windows"))]
                {
                    if self.should_use_client_region_drag() {
                        let direction = self.get_drag_resize_direction();
                        self.set_drag_resize_cursor(direction);
                    }
                }
            }
            WindowEvent::MouseInput { state, button, .. } => {
                let point = match self.mouse_position.get() {
                    Some(point) => Point2D::new(point.x as f32, point.y as f32),
                    None => {
                        log::trace!("Mouse position is None, skipping MouseInput event.");
                        return;
                    }
                };

                /* handle context menu */
                if let (ElementState::Pressed, winit::event::MouseButton::Right) = (state, button) {
                    let prompt = self.tab_manager.current_prompt();
                    if prompt.is_some() {
                        return;
                    }
                }

                /* handle Windows and Linux non-decoration window resize */
                #[cfg(any(linux, target_os = "windows"))]
                {
                    if *state == ElementState::Pressed
                        && *button == winit::event::MouseButton::Left
                        && self.should_use_client_region_drag()
                    {
                        self.drag_resize_window();
                    }
                }

                /* handle mouse events */

                let button: MouseButton = match button {
                    winit::event::MouseButton::Left => MouseButton::Left,
                    winit::event::MouseButton::Right => MouseButton::Right,
                    winit::event::MouseButton::Middle => MouseButton::Middle,
                    _ => {
                        log::trace!(
                            "Verso Window isn't supporting this mouse button yet: {button:?}"
                        );
                        return;
                    }
                };

                let event: MouseButtonEvent = match state {
                    ElementState::Pressed => MouseButtonEvent {
                        point,
                        action: MouseButtonAction::Down,
                        button,
                    },
                    ElementState::Released => {
                        self.resizing = false;
                        MouseButtonEvent {
                            point,
                            action: MouseButtonAction::Up,
                            button,
                        }
                    }
                };

                let Some(webview_id) = &compositor.webview_id_from_point(point) else {
                    log::trace!("No webview at point, skipping MouseInput event.");
                    return;
                };

                forward_input_event(
                    compositor,
                    *webview_id,
                    sender,
                    InputEvent::MouseButton(event),
                );

                // Winit didn't send click event, so we send it after mouse up
                if *state == ElementState::Released {
                    let event: MouseButtonEvent = MouseButtonEvent {
                        point,
                        action: MouseButtonAction::Click,
                        button,
                    };
                    forward_input_event(
                        compositor,
                        *webview_id,
                        sender,
                        InputEvent::MouseButton(event),
                    );
                }
            }
            WindowEvent::PinchGesture { delta, .. } => {
                compositor.on_zoom_window_event(1.0 + *delta as f32, self);
            }
            WindowEvent::MouseWheel { delta, phase, .. } => {
                let point = match self.mouse_position.get() {
                    Some(point) => point,
                    None => {
                        log::trace!("Mouse position is None, skipping MouseWheel event.");
                        return;
                    }
                };

                // FIXME: Pixels per line, should be configurable (from browser setting?) and vary by zoom level.
                const LINE_HEIGHT: f32 = 38.0;

                let (mut x, mut y, _mode) = match delta {
                    winit::event::MouseScrollDelta::LineDelta(x, y) => {
                        (*x as f64, (*y * LINE_HEIGHT) as f64, WheelMode::DeltaLine)
                    }
                    winit::event::MouseScrollDelta::PixelDelta(position) => {
                        let position = position.to_logical::<f64>(self.window.scale_factor());
                        (position.x, position.y, WheelMode::DeltaPixel)
                    }
                };

                // Scroll Event
                // Do one axis at a time.
                if y.abs() >= x.abs() {
                    x = 0.0;
                } else {
                    y = 0.0;
                }

                let phase: TouchEventType = match phase {
                    TouchPhase::Started => TouchEventType::Down,
                    TouchPhase::Moved => TouchEventType::Move,
                    TouchPhase::Ended => TouchEventType::Up,
                    TouchPhase::Cancelled => TouchEventType::Cancel,
                };

                compositor.on_scroll_event(
                    ScrollLocation::Delta(LayoutVector2D::new(x as f32, y as f32)),
                    DeviceIntPoint::new(point.x as i32, point.y as i32),
                    phase,
                );
            }
            WindowEvent::ModifiersChanged(modifier) => self.modifiers_state.set(modifier.state()),
            WindowEvent::Ime(event) => {
                let webview_id = match self.focused_webview_id {
                    Some(webview_id) => webview_id,
                    None => {
                        log::trace!("No focused webview, skipping Ime event.");
                        return;
                    }
                };
                if !self.has_webview(webview_id) {
                    log::trace!(
                        "Webview {:?} doesn't exist, skipping Ime event.",
                        webview_id
                    );
                    return;
                }

                match event {
                    Ime::Commit(text) => {
                        let text = text.clone();
                        forward_input_event(
                            compositor,
                            webview_id,
                            sender,
                            InputEvent::Ime(ImeEvent::Composition(CompositionEvent {
                                state: CompositionState::End,
                                data: text,
                            })),
                        );
                    }
                    Ime::Enabled => {
                        forward_input_event(
                            compositor,
                            webview_id,
                            sender,
                            InputEvent::Ime(ImeEvent::Composition(CompositionEvent {
                                state: CompositionState::Start,
                                data: String::new(),
                            })),
                        );
                    }
                    Ime::Preedit(text, _) => {
                        forward_input_event(
                            compositor,
                            webview_id,
                            sender,
                            InputEvent::Ime(ImeEvent::Composition(CompositionEvent {
                                state: CompositionState::Update,
                                data: text.to_string(),
                            })),
                        );
                    }
                    Ime::Disabled => {
                        forward_input_event(
                            compositor,
                            webview_id,
                            sender,
                            InputEvent::Ime(ImeEvent::Composition(CompositionEvent {
                                state: CompositionState::End,
                                data: String::new(),
                            })),
                        );
                    }
                }
            }
            WindowEvent::KeyboardInput { event, .. } => {
                let webview_id = match self.focused_webview_id {
                    Some(webview_id) => webview_id,
                    None => {
                        log::trace!("No focused webview, skipping KeyboardInput event.");
                        return;
                    }
                };
                if !self.has_webview(webview_id) {
                    log::trace!(
                        "Webview {:?} doesn't exist, skipping KeyboardInput event.",
                        webview_id
                    );
                    return;
                }
                let event = keyboard_event_from_winit(event, self.modifiers_state.get());
                log::trace!("Verso is handling {:?}", event);

                /* Window operation keyboard shortcut */
                if self.handle_keyboard_shortcut(compositor, &event) {
                    return;
                }
                forward_input_event(compositor, webview_id, sender, InputEvent::Keyboard(event));
            }
            e => log::trace!("Verso Window isn't supporting this window event yet: {e:?}"),
        }
    }

    /// Handle Window keyboard shortcut
    ///
    /// - Returns `true` if the event is handled, then we should skip sending it to constellation
    fn handle_keyboard_shortcut(
        &mut self,
        compositor: &mut IOCompositor,
        event: &KeyboardEvent,
    ) -> bool {
        let is_macos = cfg!(target_os = "macos");
        let control_or_meta = if is_macos {
            Modifiers::META
        } else {
            Modifiers::CONTROL
        };

        if event.state == KeyState::Down {
            // TODO: New Window, Close Browser
            match (event.modifiers, event.code) {
                (modifiers, Code::KeyT) if modifiers == control_or_meta => {
                    (*self).create_tab(
                        &compositor.constellation_chan,
                        ServoUrl::parse("https://example.com").unwrap(),
                    );
                    return true;
                }
                (modifiers, Code::KeyW) if modifiers == control_or_meta => {
                    if let Some(tab_id) = self.tab_manager.current_tab_id() {
                        (*self).close_tab(compositor, tab_id);
                    }
                    return true;
                }
                (modifiers, Code::KeyL) if modifiers == control_or_meta => {
                    // focus on navigation input
                    if let Some(panel) = &self.panel {
                        let webview_id = &panel.webview.webview_id;

                        let _ = compositor.constellation_chan.send(
                            EmbedderToConstellationMessage::FocusWebView(webview_id.clone()),
                        );

                        let _ = execute_script(
                            &compositor.constellation_chan,
                            webview_id,
                            "window.navbar.focusUrlInput()".to_string(),
                        );
                    }
                    return true;
                }

                _ => (),
            }
        }

        false
    }

    /// Handle servo messages. Return true if it requests a new window
    pub fn handle_servo_message(
        &mut self,
        webview_id: WebViewId,
        message: EmbedderMsg,
        sender: Sender<EmbedderToConstellationMessage>,
        to_controller_sender: &Option<IpcSender<ToControllerMessage>>,
        clipboard: Option<&mut Clipboard>,
        compositor: &mut IOCompositor,
        bookmark_manager: &mut BookmarkManager,
    ) -> bool {
        if let EmbedderMsg::SetCursor(_, cursor) = message {
            self.set_cursor_icon(cursor);
            return false;
        }

        // Handle message in Verso Panel
        if let Some(panel) = &self.panel {
            if panel.webview.webview_id == webview_id {
                return self.handle_servo_messages_with_panel(
                    webview_id,
                    message,
                    sender.clone(),
                    clipboard,
                    compositor,
                    bookmark_manager,
                );
            }
        }
        if let Some(webview_menu) = &self.webview_menu {
            if webview_menu.webview().webview_id == webview_id {
                self.handle_servo_messages_with_webview_menu(
                    webview_id, message, &sender, clipboard, compositor,
                );
                return false;
            }
        }
        if self.tab_manager.has_prompt(webview_id) {
            self.handle_servo_messages_with_prompt(
                webview_id, message, &sender, clipboard, compositor,
            );
            return false;
        }

        // Handle message in Verso WebView
        self.handle_servo_messages_with_webview(
            webview_id,
            message,
            &sender,
            to_controller_sender,
            clipboard,
            compositor,
        );
        false
    }

    /// Queues a Winit `WindowEvent::RedrawRequested` event to be emitted that aligns with the windowing system drawing loop.
    pub fn request_redraw(&self) {
        self.window.request_redraw()
    }

    /// Size of the window that's used by webrender.
    pub fn size(&self) -> DeviceSize {
        let size = self.window.inner_size();
        Size2D::new(size.width as f32, size.height as f32)
    }

    /// Size of the window, including the window decorations.
    pub fn outer_size(&self) -> DeviceSize {
        let size = self.window.outer_size();
        Size2D::new(size.width as f32, size.height as f32)
    }

    /// Get Winit window ID of the window.
    pub fn id(&self) -> WindowId {
        self.window.id()
    }

    /// Scale factor of the window. This is also known as HIDPI.
    pub fn scale_factor(&self) -> f64 {
        self.window.scale_factor()
    }

    /// Check if the window has such webview.
    pub fn has_webview(&self, id: WebViewId) -> bool {
        if self
            .webview_menu
            .as_ref()
            .is_some_and(|w| w.webview().webview_id == id)
        {
            return true;
        }

        if self.tab_manager.has_prompt(id) {
            return true;
        }

        if let Some(panel) = &self.panel {
            if panel.webview.webview_id == id {
                return true;
            }
        }

        if self.tab_manager.tab(id).is_some() {
            return true;
        }

        false
    }

    /// Remove the webview in this window by provided webview ID.
    /// If provided ID is the panel, it will shut down the compositor and then close whole application.
    pub fn remove_webview(
        &mut self,
        id: WebViewId,
        compositor: &mut IOCompositor,
    ) -> (Option<WebView>, bool) {
        if self
            .webview_menu
            .as_ref()
            .filter(|menu| menu.webview().webview_id == id)
            .is_some()
        {
            let webview_menu = self.webview_menu.take().expect("Context menu should exist");
            return (Some(webview_menu.webview().clone()), false);
        }

        if let Some(prompt) = self.tab_manager.remove_prompt_by_prompt_id(id) {
            return (Some(prompt.webview().clone()), false);
        }

        if self
            .panel
            .as_ref()
            .filter(|w| w.webview.webview_id == id)
            .is_some()
        {
            // Removing panel, remove all webviews and shut down the compositor
            let tab_ids = self.tab_manager.tab_ids();
            for tab_id in tab_ids {
                send_to_constellation(
                    &compositor.constellation_chan,
                    EmbedderToConstellationMessage::CloseWebView(tab_id),
                );
            }
            (self.panel.take().map(|panel| panel.webview), false)
        } else if let Ok(tab) = self.tab_manager.close_tab(id) {
            let close_window = self.tab_manager.count() == 0 || self.panel.is_none();
            if self.focused_webview_id == Some(id) {
                self.focused_webview_id = None;
            }
            (Some(tab.webview().clone()), close_window)
        } else {
            (None, false)
        }
    }

    /// Get the painting order of this window.
    pub fn painting_order(&self) -> Vec<&WebView> {
        let mut order = vec![];
        if let Some(panel) = &self.panel {
            order.push(&panel.webview);
        }

        if let Some(tab) = self.tab_manager.current_tab() {
            order.push(tab.webview());
        }

        if let Some(webview_menu) = &self.webview_menu {
            order.push(webview_menu.webview());
        }

        if let Some(prompt) = self.tab_manager.current_prompt() {
            order.push(prompt.webview());
        }

        order
    }

    /// Set cursor icon of the window.
    pub fn set_cursor_icon(&mut self, cursor: Cursor) {
        let winit_cursor = match cursor {
            Cursor::Default => CursorIcon::Default,
            Cursor::Pointer => CursorIcon::Pointer,
            Cursor::ContextMenu => CursorIcon::ContextMenu,
            Cursor::Help => CursorIcon::Help,
            Cursor::Progress => CursorIcon::Progress,
            Cursor::Wait => CursorIcon::Wait,
            Cursor::Cell => CursorIcon::Cell,
            Cursor::Crosshair => CursorIcon::Crosshair,
            Cursor::Text => CursorIcon::Text,
            Cursor::VerticalText => CursorIcon::VerticalText,
            Cursor::Alias => CursorIcon::Alias,
            Cursor::Copy => CursorIcon::Copy,
            Cursor::Move => CursorIcon::Move,
            Cursor::NoDrop => CursorIcon::NoDrop,
            Cursor::NotAllowed => CursorIcon::NotAllowed,
            Cursor::Grab => CursorIcon::Grab,
            Cursor::Grabbing => CursorIcon::Grabbing,
            Cursor::EResize => CursorIcon::EResize,
            Cursor::NResize => CursorIcon::NResize,
            Cursor::NeResize => CursorIcon::NeResize,
            Cursor::NwResize => CursorIcon::NwResize,
            Cursor::SResize => CursorIcon::SResize,
            Cursor::SeResize => CursorIcon::SeResize,
            Cursor::SwResize => CursorIcon::SwResize,
            Cursor::WResize => CursorIcon::WResize,
            Cursor::EwResize => CursorIcon::EwResize,
            Cursor::NsResize => CursorIcon::NsResize,
            Cursor::NeswResize => CursorIcon::NeswResize,
            Cursor::NwseResize => CursorIcon::NwseResize,
            Cursor::ColResize => CursorIcon::ColResize,
            Cursor::RowResize => CursorIcon::RowResize,
            Cursor::AllScroll => CursorIcon::AllScroll,
            Cursor::ZoomIn => CursorIcon::ZoomIn,
            Cursor::ZoomOut => CursorIcon::ZoomOut,
            Cursor::None => {
                self.window.set_cursor_visible(false);
                return;
            }
        };
        self.cursor_state.current_cursor = winit_cursor;
        self.window.set_cursor(winit_cursor);
        self.window.set_cursor_visible(true);
    }

    /// This method enables IME and set the IME cursor area of the window.
    /// The position is in logical position so it'll scale according to screen scaling factor.
    pub fn show_ime(
        &self,
        _input_method_typee: embedder_traits::InputMethodType,
        _text: Option<(String, i32)>,
        _multilinee: bool,
        position: euclid::Box2D<i32, webrender_api::units::DevicePixel>,
        show_bookmark: bool,
    ) {
        self.window.set_ime_allowed(true);
        let mut height: f64 = PANEL_HEIGHT + PANEL_PADDING;
        if self.tab_manager.count() > 1 {
            height += TAB_HEIGHT;
        }
        if show_bookmark {
            height += BOOKMARK_HEIGHT;
        }
        self.window.set_ime_cursor_area(
            LogicalPosition::new(position.min.x, position.min.y + height as i32),
            LogicalSize::new(0, position.max.y - position.min.y),
        );
    }

    /// This method disables IME of the window.
    pub fn hide_ime(&self) {
        self.window.set_ime_allowed(false);
    }

    /// Show notification
    pub fn show_notification(&self, notification: &Notification) {
        let mut display_notification = notify_rust::Notification::new();

        display_notification
            .summary(&notification.title)
            .body(&notification.body);

        #[cfg(linux)]
        {
            if let Some(icon_image) = notification.icon_resource.as_ref().and_then(|icon| {
                Image::from_rgba(icon.width as i32, icon.height as i32, icon.bytes().to_vec()).ok()
            }) {
                display_notification.image_data(icon_image);
            }
        }

        #[cfg(linux)]
        std::thread::spawn(move || {
            if let Ok(handle) = display_notification.show() {
                // prevent handler dropped immediately which will close the notification as well
                handle.on_close(|| {});
            }
        });
        #[cfg(not(linux))]
        let _ = display_notification.show();
    }

    /// Close window's webview menu
    pub(crate) fn close_webview_menu(&mut self, sender: &Sender<EmbedderToConstellationMessage>) {
        if let Some(menu) = self.webview_menu.as_mut() {
            menu.close(sender);
        }
    }
}

// Prompt methods
impl Window {
    /// Close window's prompt dialog
    pub(crate) fn close_prompt_dialog(&mut self, tab_id: WebViewId) {
        if let Some(sender) = self
            .tab_manager
            .remove_prompt_by_tab_id(tab_id)
            .and_then(|prompt| prompt.sender())
        {
            match sender {
                PromptSender::AlertSender(sender) => {
                    let _ = sender.send(AlertResponse::default());
                }
                PromptSender::ConfirmSender(sender) => {
                    let _ = sender.send(ConfirmResponse::default());
                }
                PromptSender::InputSender(sender) => {
                    let _ = sender.send(PromptResponse::default());
                }
                PromptSender::AllowDenySender(sender) => {
                    let _ = sender.send(AllowOrDeny::Deny);
                }
                PromptSender::HttpBasicAuthSender(sender) => {
                    let _ = sender.send(None);
                }
            }
        }
    }
}

// Non-decorated window resizing for Windows and Linux.
#[cfg(any(linux, target_os = "windows"))]
impl Window {
    /// Check current window state is allowed to apply drag-resize in client region.
    fn should_use_client_region_drag(&self) -> bool {
        !self.window.is_decorated()
            && !self.window.is_maximized()
            && self.window.is_resizable()
            && self.window.fullscreen().is_none()
    }

    /// Drag resize the window.
    fn drag_resize_window(&self) {
        if let Some(direction) = self.get_drag_resize_direction() {
            if let Err(err) = self.window.drag_resize_window(direction) {
                log::error!("Failed to drag-resize window: {:?}", err);
            }
        }
    }

    /// Get drag-resize direction.
    fn get_drag_resize_direction(&self) -> Option<ResizeDirection> {
        let mouse_position = match self.mouse_position.get() {
            Some(position) => position,
            None => {
                return None;
            }
        };

        let window_size = self.window.outer_size();
        let border_size = 5.0 * self.window.scale_factor();

        let x_direction = if mouse_position.x < border_size {
            Some(ResizeDirection::West)
        } else if mouse_position.x > (window_size.width as f64 - border_size) {
            Some(ResizeDirection::East)
        } else {
            None
        };

        let y_direction = if mouse_position.y < border_size {
            Some(ResizeDirection::North)
        } else if mouse_position.y > (window_size.height as f64 - border_size) {
            Some(ResizeDirection::South)
        } else {
            None
        };

        let direction = match (x_direction, y_direction) {
            (Some(ResizeDirection::East), None) => ResizeDirection::East,
            (Some(ResizeDirection::West), None) => ResizeDirection::West,
            (None, Some(ResizeDirection::South)) => ResizeDirection::South,
            (None, Some(ResizeDirection::North)) => ResizeDirection::North,
            (Some(ResizeDirection::East), Some(ResizeDirection::North)) => {
                ResizeDirection::NorthEast
            }
            (Some(ResizeDirection::West), Some(ResizeDirection::North)) => {
                ResizeDirection::NorthWest
            }
            (Some(ResizeDirection::East), Some(ResizeDirection::South)) => {
                ResizeDirection::SouthEast
            }
            (Some(ResizeDirection::West), Some(ResizeDirection::South)) => {
                ResizeDirection::SouthWest
            }
            _ => return None,
        };

        Some(direction)
    }

    /// Set drag-resize cursor when mouse is hover on the border of the window.
    fn set_drag_resize_cursor(&mut self, direction: Option<ResizeDirection>) {
        if let Some(direction) = direction {
            let cursor = match direction {
                ResizeDirection::East => CursorIcon::EResize,
                ResizeDirection::West => CursorIcon::WResize,
                ResizeDirection::South => CursorIcon::SResize,
                ResizeDirection::North => CursorIcon::NResize,
                ResizeDirection::NorthEast => CursorIcon::NeResize,
                ResizeDirection::NorthWest => CursorIcon::NwResize,
                ResizeDirection::SouthEast => CursorIcon::SeResize,
                ResizeDirection::SouthWest => CursorIcon::SwResize,
            };
            self.cursor_state.cursor_resizing = true;
            self.window.set_cursor(cursor);
        } else if self.cursor_state.cursor_resizing {
            self.cursor_state.cursor_resizing = false;
            self.window.set_cursor(self.cursor_state.current_cursor);
        }
    }
}

/* window decoration */
#[cfg(macos)]
use objc2::runtime::AnyObject;
#[cfg(macos)]
use raw_window_handle::{AppKitWindowHandle, RawWindowHandle};

/// Window decoration for macOS.
#[cfg(macos)]
pub unsafe fn decorate_window(view: *mut AnyObject, _position: LogicalPosition<f64>) {
    use objc2::rc::Id;
    use objc2_app_kit::{NSView, NSWindowStyleMask, NSWindowTitleVisibility};

    let ns_view: Id<NSView> = unsafe { Id::retain(view.cast()) }.unwrap();
    let window = ns_view
        .window()
        .expect("view was not installed in a window");
    window.setMovable(false); // let panel UI handle window moving
    window.setTitlebarAppearsTransparent(true);
    window.setTitleVisibility(NSWindowTitleVisibility::NSWindowTitleHidden);
    window.setStyleMask(
        NSWindowStyleMask::Titled
            | NSWindowStyleMask::FullSizeContentView
            | NSWindowStyleMask::Closable
            | NSWindowStyleMask::Resizable
            | NSWindowStyleMask::Miniaturizable,
    );
}

/// Forward input event to compositor or constellation.
fn forward_input_event(
    compositor: &mut IOCompositor,
    webview_id: WebViewId,
    constellation_proxy: &Sender<EmbedderToConstellationMessage>,
    event: InputEvent,
) {
    // Events with a `point` first go to the compositor for hit testing.
    if event.point().is_some() {
        compositor.on_input_event(webview_id, event);
        return;
    }

    let _ = constellation_proxy.send(EmbedderToConstellationMessage::ForwardInputEvent(
        webview_id, event, None, /* hit_test */
    ));
}