wew 0.1.0

Cross-platform WebView rendering library for rust.
Documentation
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
//! This module is used to manage a single web page.
//!
//! ## Communication with Web Pages
//!
//! This library's runtime will inject a global object into web pages for
//! communication between Rust and web pages.
//!
//! ```typescript
//! declare global {
//!     interface Window {
//!         MessageTransport: {
//!             on: (handle: (message: string) => void) => void;
//!             send: (message: string) => void;
//!         };
//!     }
//! }
//! ```
//!
//! Usage example:
//!
//! ```typescript
//! window.MessageTransport.on((message: string) => {
//!     console.log("Received message from Rust:", message);
//! });
//!
//! window.MessageTransport.send("Send message to Rust");
//! ```
//!
//! **`WebViewHandler::on_message`** is used to receive messages sent by
//! **`MessageTransport.send`**, while **`MessageTransport.on`** is used to
//! receive messages sent by **`WebView::send_message`**. Sending and receiving
//! messages are full-duplex and asynchronous.
//!
//! ## WebView Types
//!
//! There are two types of runtime:
//!
//! #### WindowlessRenderWebView
//!
//! Off-screen rendering, also known as OSR mode in CEF. In this mode, the web
//! page does not create a window, and the rendered content is passed to the
//! external through the **`WindowlessRenderWebViewHandler::on_frame`**
//! callback, allowing external code to handle rendering themselves.
//!
//! In this mode, the web page does not automatically handle any mouse,
//! keyboard, or other window-related events because there is no window. You
//! need to manually call the corresponding methods on `WebView` based on events
//! to make it respond to events.
//!
//! #### NativeWindowWebView
//!
//! Window rendering, also known as window mode in CEF. In this mode, the web
//! page creates a window and automatically handles mouse, keyboard, and other
//! window-related events.
//!
//! If you provide a window handle to `WebView`, the `WebView` window will be a
//! child window of that window.
//!
//! If you do not provide a window handle, `WebView` will automatically create a
//! Chromium-style window.

use std::{
    ffi::{CStr, CString, c_char, c_int, c_void},
    marker::PhantomData,
    mem::MaybeUninit,
    ops::Deref,
    ptr::null,
    sync::Arc,
};

use parking_lot::Mutex;
use raw_window_handle::RawWindowHandle;

use crate::{
    Error, Rect, WindowlessRenderWebView,
    events::{
        IMEAction, KeyboardEvent, KeyboardEventType, KeyboardModifiers, MouseButton, MouseEvent,
    },
    request::{CustomRequestHandlerFactory, ICustomRequestHandlerFactory},
    runtime::{IRuntime, Runtime},
    sys,
    utils::{AnyStringCast, GetSharedRef, ThreadSafePointer},
};

/// Represents the type of cursor
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum CursorType {
    Pointer = 0,
    Cross = 1,
    Hand = 2,
    IBeam = 3,
    Wait = 4,
    Help = 5,
    EastResize = 6,
    NorthResize = 7,
    NorthEastResize = 8,
    NorthWestResize = 9,
    SouthResize = 10,
    SouthEastResize = 11,
    SouthWestResize = 12,
    WestResize = 13,
    NorthSouthResize = 14,
    EastWestResize = 15,
    NorthEastSouthWestResize = 16,
    NorthWestSouthEastResize = 17,
    ColumnResize = 18,
    RowResize = 19,
    MiddlePanning = 20,
    EastPanning = 21,
    NorthPanning = 22,
    NorthEastPanning = 23,
    NorthWestPanning = 24,
    SouthPanning = 25,
    SouthEastPanning = 26,
    SouthWestPanning = 27,
    WestPanning = 28,
    Move = 29,
    VerticalText = 30,
    Cell = 31,
    ContextMenu = 32,
    Alias = 33,
    Progress = 34,
    NoDrop = 35,
    Copy = 36,
    None = 37,
    NotAllowed = 38,
    ZoomIn = 39,
    ZoomOut = 40,
    Grab = 41,
    Grabbing = 42,
    MiddlePanningVertical = 43,
    MiddlePanningHorizontal = 44,
    Custom = 45,
    DndNone = 46,
    DndMove = 47,
    DndCopy = 48,
    DndLink = 49,
    NumValues = 50,
}

