1use std::{
18 any::{Any, type_name},
19 fmt,
20 marker::PhantomData,
21 sync::Arc,
22};
23
24use raw_window_handle::{DisplayHandle, HandleError, WindowHandle};
25use tauri_utils::{
26 Theme,
27 config::{Color, Config, WindowConfig},
28};
29use url::Url;
30
31#[cfg(target_os = "macos")]
32use crate::ActivationPolicy;
33use crate::{
34 Cookie, DeviceEventFilter, Error, EventLoopProxy, Icon, ProgressBarState, ResizeDirection,
35 Result, RunEvent, Runtime, RuntimeHandle, RuntimeInitArgs, RuntimeInitAttrs, UserAttentionType,
36 UserEvent, WebviewDispatch, WindowDispatch,
37 dpi::{PhysicalPosition, PhysicalSize, Position, Rect, Size},
38 monitor::Monitor,
39 webview::{
40 DetachedWebview, NewWindowFeatures, NewWindowHandler, PendingWebview, WebviewIpcHandler,
41 },
42 window::{
43 CursorIcon, DetachedWindow, DetachedWindowWebview, PendingWindow, RawWindow, WindowBuilder,
44 WindowBuilderBase, WindowId, WindowSizeConstraints,
45 },
46};
47
48type AfterWindowCreation = Box<dyn Fn(RawWindow<'_>) + Send>;
49type RunCallback<T> = Box<dyn FnMut(RunEvent<T>)>;
50type MainThreadTask = Box<dyn FnOnce() + Send>;
51#[cfg(target_os = "android")]
52type AndroidContextTask =
53 Box<dyn FnOnce(&mut jni::JNIEnv, &jni::objects::JObject, &jni::objects::JObject) + Send>;
54
55fn mismatch<Expected: ?Sized>(what: &str) -> Error {
56 Error::RuntimeTypeMismatch(format!(
57 "expected {what} of type `{}`",
58 type_name::<Expected>()
59 ))
60}
61
62pub struct DynWebview(Box<dyn Any>);
70
71impl DynWebview {
72 pub fn new<W: Any>(webview: W) -> Self {
74 Self(Box::new(webview))
75 }
76
77 pub fn is<W: Any>(&self) -> bool {
79 self.0.is::<W>()
80 }
81
82 pub fn downcast_ref<W: Any>(&self) -> Option<&W> {
84 self.0.downcast_ref()
85 }
86
87 pub fn downcast_mut<W: Any>(&mut self) -> Option<&mut W> {
89 self.0.downcast_mut()
90 }
91
92 pub fn downcast<W: Any>(self) -> std::result::Result<W, Self> {
94 self.0.downcast::<W>().map(|w| *w).map_err(Self)
95 }
96
97 pub fn into_inner(self) -> Box<dyn Any> {
99 self.0
100 }
101}
102
103impl fmt::Debug for DynWebview {
104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 f.debug_struct("DynWebview").finish_non_exhaustive()
106 }
107}
108
109pub struct DynWindowOpener(Box<dyn Any + Send + Sync>);
113
114impl DynWindowOpener {
115 pub fn new<O: Any + Send + Sync>(opener: O) -> Self {
117 Self(Box::new(opener))
118 }
119
120 pub fn is<O: Any>(&self) -> bool {
122 self.0.is::<O>()
123 }
124
125 pub fn downcast_ref<O: Any>(&self) -> Option<&O> {
127 self.0.downcast_ref()
128 }
129
130 pub fn downcast<O: Any>(self) -> Result<O> {
132 self
133 .0
134 .downcast::<O>()
135 .map(|o| *o)
136 .map_err(|_| mismatch::<O>("window opener"))
137 }
138}
139
140impl fmt::Debug for DynWindowOpener {
141 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142 f.debug_struct("DynWindowOpener").finish_non_exhaustive()
143 }
144}
145
146#[derive(Default)]
152pub struct DynWebviewAttributes(Option<Box<dyn Any + Send + Sync>>);
153
154impl DynWebviewAttributes {
155 pub fn new<A: Any + Send + Sync>(attributes: A) -> Self {
157 Self(Some(Box::new(attributes)))
158 }
159
160 pub fn is<A: Any>(&self) -> bool {
162 self
163 .0
164 .as_ref()
165 .is_some_and(|attributes| attributes.is::<A>())
166 }
167
168 pub fn downcast_ref<A: Any>(&self) -> Option<&A> {
170 self
171 .0
172 .as_ref()
173 .and_then(|attributes| attributes.downcast_ref())
174 }
175
176 pub fn get_or_default<A: Any + Default + Send + Sync>(&mut self) -> Option<&mut A> {
180 self
181 .0
182 .get_or_insert_with(|| Box::new(A::default()))
183 .downcast_mut()
184 }
185
186 pub fn downcast<A: Any + Default>(self) -> Result<A> {
190 match self.0 {
191 None => Ok(A::default()),
192 Some(attributes) => attributes
193 .downcast::<A>()
194 .map(|a| *a)
195 .map_err(|_| mismatch::<A>("webview attributes")),
196 }
197 }
198}
199
200impl fmt::Debug for DynWebviewAttributes {
201 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
202 f.debug_struct("DynWebviewAttributes")
203 .field("set", &self.0.is_some())
204 .finish()
205 }
206}
207
208type RuntimeBuilderCustomizer = Arc<dyn Fn(&mut dyn Any) + Send + Sync>;
213
214#[derive(Clone)]
215enum WindowBuilderOp {
216 Center,
217 Position(f64, f64),
218 InnerSize(f64, f64),
219 MinInnerSize(f64, f64),
220 MaxInnerSize(f64, f64),
221 InnerSizeConstraints(WindowSizeConstraints),
222 PreventOverflow,
223 PreventOverflowWithMargin(Size),
224 Resizable(bool),
225 Maximizable(bool),
226 Minimizable(bool),
227 Closable(bool),
228 Title(String),
229 Fullscreen(bool),
230 Focused(bool),
231 Focusable(bool),
232 Maximized(bool),
233 Visible(bool),
234 Transparent(bool),
235 Decorations(bool),
236 AlwaysOnBottom(bool),
237 AlwaysOnTop(bool),
238 VisibleOnAllWorkspaces(bool),
239 ContentProtected(bool),
240 Icon(Icon<'static>),
241 SkipTaskbar(bool),
242 BackgroundColor(Color),
243 Shadow(bool),
244 #[cfg(windows)]
245 Owner(windows::Win32::Foundation::HWND),
246 #[cfg(windows)]
247 Parent(windows::Win32::Foundation::HWND),
248 #[cfg(target_os = "macos")]
249 Parent(*mut std::ffi::c_void),
250 #[cfg(any(
251 target_os = "linux",
252 target_os = "dragonfly",
253 target_os = "freebsd",
254 target_os = "netbsd",
255 target_os = "openbsd"
256 ))]
257 TransientFor(*mut std::ffi::c_void),
258 #[cfg(windows)]
259 DragAndDrop(bool),
260 #[cfg(target_os = "macos")]
261 TitleBarStyle(tauri_utils::TitleBarStyle),
262 #[cfg(target_os = "macos")]
263 TrafficLightPosition(Position),
264 #[cfg(target_os = "macos")]
265 HiddenTitle(bool),
266 #[cfg(target_os = "macos")]
267 TabbingIdentifier(String),
268 Theme(Option<Theme>),
269 WindowClassname(String),
270 NoRedirectionBitmap(bool),
271 #[cfg(target_os = "android")]
272 ActivityName(String),
273 #[cfg(target_os = "android")]
274 CreatedByActivityName(String),
275 #[cfg(target_os = "ios")]
276 RequestedBySceneIdentifier(String),
277 Customize(RuntimeBuilderCustomizer),
278}
279
280impl fmt::Debug for WindowBuilderOp {
281 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282 match self {
283 Self::Center => f.write_str("Center"),
284 Self::Position(x, y) => f.debug_tuple("Position").field(x).field(y).finish(),
285 Self::InnerSize(w, h) => f.debug_tuple("InnerSize").field(w).field(h).finish(),
286 Self::MinInnerSize(w, h) => f.debug_tuple("MinInnerSize").field(w).field(h).finish(),
287 Self::MaxInnerSize(w, h) => f.debug_tuple("MaxInnerSize").field(w).field(h).finish(),
288 Self::InnerSizeConstraints(c) => f.debug_tuple("InnerSizeConstraints").field(c).finish(),
289 Self::PreventOverflow => f.write_str("PreventOverflow"),
290 Self::PreventOverflowWithMargin(m) => {
291 f.debug_tuple("PreventOverflowWithMargin").field(m).finish()
292 }
293 Self::Resizable(v) => f.debug_tuple("Resizable").field(v).finish(),
294 Self::Maximizable(v) => f.debug_tuple("Maximizable").field(v).finish(),
295 Self::Minimizable(v) => f.debug_tuple("Minimizable").field(v).finish(),
296 Self::Closable(v) => f.debug_tuple("Closable").field(v).finish(),
297 Self::Title(v) => f.debug_tuple("Title").field(v).finish(),
298 Self::Fullscreen(v) => f.debug_tuple("Fullscreen").field(v).finish(),
299 Self::Focused(v) => f.debug_tuple("Focused").field(v).finish(),
300 Self::Focusable(v) => f.debug_tuple("Focusable").field(v).finish(),
301 Self::Maximized(v) => f.debug_tuple("Maximized").field(v).finish(),
302 Self::Visible(v) => f.debug_tuple("Visible").field(v).finish(),
303 Self::Transparent(v) => f.debug_tuple("Transparent").field(v).finish(),
304 Self::Decorations(v) => f.debug_tuple("Decorations").field(v).finish(),
305 Self::AlwaysOnBottom(v) => f.debug_tuple("AlwaysOnBottom").field(v).finish(),
306 Self::AlwaysOnTop(v) => f.debug_tuple("AlwaysOnTop").field(v).finish(),
307 Self::VisibleOnAllWorkspaces(v) => f.debug_tuple("VisibleOnAllWorkspaces").field(v).finish(),
308 Self::ContentProtected(v) => f.debug_tuple("ContentProtected").field(v).finish(),
309 Self::Icon(i) => f
310 .debug_struct("Icon")
311 .field("width", &i.width)
312 .field("height", &i.height)
313 .finish(),
314 Self::SkipTaskbar(v) => f.debug_tuple("SkipTaskbar").field(v).finish(),
315 Self::BackgroundColor(v) => f.debug_tuple("BackgroundColor").field(v).finish(),
316 Self::Shadow(v) => f.debug_tuple("Shadow").field(v).finish(),
317 #[cfg(windows)]
318 Self::Owner(v) => f.debug_tuple("Owner").field(v).finish(),
319 #[cfg(any(windows, target_os = "macos"))]
320 Self::Parent(v) => f.debug_tuple("Parent").field(v).finish(),
321 #[cfg(any(
322 target_os = "linux",
323 target_os = "dragonfly",
324 target_os = "freebsd",
325 target_os = "netbsd",
326 target_os = "openbsd"
327 ))]
328 Self::TransientFor(v) => f.debug_tuple("TransientFor").field(v).finish(),
329 #[cfg(windows)]
330 Self::DragAndDrop(v) => f.debug_tuple("DragAndDrop").field(v).finish(),
331 #[cfg(target_os = "macos")]
332 Self::TitleBarStyle(v) => f.debug_tuple("TitleBarStyle").field(v).finish(),
333 #[cfg(target_os = "macos")]
334 Self::TrafficLightPosition(v) => f.debug_tuple("TrafficLightPosition").field(v).finish(),
335 #[cfg(target_os = "macos")]
336 Self::HiddenTitle(v) => f.debug_tuple("HiddenTitle").field(v).finish(),
337 #[cfg(target_os = "macos")]
338 Self::TabbingIdentifier(v) => f.debug_tuple("TabbingIdentifier").field(v).finish(),
339 Self::Theme(v) => f.debug_tuple("Theme").field(v).finish(),
340 Self::WindowClassname(v) => f.debug_tuple("WindowClassname").field(v).finish(),
341 Self::NoRedirectionBitmap(v) => f.debug_tuple("NoRedirectionBitmap").field(v).finish(),
342 #[cfg(target_os = "android")]
343 Self::ActivityName(v) => f.debug_tuple("ActivityName").field(v).finish(),
344 #[cfg(target_os = "android")]
345 Self::CreatedByActivityName(v) => f.debug_tuple("CreatedByActivityName").field(v).finish(),
346 #[cfg(target_os = "ios")]
347 Self::RequestedBySceneIdentifier(v) => f
348 .debug_tuple("RequestedBySceneIdentifier")
349 .field(v)
350 .finish(),
351 Self::Customize(_) => f.write_str("Customize"),
352 }
353 }
354}
355
356#[derive(Debug, Clone, Default)]
362pub struct DynWindowBuilder {
363 config: Option<WindowConfig>,
364 ops: Vec<WindowBuilderOp>,
365}
366
367#[allow(clippy::non_send_fields_in_send_ty)]
370unsafe impl Send for DynWindowBuilder {}
371
372impl DynWindowBuilder {
373 #[must_use]
379 pub fn customize<F: Fn(&mut dyn Any) + Send + Sync + 'static>(mut self, f: F) -> Self {
380 self.ops.push(WindowBuilderOp::Customize(Arc::new(f)));
381 self
382 }
383
384 pub fn apply<B: WindowBuilder>(self) -> Result<B> {
386 let mut builder = match &self.config {
387 Some(config) => B::with_config(config),
388 None => B::new(),
389 };
390 for op in self.ops {
391 builder = match op {
392 WindowBuilderOp::Center => builder.center(),
393 WindowBuilderOp::Position(x, y) => builder.position(x, y),
394 WindowBuilderOp::InnerSize(w, h) => builder.inner_size(w, h),
395 WindowBuilderOp::MinInnerSize(w, h) => builder.min_inner_size(w, h),
396 WindowBuilderOp::MaxInnerSize(w, h) => builder.max_inner_size(w, h),
397 WindowBuilderOp::InnerSizeConstraints(c) => builder.inner_size_constraints(c),
398 WindowBuilderOp::PreventOverflow => builder.prevent_overflow(),
399 WindowBuilderOp::PreventOverflowWithMargin(m) => builder.prevent_overflow_with_margin(m),
400 WindowBuilderOp::Resizable(v) => builder.resizable(v),
401 WindowBuilderOp::Maximizable(v) => builder.maximizable(v),
402 WindowBuilderOp::Minimizable(v) => builder.minimizable(v),
403 WindowBuilderOp::Closable(v) => builder.closable(v),
404 WindowBuilderOp::Title(v) => builder.title(v),
405 WindowBuilderOp::Fullscreen(v) => builder.fullscreen(v),
406 WindowBuilderOp::Focused(v) => builder.focused(v),
407 WindowBuilderOp::Focusable(v) => builder.focusable(v),
408 WindowBuilderOp::Maximized(v) => builder.maximized(v),
409 WindowBuilderOp::Visible(v) => builder.visible(v),
410 WindowBuilderOp::Transparent(v) => builder.transparent(v),
411 WindowBuilderOp::Decorations(v) => builder.decorations(v),
412 WindowBuilderOp::AlwaysOnBottom(v) => builder.always_on_bottom(v),
413 WindowBuilderOp::AlwaysOnTop(v) => builder.always_on_top(v),
414 WindowBuilderOp::VisibleOnAllWorkspaces(v) => builder.visible_on_all_workspaces(v),
415 WindowBuilderOp::ContentProtected(v) => builder.content_protected(v),
416 WindowBuilderOp::Icon(icon) => builder.icon(icon)?,
417 WindowBuilderOp::SkipTaskbar(v) => builder.skip_taskbar(v),
418 WindowBuilderOp::BackgroundColor(v) => builder.background_color(v),
419 WindowBuilderOp::Shadow(v) => builder.shadow(v),
420 #[cfg(windows)]
421 WindowBuilderOp::Owner(v) => builder.owner(v),
422 #[cfg(any(windows, target_os = "macos"))]
423 WindowBuilderOp::Parent(v) => builder.parent(v),
424 #[cfg(any(
425 target_os = "linux",
426 target_os = "dragonfly",
427 target_os = "freebsd",
428 target_os = "netbsd",
429 target_os = "openbsd"
430 ))]
431 WindowBuilderOp::TransientFor(v) => builder.transient_for(v),
432 #[cfg(windows)]
433 WindowBuilderOp::DragAndDrop(v) => builder.drag_and_drop(v),
434 #[cfg(target_os = "macos")]
435 WindowBuilderOp::TitleBarStyle(v) => builder.title_bar_style(v),
436 #[cfg(target_os = "macos")]
437 WindowBuilderOp::TrafficLightPosition(v) => builder.traffic_light_position(v),
438 #[cfg(target_os = "macos")]
439 WindowBuilderOp::HiddenTitle(v) => builder.hidden_title(v),
440 #[cfg(target_os = "macos")]
441 WindowBuilderOp::TabbingIdentifier(v) => builder.tabbing_identifier(&v),
442 WindowBuilderOp::Theme(v) => builder.theme(v),
443 WindowBuilderOp::WindowClassname(v) => builder.window_classname(v),
444 WindowBuilderOp::NoRedirectionBitmap(v) => builder.no_redirection_bitmap(v),
445 #[cfg(target_os = "android")]
446 WindowBuilderOp::ActivityName(v) => builder.activity_name(v),
447 #[cfg(target_os = "android")]
448 WindowBuilderOp::CreatedByActivityName(v) => builder.created_by_activity_name(v),
449 #[cfg(target_os = "ios")]
450 WindowBuilderOp::RequestedBySceneIdentifier(v) => builder.requested_by_scene_identifier(v),
451 WindowBuilderOp::Customize(f) => {
452 f(&mut builder);
453 builder
454 }
455 };
456 }
457 Ok(builder)
458 }
459
460 fn push(mut self, op: WindowBuilderOp) -> Self {
461 self.ops.push(op);
462 self
463 }
464}
465
466impl WindowBuilderBase for DynWindowBuilder {}
467
468impl WindowBuilder for DynWindowBuilder {
469 fn new() -> Self {
470 Self::default()
471 }
472
473 fn with_config(config: &WindowConfig) -> Self {
474 Self {
475 config: Some(config.clone()),
476 ops: Vec::new(),
477 }
478 }
479
480 fn center(self) -> Self {
481 self.push(WindowBuilderOp::Center)
482 }
483
484 fn position(self, x: f64, y: f64) -> Self {
485 self.push(WindowBuilderOp::Position(x, y))
486 }
487
488 fn inner_size(self, width: f64, height: f64) -> Self {
489 self.push(WindowBuilderOp::InnerSize(width, height))
490 }
491
492 fn min_inner_size(self, min_width: f64, min_height: f64) -> Self {
493 self.push(WindowBuilderOp::MinInnerSize(min_width, min_height))
494 }
495
496 fn max_inner_size(self, max_width: f64, max_height: f64) -> Self {
497 self.push(WindowBuilderOp::MaxInnerSize(max_width, max_height))
498 }
499
500 fn inner_size_constraints(self, constraints: WindowSizeConstraints) -> Self {
501 self.push(WindowBuilderOp::InnerSizeConstraints(constraints))
502 }
503
504 fn prevent_overflow(self) -> Self {
505 self.push(WindowBuilderOp::PreventOverflow)
506 }
507
508 fn prevent_overflow_with_margin(self, margin: Size) -> Self {
509 self.push(WindowBuilderOp::PreventOverflowWithMargin(margin))
510 }
511
512 fn resizable(self, resizable: bool) -> Self {
513 self.push(WindowBuilderOp::Resizable(resizable))
514 }
515
516 fn maximizable(self, maximizable: bool) -> Self {
517 self.push(WindowBuilderOp::Maximizable(maximizable))
518 }
519
520 fn minimizable(self, minimizable: bool) -> Self {
521 self.push(WindowBuilderOp::Minimizable(minimizable))
522 }
523
524 fn closable(self, closable: bool) -> Self {
525 self.push(WindowBuilderOp::Closable(closable))
526 }
527
528 fn title<S: Into<String>>(self, title: S) -> Self {
529 self.push(WindowBuilderOp::Title(title.into()))
530 }
531
532 fn fullscreen(self, fullscreen: bool) -> Self {
533 self.push(WindowBuilderOp::Fullscreen(fullscreen))
534 }
535
536 fn focused(self, focused: bool) -> Self {
537 self.push(WindowBuilderOp::Focused(focused))
538 }
539
540 fn focusable(self, focusable: bool) -> Self {
541 self.push(WindowBuilderOp::Focusable(focusable))
542 }
543
544 fn maximized(self, maximized: bool) -> Self {
545 self.push(WindowBuilderOp::Maximized(maximized))
546 }
547
548 fn visible(self, visible: bool) -> Self {
549 self.push(WindowBuilderOp::Visible(visible))
550 }
551
552 fn transparent(self, transparent: bool) -> Self {
553 self.push(WindowBuilderOp::Transparent(transparent))
554 }
555
556 fn decorations(self, decorations: bool) -> Self {
557 self.push(WindowBuilderOp::Decorations(decorations))
558 }
559
560 fn always_on_bottom(self, always_on_bottom: bool) -> Self {
561 self.push(WindowBuilderOp::AlwaysOnBottom(always_on_bottom))
562 }
563
564 fn always_on_top(self, always_on_top: bool) -> Self {
565 self.push(WindowBuilderOp::AlwaysOnTop(always_on_top))
566 }
567
568 fn visible_on_all_workspaces(self, visible_on_all_workspaces: bool) -> Self {
569 self.push(WindowBuilderOp::VisibleOnAllWorkspaces(
570 visible_on_all_workspaces,
571 ))
572 }
573
574 fn content_protected(self, protected: bool) -> Self {
575 self.push(WindowBuilderOp::ContentProtected(protected))
576 }
577
578 fn icon(self, icon: Icon) -> Result<Self> {
579 let expected_len = (icon.width as usize)
580 .saturating_mul(icon.height as usize)
581 .saturating_mul(4);
582 if icon.rgba.len() != expected_len {
583 return Err(Error::InvalidIcon(
584 format!(
585 "the icon RGBA buffer has {} bytes but {}x{} pixels require {expected_len}",
586 icon.rgba.len(),
587 icon.width,
588 icon.height
589 )
590 .into(),
591 ));
592 }
593 Ok(self.push(WindowBuilderOp::Icon(icon.into_owned())))
594 }
595
596 fn skip_taskbar(self, skip: bool) -> Self {
597 self.push(WindowBuilderOp::SkipTaskbar(skip))
598 }
599
600 fn background_color(self, color: Color) -> Self {
601 self.push(WindowBuilderOp::BackgroundColor(color))
602 }
603
604 fn shadow(self, enable: bool) -> Self {
605 self.push(WindowBuilderOp::Shadow(enable))
606 }
607
608 #[cfg(windows)]
609 fn owner(self, owner: windows::Win32::Foundation::HWND) -> Self {
610 self.push(WindowBuilderOp::Owner(owner))
611 }
612
613 #[cfg(windows)]
614 fn parent(self, parent: windows::Win32::Foundation::HWND) -> Self {
615 self.push(WindowBuilderOp::Parent(parent))
616 }
617
618 #[cfg(target_os = "macos")]
619 fn parent(self, parent: *mut std::ffi::c_void) -> Self {
620 self.push(WindowBuilderOp::Parent(parent))
621 }
622
623 #[cfg(any(
624 target_os = "linux",
625 target_os = "dragonfly",
626 target_os = "freebsd",
627 target_os = "netbsd",
628 target_os = "openbsd"
629 ))]
630 fn transient_for(self, parent: *mut std::ffi::c_void) -> Self {
631 self.push(WindowBuilderOp::TransientFor(parent))
632 }
633
634 #[cfg(windows)]
635 fn drag_and_drop(self, enabled: bool) -> Self {
636 self.push(WindowBuilderOp::DragAndDrop(enabled))
637 }
638
639 #[cfg(target_os = "macos")]
640 fn title_bar_style(self, style: tauri_utils::TitleBarStyle) -> Self {
641 self.push(WindowBuilderOp::TitleBarStyle(style))
642 }
643
644 #[cfg(target_os = "macos")]
645 fn traffic_light_position<P: Into<Position>>(self, position: P) -> Self {
646 self.push(WindowBuilderOp::TrafficLightPosition(position.into()))
647 }
648
649 #[cfg(target_os = "macos")]
650 fn hidden_title(self, hidden: bool) -> Self {
651 self.push(WindowBuilderOp::HiddenTitle(hidden))
652 }
653
654 #[cfg(target_os = "macos")]
655 fn tabbing_identifier(self, identifier: &str) -> Self {
656 self.push(WindowBuilderOp::TabbingIdentifier(identifier.to_string()))
657 }
658
659 fn theme(self, theme: Option<Theme>) -> Self {
660 self.push(WindowBuilderOp::Theme(theme))
661 }
662
663 fn has_icon(&self) -> bool {
664 self
665 .ops
666 .iter()
667 .any(|op| matches!(op, WindowBuilderOp::Icon(_)))
668 }
669
670 fn get_theme(&self) -> Option<Theme> {
671 self
672 .ops
673 .iter()
674 .rev()
675 .find_map(|op| match op {
676 WindowBuilderOp::Theme(theme) => Some(*theme),
677 _ => None,
678 })
679 .unwrap_or_else(|| self.config.as_ref().and_then(|config| config.theme))
680 }
681
682 fn window_classname<S: Into<String>>(self, window_classname: S) -> Self {
683 self.push(WindowBuilderOp::WindowClassname(window_classname.into()))
684 }
685
686 fn no_redirection_bitmap(self, enable: bool) -> Self {
687 self.push(WindowBuilderOp::NoRedirectionBitmap(enable))
688 }
689
690 #[cfg(target_os = "android")]
691 fn activity_name<S: Into<String>>(self, class_name: S) -> Self {
692 self.push(WindowBuilderOp::ActivityName(class_name.into()))
693 }
694
695 #[cfg(target_os = "android")]
696 fn created_by_activity_name<S: Into<String>>(self, class_name: S) -> Self {
697 self.push(WindowBuilderOp::CreatedByActivityName(class_name.into()))
698 }
699
700 #[cfg(target_os = "ios")]
701 fn requested_by_scene_identifier<S: Into<String>>(self, identifier: S) -> Self {
702 self.push(WindowBuilderOp::RequestedBySceneIdentifier(
703 identifier.into(),
704 ))
705 }
706}
707
708fn pending_window_from_dyn<T: UserEvent, R: Runtime<T>>(
713 pending: PendingWindow<T, DynRuntime<T>>,
714) -> Result<PendingWindow<T, R>> {
715 let PendingWindow {
716 label,
717 window_builder,
718 webview,
719 } = pending;
720 Ok(PendingWindow {
721 label,
722 window_builder: window_builder.apply()?,
723 webview: webview.map(pending_webview_from_dyn::<T, R>).transpose()?,
724 })
725}
726
727fn pending_webview_from_dyn<T: UserEvent, R: Runtime<T>>(
728 pending: PendingWebview<T, DynRuntime<T>>,
729) -> Result<PendingWebview<T, R>> {
730 let PendingWebview {
731 label,
732 webview_attributes,
733 opener,
734 runtime_specific_attributes,
735 uri_scheme_protocols,
736 ipc_handler,
737 navigation_handler,
738 new_window_handler,
739 document_title_changed_handler,
740 url,
741 #[cfg(target_os = "android")]
742 on_webview_created,
743 web_resource_request_handler,
744 on_page_load_handler,
745 download_handler,
746 permission_request_handler,
747 on_web_content_process_terminate_handler,
748 } = pending;
749
750 let opener = opener
751 .map(|opener| opener.downcast::<R::WindowOpener>())
752 .transpose()?;
753
754 let runtime_specific_attributes =
755 runtime_specific_attributes.downcast::<R::RuntimeWebviewAttributes>()?;
756
757 let ipc_handler = ipc_handler.map(|handler| -> WebviewIpcHandler<T, R> {
758 Box::new(move |webview, request| handler(detached_webview_into_dyn(webview), request))
759 });
760
761 let new_window_handler = new_window_handler.map(|handler| -> Box<NewWindowHandler<T, R>> {
762 Box::new(move |url, features| handler(url, new_window_features_into_dyn(features)))
763 });
764
765 Ok(PendingWebview {
766 label,
767 webview_attributes,
768 opener,
769 runtime_specific_attributes,
770 uri_scheme_protocols,
771 ipc_handler,
772 navigation_handler,
773 new_window_handler,
774 document_title_changed_handler,
775 url,
776 #[cfg(target_os = "android")]
777 on_webview_created,
778 web_resource_request_handler,
779 on_page_load_handler,
780 download_handler,
781 permission_request_handler,
782 on_web_content_process_terminate_handler,
783 })
784}
785
786fn new_window_features_into_dyn<T: UserEvent, R: Runtime<T>>(
787 features: NewWindowFeatures<T, R>,
788) -> NewWindowFeatures<T, DynRuntime<T>> {
789 let size = features.size();
790 let position = features.position();
791 NewWindowFeatures::new(size, position, DynWindowOpener::new(features.into_opener()))
792}
793
794fn detached_window_into_dyn<T: UserEvent, R: Runtime<T>>(
795 window: DetachedWindow<T, R>,
796) -> DetachedWindow<T, DynRuntime<T>> {
797 DetachedWindow {
798 id: window.id,
799 label: window.label,
800 dispatcher: DynWindowDispatcher::new(window.dispatcher),
801 webview: window.webview.map(|webview| DetachedWindowWebview {
802 webview: detached_webview_into_dyn(webview.webview),
803 use_https_scheme: webview.use_https_scheme,
804 devtools: webview.devtools,
805 }),
806 }
807}
808
809fn detached_webview_into_dyn<T: UserEvent, R: Runtime<T>>(
810 webview: DetachedWebview<T, R>,
811) -> DetachedWebview<T, DynRuntime<T>> {
812 DetachedWebview {
813 label: webview.label,
814 dispatcher: DynWebviewDispatcher::new(webview.dispatcher),
815 }
816}
817
818trait ErasedEventLoopProxy<T: UserEvent>: fmt::Debug + Send + Sync {
823 fn send_event(&self, event: T) -> Result<()>;
824}
825
826impl<T: UserEvent, P: EventLoopProxy<T>> ErasedEventLoopProxy<T> for P {
827 fn send_event(&self, event: T) -> Result<()> {
828 EventLoopProxy::send_event(self, event)
829 }
830}
831
832#[derive(Debug)]
834pub struct DynEventLoopProxy<T: UserEvent> {
835 inner: Arc<dyn ErasedEventLoopProxy<T>>,
836}
837
838impl<T: UserEvent> Clone for DynEventLoopProxy<T> {
839 fn clone(&self) -> Self {
840 Self {
841 inner: self.inner.clone(),
842 }
843 }
844}
845
846impl<T: UserEvent> DynEventLoopProxy<T> {
847 fn new<P: EventLoopProxy<T> + 'static>(proxy: P) -> Self {
848 Self {
849 inner: Arc::new(proxy),
850 }
851 }
852}
853
854impl<T: UserEvent> EventLoopProxy<T> for DynEventLoopProxy<T> {
855 fn send_event(&self, event: T) -> Result<()> {
856 self.inner.send_event(event)
857 }
858}
859
860trait ErasedRuntimeHandle<T: UserEvent>: fmt::Debug + Send + Sync + Any {
865 fn create_proxy(&self) -> DynEventLoopProxy<T>;
866 #[cfg(target_os = "macos")]
867 fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()>;
868 #[cfg(target_os = "macos")]
869 fn set_dock_visibility(&self, visible: bool) -> Result<()>;
870 fn request_exit(&self, code: i32) -> Result<()>;
871 fn create_window(
872 &self,
873 pending: PendingWindow<T, DynRuntime<T>>,
874 after_window_creation: Option<AfterWindowCreation>,
875 ) -> Result<DetachedWindow<T, DynRuntime<T>>>;
876 fn create_webview(
877 &self,
878 window_id: WindowId,
879 pending: PendingWebview<T, DynRuntime<T>>,
880 ) -> Result<DetachedWebview<T, DynRuntime<T>>>;
881 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()>;
882 fn display_handle(&self) -> std::result::Result<DisplayHandle<'_>, HandleError>;
883 fn primary_monitor(&self) -> Result<Option<Monitor>>;
884 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>;
885 fn available_monitors(&self) -> Result<Vec<Monitor>>;
886 fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
887 fn set_theme(&self, theme: Option<Theme>);
888 #[cfg(target_os = "macos")]
889 fn show(&self) -> Result<()>;
890 #[cfg(target_os = "macos")]
891 fn hide(&self) -> Result<()>;
892 fn set_device_event_filter(&self, filter: DeviceEventFilter);
893 fn custom_scheme_url(&self, scheme: &str, https: bool) -> String;
894 fn webview_version(&self) -> Result<String>;
895 #[cfg(target_os = "android")]
896 fn find_class<'a>(
897 &self,
898 env: &mut jni::JNIEnv<'a>,
899 activity: &jni::objects::JObject<'_>,
900 name: String,
901 ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error>;
902 #[cfg(target_os = "android")]
903 fn run_on_android_context(&self, f: AndroidContextTask);
904 #[cfg(any(target_os = "macos", target_os = "ios"))]
905 fn fetch_data_store_identifiers(&self, cb: Box<dyn FnOnce(Vec<[u8; 16]>) + Send>) -> Result<()>;
906 #[cfg(any(target_os = "macos", target_os = "ios"))]
907 fn remove_data_store(&self, uuid: [u8; 16], cb: Box<dyn FnOnce(Result<()>) + Send>)
908 -> Result<()>;
909 fn as_any(&self) -> &dyn Any;
910}
911
912impl<T: UserEvent, H: RuntimeHandle<T>> ErasedRuntimeHandle<T> for H {
913 fn create_proxy(&self) -> DynEventLoopProxy<T> {
914 DynEventLoopProxy::new(RuntimeHandle::create_proxy(self))
915 }
916
917 #[cfg(target_os = "macos")]
918 fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()> {
919 RuntimeHandle::set_activation_policy(self, activation_policy)
920 }
921
922 #[cfg(target_os = "macos")]
923 fn set_dock_visibility(&self, visible: bool) -> Result<()> {
924 RuntimeHandle::set_dock_visibility(self, visible)
925 }
926
927 fn request_exit(&self, code: i32) -> Result<()> {
928 RuntimeHandle::request_exit(self, code)
929 }
930
931 fn create_window(
932 &self,
933 pending: PendingWindow<T, DynRuntime<T>>,
934 after_window_creation: Option<AfterWindowCreation>,
935 ) -> Result<DetachedWindow<T, DynRuntime<T>>> {
936 let pending = pending_window_from_dyn::<T, H::Runtime>(pending)?;
937 RuntimeHandle::create_window(self, pending, after_window_creation).map(detached_window_into_dyn)
938 }
939
940 fn create_webview(
941 &self,
942 window_id: WindowId,
943 pending: PendingWebview<T, DynRuntime<T>>,
944 ) -> Result<DetachedWebview<T, DynRuntime<T>>> {
945 let pending = pending_webview_from_dyn::<T, H::Runtime>(pending)?;
946 RuntimeHandle::create_webview(self, window_id, pending).map(detached_webview_into_dyn)
947 }
948
949 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()> {
950 RuntimeHandle::run_on_main_thread(self, f)
951 }
952
953 fn display_handle(&self) -> std::result::Result<DisplayHandle<'_>, HandleError> {
954 RuntimeHandle::display_handle(self)
955 }
956
957 fn primary_monitor(&self) -> Result<Option<Monitor>> {
958 RuntimeHandle::primary_monitor(self)
959 }
960
961 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
962 RuntimeHandle::monitor_from_point(self, x, y)
963 }
964
965 fn available_monitors(&self) -> Result<Vec<Monitor>> {
966 RuntimeHandle::available_monitors(self)
967 }
968
969 fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
970 RuntimeHandle::cursor_position(self)
971 }
972
973 fn set_theme(&self, theme: Option<Theme>) {
974 RuntimeHandle::set_theme(self, theme)
975 }
976
977 #[cfg(target_os = "macos")]
978 fn show(&self) -> Result<()> {
979 RuntimeHandle::show(self)
980 }
981
982 #[cfg(target_os = "macos")]
983 fn hide(&self) -> Result<()> {
984 RuntimeHandle::hide(self)
985 }
986
987 fn set_device_event_filter(&self, filter: DeviceEventFilter) {
988 RuntimeHandle::set_device_event_filter(self, filter)
989 }
990
991 fn custom_scheme_url(&self, scheme: &str, https: bool) -> String {
992 RuntimeHandle::custom_scheme_url(self, scheme, https)
993 }
994
995 fn webview_version(&self) -> Result<String> {
996 RuntimeHandle::webview_version(self)
997 }
998
999 #[cfg(target_os = "android")]
1000 fn find_class<'a>(
1001 &self,
1002 env: &mut jni::JNIEnv<'a>,
1003 activity: &jni::objects::JObject<'_>,
1004 name: String,
1005 ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error> {
1006 RuntimeHandle::find_class(self, env, activity, name)
1007 }
1008
1009 #[cfg(target_os = "android")]
1010 fn run_on_android_context(&self, f: AndroidContextTask) {
1011 RuntimeHandle::run_on_android_context(self, f)
1012 }
1013
1014 #[cfg(any(target_os = "macos", target_os = "ios"))]
1015 fn fetch_data_store_identifiers(&self, cb: Box<dyn FnOnce(Vec<[u8; 16]>) + Send>) -> Result<()> {
1016 RuntimeHandle::fetch_data_store_identifiers(self, cb)
1017 }
1018
1019 #[cfg(any(target_os = "macos", target_os = "ios"))]
1020 fn remove_data_store(
1021 &self,
1022 uuid: [u8; 16],
1023 cb: Box<dyn FnOnce(Result<()>) + Send>,
1024 ) -> Result<()> {
1025 RuntimeHandle::remove_data_store(self, uuid, cb)
1026 }
1027
1028 fn as_any(&self) -> &dyn Any {
1029 self
1030 }
1031}
1032
1033#[derive(Debug)]
1035pub struct DynRuntimeHandle<T: UserEvent> {
1036 inner: Arc<dyn ErasedRuntimeHandle<T>>,
1037}
1038
1039impl<T: UserEvent> Clone for DynRuntimeHandle<T> {
1040 fn clone(&self) -> Self {
1041 Self {
1042 inner: self.inner.clone(),
1043 }
1044 }
1045}
1046
1047impl<T: UserEvent> DynRuntimeHandle<T> {
1048 pub fn new<H: RuntimeHandle<T>>(handle: H) -> Self {
1050 Self {
1051 inner: Arc::new(handle),
1052 }
1053 }
1054
1055 pub fn is<H: RuntimeHandle<T>>(&self) -> bool {
1057 self.inner.as_any().is::<H>()
1058 }
1059
1060 pub fn downcast_ref<H: RuntimeHandle<T>>(&self) -> Option<&H> {
1062 self.inner.as_any().downcast_ref()
1063 }
1064}
1065
1066impl<T: UserEvent> RuntimeHandle<T> for DynRuntimeHandle<T> {
1067 type Runtime = DynRuntime<T>;
1068
1069 fn create_proxy(&self) -> DynEventLoopProxy<T> {
1070 self.inner.create_proxy()
1071 }
1072
1073 #[cfg(target_os = "macos")]
1074 fn set_activation_policy(&self, activation_policy: ActivationPolicy) -> Result<()> {
1075 self.inner.set_activation_policy(activation_policy)
1076 }
1077
1078 #[cfg(target_os = "macos")]
1079 fn set_dock_visibility(&self, visible: bool) -> Result<()> {
1080 self.inner.set_dock_visibility(visible)
1081 }
1082
1083 fn request_exit(&self, code: i32) -> Result<()> {
1084 self.inner.request_exit(code)
1085 }
1086
1087 fn create_window<F: Fn(RawWindow) + Send + 'static>(
1088 &self,
1089 pending: PendingWindow<T, Self::Runtime>,
1090 after_window_creation: Option<F>,
1091 ) -> Result<DetachedWindow<T, Self::Runtime>> {
1092 self.inner.create_window(
1093 pending,
1094 after_window_creation.map(|f| Box::new(f) as AfterWindowCreation),
1095 )
1096 }
1097
1098 fn create_webview(
1099 &self,
1100 window_id: WindowId,
1101 pending: PendingWebview<T, Self::Runtime>,
1102 ) -> Result<DetachedWebview<T, Self::Runtime>> {
1103 self.inner.create_webview(window_id, pending)
1104 }
1105
1106 fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
1107 self.inner.run_on_main_thread(Box::new(f))
1108 }
1109
1110 fn display_handle(&self) -> std::result::Result<DisplayHandle<'_>, HandleError> {
1111 self.inner.display_handle()
1112 }
1113
1114 fn primary_monitor(&self) -> Result<Option<Monitor>> {
1115 self.inner.primary_monitor()
1116 }
1117
1118 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
1119 self.inner.monitor_from_point(x, y)
1120 }
1121
1122 fn available_monitors(&self) -> Result<Vec<Monitor>> {
1123 self.inner.available_monitors()
1124 }
1125
1126 fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
1127 self.inner.cursor_position()
1128 }
1129
1130 fn set_theme(&self, theme: Option<Theme>) {
1131 self.inner.set_theme(theme)
1132 }
1133
1134 #[cfg(target_os = "macos")]
1135 fn show(&self) -> Result<()> {
1136 self.inner.show()
1137 }
1138
1139 #[cfg(target_os = "macos")]
1140 fn hide(&self) -> Result<()> {
1141 self.inner.hide()
1142 }
1143
1144 fn set_device_event_filter(&self, filter: DeviceEventFilter) {
1145 self.inner.set_device_event_filter(filter)
1146 }
1147
1148 fn custom_scheme_url(&self, scheme: &str, https: bool) -> String {
1149 self.inner.custom_scheme_url(scheme, https)
1150 }
1151
1152 fn webview_version(&self) -> Result<String> {
1153 self.inner.webview_version()
1154 }
1155
1156 #[cfg(target_os = "android")]
1157 fn find_class<'a>(
1158 &self,
1159 env: &mut jni::JNIEnv<'a>,
1160 activity: &jni::objects::JObject<'_>,
1161 name: impl Into<String>,
1162 ) -> std::result::Result<jni::objects::JClass<'a>, jni::errors::Error> {
1163 self.inner.find_class(env, activity, name.into())
1164 }
1165
1166 #[cfg(target_os = "android")]
1167 fn run_on_android_context<F>(&self, f: F)
1168 where
1169 F: FnOnce(&mut jni::JNIEnv, &jni::objects::JObject, &jni::objects::JObject) + Send + 'static,
1170 {
1171 self.inner.run_on_android_context(Box::new(f))
1172 }
1173
1174 #[cfg(any(target_os = "macos", target_os = "ios"))]
1175 fn fetch_data_store_identifiers<F: FnOnce(Vec<[u8; 16]>) + Send + 'static>(
1176 &self,
1177 cb: F,
1178 ) -> Result<()> {
1179 self.inner.fetch_data_store_identifiers(Box::new(cb))
1180 }
1181
1182 #[cfg(any(target_os = "macos", target_os = "ios"))]
1183 fn remove_data_store<F: FnOnce(Result<()>) + Send + 'static>(
1184 &self,
1185 uuid: [u8; 16],
1186 cb: F,
1187 ) -> Result<()> {
1188 self.inner.remove_data_store(uuid, Box::new(cb))
1189 }
1190}
1191
1192trait ErasedWindowDispatch<T: UserEvent>: fmt::Debug + Send + Sync + Any {
1197 fn box_clone(&self) -> Box<dyn ErasedWindowDispatch<T>>;
1198 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()>;
1199 fn scale_factor(&self) -> Result<f64>;
1200 fn inner_position(&self) -> Result<PhysicalPosition<i32>>;
1201 fn outer_position(&self) -> Result<PhysicalPosition<i32>>;
1202 fn inner_size(&self) -> Result<PhysicalSize<u32>>;
1203 fn outer_size(&self) -> Result<PhysicalSize<u32>>;
1204 fn is_fullscreen(&self) -> Result<bool>;
1205 fn is_minimized(&self) -> Result<bool>;
1206 fn is_maximized(&self) -> Result<bool>;
1207 fn is_focused(&self) -> Result<bool>;
1208 fn is_decorated(&self) -> Result<bool>;
1209 fn is_resizable(&self) -> Result<bool>;
1210 fn is_maximizable(&self) -> Result<bool>;
1211 fn is_minimizable(&self) -> Result<bool>;
1212 fn is_closable(&self) -> Result<bool>;
1213 fn is_visible(&self) -> Result<bool>;
1214 fn is_enabled(&self) -> Result<bool>;
1215 fn is_always_on_top(&self) -> Result<bool>;
1216 fn title(&self) -> Result<String>;
1217 fn current_monitor(&self) -> Result<Option<Monitor>>;
1218 fn primary_monitor(&self) -> Result<Option<Monitor>>;
1219 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>;
1220 fn available_monitors(&self) -> Result<Vec<Monitor>>;
1221 #[cfg(any(
1222 target_os = "linux",
1223 target_os = "dragonfly",
1224 target_os = "freebsd",
1225 target_os = "netbsd",
1226 target_os = "openbsd"
1227 ))]
1228 fn gtk_window(&self) -> Result<*mut std::ffi::c_void>;
1229 #[cfg(any(
1230 target_os = "linux",
1231 target_os = "dragonfly",
1232 target_os = "freebsd",
1233 target_os = "netbsd",
1234 target_os = "openbsd"
1235 ))]
1236 fn default_vbox(&self) -> Result<*mut std::ffi::c_void>;
1237 #[cfg(target_os = "android")]
1238 fn activity_name(&self) -> Result<String>;
1239 #[cfg(target_os = "ios")]
1240 fn scene_identifier(&self) -> Result<String>;
1241 fn window_handle(&self) -> std::result::Result<WindowHandle<'_>, HandleError>;
1242 fn theme(&self) -> Result<Theme>;
1243 fn center(&self) -> Result<()>;
1244 fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()>;
1245 fn create_window(
1246 &mut self,
1247 pending: PendingWindow<T, DynRuntime<T>>,
1248 after_window_creation: Option<AfterWindowCreation>,
1249 ) -> Result<DetachedWindow<T, DynRuntime<T>>>;
1250 fn create_webview(
1251 &mut self,
1252 pending: PendingWebview<T, DynRuntime<T>>,
1253 ) -> Result<DetachedWebview<T, DynRuntime<T>>>;
1254 fn set_resizable(&self, resizable: bool) -> Result<()>;
1255 fn set_enabled(&self, enabled: bool) -> Result<()>;
1256 fn set_maximizable(&self, maximizable: bool) -> Result<()>;
1257 fn set_minimizable(&self, minimizable: bool) -> Result<()>;
1258 fn set_closable(&self, closable: bool) -> Result<()>;
1259 fn set_title(&self, title: String) -> Result<()>;
1260 fn maximize(&self) -> Result<()>;
1261 fn unmaximize(&self) -> Result<()>;
1262 fn minimize(&self) -> Result<()>;
1263 fn unminimize(&self) -> Result<()>;
1264 fn show(&self) -> Result<()>;
1265 fn hide(&self) -> Result<()>;
1266 fn close(&self) -> Result<()>;
1267 fn destroy(&self) -> Result<()>;
1268 fn set_decorations(&self, decorations: bool) -> Result<()>;
1269 fn set_shadow(&self, enable: bool) -> Result<()>;
1270 fn set_always_on_bottom(&self, always_on_bottom: bool) -> Result<()>;
1271 fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;
1272 fn set_visible_on_all_workspaces(&self, visible_on_all_workspaces: bool) -> Result<()>;
1273 fn set_background_color(&self, color: Option<Color>) -> Result<()>;
1274 fn set_content_protected(&self, protected: bool) -> Result<()>;
1275 fn set_size(&self, size: Size) -> Result<()>;
1276 fn set_min_size(&self, size: Option<Size>) -> Result<()>;
1277 fn set_max_size(&self, size: Option<Size>) -> Result<()>;
1278 fn set_size_constraints(&self, constraints: WindowSizeConstraints) -> Result<()>;
1279 fn set_position(&self, position: Position) -> Result<()>;
1280 fn set_fullscreen(&self, fullscreen: bool) -> Result<()>;
1281 fn set_fullscreen_on_monitor(&self, position: PhysicalPosition<f64>) -> Result<()>;
1282 #[cfg(target_os = "macos")]
1283 fn set_simple_fullscreen(&self, enable: bool) -> Result<()>;
1284 fn set_focus(&self) -> Result<()>;
1285 fn set_focusable(&self, focusable: bool) -> Result<()>;
1286 fn set_icon(&self, icon: Icon<'_>) -> Result<()>;
1287 fn set_skip_taskbar(&self, skip: bool) -> Result<()>;
1288 fn set_cursor_grab(&self, grab: bool) -> Result<()>;
1289 fn set_cursor_visible(&self, visible: bool) -> Result<()>;
1290 fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>;
1291 fn set_cursor_position(&self, position: Position) -> Result<()>;
1292 fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()>;
1293 fn start_dragging(&self) -> Result<()>;
1294 fn start_resize_dragging(&self, direction: ResizeDirection) -> Result<()>;
1295 fn set_badge_count(&self, count: Option<i64>, desktop_filename: Option<String>) -> Result<()>;
1296 fn set_badge_label(&self, label: Option<String>) -> Result<()>;
1297 fn set_overlay_icon(&self, icon: Option<Icon<'_>>) -> Result<()>;
1298 fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()>;
1299 fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()>;
1300 fn set_traffic_light_position(&self, position: Position) -> Result<()>;
1301 fn set_theme(&self, theme: Option<Theme>) -> Result<()>;
1302 fn as_any(&self) -> &dyn Any;
1303}
1304
1305impl<T: UserEvent, D: WindowDispatch<T>> ErasedWindowDispatch<T> for D {
1306 fn box_clone(&self) -> Box<dyn ErasedWindowDispatch<T>> {
1307 Box::new(self.clone())
1308 }
1309
1310 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()> {
1311 WindowDispatch::run_on_main_thread(self, f)
1312 }
1313
1314 fn scale_factor(&self) -> Result<f64> {
1315 WindowDispatch::scale_factor(self)
1316 }
1317
1318 fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
1319 WindowDispatch::inner_position(self)
1320 }
1321
1322 fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
1323 WindowDispatch::outer_position(self)
1324 }
1325
1326 fn inner_size(&self) -> Result<PhysicalSize<u32>> {
1327 WindowDispatch::inner_size(self)
1328 }
1329
1330 fn outer_size(&self) -> Result<PhysicalSize<u32>> {
1331 WindowDispatch::outer_size(self)
1332 }
1333
1334 fn is_fullscreen(&self) -> Result<bool> {
1335 WindowDispatch::is_fullscreen(self)
1336 }
1337
1338 fn is_minimized(&self) -> Result<bool> {
1339 WindowDispatch::is_minimized(self)
1340 }
1341
1342 fn is_maximized(&self) -> Result<bool> {
1343 WindowDispatch::is_maximized(self)
1344 }
1345
1346 fn is_focused(&self) -> Result<bool> {
1347 WindowDispatch::is_focused(self)
1348 }
1349
1350 fn is_decorated(&self) -> Result<bool> {
1351 WindowDispatch::is_decorated(self)
1352 }
1353
1354 fn is_resizable(&self) -> Result<bool> {
1355 WindowDispatch::is_resizable(self)
1356 }
1357
1358 fn is_maximizable(&self) -> Result<bool> {
1359 WindowDispatch::is_maximizable(self)
1360 }
1361
1362 fn is_minimizable(&self) -> Result<bool> {
1363 WindowDispatch::is_minimizable(self)
1364 }
1365
1366 fn is_closable(&self) -> Result<bool> {
1367 WindowDispatch::is_closable(self)
1368 }
1369
1370 fn is_visible(&self) -> Result<bool> {
1371 WindowDispatch::is_visible(self)
1372 }
1373
1374 fn is_enabled(&self) -> Result<bool> {
1375 WindowDispatch::is_enabled(self)
1376 }
1377
1378 fn is_always_on_top(&self) -> Result<bool> {
1379 WindowDispatch::is_always_on_top(self)
1380 }
1381
1382 fn title(&self) -> Result<String> {
1383 WindowDispatch::title(self)
1384 }
1385
1386 fn current_monitor(&self) -> Result<Option<Monitor>> {
1387 WindowDispatch::current_monitor(self)
1388 }
1389
1390 fn primary_monitor(&self) -> Result<Option<Monitor>> {
1391 WindowDispatch::primary_monitor(self)
1392 }
1393
1394 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
1395 WindowDispatch::monitor_from_point(self, x, y)
1396 }
1397
1398 fn available_monitors(&self) -> Result<Vec<Monitor>> {
1399 WindowDispatch::available_monitors(self)
1400 }
1401
1402 #[cfg(any(
1403 target_os = "linux",
1404 target_os = "dragonfly",
1405 target_os = "freebsd",
1406 target_os = "netbsd",
1407 target_os = "openbsd"
1408 ))]
1409 fn gtk_window(&self) -> Result<*mut std::ffi::c_void> {
1410 WindowDispatch::gtk_window(self)
1411 }
1412
1413 #[cfg(any(
1414 target_os = "linux",
1415 target_os = "dragonfly",
1416 target_os = "freebsd",
1417 target_os = "netbsd",
1418 target_os = "openbsd"
1419 ))]
1420 fn default_vbox(&self) -> Result<*mut std::ffi::c_void> {
1421 WindowDispatch::default_vbox(self)
1422 }
1423
1424 #[cfg(target_os = "android")]
1425 fn activity_name(&self) -> Result<String> {
1426 WindowDispatch::activity_name(self)
1427 }
1428
1429 #[cfg(target_os = "ios")]
1430 fn scene_identifier(&self) -> Result<String> {
1431 WindowDispatch::scene_identifier(self)
1432 }
1433
1434 fn window_handle(&self) -> std::result::Result<WindowHandle<'_>, HandleError> {
1435 WindowDispatch::window_handle(self)
1436 }
1437
1438 fn theme(&self) -> Result<Theme> {
1439 WindowDispatch::theme(self)
1440 }
1441
1442 fn center(&self) -> Result<()> {
1443 WindowDispatch::center(self)
1444 }
1445
1446 fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()> {
1447 WindowDispatch::request_user_attention(self, request_type)
1448 }
1449
1450 fn create_window(
1451 &mut self,
1452 pending: PendingWindow<T, DynRuntime<T>>,
1453 after_window_creation: Option<AfterWindowCreation>,
1454 ) -> Result<DetachedWindow<T, DynRuntime<T>>> {
1455 let pending = pending_window_from_dyn::<T, D::Runtime>(pending)?;
1456 WindowDispatch::create_window(self, pending, after_window_creation)
1457 .map(detached_window_into_dyn)
1458 }
1459
1460 fn create_webview(
1461 &mut self,
1462 pending: PendingWebview<T, DynRuntime<T>>,
1463 ) -> Result<DetachedWebview<T, DynRuntime<T>>> {
1464 let pending = pending_webview_from_dyn::<T, D::Runtime>(pending)?;
1465 WindowDispatch::create_webview(self, pending).map(detached_webview_into_dyn)
1466 }
1467
1468 fn set_resizable(&self, resizable: bool) -> Result<()> {
1469 WindowDispatch::set_resizable(self, resizable)
1470 }
1471
1472 fn set_enabled(&self, enabled: bool) -> Result<()> {
1473 WindowDispatch::set_enabled(self, enabled)
1474 }
1475
1476 fn set_maximizable(&self, maximizable: bool) -> Result<()> {
1477 WindowDispatch::set_maximizable(self, maximizable)
1478 }
1479
1480 fn set_minimizable(&self, minimizable: bool) -> Result<()> {
1481 WindowDispatch::set_minimizable(self, minimizable)
1482 }
1483
1484 fn set_closable(&self, closable: bool) -> Result<()> {
1485 WindowDispatch::set_closable(self, closable)
1486 }
1487
1488 fn set_title(&self, title: String) -> Result<()> {
1489 WindowDispatch::set_title(self, title)
1490 }
1491
1492 fn maximize(&self) -> Result<()> {
1493 WindowDispatch::maximize(self)
1494 }
1495
1496 fn unmaximize(&self) -> Result<()> {
1497 WindowDispatch::unmaximize(self)
1498 }
1499
1500 fn minimize(&self) -> Result<()> {
1501 WindowDispatch::minimize(self)
1502 }
1503
1504 fn unminimize(&self) -> Result<()> {
1505 WindowDispatch::unminimize(self)
1506 }
1507
1508 fn show(&self) -> Result<()> {
1509 WindowDispatch::show(self)
1510 }
1511
1512 fn hide(&self) -> Result<()> {
1513 WindowDispatch::hide(self)
1514 }
1515
1516 fn close(&self) -> Result<()> {
1517 WindowDispatch::close(self)
1518 }
1519
1520 fn destroy(&self) -> Result<()> {
1521 WindowDispatch::destroy(self)
1522 }
1523
1524 fn set_decorations(&self, decorations: bool) -> Result<()> {
1525 WindowDispatch::set_decorations(self, decorations)
1526 }
1527
1528 fn set_shadow(&self, enable: bool) -> Result<()> {
1529 WindowDispatch::set_shadow(self, enable)
1530 }
1531
1532 fn set_always_on_bottom(&self, always_on_bottom: bool) -> Result<()> {
1533 WindowDispatch::set_always_on_bottom(self, always_on_bottom)
1534 }
1535
1536 fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
1537 WindowDispatch::set_always_on_top(self, always_on_top)
1538 }
1539
1540 fn set_visible_on_all_workspaces(&self, visible_on_all_workspaces: bool) -> Result<()> {
1541 WindowDispatch::set_visible_on_all_workspaces(self, visible_on_all_workspaces)
1542 }
1543
1544 fn set_background_color(&self, color: Option<Color>) -> Result<()> {
1545 WindowDispatch::set_background_color(self, color)
1546 }
1547
1548 fn set_content_protected(&self, protected: bool) -> Result<()> {
1549 WindowDispatch::set_content_protected(self, protected)
1550 }
1551
1552 fn set_size(&self, size: Size) -> Result<()> {
1553 WindowDispatch::set_size(self, size)
1554 }
1555
1556 fn set_min_size(&self, size: Option<Size>) -> Result<()> {
1557 WindowDispatch::set_min_size(self, size)
1558 }
1559
1560 fn set_max_size(&self, size: Option<Size>) -> Result<()> {
1561 WindowDispatch::set_max_size(self, size)
1562 }
1563
1564 fn set_size_constraints(&self, constraints: WindowSizeConstraints) -> Result<()> {
1565 WindowDispatch::set_size_constraints(self, constraints)
1566 }
1567
1568 fn set_position(&self, position: Position) -> Result<()> {
1569 WindowDispatch::set_position(self, position)
1570 }
1571
1572 fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
1573 WindowDispatch::set_fullscreen(self, fullscreen)
1574 }
1575
1576 fn set_fullscreen_on_monitor(&self, position: PhysicalPosition<f64>) -> Result<()> {
1577 WindowDispatch::set_fullscreen_on_monitor(self, position)
1578 }
1579
1580 #[cfg(target_os = "macos")]
1581 fn set_simple_fullscreen(&self, enable: bool) -> Result<()> {
1582 WindowDispatch::set_simple_fullscreen(self, enable)
1583 }
1584
1585 fn set_focus(&self) -> Result<()> {
1586 WindowDispatch::set_focus(self)
1587 }
1588
1589 fn set_focusable(&self, focusable: bool) -> Result<()> {
1590 WindowDispatch::set_focusable(self, focusable)
1591 }
1592
1593 fn set_icon(&self, icon: Icon<'_>) -> Result<()> {
1594 WindowDispatch::set_icon(self, icon)
1595 }
1596
1597 fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
1598 WindowDispatch::set_skip_taskbar(self, skip)
1599 }
1600
1601 fn set_cursor_grab(&self, grab: bool) -> Result<()> {
1602 WindowDispatch::set_cursor_grab(self, grab)
1603 }
1604
1605 fn set_cursor_visible(&self, visible: bool) -> Result<()> {
1606 WindowDispatch::set_cursor_visible(self, visible)
1607 }
1608
1609 fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()> {
1610 WindowDispatch::set_cursor_icon(self, icon)
1611 }
1612
1613 fn set_cursor_position(&self, position: Position) -> Result<()> {
1614 WindowDispatch::set_cursor_position(self, position)
1615 }
1616
1617 fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()> {
1618 WindowDispatch::set_ignore_cursor_events(self, ignore)
1619 }
1620
1621 fn start_dragging(&self) -> Result<()> {
1622 WindowDispatch::start_dragging(self)
1623 }
1624
1625 fn start_resize_dragging(&self, direction: ResizeDirection) -> Result<()> {
1626 WindowDispatch::start_resize_dragging(self, direction)
1627 }
1628
1629 fn set_badge_count(&self, count: Option<i64>, desktop_filename: Option<String>) -> Result<()> {
1630 WindowDispatch::set_badge_count(self, count, desktop_filename)
1631 }
1632
1633 fn set_badge_label(&self, label: Option<String>) -> Result<()> {
1634 WindowDispatch::set_badge_label(self, label)
1635 }
1636
1637 fn set_overlay_icon(&self, icon: Option<Icon<'_>>) -> Result<()> {
1638 WindowDispatch::set_overlay_icon(self, icon)
1639 }
1640
1641 fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
1642 WindowDispatch::set_progress_bar(self, progress_state)
1643 }
1644
1645 fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()> {
1646 WindowDispatch::set_title_bar_style(self, style)
1647 }
1648
1649 fn set_traffic_light_position(&self, position: Position) -> Result<()> {
1650 WindowDispatch::set_traffic_light_position(self, position)
1651 }
1652
1653 fn set_theme(&self, theme: Option<Theme>) -> Result<()> {
1654 WindowDispatch::set_theme(self, theme)
1655 }
1656
1657 fn as_any(&self) -> &dyn Any {
1658 self
1659 }
1660}
1661
1662#[derive(Debug)]
1664pub struct DynWindowDispatcher<T: UserEvent> {
1665 inner: Box<dyn ErasedWindowDispatch<T>>,
1666}
1667
1668impl<T: UserEvent> Clone for DynWindowDispatcher<T> {
1669 fn clone(&self) -> Self {
1670 Self {
1671 inner: self.inner.box_clone(),
1672 }
1673 }
1674}
1675
1676impl<T: UserEvent> DynWindowDispatcher<T> {
1677 pub fn new<D: WindowDispatch<T>>(dispatcher: D) -> Self {
1679 Self {
1680 inner: Box::new(dispatcher),
1681 }
1682 }
1683
1684 pub fn is<D: WindowDispatch<T>>(&self) -> bool {
1686 self.inner.as_any().is::<D>()
1687 }
1688
1689 pub fn downcast_ref<D: WindowDispatch<T>>(&self) -> Option<&D> {
1691 self.inner.as_any().downcast_ref()
1692 }
1693}
1694
1695impl<T: UserEvent> WindowDispatch<T> for DynWindowDispatcher<T> {
1696 type Runtime = DynRuntime<T>;
1697 type WindowBuilder = DynWindowBuilder;
1698
1699 fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
1700 self.inner.run_on_main_thread(Box::new(f))
1701 }
1702
1703 fn scale_factor(&self) -> Result<f64> {
1704 self.inner.scale_factor()
1705 }
1706
1707 fn inner_position(&self) -> Result<PhysicalPosition<i32>> {
1708 self.inner.inner_position()
1709 }
1710
1711 fn outer_position(&self) -> Result<PhysicalPosition<i32>> {
1712 self.inner.outer_position()
1713 }
1714
1715 fn inner_size(&self) -> Result<PhysicalSize<u32>> {
1716 self.inner.inner_size()
1717 }
1718
1719 fn outer_size(&self) -> Result<PhysicalSize<u32>> {
1720 self.inner.outer_size()
1721 }
1722
1723 fn is_fullscreen(&self) -> Result<bool> {
1724 self.inner.is_fullscreen()
1725 }
1726
1727 fn is_minimized(&self) -> Result<bool> {
1728 self.inner.is_minimized()
1729 }
1730
1731 fn is_maximized(&self) -> Result<bool> {
1732 self.inner.is_maximized()
1733 }
1734
1735 fn is_focused(&self) -> Result<bool> {
1736 self.inner.is_focused()
1737 }
1738
1739 fn is_decorated(&self) -> Result<bool> {
1740 self.inner.is_decorated()
1741 }
1742
1743 fn is_resizable(&self) -> Result<bool> {
1744 self.inner.is_resizable()
1745 }
1746
1747 fn is_maximizable(&self) -> Result<bool> {
1748 self.inner.is_maximizable()
1749 }
1750
1751 fn is_minimizable(&self) -> Result<bool> {
1752 self.inner.is_minimizable()
1753 }
1754
1755 fn is_closable(&self) -> Result<bool> {
1756 self.inner.is_closable()
1757 }
1758
1759 fn is_visible(&self) -> Result<bool> {
1760 self.inner.is_visible()
1761 }
1762
1763 fn is_enabled(&self) -> Result<bool> {
1764 self.inner.is_enabled()
1765 }
1766
1767 fn is_always_on_top(&self) -> Result<bool> {
1768 self.inner.is_always_on_top()
1769 }
1770
1771 fn title(&self) -> Result<String> {
1772 self.inner.title()
1773 }
1774
1775 fn current_monitor(&self) -> Result<Option<Monitor>> {
1776 self.inner.current_monitor()
1777 }
1778
1779 fn primary_monitor(&self) -> Result<Option<Monitor>> {
1780 self.inner.primary_monitor()
1781 }
1782
1783 fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>> {
1784 self.inner.monitor_from_point(x, y)
1785 }
1786
1787 fn available_monitors(&self) -> Result<Vec<Monitor>> {
1788 self.inner.available_monitors()
1789 }
1790
1791 #[cfg(any(
1792 target_os = "linux",
1793 target_os = "dragonfly",
1794 target_os = "freebsd",
1795 target_os = "netbsd",
1796 target_os = "openbsd"
1797 ))]
1798 fn gtk_window(&self) -> Result<*mut std::ffi::c_void> {
1799 self.inner.gtk_window()
1800 }
1801
1802 #[cfg(any(
1803 target_os = "linux",
1804 target_os = "dragonfly",
1805 target_os = "freebsd",
1806 target_os = "netbsd",
1807 target_os = "openbsd"
1808 ))]
1809 fn default_vbox(&self) -> Result<*mut std::ffi::c_void> {
1810 self.inner.default_vbox()
1811 }
1812
1813 #[cfg(target_os = "android")]
1814 fn activity_name(&self) -> Result<String> {
1815 self.inner.activity_name()
1816 }
1817
1818 #[cfg(target_os = "ios")]
1819 fn scene_identifier(&self) -> Result<String> {
1820 self.inner.scene_identifier()
1821 }
1822
1823 fn window_handle(&self) -> std::result::Result<WindowHandle<'_>, HandleError> {
1824 self.inner.window_handle()
1825 }
1826
1827 fn theme(&self) -> Result<Theme> {
1828 self.inner.theme()
1829 }
1830
1831 fn center(&self) -> Result<()> {
1832 self.inner.center()
1833 }
1834
1835 fn request_user_attention(&self, request_type: Option<UserAttentionType>) -> Result<()> {
1836 self.inner.request_user_attention(request_type)
1837 }
1838
1839 fn create_window<F: Fn(RawWindow) + Send + 'static>(
1840 &mut self,
1841 pending: PendingWindow<T, Self::Runtime>,
1842 after_window_creation: Option<F>,
1843 ) -> Result<DetachedWindow<T, Self::Runtime>> {
1844 self.inner.create_window(
1845 pending,
1846 after_window_creation.map(|f| Box::new(f) as AfterWindowCreation),
1847 )
1848 }
1849
1850 fn create_webview(
1851 &mut self,
1852 pending: PendingWebview<T, Self::Runtime>,
1853 ) -> Result<DetachedWebview<T, Self::Runtime>> {
1854 self.inner.create_webview(pending)
1855 }
1856
1857 fn set_resizable(&self, resizable: bool) -> Result<()> {
1858 self.inner.set_resizable(resizable)
1859 }
1860
1861 fn set_enabled(&self, enabled: bool) -> Result<()> {
1862 self.inner.set_enabled(enabled)
1863 }
1864
1865 fn set_maximizable(&self, maximizable: bool) -> Result<()> {
1866 self.inner.set_maximizable(maximizable)
1867 }
1868
1869 fn set_minimizable(&self, minimizable: bool) -> Result<()> {
1870 self.inner.set_minimizable(minimizable)
1871 }
1872
1873 fn set_closable(&self, closable: bool) -> Result<()> {
1874 self.inner.set_closable(closable)
1875 }
1876
1877 fn set_title<S: Into<String>>(&self, title: S) -> Result<()> {
1878 self.inner.set_title(title.into())
1879 }
1880
1881 fn maximize(&self) -> Result<()> {
1882 self.inner.maximize()
1883 }
1884
1885 fn unmaximize(&self) -> Result<()> {
1886 self.inner.unmaximize()
1887 }
1888
1889 fn minimize(&self) -> Result<()> {
1890 self.inner.minimize()
1891 }
1892
1893 fn unminimize(&self) -> Result<()> {
1894 self.inner.unminimize()
1895 }
1896
1897 fn show(&self) -> Result<()> {
1898 self.inner.show()
1899 }
1900
1901 fn hide(&self) -> Result<()> {
1902 self.inner.hide()
1903 }
1904
1905 fn close(&self) -> Result<()> {
1906 self.inner.close()
1907 }
1908
1909 fn destroy(&self) -> Result<()> {
1910 self.inner.destroy()
1911 }
1912
1913 fn set_decorations(&self, decorations: bool) -> Result<()> {
1914 self.inner.set_decorations(decorations)
1915 }
1916
1917 fn set_shadow(&self, enable: bool) -> Result<()> {
1918 self.inner.set_shadow(enable)
1919 }
1920
1921 fn set_always_on_bottom(&self, always_on_bottom: bool) -> Result<()> {
1922 self.inner.set_always_on_bottom(always_on_bottom)
1923 }
1924
1925 fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
1926 self.inner.set_always_on_top(always_on_top)
1927 }
1928
1929 fn set_visible_on_all_workspaces(&self, visible_on_all_workspaces: bool) -> Result<()> {
1930 self
1931 .inner
1932 .set_visible_on_all_workspaces(visible_on_all_workspaces)
1933 }
1934
1935 fn set_background_color(&self, color: Option<Color>) -> Result<()> {
1936 self.inner.set_background_color(color)
1937 }
1938
1939 fn set_content_protected(&self, protected: bool) -> Result<()> {
1940 self.inner.set_content_protected(protected)
1941 }
1942
1943 fn set_size(&self, size: Size) -> Result<()> {
1944 self.inner.set_size(size)
1945 }
1946
1947 fn set_min_size(&self, size: Option<Size>) -> Result<()> {
1948 self.inner.set_min_size(size)
1949 }
1950
1951 fn set_max_size(&self, size: Option<Size>) -> Result<()> {
1952 self.inner.set_max_size(size)
1953 }
1954
1955 fn set_size_constraints(&self, constraints: WindowSizeConstraints) -> Result<()> {
1956 self.inner.set_size_constraints(constraints)
1957 }
1958
1959 fn set_position(&self, position: Position) -> Result<()> {
1960 self.inner.set_position(position)
1961 }
1962
1963 fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
1964 self.inner.set_fullscreen(fullscreen)
1965 }
1966
1967 fn set_fullscreen_on_monitor(&self, position: PhysicalPosition<f64>) -> Result<()> {
1968 self.inner.set_fullscreen_on_monitor(position)
1969 }
1970
1971 #[cfg(target_os = "macos")]
1972 fn set_simple_fullscreen(&self, enable: bool) -> Result<()> {
1973 self.inner.set_simple_fullscreen(enable)
1974 }
1975
1976 fn set_focus(&self) -> Result<()> {
1977 self.inner.set_focus()
1978 }
1979
1980 fn set_focusable(&self, focusable: bool) -> Result<()> {
1981 self.inner.set_focusable(focusable)
1982 }
1983
1984 fn set_icon(&self, icon: Icon) -> Result<()> {
1985 self.inner.set_icon(icon)
1986 }
1987
1988 fn set_skip_taskbar(&self, skip: bool) -> Result<()> {
1989 self.inner.set_skip_taskbar(skip)
1990 }
1991
1992 fn set_cursor_grab(&self, grab: bool) -> Result<()> {
1993 self.inner.set_cursor_grab(grab)
1994 }
1995
1996 fn set_cursor_visible(&self, visible: bool) -> Result<()> {
1997 self.inner.set_cursor_visible(visible)
1998 }
1999
2000 fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()> {
2001 self.inner.set_cursor_icon(icon)
2002 }
2003
2004 fn set_cursor_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()> {
2005 self.inner.set_cursor_position(position.into())
2006 }
2007
2008 fn set_ignore_cursor_events(&self, ignore: bool) -> Result<()> {
2009 self.inner.set_ignore_cursor_events(ignore)
2010 }
2011
2012 fn start_dragging(&self) -> Result<()> {
2013 self.inner.start_dragging()
2014 }
2015
2016 fn start_resize_dragging(&self, direction: ResizeDirection) -> Result<()> {
2017 self.inner.start_resize_dragging(direction)
2018 }
2019
2020 fn set_badge_count(&self, count: Option<i64>, desktop_filename: Option<String>) -> Result<()> {
2021 self.inner.set_badge_count(count, desktop_filename)
2022 }
2023
2024 fn set_badge_label(&self, label: Option<String>) -> Result<()> {
2025 self.inner.set_badge_label(label)
2026 }
2027
2028 fn set_overlay_icon(&self, icon: Option<Icon>) -> Result<()> {
2029 self.inner.set_overlay_icon(icon)
2030 }
2031
2032 fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
2033 self.inner.set_progress_bar(progress_state)
2034 }
2035
2036 fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()> {
2037 self.inner.set_title_bar_style(style)
2038 }
2039
2040 fn set_traffic_light_position(&self, position: Position) -> Result<()> {
2041 self.inner.set_traffic_light_position(position)
2042 }
2043
2044 fn set_theme(&self, theme: Option<Theme>) -> Result<()> {
2045 self.inner.set_theme(theme)
2046 }
2047}
2048
2049trait ErasedWebviewDispatch<T: UserEvent>: fmt::Debug + Send + Sync + Any {
2054 fn box_clone(&self) -> Box<dyn ErasedWebviewDispatch<T>>;
2055 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()>;
2056 fn with_webview(&self, f: Box<dyn FnOnce(DynWebview) + Send>) -> Result<()>;
2057 #[cfg(target_os = "ios")]
2058 fn with_ios_webview(
2059 &self,
2060 f: Box<dyn FnOnce(crate::webview::IosWebviewHandle) + Send>,
2061 ) -> Result<()>;
2062 fn open_devtools(&self);
2063 fn close_devtools(&self);
2064 fn is_devtools_open(&self) -> Result<bool>;
2065 fn url(&self) -> Result<String>;
2066 fn bounds(&self) -> Result<Rect>;
2067 fn position(&self) -> Result<PhysicalPosition<i32>>;
2068 fn size(&self) -> Result<PhysicalSize<u32>>;
2069 fn navigate(&self, url: Url) -> Result<()>;
2070 fn reload(&self) -> Result<()>;
2071 fn go_back(&self) -> Result<()>;
2072 fn can_go_back(&self) -> Result<bool>;
2073 fn go_forward(&self) -> Result<()>;
2074 fn can_go_forward(&self) -> Result<bool>;
2075 fn print(&self) -> Result<()>;
2076 fn close(&self) -> Result<()>;
2077 fn set_bounds(&self, bounds: Rect) -> Result<()>;
2078 fn set_size(&self, size: Size) -> Result<()>;
2079 fn set_position(&self, position: Position) -> Result<()>;
2080 fn set_focus(&self) -> Result<()>;
2081 fn hide(&self) -> Result<()>;
2082 fn show(&self) -> Result<()>;
2083 fn eval_script(&self, script: String) -> Result<()>;
2084 fn eval_script_with_callback(
2085 &self,
2086 script: String,
2087 callback: Box<dyn Fn(String) + Send>,
2088 ) -> Result<()>;
2089 fn reparent(&self, window_id: WindowId) -> Result<()>;
2090 fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>>;
2091 fn cookies(&self) -> Result<Vec<Cookie<'static>>>;
2092 fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()>;
2093 fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()>;
2094 fn set_auto_resize(&self, auto_resize: bool) -> Result<()>;
2095 fn set_zoom(&self, scale_factor: f64) -> Result<()>;
2096 fn set_background_color(&self, color: Option<Color>) -> Result<()>;
2097 fn clear_all_browsing_data(&self) -> Result<()>;
2098 fn as_any(&self) -> &dyn Any;
2099}
2100
2101impl<T: UserEvent, D: WebviewDispatch<T>> ErasedWebviewDispatch<T> for D {
2102 fn box_clone(&self) -> Box<dyn ErasedWebviewDispatch<T>> {
2103 Box::new(self.clone())
2104 }
2105
2106 fn run_on_main_thread(&self, f: MainThreadTask) -> Result<()> {
2107 WebviewDispatch::run_on_main_thread(self, f)
2108 }
2109
2110 fn with_webview(&self, f: Box<dyn FnOnce(DynWebview) + Send>) -> Result<()> {
2111 WebviewDispatch::with_webview(self, move |webview| f(DynWebview::new(webview)))
2112 }
2113
2114 #[cfg(target_os = "ios")]
2115 fn with_ios_webview(
2116 &self,
2117 f: Box<dyn FnOnce(crate::webview::IosWebviewHandle) + Send>,
2118 ) -> Result<()> {
2119 WebviewDispatch::with_ios_webview(self, f)
2120 }
2121
2122 fn open_devtools(&self) {
2123 WebviewDispatch::open_devtools(self)
2124 }
2125
2126 fn close_devtools(&self) {
2127 WebviewDispatch::close_devtools(self)
2128 }
2129
2130 fn is_devtools_open(&self) -> Result<bool> {
2131 WebviewDispatch::is_devtools_open(self)
2132 }
2133
2134 fn url(&self) -> Result<String> {
2135 WebviewDispatch::url(self)
2136 }
2137
2138 fn bounds(&self) -> Result<Rect> {
2139 WebviewDispatch::bounds(self)
2140 }
2141
2142 fn position(&self) -> Result<PhysicalPosition<i32>> {
2143 WebviewDispatch::position(self)
2144 }
2145
2146 fn size(&self) -> Result<PhysicalSize<u32>> {
2147 WebviewDispatch::size(self)
2148 }
2149
2150 fn navigate(&self, url: Url) -> Result<()> {
2151 WebviewDispatch::navigate(self, url)
2152 }
2153
2154 fn reload(&self) -> Result<()> {
2155 WebviewDispatch::reload(self)
2156 }
2157
2158 fn go_back(&self) -> Result<()> {
2159 WebviewDispatch::go_back(self)
2160 }
2161
2162 fn can_go_back(&self) -> Result<bool> {
2163 WebviewDispatch::can_go_back(self)
2164 }
2165
2166 fn go_forward(&self) -> Result<()> {
2167 WebviewDispatch::go_forward(self)
2168 }
2169
2170 fn can_go_forward(&self) -> Result<bool> {
2171 WebviewDispatch::can_go_forward(self)
2172 }
2173
2174 fn print(&self) -> Result<()> {
2175 WebviewDispatch::print(self)
2176 }
2177
2178 fn close(&self) -> Result<()> {
2179 WebviewDispatch::close(self)
2180 }
2181
2182 fn set_bounds(&self, bounds: Rect) -> Result<()> {
2183 WebviewDispatch::set_bounds(self, bounds)
2184 }
2185
2186 fn set_size(&self, size: Size) -> Result<()> {
2187 WebviewDispatch::set_size(self, size)
2188 }
2189
2190 fn set_position(&self, position: Position) -> Result<()> {
2191 WebviewDispatch::set_position(self, position)
2192 }
2193
2194 fn set_focus(&self) -> Result<()> {
2195 WebviewDispatch::set_focus(self)
2196 }
2197
2198 fn hide(&self) -> Result<()> {
2199 WebviewDispatch::hide(self)
2200 }
2201
2202 fn show(&self) -> Result<()> {
2203 WebviewDispatch::show(self)
2204 }
2205
2206 fn eval_script(&self, script: String) -> Result<()> {
2207 WebviewDispatch::eval_script(self, script)
2208 }
2209
2210 fn eval_script_with_callback(
2211 &self,
2212 script: String,
2213 callback: Box<dyn Fn(String) + Send>,
2214 ) -> Result<()> {
2215 WebviewDispatch::eval_script_with_callback(self, script, callback)
2216 }
2217
2218 fn reparent(&self, window_id: WindowId) -> Result<()> {
2219 WebviewDispatch::reparent(self, window_id)
2220 }
2221
2222 fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>> {
2223 WebviewDispatch::cookies_for_url(self, url)
2224 }
2225
2226 fn cookies(&self) -> Result<Vec<Cookie<'static>>> {
2227 WebviewDispatch::cookies(self)
2228 }
2229
2230 fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
2231 WebviewDispatch::set_cookie(self, cookie)
2232 }
2233
2234 fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
2235 WebviewDispatch::delete_cookie(self, cookie)
2236 }
2237
2238 fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
2239 WebviewDispatch::set_auto_resize(self, auto_resize)
2240 }
2241
2242 fn set_zoom(&self, scale_factor: f64) -> Result<()> {
2243 WebviewDispatch::set_zoom(self, scale_factor)
2244 }
2245
2246 fn set_background_color(&self, color: Option<Color>) -> Result<()> {
2247 WebviewDispatch::set_background_color(self, color)
2248 }
2249
2250 fn clear_all_browsing_data(&self) -> Result<()> {
2251 WebviewDispatch::clear_all_browsing_data(self)
2252 }
2253
2254 fn as_any(&self) -> &dyn Any {
2255 self
2256 }
2257}
2258
2259#[derive(Debug)]
2261pub struct DynWebviewDispatcher<T: UserEvent> {
2262 inner: Box<dyn ErasedWebviewDispatch<T>>,
2263}
2264
2265impl<T: UserEvent> Clone for DynWebviewDispatcher<T> {
2266 fn clone(&self) -> Self {
2267 Self {
2268 inner: self.inner.box_clone(),
2269 }
2270 }
2271}
2272
2273impl<T: UserEvent> DynWebviewDispatcher<T> {
2274 pub fn new<D: WebviewDispatch<T>>(dispatcher: D) -> Self {
2276 Self {
2277 inner: Box::new(dispatcher),
2278 }
2279 }
2280
2281 pub fn is<D: WebviewDispatch<T>>(&self) -> bool {
2283 self.inner.as_any().is::<D>()
2284 }
2285
2286 pub fn downcast_ref<D: WebviewDispatch<T>>(&self) -> Option<&D> {
2288 self.inner.as_any().downcast_ref()
2289 }
2290}
2291
2292impl<T: UserEvent> WebviewDispatch<T> for DynWebviewDispatcher<T> {
2293 type Runtime = DynRuntime<T>;
2294
2295 fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
2296 self.inner.run_on_main_thread(Box::new(f))
2297 }
2298
2299 fn with_webview<F: FnOnce(DynWebview) + Send + 'static>(&self, f: F) -> Result<()> {
2300 self.inner.with_webview(Box::new(f))
2301 }
2302
2303 #[cfg(target_os = "ios")]
2304 fn with_ios_webview<F: FnOnce(crate::webview::IosWebviewHandle) + Send + 'static>(
2305 &self,
2306 f: F,
2307 ) -> Result<()> {
2308 self.inner.with_ios_webview(Box::new(f))
2309 }
2310
2311 fn open_devtools(&self) {
2312 self.inner.open_devtools()
2313 }
2314
2315 fn close_devtools(&self) {
2316 self.inner.close_devtools()
2317 }
2318
2319 fn is_devtools_open(&self) -> Result<bool> {
2320 self.inner.is_devtools_open()
2321 }
2322
2323 fn url(&self) -> Result<String> {
2324 self.inner.url()
2325 }
2326
2327 fn bounds(&self) -> Result<Rect> {
2328 self.inner.bounds()
2329 }
2330
2331 fn position(&self) -> Result<PhysicalPosition<i32>> {
2332 self.inner.position()
2333 }
2334
2335 fn size(&self) -> Result<PhysicalSize<u32>> {
2336 self.inner.size()
2337 }
2338
2339 fn navigate(&self, url: Url) -> Result<()> {
2340 self.inner.navigate(url)
2341 }
2342
2343 fn reload(&self) -> Result<()> {
2344 self.inner.reload()
2345 }
2346
2347 fn go_back(&self) -> Result<()> {
2348 self.inner.go_back()
2349 }
2350
2351 fn can_go_back(&self) -> Result<bool> {
2352 self.inner.can_go_back()
2353 }
2354
2355 fn go_forward(&self) -> Result<()> {
2356 self.inner.go_forward()
2357 }
2358
2359 fn can_go_forward(&self) -> Result<bool> {
2360 self.inner.can_go_forward()
2361 }
2362
2363 fn print(&self) -> Result<()> {
2364 self.inner.print()
2365 }
2366
2367 fn close(&self) -> Result<()> {
2368 self.inner.close()
2369 }
2370
2371 fn set_bounds(&self, bounds: Rect) -> Result<()> {
2372 self.inner.set_bounds(bounds)
2373 }
2374
2375 fn set_size(&self, size: Size) -> Result<()> {
2376 self.inner.set_size(size)
2377 }
2378
2379 fn set_position(&self, position: Position) -> Result<()> {
2380 self.inner.set_position(position)
2381 }
2382
2383 fn set_focus(&self) -> Result<()> {
2384 self.inner.set_focus()
2385 }
2386
2387 fn hide(&self) -> Result<()> {
2388 self.inner.hide()
2389 }
2390
2391 fn show(&self) -> Result<()> {
2392 self.inner.show()
2393 }
2394
2395 fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
2396 self.inner.eval_script(script.into())
2397 }
2398
2399 fn eval_script_with_callback<S: Into<String>>(
2400 &self,
2401 script: S,
2402 callback: impl Fn(String) + Send + 'static,
2403 ) -> Result<()> {
2404 self
2405 .inner
2406 .eval_script_with_callback(script.into(), Box::new(callback))
2407 }
2408
2409 fn reparent(&self, window_id: WindowId) -> Result<()> {
2410 self.inner.reparent(window_id)
2411 }
2412
2413 fn cookies_for_url(&self, url: Url) -> Result<Vec<Cookie<'static>>> {
2414 self.inner.cookies_for_url(url)
2415 }
2416
2417 fn cookies(&self) -> Result<Vec<Cookie<'static>>> {
2418 self.inner.cookies()
2419 }
2420
2421 fn set_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
2422 self.inner.set_cookie(cookie)
2423 }
2424
2425 fn delete_cookie(&self, cookie: Cookie<'_>) -> Result<()> {
2426 self.inner.delete_cookie(cookie)
2427 }
2428
2429 fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
2430 self.inner.set_auto_resize(auto_resize)
2431 }
2432
2433 fn set_zoom(&self, scale_factor: f64) -> Result<()> {
2434 self.inner.set_zoom(scale_factor)
2435 }
2436
2437 fn set_background_color(&self, color: Option<Color>) -> Result<()> {
2438 self.inner.set_background_color(color)
2439 }
2440
2441 fn clear_all_browsing_data(&self) -> Result<()> {
2442 self.inner.clear_all_browsing_data()
2443 }
2444}
2445
2446trait ErasedRuntimeInitAttrs<T: UserEvent>: Send + Sync {
2451 fn apply_config(&mut self, config: &Config) -> Result<()>;
2452 fn build(self: Box<Self>, args: RuntimeInitArgs<()>) -> Result<Box<dyn ErasedRuntime<T>>>;
2453 #[cfg(any(
2454 windows,
2455 target_os = "linux",
2456 target_os = "dragonfly",
2457 target_os = "freebsd",
2458 target_os = "netbsd",
2459 target_os = "openbsd"
2460 ))]
2461 fn build_any_thread(
2462 self: Box<Self>,
2463 args: RuntimeInitArgs<()>,
2464 ) -> Result<Box<dyn ErasedRuntime<T>>>;
2465}
2466
2467struct TypedRuntimeInitAttrs<T: UserEvent, A: RuntimeInitAttrs<T>> {
2468 attrs: A,
2469 _marker: PhantomData<fn() -> T>,
2470}
2471
2472impl<T: UserEvent, A: RuntimeInitAttrs<T>> ErasedRuntimeInitAttrs<T>
2473 for TypedRuntimeInitAttrs<T, A>
2474{
2475 fn apply_config(&mut self, config: &Config) -> Result<()> {
2476 self.attrs.apply_config(config)
2477 }
2478
2479 fn build(self: Box<Self>, args: RuntimeInitArgs<()>) -> Result<Box<dyn ErasedRuntime<T>>> {
2480 let (args, ()) = args.with_attrs(self.attrs);
2481 <A::Runtime as Runtime<T>>::new(args)
2482 .map(|runtime| Box::new(runtime) as Box<dyn ErasedRuntime<T>>)
2483 }
2484
2485 #[cfg(any(
2486 windows,
2487 target_os = "linux",
2488 target_os = "dragonfly",
2489 target_os = "freebsd",
2490 target_os = "netbsd",
2491 target_os = "openbsd"
2492 ))]
2493 fn build_any_thread(
2494 self: Box<Self>,
2495 args: RuntimeInitArgs<()>,
2496 ) -> Result<Box<dyn ErasedRuntime<T>>> {
2497 let (args, ()) = args.with_attrs(self.attrs);
2498 <A::Runtime as Runtime<T>>::new_any_thread(args)
2499 .map(|runtime| Box::new(runtime) as Box<dyn ErasedRuntime<T>>)
2500 }
2501}
2502
2503pub struct DynRuntimeInitAttrs<T: UserEvent> {
2509 inner: Option<Box<dyn ErasedRuntimeInitAttrs<T>>>,
2510}
2511
2512impl<T: UserEvent> DynRuntimeInitAttrs<T> {
2513 pub fn new<A: RuntimeInitAttrs<T>>(attrs: A) -> Self {
2515 Self {
2516 inner: Some(Box::new(TypedRuntimeInitAttrs {
2517 attrs,
2518 _marker: PhantomData,
2519 })),
2520 }
2521 }
2522
2523 pub fn is_configured(&self) -> bool {
2525 self.inner.is_some()
2526 }
2527}
2528
2529impl<T: UserEvent> Default for DynRuntimeInitAttrs<T> {
2530 fn default() -> Self {
2531 Self { inner: None }
2532 }
2533}
2534
2535impl<T: UserEvent> fmt::Debug for DynRuntimeInitAttrs<T> {
2536 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2537 f.debug_struct("DynRuntimeInitAttrs")
2538 .field("configured", &self.is_configured())
2539 .finish()
2540 }
2541}
2542
2543impl<T: UserEvent> RuntimeInitAttrs<T> for DynRuntimeInitAttrs<T> {
2544 type Runtime = DynRuntime<T>;
2545
2546 fn apply_config(&mut self, config: &Config) -> Result<()> {
2547 match &mut self.inner {
2548 Some(inner) => inner.apply_config(config),
2549 None => Ok(()),
2550 }
2551 }
2552}
2553
2554trait ErasedRuntime<T: UserEvent>: fmt::Debug + Any {
2559 fn create_proxy(&self) -> DynEventLoopProxy<T>;
2560 fn handle(&self) -> DynRuntimeHandle<T>;
2561 fn create_window(
2562 &self,
2563 pending: PendingWindow<T, DynRuntime<T>>,
2564 after_window_creation: Option<AfterWindowCreation>,
2565 ) -> Result<DetachedWindow<T, DynRuntime<T>>>;
2566 fn create_webview(
2567 &self,
2568 window_id: WindowId,
2569 pending: PendingWebview<T, DynRuntime<T>>,
2570 ) -> Result<DetachedWebview<T, DynRuntime<T>>>;
2571 fn primary_monitor(&self) -> Option<Monitor>;
2572 fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor>;
2573 fn available_monitors(&self) -> Vec<Monitor>;
2574 fn cursor_position(&self) -> Result<PhysicalPosition<f64>>;
2575 fn set_theme(&self, theme: Option<Theme>);
2576 #[cfg(target_os = "macos")]
2577 fn set_activation_policy(&mut self, activation_policy: ActivationPolicy);
2578 #[cfg(target_os = "macos")]
2579 fn set_activate_ignoring_other_apps(&mut self, ignore: bool);
2580 #[cfg(target_os = "macos")]
2581 fn set_dock_visibility(&mut self, visible: bool);
2582 #[cfg(target_os = "macos")]
2583 fn show(&self);
2584 #[cfg(target_os = "macos")]
2585 fn hide(&self);
2586 fn set_device_event_filter(&mut self, filter: DeviceEventFilter);
2587 #[cfg(desktop)]
2588 fn run_iteration(&mut self, callback: RunCallback<T>);
2589 fn run_return(self: Box<Self>, callback: RunCallback<T>) -> i32;
2590 fn run(self: Box<Self>, callback: RunCallback<T>);
2591 fn as_any(&self) -> &dyn Any;
2592 fn as_any_mut(&mut self) -> &mut dyn Any;
2593}
2594
2595impl<T: UserEvent, R: Runtime<T>> ErasedRuntime<T> for R {
2596 fn create_proxy(&self) -> DynEventLoopProxy<T> {
2597 DynEventLoopProxy::new(Runtime::create_proxy(self))
2598 }
2599
2600 fn handle(&self) -> DynRuntimeHandle<T> {
2601 DynRuntimeHandle::new(Runtime::handle(self))
2602 }
2603
2604 fn create_window(
2605 &self,
2606 pending: PendingWindow<T, DynRuntime<T>>,
2607 after_window_creation: Option<AfterWindowCreation>,
2608 ) -> Result<DetachedWindow<T, DynRuntime<T>>> {
2609 let pending = pending_window_from_dyn::<T, R>(pending)?;
2610 Runtime::create_window(self, pending, after_window_creation).map(detached_window_into_dyn)
2611 }
2612
2613 fn create_webview(
2614 &self,
2615 window_id: WindowId,
2616 pending: PendingWebview<T, DynRuntime<T>>,
2617 ) -> Result<DetachedWebview<T, DynRuntime<T>>> {
2618 let pending = pending_webview_from_dyn::<T, R>(pending)?;
2619 Runtime::create_webview(self, window_id, pending).map(detached_webview_into_dyn)
2620 }
2621
2622 fn primary_monitor(&self) -> Option<Monitor> {
2623 Runtime::primary_monitor(self)
2624 }
2625
2626 fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
2627 Runtime::monitor_from_point(self, x, y)
2628 }
2629
2630 fn available_monitors(&self) -> Vec<Monitor> {
2631 Runtime::available_monitors(self)
2632 }
2633
2634 fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
2635 Runtime::cursor_position(self)
2636 }
2637
2638 fn set_theme(&self, theme: Option<Theme>) {
2639 Runtime::set_theme(self, theme)
2640 }
2641
2642 #[cfg(target_os = "macos")]
2643 fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
2644 Runtime::set_activation_policy(self, activation_policy)
2645 }
2646
2647 #[cfg(target_os = "macos")]
2648 fn set_activate_ignoring_other_apps(&mut self, ignore: bool) {
2649 Runtime::set_activate_ignoring_other_apps(self, ignore)
2650 }
2651
2652 #[cfg(target_os = "macos")]
2653 fn set_dock_visibility(&mut self, visible: bool) {
2654 Runtime::set_dock_visibility(self, visible)
2655 }
2656
2657 #[cfg(target_os = "macos")]
2658 fn show(&self) {
2659 Runtime::show(self)
2660 }
2661
2662 #[cfg(target_os = "macos")]
2663 fn hide(&self) {
2664 Runtime::hide(self)
2665 }
2666
2667 fn set_device_event_filter(&mut self, filter: DeviceEventFilter) {
2668 Runtime::set_device_event_filter(self, filter)
2669 }
2670
2671 #[cfg(desktop)]
2672 fn run_iteration(&mut self, callback: RunCallback<T>) {
2673 Runtime::run_iteration(self, callback)
2674 }
2675
2676 fn run_return(self: Box<Self>, callback: RunCallback<T>) -> i32 {
2677 Runtime::run_return(*self, callback)
2678 }
2679
2680 fn run(self: Box<Self>, callback: RunCallback<T>) {
2681 Runtime::run(*self, callback)
2682 }
2683
2684 fn as_any(&self) -> &dyn Any {
2685 self
2686 }
2687
2688 fn as_any_mut(&mut self) -> &mut dyn Any {
2689 self
2690 }
2691}
2692
2693#[derive(Debug)]
2698pub struct DynRuntime<T: UserEvent> {
2699 inner: Box<dyn ErasedRuntime<T>>,
2700}
2701
2702impl<T: UserEvent> DynRuntime<T> {
2703 pub fn from_runtime<R: Runtime<T>>(runtime: R) -> Self {
2708 Self {
2709 inner: Box::new(runtime),
2710 }
2711 }
2712
2713 pub fn is<R: Runtime<T>>(&self) -> bool {
2715 self.inner.as_any().is::<R>()
2716 }
2717
2718 pub fn downcast_ref<R: Runtime<T>>(&self) -> Option<&R> {
2720 self.inner.as_any().downcast_ref()
2721 }
2722
2723 pub fn downcast_mut<R: Runtime<T>>(&mut self) -> Option<&mut R> {
2725 self.inner.as_any_mut().downcast_mut()
2726 }
2727}
2728
2729impl<T: UserEvent> Runtime<T> for DynRuntime<T> {
2730 type WindowDispatcher = DynWindowDispatcher<T>;
2731 type WebviewDispatcher = DynWebviewDispatcher<T>;
2732 type Handle = DynRuntimeHandle<T>;
2733 type EventLoopProxy = DynEventLoopProxy<T>;
2734 type RuntimeWebviewAttributes = DynWebviewAttributes;
2735 type Webview = DynWebview;
2736 type RuntimeInitAttrs = DynRuntimeInitAttrs<T>;
2737 type WindowOpener = DynWindowOpener;
2738
2739 fn new(args: RuntimeInitArgs<Self::RuntimeInitAttrs>) -> Result<Self> {
2740 let (args, attrs) = args.with_attrs(());
2741 let attrs = attrs.inner.ok_or(Error::RuntimeNotConfigured)?;
2742 Ok(Self {
2743 inner: attrs.build(args)?,
2744 })
2745 }
2746
2747 #[cfg(any(
2748 windows,
2749 target_os = "linux",
2750 target_os = "dragonfly",
2751 target_os = "freebsd",
2752 target_os = "netbsd",
2753 target_os = "openbsd"
2754 ))]
2755 fn new_any_thread(args: RuntimeInitArgs<Self::RuntimeInitAttrs>) -> Result<Self> {
2756 let (args, attrs) = args.with_attrs(());
2757 let attrs = attrs.inner.ok_or(Error::RuntimeNotConfigured)?;
2758 Ok(Self {
2759 inner: attrs.build_any_thread(args)?,
2760 })
2761 }
2762
2763 fn create_proxy(&self) -> Self::EventLoopProxy {
2764 self.inner.create_proxy()
2765 }
2766
2767 fn handle(&self) -> Self::Handle {
2768 self.inner.handle()
2769 }
2770
2771 fn create_window<F: Fn(RawWindow) + Send + 'static>(
2772 &self,
2773 pending: PendingWindow<T, Self>,
2774 after_window_creation: Option<F>,
2775 ) -> Result<DetachedWindow<T, Self>> {
2776 self.inner.create_window(
2777 pending,
2778 after_window_creation.map(|f| Box::new(f) as AfterWindowCreation),
2779 )
2780 }
2781
2782 fn create_webview(
2783 &self,
2784 window_id: WindowId,
2785 pending: PendingWebview<T, Self>,
2786 ) -> Result<DetachedWebview<T, Self>> {
2787 self.inner.create_webview(window_id, pending)
2788 }
2789
2790 fn primary_monitor(&self) -> Option<Monitor> {
2791 self.inner.primary_monitor()
2792 }
2793
2794 fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
2795 self.inner.monitor_from_point(x, y)
2796 }
2797
2798 fn available_monitors(&self) -> Vec<Monitor> {
2799 self.inner.available_monitors()
2800 }
2801
2802 fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
2803 self.inner.cursor_position()
2804 }
2805
2806 fn set_theme(&self, theme: Option<Theme>) {
2807 self.inner.set_theme(theme)
2808 }
2809
2810 #[cfg(target_os = "macos")]
2811 fn set_activation_policy(&mut self, activation_policy: ActivationPolicy) {
2812 self.inner.set_activation_policy(activation_policy)
2813 }
2814
2815 #[cfg(target_os = "macos")]
2816 fn set_activate_ignoring_other_apps(&mut self, ignore: bool) {
2817 self.inner.set_activate_ignoring_other_apps(ignore)
2818 }
2819
2820 #[cfg(target_os = "macos")]
2821 fn set_dock_visibility(&mut self, visible: bool) {
2822 self.inner.set_dock_visibility(visible)
2823 }
2824
2825 #[cfg(target_os = "macos")]
2826 fn show(&self) {
2827 self.inner.show()
2828 }
2829
2830 #[cfg(target_os = "macos")]
2831 fn hide(&self) {
2832 self.inner.hide()
2833 }
2834
2835 fn set_device_event_filter(&mut self, filter: DeviceEventFilter) {
2836 self.inner.set_device_event_filter(filter)
2837 }
2838
2839 #[cfg(desktop)]
2840 fn run_iteration<F: FnMut(RunEvent<T>) + 'static>(&mut self, callback: F) {
2841 self.inner.run_iteration(Box::new(callback))
2842 }
2843
2844 fn run_return<F: FnMut(RunEvent<T>) + 'static>(self, callback: F) -> i32 {
2845 self.inner.run_return(Box::new(callback))
2846 }
2847
2848 fn run<F: FnMut(RunEvent<T>) + 'static>(self, callback: F) {
2849 self.inner.run(Box::new(callback))
2850 }
2851}
2852
2853#[cfg(test)]
2854mod tests {
2855 use super::*;
2856
2857 #[test]
2858 fn window_builder_records_theme_and_icon() {
2859 let builder = DynWindowBuilder::new();
2860 assert!(!builder.has_icon());
2861 assert_eq!(builder.get_theme(), None);
2862
2863 let builder = builder.theme(Some(Theme::Dark)).theme(Some(Theme::Light));
2864 assert_eq!(builder.get_theme(), Some(Theme::Light));
2865
2866 let icon = Icon {
2867 rgba: vec![0; 2 * 2 * 4].into(),
2868 width: 2,
2869 height: 2,
2870 };
2871 let builder = builder.icon(icon).expect("valid icon");
2872 assert!(builder.has_icon());
2873 assert_eq!(builder.ops.len(), 3);
2874 }
2875
2876 #[test]
2877 fn window_builder_rejects_invalid_icon() {
2878 let icon = Icon {
2879 rgba: vec![0; 3].into(),
2880 width: 2,
2881 height: 2,
2882 };
2883 assert!(matches!(
2884 DynWindowBuilder::new().icon(icon),
2885 Err(Error::InvalidIcon(_))
2886 ));
2887 }
2888
2889 #[test]
2890 fn window_builder_theme_falls_back_to_config() {
2891 let config = WindowConfig {
2892 theme: Some(Theme::Dark),
2893 ..Default::default()
2894 };
2895 let builder = DynWindowBuilder::with_config(&config);
2896 assert_eq!(builder.get_theme(), Some(Theme::Dark));
2897 assert_eq!(builder.theme(None).get_theme(), None);
2898 }
2899
2900 #[test]
2901 fn erased_values_downcast() {
2902 let webview = DynWebview::new(42u32);
2903 assert!(webview.is::<u32>());
2904 assert_eq!(webview.downcast_ref::<u32>(), Some(&42));
2905 assert!(webview.downcast::<String>().is_err());
2906
2907 let opener = DynWindowOpener::new("opener".to_string());
2908 assert!(opener.downcast::<u32>().is_err());
2909 let opener = DynWindowOpener::new("opener".to_string());
2910 assert_eq!(opener.downcast::<String>().unwrap(), "opener");
2911
2912 let attributes = DynWebviewAttributes::new(7u8);
2913 assert_eq!(attributes.downcast::<u8>().unwrap(), 7);
2914 assert!(DynWebviewAttributes::new(7u8).downcast::<u16>().is_err());
2915 assert_eq!(DynWebviewAttributes::default().downcast::<u8>().unwrap(), 0);
2917 let mut attributes = DynWebviewAttributes::default();
2918 *attributes.get_or_default::<u8>().unwrap() = 3;
2919 assert!(attributes.get_or_default::<u16>().is_none());
2920 assert_eq!(attributes.downcast::<u8>().unwrap(), 3);
2921 }
2922
2923 #[test]
2924 fn init_attrs_default_is_unconfigured() {
2925 let attrs = DynRuntimeInitAttrs::<()>::default();
2926 assert!(!attrs.is_configured());
2927 let args = RuntimeInitArgs {
2928 runtime_init_attrs: attrs,
2929 ..Default::default()
2930 };
2931 assert!(matches!(
2932 <DynRuntime<()> as Runtime<()>>::new(args),
2933 Err(Error::RuntimeNotConfigured)
2934 ));
2935 }
2936}