add with_on_window_ready to allow modifying window before webview (#4919)

* wip

* cleanups
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-11-05 17:00:58 -03:00
1 parent 06c5a4499f
commit 6a50f1ba84
6 files changed
+63 -40

No files matched your search

+19 -3
View File
@@ -1,8 +1,11 @@
use dioxus_core::LaunchConfig;
use std::borrow::Cow;
use dioxus_core::{LaunchConfig, VirtualDom};
use std::path::PathBuf;
use tao::event_loop::{EventLoop, EventLoopWindowTarget};
use std::{borrow::Cow, sync::Arc};
use tao::window::{Icon, WindowBuilder};
use tao::{
event_loop::{EventLoop, EventLoopWindowTarget},
window::Window,
};
use wry::http::{Request as HttpRequest, Response as HttpResponse};
use wry::{RequestAsyncResponder, WebViewId};
@@ -64,6 +67,9 @@ pub struct Config {
pub(crate) window_close_behavior: WindowCloseBehaviour,
pub(crate) custom_event_handler: Option<CustomEventHandler>,
pub(crate) disable_file_drop_handler: bool,
#[allow(clippy::type_complexity)]
pub(crate) on_window: Option<Box<dyn FnMut(Arc<Window>, &mut VirtualDom) + 'static>>,
}
impl LaunchConfig for Config {}
@@ -111,6 +117,7 @@ impl Config {
window_close_behavior: WindowCloseBehaviour::WindowCloses,
custom_event_handler: None,
disable_file_drop_handler: false,
on_window: None,
}
}
@@ -296,6 +303,15 @@ impl Config {
}
self
}
/// Allows modifying the window and virtual dom right after they are built, but before the webview is created.
///
/// This is important for z-ordering textures in child windows. Note that this callback runs on
/// every window creation, so it's up to you to
pub fn with_on_window(mut self, f: impl FnMut(Arc<Window>, &mut VirtualDom) + 'static) -> Self {
self.on_window = Some(Box::new(f));
self
}
}
impl Default for Config {
+3 -2
View File
@@ -14,6 +14,7 @@ use std::{
future::{Future, IntoFuture},
pin::Pin,
rc::{Rc, Weak},
sync::Arc,
};
use tao::{
event::Event,
@@ -59,7 +60,7 @@ pub struct DesktopService {
pub webview: WebView,
/// The tao window itself
pub window: Window,
pub window: Arc<Window>,
pub(crate) shared: Rc<SharedContext>,
@@ -85,7 +86,7 @@ impl std::ops::Deref for DesktopService {
impl DesktopService {
pub(crate) fn new(
webview: WebView,
window: Window,
window: Arc<Window>,
shared: Rc<SharedContext>,
asset_handlers: AssetHandlerRegistry,
file_hover: NativeFileHover,
+9 -3
View File
@@ -16,7 +16,7 @@ use dioxus_history::{History, MemoryHistory};
use dioxus_hooks::to_owned;
use dioxus_html::{FileData, FormValue, HtmlEvent, PlatformEventData};
use futures_util::{pin_mut, FutureExt};
use std::sync::atomic::AtomicBool;
use std::sync::{atomic::AtomicBool, Arc};
use std::{cell::OnceCell, time::Duration};
use std::{rc::Rc, task::Waker};
use wry::{DragDropEvent, RequestAsyncResponder, WebContext, WebViewBuilder, WebViewId};
@@ -200,7 +200,7 @@ pub(crate) struct WebviewInstance {
impl WebviewInstance {
pub(crate) fn new(
mut cfg: Config,
dom: VirtualDom,
mut dom: VirtualDom,
shared: Rc<SharedContext>,
) -> WebviewInstance {
let mut window = cfg.window.clone();
@@ -228,7 +228,10 @@ impl WebviewInstance {
));
}
let window = window.build(&shared.target).unwrap();
let window = Arc::new(window.build(&shared.target).unwrap());
if let Some(on_build) = cfg.on_window.as_mut() {
on_build(window.clone(), &mut dom);
}
// https://developer.apple.com/documentation/appkit/nswindowcollectionbehavior/nswindowcollectionbehaviormanaged
#[cfg(target_os = "macos")]
@@ -485,6 +488,9 @@ impl WebviewInstance {
provide_context(history_provider);
});
// Request an initial redraw
desktop_context.window.request_redraw();
WebviewInstance {
dom,
edits,