/// Represents the type of a frame
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum FrameType {
    View,
    Popup,
}

/// Represents a rendered frame of a web page
#[derive(Clone, Copy)]
pub struct Frame<'a> {
    pub ty: FrameType,
    /// The buffer of the frame
    pub buffer: &'a [u8],
    /// The x coordinate of the frame
    pub x: u32,
    /// The y coordinate of the frame
    pub y: u32,
    /// The width of the frame
    pub width: u32,
    /// The height of the frame
    pub height: u32,
}

impl std::fmt::Debug for Frame<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Frame")
            .field("ty", &self.ty)
            .field("x", &self.x)
            .field("y", &self.y)
            .field("width", &self.width)
            .field("height", &self.height)
            .finish()
    }
}

/// Represents the state of a web page
///
/// The order of events is as follows:
///
/// ```text
/// BeforeLoad -> Loaded -> RequestClose -> Close
///          \ -> LoadError -> Loaded -> RequestClose -> Close
/// ```
///
/// Regardless of whether the loading exists an error, the `Loaded` event is
/// triggered, the difference is that if the loading error occurs, the
/// `LoadError` event is triggered first.
#[repr(u32)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum WebViewState {
    /// The web page is before loading
    BeforeLoad = 1,
    /// The web page is loaded
    Loaded = 2,
    /// The web page is loading error
    LoadError = 3,
    /// The web page is requesting to close
    RequestClose = 4,
    /// The web page is closed
    Close = 5,
}

/// WebView handler
///
/// This trait is used to handle web view events.
#[allow(unused)]
pub trait WebViewHandler: Send + Sync {
    /// Called when the cursor changes
    ///
    /// When the web page wants to change the mouse pointer style, it will be
    /// triggered, such as moving to a link.
    fn on_cursor_change(&self, ty: CursorType) {}
    /// Called when the web page state changes
    ///
    /// You need to pay attention to status changes, determine whether loading
    /// was successful, and monitor events related to the page closing.
    fn on_state_change(&self, state: WebViewState) {}

    /// Called when the title changes
    fn on_title_change(&self, title: &str) {}

    /// Called when the fullscreen state changes
    fn on_fullscreen_change(&self, fullscreen: bool) {}

    /// Called when a message is received
    ///
    /// This callback is called when a message is received from the web page.
    fn on_message(&self, message: &str) {}
}

/// Windowless render web view handler
///
/// A specific event handler for windowless rendering WebView.
#[allow(unused)]
pub trait WindowlessRenderWebViewHandler: WebViewHandler {
    /// Called when the IME composition rectangle changes
    ///
    /// When the IME region changes, you should notify the external window.
    fn on_ime_rect(&self, rect: Rect) {}

    /// Push a new frame when rendering changes
    ///
    /// This only works in windowless rendering mode.
    ///
    /// #### Note:
    ///
    /// Fixed as BGRA texture buffer, not padded and not aligned.
    ///
    /// It should be noted that if the webview is resized, the width and height
    /// of the texture will also change.
    fn on_frame(&self, frame: &Frame) {}
}

