1use super::*;
2
3pub struct NavigationCompletedArgs(pub(crate) ICoreWebView2NavigationCompletedEventArgs);
6
7impl NavigationCompletedArgs {
8 pub fn is_success(&self) -> bool {
10 unsafe { self.0.IsSuccess() }.is_ok_and(|value| value.as_bool())
11 }
12
13 pub fn navigation_id(&self) -> u64 {
15 unsafe { self.0.NavigationId() }.unwrap_or(0)
16 }
17}
18
19pub struct NavigationStartingArgs(pub(crate) ICoreWebView2NavigationStartingEventArgs);
21
22impl NavigationStartingArgs {
23 pub fn uri(&self) -> String {
25 unsafe { string::take_result(self.0.Uri()) }
26 }
27
28 pub fn is_user_initiated(&self) -> bool {
31 unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
32 }
33
34 pub fn is_redirected(&self) -> bool {
36 unsafe { self.0.IsRedirected() }.is_ok_and(|value| value.as_bool())
37 }
38
39 pub fn navigation_id(&self) -> u64 {
41 unsafe { self.0.NavigationId() }.unwrap_or(0)
42 }
43
44 pub fn is_cancelled(&self) -> bool {
46 unsafe { self.0.Cancel() }.is_ok_and(|value| value.as_bool())
47 }
48
49 pub fn set_cancel(&self, cancel: bool) -> Result<()> {
51 unsafe { self.0.SetCancel(cancel) }.ok()
52 }
53}
54
55pub struct WebMessageReceivedArgs(pub(crate) ICoreWebView2WebMessageReceivedEventArgs);
58
59impl WebMessageReceivedArgs {
60 pub fn source(&self) -> String {
62 unsafe { string::take_result(self.0.Source()) }
63 }
64
65 pub fn web_message_as_json(&self) -> String {
68 unsafe { string::take_result(self.0.WebMessageAsJson()) }
69 }
70
71 pub fn try_web_message_as_string(&self) -> Result<String> {
74 let value = unsafe { self.0.TryGetWebMessageAsString()? };
75 Ok(unsafe { string::take(value) })
76 }
77}
78
79pub struct ContentLoadingArgs(pub(crate) ICoreWebView2ContentLoadingEventArgs);
82
83impl ContentLoadingArgs {
84 pub fn is_error_page(&self) -> bool {
86 unsafe { self.0.IsErrorPage() }.is_ok_and(|value| value.as_bool())
87 }
88
89 pub fn navigation_id(&self) -> u64 {
91 unsafe { self.0.NavigationId() }.unwrap_or(0)
92 }
93}
94
95pub struct NewWindowRequestedArgs(pub(crate) ICoreWebView2NewWindowRequestedEventArgs);
97
98impl NewWindowRequestedArgs {
99 pub fn uri(&self) -> String {
101 unsafe { string::take_result(self.0.Uri()) }
102 }
103
104 pub fn is_user_initiated(&self) -> bool {
107 unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
108 }
109
110 pub fn is_handled(&self) -> bool {
112 unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
113 }
114
115 pub fn set_handled(&self, handled: bool) -> Result<()> {
119 unsafe { self.0.SetHandled(handled) }.ok()
120 }
121
122 pub fn set_new_window(&self, webview: &WebView) -> Result<()> {
125 unsafe { self.0.SetNewWindow(&webview.0) }.ok()
126 }
127
128 pub fn defer(&self) -> Result<Deferral> {
131 Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
132 }
133}
134
135#[derive(Clone, Copy, Debug, PartialEq, Eq)]
138#[non_exhaustive]
139pub enum PermissionKind {
140 Unknown,
142 Microphone,
144 Camera,
146 Geolocation,
148 Notifications,
150 OtherSensors,
152 ClipboardRead,
154 MultipleAutomaticDownloads,
156 FileReadWrite,
158 Autoplay,
160 LocalFonts,
162 MidiSystemExclusiveMessages,
164 WindowManagement,
166}
167
168impl PermissionKind {
169 fn from_raw(value: COREWEBVIEW2_PERMISSION_KIND) -> Self {
170 match value {
171 1 => Self::Microphone,
172 2 => Self::Camera,
173 3 => Self::Geolocation,
174 4 => Self::Notifications,
175 5 => Self::OtherSensors,
176 6 => Self::ClipboardRead,
177 7 => Self::MultipleAutomaticDownloads,
178 8 => Self::FileReadWrite,
179 9 => Self::Autoplay,
180 10 => Self::LocalFonts,
181 11 => Self::MidiSystemExclusiveMessages,
182 12 => Self::WindowManagement,
183 _ => Self::Unknown,
184 }
185 }
186}
187
188#[derive(Clone, Copy, Debug, PartialEq, Eq)]
190pub enum PermissionState {
191 Default,
193 Allow,
195 Deny,
197}
198
199impl PermissionState {
200 fn from_raw(value: COREWEBVIEW2_PERMISSION_STATE) -> Self {
201 match value {
202 1 => Self::Allow,
203 2 => Self::Deny,
204 _ => Self::Default,
205 }
206 }
207
208 fn to_raw(self) -> COREWEBVIEW2_PERMISSION_STATE {
209 match self {
210 Self::Default => 0,
211 Self::Allow => 1,
212 Self::Deny => 2,
213 }
214 }
215}
216
217pub struct PermissionRequestedArgs(pub(crate) ICoreWebView2PermissionRequestedEventArgs);
221
222impl PermissionRequestedArgs {
223 pub fn uri(&self) -> String {
225 unsafe { string::take_result(self.0.Uri()) }
226 }
227
228 pub fn kind(&self) -> PermissionKind {
230 unsafe { self.0.PermissionKind() }.map_or(PermissionKind::Unknown, PermissionKind::from_raw)
231 }
232
233 pub fn is_user_initiated(&self) -> bool {
236 unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
237 }
238
239 pub fn state(&self) -> PermissionState {
241 unsafe { self.0.State() }.map_or(PermissionState::Default, PermissionState::from_raw)
242 }
243
244 pub fn set_state(&self, state: PermissionState) -> Result<()> {
246 unsafe { self.0.SetState(state.to_raw()) }.ok()
247 }
248
249 pub fn defer(&self) -> Result<Deferral> {
252 Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
253 }
254}
255
256#[derive(Clone, Copy, Debug, PartialEq, Eq)]
258#[non_exhaustive]
259pub enum ProcessFailedKind {
260 Unknown,
262 BrowserProcessExited,
264 RenderProcessExited,
267 RenderProcessUnresponsive,
269 FrameRenderProcessExited,
271 UtilityProcessExited,
273 SandboxHelperProcessExited,
275 GpuProcessExited,
277 PpapiPluginProcessExited,
279 PpapiBrokerProcessExited,
281 UnknownProcessExited,
283}
284
285impl ProcessFailedKind {
286 fn from_raw(value: COREWEBVIEW2_PROCESS_FAILED_KIND) -> Self {
287 match value {
288 0 => Self::BrowserProcessExited,
289 1 => Self::RenderProcessExited,
290 2 => Self::RenderProcessUnresponsive,
291 3 => Self::FrameRenderProcessExited,
292 4 => Self::UtilityProcessExited,
293 5 => Self::SandboxHelperProcessExited,
294 6 => Self::GpuProcessExited,
295 7 => Self::PpapiPluginProcessExited,
296 8 => Self::PpapiBrokerProcessExited,
297 9 => Self::UnknownProcessExited,
298 _ => Self::Unknown,
299 }
300 }
301}
302
303pub struct ProcessFailedArgs(pub(crate) ICoreWebView2ProcessFailedEventArgs);
306
307impl ProcessFailedArgs {
308 pub fn kind(&self) -> ProcessFailedKind {
310 unsafe { self.0.ProcessFailedKind() }
311 .map_or(ProcessFailedKind::Unknown, ProcessFailedKind::from_raw)
312 }
313}
314
315#[derive(Clone, Copy, Debug, PartialEq, Eq)]
318pub enum MoveFocusReason {
319 Programmatic,
321 Next,
323 Previous,
325}
326
327impl MoveFocusReason {
328 fn from_raw(value: COREWEBVIEW2_MOVE_FOCUS_REASON) -> Self {
329 match value {
330 1 => Self::Next,
331 2 => Self::Previous,
332 _ => Self::Programmatic,
333 }
334 }
335
336 pub(crate) fn to_raw(self) -> COREWEBVIEW2_MOVE_FOCUS_REASON {
337 match self {
338 Self::Programmatic => 0,
339 Self::Next => 1,
340 Self::Previous => 2,
341 }
342 }
343}
344
345pub struct MoveFocusRequestedArgs(pub(crate) ICoreWebView2MoveFocusRequestedEventArgs);
347
348impl MoveFocusRequestedArgs {
349 pub fn reason(&self) -> MoveFocusReason {
351 unsafe { self.0.Reason() }.map_or(MoveFocusReason::Programmatic, MoveFocusReason::from_raw)
352 }
353
354 pub fn is_handled(&self) -> bool {
356 unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
357 }
358
359 pub fn set_handled(&self, handled: bool) -> Result<()> {
362 unsafe { self.0.SetHandled(handled) }.ok()
363 }
364}
365
366#[derive(Clone, Copy, Debug, PartialEq, Eq)]
368pub enum KeyEventKind {
369 KeyDown,
371 KeyUp,
373 SystemKeyDown,
375 SystemKeyUp,
377}
378
379impl KeyEventKind {
380 fn from_raw(value: COREWEBVIEW2_KEY_EVENT_KIND) -> Self {
381 match value {
382 1 => Self::KeyUp,
383 2 => Self::SystemKeyDown,
384 3 => Self::SystemKeyUp,
385 _ => Self::KeyDown,
386 }
387 }
388}
389
390pub struct AcceleratorKeyPressedArgs(pub(crate) ICoreWebView2AcceleratorKeyPressedEventArgs);
392
393impl AcceleratorKeyPressedArgs {
394 pub fn key_event_kind(&self) -> KeyEventKind {
397 unsafe { self.0.KeyEventKind() }.map_or(KeyEventKind::KeyDown, KeyEventKind::from_raw)
398 }
399
400 pub fn virtual_key(&self) -> u32 {
402 unsafe { self.0.VirtualKey() }.unwrap_or(0)
403 }
404
405 pub fn is_handled(&self) -> bool {
407 unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
408 }
409
410 pub fn set_handled(&self, handled: bool) -> Result<()> {
413 unsafe { self.0.SetHandled(handled) }.ok()
414 }
415}
416
417pub struct DevToolsProtocolEventReceivedArgs(
421 pub(crate) ICoreWebView2DevToolsProtocolEventReceivedEventArgs,
422);
423
424impl DevToolsProtocolEventReceivedArgs {
425 pub fn parameter_object_as_json(&self) -> String {
428 unsafe { string::take_result(self.0.ParameterObjectAsJson()) }
429 }
430}
431
432#[must_use]
434pub struct EventRegistration(Option<Box<dyn FnOnce()>>);
435
436impl EventRegistration {
437 pub(crate) fn new<F: FnOnce() + 'static>(remove: F) -> Self {
438 Self(Some(Box::new(remove)))
439 }
440
441 pub fn remove(mut self) {
443 if let Some(remove) = self.0.take() {
444 remove();
445 }
446 }
447}
448
449impl Drop for EventRegistration {
450 fn drop(&mut self) {
451 if let Some(remove) = self.0.take() {
452 remove();
453 }
454 }
455}