wew/lib.rs
1#![cfg_attr(
2 docsrs,
3 feature(doc_auto_cfg, doc_cfg_hide),
4 doc(cfg_hide(doc, docsrs))
5)]
6
7pub mod utils;
8
9/// Used to handle window events.
10pub mod events;
11
12/// Network request related, including implementing custom request interception.
13pub mod request;
14
15/// This module is used to manage the runtime.
16pub mod runtime;
17
18/// `WebView` module and related types.
19pub mod webview;
20
21use self::runtime::{RUNTIME_RUNNING, RuntimeAttributesBuilder};
22
23#[cfg(feature = "winit")]
24pub use winit;
25
26pub use log;
27
28#[allow(
29 dead_code,
30 unused_imports,
31 non_snake_case,
32 non_camel_case_types,
33 non_upper_case_globals
34)]
35mod sys {
36 include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
37}
38
39#[derive(Debug)]
40pub enum Error {
41 /// The current thread is not the main thread.
42 NonUIThread,
43 FailedToCreateRuntime,
44 /// Only one runtime can be created in a process. Repeated creation will
45 /// trigger this error.
46 RuntimeAlreadyExists,
47 /// If the runtime is not initialized, creating WebView and other operations
48 /// will trigger this error.
49 RuntimeNotInitialization,
50 FailedToCreateWebView,
51}
52
53impl std::error::Error for Error {}
54
55impl std::fmt::Display for Error {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 write!(f, "{:?}", self)
58 }
59}
60
61/// Represents a rectangular area
62#[derive(Debug, Clone, Copy)]
63pub struct Rect {
64 pub x: u32,
65 pub y: u32,
66 pub width: u32,
67 pub height: u32,
68}
69
70/// Message loop abstraction
71///
72/// Message loop abstraction, used to implement different message loop types.
73pub trait MessageLoopAbstract: Default + Clone + Copy {
74 /// Create a runtime attributes builder
75 ///
76 /// This function is used to create a runtime attributes builder.
77 fn create_runtime_attributes_builder<W: Default>(&self) -> RuntimeAttributesBuilder<Self, W> {
78 RuntimeAttributesBuilder::<Self, W>::default()
79 }
80}
81
82/// Multi-threaded message loop
83///
84/// Using multi-threaded message runtime will create a separate thread inside
85/// the runtime to run the message loop.
86///
87/// Note that macOS does not support this type of message loop.
88#[derive(Default, Clone, Copy)]
89pub struct MultiThreadMessageLoop;
90
91impl MessageLoopAbstract for MultiThreadMessageLoop {}
92
93/// Main thread message loop
94///
95/// You need to manually run the message loop in the main thread of the process.
96#[derive(Default, Clone, Copy)]
97pub struct MainThreadMessageLoop;
98
99impl MessageLoopAbstract for MainThreadMessageLoop {}
100
101impl MainThreadMessageLoop {
102 /// Run the message loop on main thread
103 ///
104 /// This function is used to run the message loop on main thread.
105 ///
106 /// Note that this function will block the current thread until the message
107 /// loop ends.
108 pub fn block_run(&self) {
109 unsafe { sys::run_message_loop() }
110 }
111
112 /// Quit the message loop on main thread
113 ///
114 /// This function is used to quit the message loop on main thread.
115 ///
116 /// Calling this function will cause `block_run` to exit and return.
117 pub fn quit(&self) {
118 unsafe {
119 sys::quit_message_loop();
120 }
121 }
122}
123
124/// Message loop pump
125///
126/// If you need to integrate with existing message loops, the message pump
127/// mechanism provides a way for you to drive the message loop yourself.
128#[derive(Default, Clone, Copy)]
129pub struct MessagePumpLoop;
130
131impl MessageLoopAbstract for MessagePumpLoop {}
132
133impl MessagePumpLoop {
134 /// Drive the message loop pump on main thread
135 ///
136 /// This function is used to poll the message loop on main thread.
137 ///
138 /// Note that this function won't block the current thread, external code
139 /// needs to drive the message loop pump.
140 pub fn poll(&self) {
141 use std::sync::atomic::Ordering;
142
143 if RUNTIME_RUNNING.load(Ordering::Relaxed) {
144 unsafe { sys::poll_message_loop() }
145 }
146 }
147}
148
149/// WebView abstraction
150///
151/// WebView abstraction, used to implement different WebView types.
152pub trait WebViewAbstract: Default {}
153
154/// Off-screen rendering mode
155///
156/// When using off-screen rendering mode, the WebView will not be displayed on
157/// screen, but the rendering results will be pushed through
158/// `WindowlessRenderWebViewHandler::on_frame`, and you can handle the video
159/// frames yourself. Also, in this mode, mouse and keyboard events need to be
160/// passed to the WebView by yourself.
161#[derive(Default, Clone, Copy)]
162pub struct WindowlessRenderWebView;
163
164impl WebViewAbstract for WindowlessRenderWebView {}
165
166/// Native window mode
167///
168/// When using native window mode, the WebView will create a native window and
169/// display it on screen.
170#[derive(Default, Clone, Copy)]
171pub struct NativeWindowWebView;
172
173impl WebViewAbstract for NativeWindowWebView {}
174
175/// Execute subprocess
176///
177/// This function is used to execute subprocesses.
178///
179/// ### Please be careful!
180///
181/// Do not call this function in an asynchronous runtime, such as tokio,
182/// which can lead to unexpected crashes!
183///
184/// Enabling the `tokio` feature allows for automatic checking.
185pub fn execute_subprocess() -> bool {
186 #[cfg(feature = "tokio")]
187 {
188 if tokio::runtime::Handle::try_current().is_ok() {
189 panic!("execute_subprocess is not allowed in tokio runtime");
190 }
191 }
192
193 if !utils::is_main_thread() {
194 return false;
195 }
196
197 let args = utils::Args::default();
198 (unsafe { sys::execute_subprocess(args.size() as _, args.as_ptr() as _) }) == 0
199}
200
201/// Check if current process is a subprocess
202///
203/// This function is used to check if the current process is a subprocess.
204///
205/// Note that if the current process is a subprocess, it will block until the
206/// subprocess exits.
207pub fn is_subprocess() -> bool {
208 // This check is not very strict, but processes with a "type" parameter can
209 // generally be considered subprocesses, unless the main process also uses
210 // this parameter.
211 std::env::args().any(|it| it.contains("--type"))
212}