/// WebView configuration attributes
pub struct WebViewAttributes {
    /// Request handler factory.
    pub request_handler_factory: Option<CustomRequestHandlerFactory>,
    /// External native window handle.
    pub window_handle: Option<RawWindowHandle>,
    /// The maximum rate in frames per second (fps).
    pub windowless_frame_rate: u32,
    /// window size width.
    pub width: u32,
    /// window size height.
    pub height: u32,
    /// window device scale factor.
    pub device_scale_factor: f32,
    /// page defalt font size.
    pub default_font_size: u32,
    /// page defalt fixed font size.
    pub default_fixed_font_size: u32,
    /// The minimum font size.
    pub minimum_font_size: u32,
    /// The minimum logical font size.
    pub minimum_logical_font_size: u32,
    /// Controls whether WebGL is enabled.
    pub webgl: bool,
    /// Controls whether databases are enabled.
    pub databases: bool,
    /// Controls whether JavaScript can be executed.
    pub javascript: bool,
    /// Controls whether JavaScript can access the clipboard.
    pub javascript_access_clipboard: bool,
    /// Controls whether JavaScript can be used to close windows that were not
    /// opened via JavaScript.
    pub javascript_close_windows: bool,
    /// Controls whether DOM pasting is supported in the editor via
    /// execCommand("paste").
    pub javascript_dom_paste: bool,
    /// Controls whether local storage can be used.
    pub local_storage: bool,
    /// END values that map to WebPreferences settings.
    pub background_color: u32,
}

unsafe impl Send for WebViewAttributes {}
unsafe impl Sync for WebViewAttributes {}

impl Default for WebViewAttributes {
    fn default() -> Self {
        Self {
            width: 800,
            height: 600,
            window_handle: None,
            device_scale_factor: 1.0,
            windowless_frame_rate: 30,
            default_font_size: 12,
            default_fixed_font_size: 12,
            javascript: true,
            local_storage: true,
            javascript_access_clipboard: false,
            request_handler_factory: None,
            webgl: false,
            databases: false,
            javascript_close_windows: false,
            javascript_dom_paste: false,
            background_color: 0xFFFFFFFF,
            minimum_font_size: 12,
            minimum_logical_font_size: 12,
        }
    }
}

/// WebView configuration attributes builder
#[derive(Default)]
pub struct WebViewAttributesBuilder(WebViewAttributes);

impl WebViewAttributesBuilder {
    /// Set the request handler factory
    ///
    /// This function is used to set the request handler factory.
    pub fn with_request_handler_factory(mut self, value: CustomRequestHandlerFactory) -> Self {
        self.0.request_handler_factory = Some(value);
        self
    }

    /// Set the window handle
    ///
    /// In windowed mode, setting the window handle will set the browser as a
    /// child view.
    ///
    /// In windowless mode, setting the window handle is used to identify
    /// monitor information and as a parent view for dialog boxes, context
    /// menus, and other elements. If not provided, the main screen monitor will
    /// be used, and some features that require a parent view may not work
    /// properly.
    pub fn with_window_handle(mut self, value: RawWindowHandle) -> Self {
        self.0.window_handle = Some(value);
        self
    }

    /// Set the frame rate in windowless rendering mode
    ///
    /// This function is used to set the frame rate in windowless rendering
    /// mode.
    ///
    /// Note that this parameter only works in windowless rendering mode.
    pub fn with_windowless_frame_rate(mut self, value: u32) -> Self {
        self.0.windowless_frame_rate = value;
        self
    }

    /// Set the window width
    ///
    /// This function is used to set the window width.
    ///
    /// Note that this parameter only works in windowless rendering mode.
    pub fn with_width(mut self, value: u32) -> Self {
        self.0.width = value;
        self
    }

    /// Set the window height
    ///
    /// This function is used to set the window height.
    ///
    /// Note that this parameter only works in windowless rendering mode.
    pub fn with_height(mut self, value: u32) -> Self {
        self.0.height = value;
        self
    }

    /// Set the device scale factor
    ///
    /// This function is used to set the device scale factor.
    pub fn with_device_scale_factor(mut self, value: f32) -> Self {
        self.0.device_scale_factor = value;
        self
    }

    /// Set the default font size
    ///
    /// This function is used to set the default font size.
    pub fn with_default_font_size(mut self, value: u32) -> Self {
        self.0.default_font_size = value;
        self
    }

    /// Set the default fixed font size
    ///
    /// This function is used to set the default fixed font size.
    pub fn with_default_fixed_font_size(mut self, value: u32) -> Self {
        self.0.default_fixed_font_size = value;
        self
    }

    /// Set the minimum font size
    ///
    /// This function is used to set the minimum font size.
    pub fn with_minimum_font_size(mut self, value: u32) -> Self {
        self.0.minimum_font_size = value;
        self
    }

    /// Set the minimum logical font size
    ///
    /// This function is used to set the minimum logical font size.
    pub fn with_minimum_logical_font_size(mut self, value: u32) -> Self {
        self.0.minimum_logical_font_size = value;
        self
    }

    /// Set whether local storage is enabled
    ///
    /// This function is used to set whether local storage is enabled.
    pub fn with_local_storage(mut self, value: bool) -> Self {
        self.0.local_storage = value;
        self
    }

    /// Set whether WebGL is enabled
    ///
    /// This function is used to set whether WebGL is enabled.
    pub fn with_webgl(mut self, value: bool) -> Self {
        self.0.webgl = value;
        self
    }

    /// Set whether databases are enabled
    ///
    /// This function is used to set whether databases are enabled.
    pub fn with_databases(mut self, value: bool) -> Self {
        self.0.databases = value;
        self
    }

    /// Set whether JavaScript is enabled
    ///
    /// This function is used to set whether JavaScript is enabled.
    pub fn with_javascript(mut self, value: bool) -> Self {
        self.0.javascript = value;
        self
    }

    /// Set whether JavaScript can access the clipboard
    ///
    /// This function is used to set whether JavaScript can access the
    /// clipboard.
    pub fn with_javascript_access_clipboard(mut self, value: bool) -> Self {
        self.0.javascript_access_clipboard = value;
        self
    }

    /// Set whether JavaScript can be used to close windows that were not opened
    /// via JavaScript.
    ///
    /// This function is used to set whether JavaScript can be used to close
    /// windows that were not opened via JavaScript.
    pub fn with_javascript_close_windows(mut self, value: bool) -> Self {
        self.0.javascript_close_windows = value;
        self
    }

    /// Set whether DOM pasting is supported in the editor via
    /// execCommand("paste").
    ///
    /// This function is used to set whether DOM pasting is supported in the
    /// editor via execCommand("paste").
    pub fn with_javascript_dom_paste(mut self, value: bool) -> Self {
        self.0.javascript_dom_paste = value;
        self
    }

    /// Set the background color
    ///
    /// This function is used to set the background color.
    pub fn with_background_color(mut self, value: u32) -> Self {
        self.0.background_color = value;
        self
    }

    pub fn build(self) -> WebViewAttributes {
        self.0
    }
}

impl Deref for WebViewAttributesBuilder {
    type Target = WebViewAttributes;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub(crate) struct IWebView {
    mouse_event: Mutex<sys::MouseEvent>,
    // The runtime may use a custom request interceptor; a reference is kept here to ensure correct
    // lifetime management.
    #[allow(unused)]
    request_handler_factory: Option<Arc<ICustomRequestHandlerFactory>>,
    context: ThreadSafePointer<WebViewContext>,
    raw: Mutex<ThreadSafePointer<c_void>>,
}

impl IWebView {
    fn new<R, W>(
        runtime: &Runtime<R, W>,
        url: &str,
        attr: WebViewAttributes,
        handler: MixWebviewHnadler,
    ) -> Result<Self, Error> {
        let runtime = runtime.get_shared_ref();
        let raw_runtime = runtime.get_raw();

        let options = sys::WebViewSettings {
            width: attr.width,
            height: attr.height,
            webgl: attr.webgl,
            databases: attr.databases,
            local_storage: attr.local_storage,
            background_color: attr.background_color,
            javascript: attr.javascript,
            javascript_access_clipboard: attr.javascript_access_clipboard,
            javascript_close_windows: attr.javascript_close_windows,
            javascript_dom_paste: attr.javascript_dom_paste,
            minimum_font_size: attr.minimum_font_size as _,
            minimum_logical_font_size: attr.minimum_logical_font_size as _,
            device_scale_factor: attr.device_scale_factor,
            windowless_frame_rate: attr.windowless_frame_rate,
            default_fixed_font_size: attr.default_fixed_font_size as _,
            default_font_size: attr.default_font_size as _,
            window_handle: {
                #[cfg(not(target_os = "linux"))]
                let mut value = null();

                #[cfg(target_os = "linux")]
                let mut value = 0;

                if let Some(it) = &attr.window_handle {
                    value = match it {
                        #[cfg(target_os = "linux")]
                        RawWindowHandle::Xlib(it) => it.window,
                        #[cfg(target_os = "windows")]
                        RawWindowHandle::Win32(it) => it.hwnd.get() as _,
                        #[cfg(target_os = "macos")]
                        RawWindowHandle::AppKit(it) => it.ns_view.as_ptr() as _,
                        _ => unimplemented!("Unsupported window handle type: {:?}", it),
                    };
                }

                value
            },
            request_handler_factory: if let Some(it) = &attr.request_handler_factory {
                it.as_raw().as_ptr() as _
            } else {
                null()
            },
        };

        let context: *mut WebViewContext = Box::into_raw(Box::new(WebViewContext {
            runtime: Some(runtime),
            handler,
        }));

        let url = CString::new(url).unwrap();
        let ptr = unsafe {
            sys::create_webview(
                raw_runtime.as_ptr(),
                url.as_raw(),
                &options,
                sys::WebViewHandler {
                    on_cursor: Some(on_cursor_callback),
                    on_state_change: Some(on_state_change_callback),
                    on_ime_rect: Some(on_ime_rect_callback),
                    on_frame: Some(on_frame_callback),
                    on_title_change: Some(on_title_change_callback),
                    on_fullscreen_change: Some(on_fullscreen_change_callback),
                    on_message: Some(on_message_callback),
                    context: context as _,
                },
            )
        };

        let raw = if ptr.is_null() {
            return Err(Error::FailedToCreateWebView);
        } else {
            ThreadSafePointer::new(ptr)
        };

        Ok(Self {
            raw: Mutex::new(raw),
            context: ThreadSafePointer::new(context),
            mouse_event: Mutex::new(unsafe { std::mem::zeroed() }),
            request_handler_factory: attr
                .request_handler_factory
                .as_ref()
                .map(|it| it.get_shared_ref()),
        })
    }
}

impl Drop for IWebView {
    fn drop(&mut self) {
        unsafe {
            sys::close_webview(self.raw.lock().as_ptr());
        }

        drop(unsafe { Box::from_raw(self.context.as_ptr()) });
    }
}

/// Represents an opened web page
#[allow(unused)]
pub struct WebView<W> {
    _w: PhantomData<W>,
    inner: Arc<IWebView>,
}

impl<W> GetSharedRef for WebView<W> {
    type Ref = Arc<IWebView>;

    fn get_shared_ref(&self) -> Self::Ref {
        self.inner.clone()
    }
}

impl<W> WebView<W> {
    pub(crate) fn new<R>(
        runtime: &Runtime<R, W>,
        url: &str,
        attr: WebViewAttributes,
        handler: MixWebviewHnadler,
    ) -> Result<Self, Error> {
        Ok(Self {
            _w: PhantomData,
            inner: Arc::new(IWebView::new(runtime, url, attr, handler)?),
        })
    }

    /// Get the window handle
    ///
    /// This function is used to get the window handle.
    pub fn window_handle(&self) -> Option<RawWindowHandle> {
        let handle = unsafe { sys::webview_get_window_handle(self.inner.raw.lock().as_ptr()) };

        let mut value = MaybeUninit::<RawWindowHandle>::uninit();

        #[cfg(target_os = "linux")]
        if handle == 0 {
            return None;
        } else {
            unsafe {
                value.as_mut_ptr().write(RawWindowHandle::Xlib(
                    raw_window_handle::XlibWindowHandle::new(handle),
                ));
            }
        }

        #[cfg(not(target_os = "linux"))]
        if handle.is_null() {
            return None;
        } else {
            #[cfg(target_os = "windows")]
            unsafe {
                value.as_mut_ptr().write(RawWindowHandle::Win32(
                    raw_window_handle::Win32WindowHandle::new(
                        std::num::NonZeroIsize::new(handle as _).unwrap(),
                    ),
                ));
            }

            #[cfg(target_os = "macos")]
            unsafe {
                value.as_mut_ptr().write(RawWindowHandle::AppKit(
                    raw_window_handle::AppKitWindowHandle::new(std::ptr::NonNull::new_unchecked(
                        handle as _,
                    )),
                ));
            }
        }

        Some(unsafe { value.assume_init() })
    }

    /// Send a message
    ///
    /// This function is used to send a message to the web page.
    ///
    /// Messages sent from the web page are received through the
    /// **`WebViewHandler::on_message`** callback.
    pub fn send_message(&self, message: &str) {
        let message = CString::new(message).unwrap();

        unsafe {
            sys::webview_send_message(self.inner.raw.lock().as_ptr(), message.as_raw());
        }
    }

    /// Set whether developer tools are enabled
    ///
    /// This function is used to set whether developer tools are enabled.
    pub fn devtools_enabled(&self, enable: bool) {
        unsafe { sys::webview_set_devtools_state(self.inner.raw.lock().as_ptr(), enable) }
    }
}

impl WebView<WindowlessRenderWebView> {
    /// Send a mouse event
    ///
    /// This function is used to send mouse events.
    ///
    /// Note that this function only works in windowless rendering mode.
    pub fn mouse(&self, action: &MouseEvent) {
        let mut event = self.inner.mouse_event.lock();

        match action {
            MouseEvent::Move(pos) => unsafe {
                event.x = pos.x;
                event.y = pos.y;

                sys::webview_mouse_move(self.inner.raw.lock().as_ptr(), *event)
            },
            MouseEvent::Wheel(pos) => unsafe {
                sys::webview_mouse_wheel(self.inner.raw.lock().as_ptr(), *event, pos.x, pos.y)
            },
            MouseEvent::Click(button, is_pressed, pos) => {
                if let Some(pos) = pos {
                    event.x = pos.x;
                    event.y = pos.y;
                }

                if *is_pressed {
                    event.modifiers |= match button {
                        MouseButton::Left => sys::EventFlags::WEW_EVENTFLAG_LEFT_MOUSE_BUTTON,
                        MouseButton::Right => sys::EventFlags::WEW_EVENTFLAG_RIGHT_MOUSE_BUTTON,
                        MouseButton::Middle => sys::EventFlags::WEW_EVENTFLAG_MIDDLE_MOUSE_BUTTON,
                    } as u32;
                } else {
                    event.modifiers = 0;
                };

                unsafe {
                    sys::webview_mouse_click(
                        self.inner.raw.lock().as_ptr(),
                        *event,
                        (*button).into(),
                        *is_pressed,
                    )
                }
            }
        }
    }

    /// Send a keyboard event
    ///
    /// This function is used to send keyboard events.
    ///
    /// Note that this function only works in windowless rendering mode.
    pub fn keyboard(&self, event: &KeyboardEvent) {
        let mut modifiers = sys::EventFlags::WEW_EVENTFLAG_NONE as u32;
        for it in KeyboardModifiers::all() {
            if event.modifiers.contains(it) {
                let flag: sys::EventFlags = it.into();
                modifiers |= flag as u32;
            }
        }

        unsafe {
            sys::webview_keyboard(
                self.inner.raw.lock().as_ptr(),
                sys::KeyEvent {
                    modifiers,
                    character: event.character,
                    unmodified_character: event.unmodified_character,
                    windows_key_code: event.windows_key_code as i32,
                    native_key_code: event.native_key_code as i32,
                    is_system_key: event.is_system_key as i32,
                    focus_on_editable_field: event.focus_on_editable_field as i32,
                    type_: event.ty.into(),
                },
            )
        }
    }

    /// Send an IME event
    ///
    /// This function is used to send IME events.
    ///
    /// Note that this function only works in windowless rendering mode.
    pub fn ime(&self, action: &IMEAction) {
        let input = match action {
            IMEAction::Composition(it) | IMEAction::Pre(it, _, _) => CString::new(*it).unwrap(),
        };

        match action {
            IMEAction::Composition(_) => unsafe {
                sys::webview_ime_composition(self.inner.raw.lock().as_ptr(), input.as_raw())
            },
            IMEAction::Pre(_, x, y) => unsafe {
                sys::webview_ime_set_composition(
                    self.inner.raw.lock().as_ptr(),
                    input.as_raw(),
                    *x,
                    *y,
                )
            },
        }
    }

    /// Resize the window
    ///
    /// This function is used to resize the window.
    ///
    /// Note that this function only works in windowless rendering mode.
    pub fn resize(&self, width: u32, height: u32) {
        unsafe {
            sys::webview_resize(
                self.inner.raw.lock().as_ptr(),
                width as c_int,
                height as c_int,
            )
        }
    }

    /// Set the focus state
    ///
    /// This function is used to set the focus state.
    ///
    /// Note that this function only works in windowless rendering mode.
    pub fn focus(&self, state: bool) {
        unsafe { sys::webview_set_focus(self.inner.raw.lock().as_ptr(), state) }
    }
}

impl From<sys::WebViewState> for WebViewState {
    fn from(value: sys::WebViewState) -> Self {
        match value {
            sys::WebViewState::WEW_BEFORE_LOAD => Self::BeforeLoad,
            sys::WebViewState::WEW_LOADED => Self::Loaded,
            sys::WebViewState::WEW_LOAD_ERROR => Self::LoadError,
            sys::WebViewState::WEW_REQUEST_CLOSE => Self::RequestClose,
            sys::WebViewState::WEW_CLOSE => Self::Close,
        }
    }
}

impl From<KeyboardEventType> for sys::KeyEventType {
    fn from(val: KeyboardEventType) -> Self {
        match val {
            KeyboardEventType::KeyDown => sys::KeyEventType::WEW_KEYEVENT_KEYDOWN,
            KeyboardEventType::KeyUp => sys::KeyEventType::WEW_KEYEVENT_KEYUP,
            KeyboardEventType::Char => sys::KeyEventType::WEW_KEYEVENT_CHAR,
        }
    }
}

impl From<KeyboardModifiers> for sys::EventFlags {
    fn from(val: KeyboardModifiers) -> Self {
        match val {
            KeyboardModifiers::None => sys::EventFlags::WEW_EVENTFLAG_NONE,
            KeyboardModifiers::Win => sys::EventFlags::WEW_EVENTFLAG_COMMAND_DOWN,
            KeyboardModifiers::Shift => sys::EventFlags::WEW_EVENTFLAG_SHIFT_DOWN,
            KeyboardModifiers::Ctrl => sys::EventFlags::WEW_EVENTFLAG_CONTROL_DOWN,
            KeyboardModifiers::Alt => sys::EventFlags::WEW_EVENTFLAG_ALT_DOWN,
            KeyboardModifiers::Command => sys::EventFlags::WEW_EVENTFLAG_COMMAND_DOWN,
            KeyboardModifiers::CapsLock => sys::EventFlags::WEW_EVENTFLAG_CAPS_LOCK_ON,
            _ => sys::EventFlags::WEW_EVENTFLAG_NONE,
        }
    }
}

impl From<MouseButton> for sys::MouseButton {
    fn from(val: MouseButton) -> Self {
        match val {
            MouseButton::Left => sys::MouseButton::WEW_MBT_LEFT,
            MouseButton::Middle => sys::MouseButton::WEW_MBT_MIDDLE,
            MouseButton::Right => sys::MouseButton::WEW_MBT_RIGHT,
        }
    }
}

struct WebViewContext {
    runtime: Option<Arc<IRuntime>>,
    handler: MixWebviewHnadler,
}

pub(crate) enum MixWebviewHnadler {
    WebViewHandler(Box<dyn WebViewHandler>),
    WindowlessRenderWebViewHandler(Box<dyn WindowlessRenderWebViewHandler>),
}

extern "C" fn on_state_change_callback(state: sys::WebViewState, context: *mut c_void) {
    if context.is_null() {
        return;
    }

    let state = WebViewState::from(state);
    let context = unsafe { &mut *(context as *mut WebViewContext) };

    // Only after all webviews are closed can the runtime be closed. Here, we clear
    // the reference held by the current webview.
    //
    // If all webviews are closed, the runtime reference will be cleared,
    // and only then will the runtime's Drop be triggered.
    if state == WebViewState::Close {
        drop(context.runtime.take());
    }

    match &context.handler {
        MixWebviewHnadler::WebViewHandler(handler) => handler.on_state_change(state),
        MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) => {
            handler.on_state_change(state)
        }
    }
}

extern "C" fn on_ime_rect_callback(rect: sys::Rect, context: *mut c_void) {
    if context.is_null() {
        return;
    }

    let context = unsafe { &*(context as *mut WebViewContext) };

    if let MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) = &context.handler {
        handler.on_ime_rect(Rect {
            x: rect.x as u32,
            y: rect.y as u32,
            width: rect.width as u32,
            height: rect.height as u32,
        })
    }
}

extern "C" fn on_frame_callback(frame: *const sys::Frame, context: *mut c_void) {
    if context.is_null() || frame.is_null() {
        return;
    }

    let raw_frame = unsafe { &*frame };
    let context = unsafe { &*(context as *mut WebViewContext) };

    let frame = Frame {
        x: raw_frame.x,
        y: raw_frame.y,
        width: raw_frame.width,
        height: raw_frame.height,
        buffer: unsafe {
            std::slice::from_raw_parts(
                raw_frame.buffer as *const u8,
                raw_frame.width as usize * raw_frame.height as usize * 4,
            )
        },
        ty: if raw_frame.is_popup {
            FrameType::Popup
        } else {
            FrameType::View
        },
    };

    if let MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) = &context.handler {
        handler.on_frame(&frame);
    }
}

extern "C" fn on_title_change_callback(title: *const c_char, context: *mut c_void) {
    if context.is_null() || title.is_null() {
        return;
    }

    let context = unsafe { &*(context as *mut WebViewContext) };

    if let Ok(title) = unsafe { CStr::from_ptr(title) }.to_str() {
        match &context.handler {
            MixWebviewHnadler::WebViewHandler(handler) => handler.on_title_change(title),
            MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) => {
                handler.on_title_change(title)
            }
        }
    }
}
extern "C" fn on_fullscreen_change_callback(fullscreen: bool, context: *mut c_void) {
    if context.is_null() {
        return;
    }

    let context = unsafe { &*(context as *mut WebViewContext) };

    match &context.handler {
        MixWebviewHnadler::WebViewHandler(handler) => handler.on_fullscreen_change(fullscreen),
        MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) => {
            handler.on_fullscreen_change(fullscreen)
        }
    }
}

extern "C" fn on_message_callback(message: *const c_char, context: *mut c_void) {
    if context.is_null() || message.is_null() {
        return;
    }

    let context = unsafe { &*(context as *mut WebViewContext) };

    if let Ok(message) = unsafe { CStr::from_ptr(message) }.to_str() {
        match &context.handler {
            MixWebviewHnadler::WebViewHandler(handler) => handler.on_message(message),
            MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) => {
                handler.on_message(message)
            }
        }
    }
}

extern "C" fn on_cursor_callback(ty: sys::CursorType, context: *mut c_void) {
    if context.is_null() {
        return;
    }

    let ty = unsafe { std::mem::transmute::<sys::CursorType, CursorType>(ty) };

    let context = unsafe { &*(context as *mut WebViewContext) };
    match &context.handler {
        MixWebviewHnadler::WebViewHandler(handler) => handler.on_cursor_change(ty),
        MixWebviewHnadler::WindowlessRenderWebViewHandler(handler) => handler.on_cursor_change(ty),
    }
}