add with_on_window_ready to allow modifying window before webview (#4919)
* wip * cleanups
This commit is contained in:
1 parent
06c5a4499f
commit
6a50f1ba84
6 files changed
+63
-40
No files matched your search
Generated
+1
@@ -5839,6 +5839,7 @@ dependencies = [
|
|||||||
"getrandom 0.3.4",
|
"getrandom 0.3.4",
|
||||||
"http-range",
|
"http-range",
|
||||||
"ouroboros",
|
"ouroboros",
|
||||||
|
"pollster",
|
||||||
"rand 0.9.2",
|
"rand 0.9.2",
|
||||||
"reqwest 0.12.24",
|
"reqwest 0.12.24",
|
||||||
"separator",
|
"separator",
|
||||||
|
|||||||
@@ -485,6 +485,7 @@ futures = { workspace = true }
|
|||||||
axum-core = { workspace = true }
|
axum-core = { workspace = true }
|
||||||
uuid = { workspace = true, features = ["v4", "serde"] }
|
uuid = { workspace = true, features = ["v4", "serde"] }
|
||||||
tower-http = { workspace = true, features = ["timeout"] }
|
tower-http = { workspace = true, features = ["timeout"] }
|
||||||
|
pollster = "0.4.0"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||||
getrandom = { workspace = true, features = ["wasm_js"] }
|
getrandom = { workspace = true, features = ["wasm_js"] }
|
||||||
|
|||||||
@@ -5,16 +5,32 @@
|
|||||||
//!
|
//!
|
||||||
//! To use this feature set `with_as_child_window()` on your desktop config which will then let you
|
//! To use this feature set `with_as_child_window()` on your desktop config which will then let you
|
||||||
|
|
||||||
use dioxus::desktop::{
|
|
||||||
Config, DesktopContext,
|
|
||||||
tao::{event::Event as WryEvent, window::WindowBuilder},
|
|
||||||
use_wry_event_handler, window,
|
|
||||||
};
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
use dioxus::{
|
||||||
|
desktop::tao::{event::Event as WryEvent, window::Window},
|
||||||
|
desktop::{Config, tao::window::WindowBuilder, use_wry_event_handler, window},
|
||||||
|
};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let config = Config::new()
|
let config = Config::new()
|
||||||
.with_window(WindowBuilder::new().with_transparent(true))
|
.with_window(WindowBuilder::new().with_transparent(true))
|
||||||
|
.with_on_window(|window, dom| {
|
||||||
|
let resources = Arc::new(pollster::block_on(async {
|
||||||
|
let resource = GraphicsContextAsyncBuilder {
|
||||||
|
desktop: window,
|
||||||
|
resources_builder: |ctx| Box::pin(GraphicsResources::new(ctx.clone())),
|
||||||
|
}
|
||||||
|
.build()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
resource.with_resources(|resources| resources.render());
|
||||||
|
|
||||||
|
resource
|
||||||
|
}));
|
||||||
|
|
||||||
|
dom.provide_root_context(resources);
|
||||||
|
})
|
||||||
.with_as_child_window();
|
.with_as_child_window();
|
||||||
|
|
||||||
dioxus::LaunchBuilder::desktop()
|
dioxus::LaunchBuilder::desktop()
|
||||||
@@ -23,14 +39,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app() -> Element {
|
fn app() -> Element {
|
||||||
let graphics_resources = use_resource(move || async {
|
let graphics_resources = consume_context::<Arc<GraphicsContext>>();
|
||||||
GraphicsContextAsyncBuilder {
|
|
||||||
desktop: window(),
|
|
||||||
resources_builder: |ctx| Box::pin(GraphicsResources::new(ctx)),
|
|
||||||
}
|
|
||||||
.build()
|
|
||||||
.await
|
|
||||||
});
|
|
||||||
|
|
||||||
// on first render request a redraw
|
// on first render request a redraw
|
||||||
use_effect(|| {
|
use_effect(|| {
|
||||||
@@ -40,27 +49,17 @@ fn app() -> Element {
|
|||||||
use_wry_event_handler(move |event, _| {
|
use_wry_event_handler(move |event, _| {
|
||||||
use dioxus::desktop::tao::event::WindowEvent;
|
use dioxus::desktop::tao::event::WindowEvent;
|
||||||
|
|
||||||
if let WryEvent::RedrawRequested(_id) = event {
|
|
||||||
let resources = graphics_resources.read();
|
|
||||||
if let Some(resources) = resources.as_ref() {
|
|
||||||
resources.with_resources(|resources| resources.render());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let WryEvent::WindowEvent {
|
if let WryEvent::WindowEvent {
|
||||||
event: WindowEvent::Resized(new_size),
|
event: WindowEvent::Resized(new_size),
|
||||||
..
|
..
|
||||||
} = event
|
} = event
|
||||||
{
|
{
|
||||||
let ctx = graphics_resources.value();
|
graphics_resources.with_resources(|srcs| {
|
||||||
if let Some(ctx_ref) = ctx.as_ref() {
|
let mut cfg = srcs.config.clone();
|
||||||
ctx_ref.with_resources(|srcs| {
|
cfg.width = new_size.width;
|
||||||
let mut cfg = srcs.config.clone();
|
cfg.height = new_size.height;
|
||||||
cfg.width = new_size.width;
|
srcs.surface.configure(&srcs.device, &cfg);
|
||||||
cfg.height = new_size.height;
|
});
|
||||||
srcs.surface.configure(&srcs.device, &cfg);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
window().window.request_redraw();
|
window().window.request_redraw();
|
||||||
}
|
}
|
||||||
@@ -84,7 +83,7 @@ fn app() -> Element {
|
|||||||
/// to be able to borrow the window for the wgpu::Surface
|
/// to be able to borrow the window for the wgpu::Surface
|
||||||
#[ouroboros::self_referencing]
|
#[ouroboros::self_referencing]
|
||||||
struct GraphicsContext {
|
struct GraphicsContext {
|
||||||
desktop: DesktopContext,
|
desktop: Arc<Window>,
|
||||||
#[borrows(desktop)]
|
#[borrows(desktop)]
|
||||||
#[not_covariant]
|
#[not_covariant]
|
||||||
resources: GraphicsResources<'this>,
|
resources: GraphicsResources<'this>,
|
||||||
@@ -99,8 +98,7 @@ struct GraphicsResources<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> GraphicsResources<'a> {
|
impl<'a> GraphicsResources<'a> {
|
||||||
async fn new(context: &'a DesktopContext) -> Self {
|
async fn new(window: Arc<Window>) -> Self {
|
||||||
let window = &context.window;
|
|
||||||
let size = window.inner_size();
|
let size = window.inner_size();
|
||||||
|
|
||||||
let instance = wgpu::Instance::default();
|
let instance = wgpu::Instance::default();
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use dioxus_core::LaunchConfig;
|
use dioxus_core::{LaunchConfig, VirtualDom};
|
||||||
use std::borrow::Cow;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tao::event_loop::{EventLoop, EventLoopWindowTarget};
|
use std::{borrow::Cow, sync::Arc};
|
||||||
use tao::window::{Icon, WindowBuilder};
|
use tao::window::{Icon, WindowBuilder};
|
||||||
|
use tao::{
|
||||||
|
event_loop::{EventLoop, EventLoopWindowTarget},
|
||||||
|
window::Window,
|
||||||
|
};
|
||||||
use wry::http::{Request as HttpRequest, Response as HttpResponse};
|
use wry::http::{Request as HttpRequest, Response as HttpResponse};
|
||||||
use wry::{RequestAsyncResponder, WebViewId};
|
use wry::{RequestAsyncResponder, WebViewId};
|
||||||
|
|
||||||
@@ -64,6 +67,9 @@ pub struct Config {
|
|||||||
pub(crate) window_close_behavior: WindowCloseBehaviour,
|
pub(crate) window_close_behavior: WindowCloseBehaviour,
|
||||||
pub(crate) custom_event_handler: Option<CustomEventHandler>,
|
pub(crate) custom_event_handler: Option<CustomEventHandler>,
|
||||||
pub(crate) disable_file_drop_handler: bool,
|
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 {}
|
impl LaunchConfig for Config {}
|
||||||
@@ -111,6 +117,7 @@ impl Config {
|
|||||||
window_close_behavior: WindowCloseBehaviour::WindowCloses,
|
window_close_behavior: WindowCloseBehaviour::WindowCloses,
|
||||||
custom_event_handler: None,
|
custom_event_handler: None,
|
||||||
disable_file_drop_handler: false,
|
disable_file_drop_handler: false,
|
||||||
|
on_window: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,6 +303,15 @@ impl Config {
|
|||||||
}
|
}
|
||||||
self
|
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 {
|
impl Default for Config {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use std::{
|
|||||||
future::{Future, IntoFuture},
|
future::{Future, IntoFuture},
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
rc::{Rc, Weak},
|
rc::{Rc, Weak},
|
||||||
|
sync::Arc,
|
||||||
};
|
};
|
||||||
use tao::{
|
use tao::{
|
||||||
event::Event,
|
event::Event,
|
||||||
@@ -59,7 +60,7 @@ pub struct DesktopService {
|
|||||||
pub webview: WebView,
|
pub webview: WebView,
|
||||||
|
|
||||||
/// The tao window itself
|
/// The tao window itself
|
||||||
pub window: Window,
|
pub window: Arc<Window>,
|
||||||
|
|
||||||
pub(crate) shared: Rc<SharedContext>,
|
pub(crate) shared: Rc<SharedContext>,
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ impl std::ops::Deref for DesktopService {
|
|||||||
impl DesktopService {
|
impl DesktopService {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
webview: WebView,
|
webview: WebView,
|
||||||
window: Window,
|
window: Arc<Window>,
|
||||||
shared: Rc<SharedContext>,
|
shared: Rc<SharedContext>,
|
||||||
asset_handlers: AssetHandlerRegistry,
|
asset_handlers: AssetHandlerRegistry,
|
||||||
file_hover: NativeFileHover,
|
file_hover: NativeFileHover,
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use dioxus_history::{History, MemoryHistory};
|
|||||||
use dioxus_hooks::to_owned;
|
use dioxus_hooks::to_owned;
|
||||||
use dioxus_html::{FileData, FormValue, HtmlEvent, PlatformEventData};
|
use dioxus_html::{FileData, FormValue, HtmlEvent, PlatformEventData};
|
||||||
use futures_util::{pin_mut, FutureExt};
|
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::{cell::OnceCell, time::Duration};
|
||||||
use std::{rc::Rc, task::Waker};
|
use std::{rc::Rc, task::Waker};
|
||||||
use wry::{DragDropEvent, RequestAsyncResponder, WebContext, WebViewBuilder, WebViewId};
|
use wry::{DragDropEvent, RequestAsyncResponder, WebContext, WebViewBuilder, WebViewId};
|
||||||
@@ -200,7 +200,7 @@ pub(crate) struct WebviewInstance {
|
|||||||
impl WebviewInstance {
|
impl WebviewInstance {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
mut cfg: Config,
|
mut cfg: Config,
|
||||||
dom: VirtualDom,
|
mut dom: VirtualDom,
|
||||||
shared: Rc<SharedContext>,
|
shared: Rc<SharedContext>,
|
||||||
) -> WebviewInstance {
|
) -> WebviewInstance {
|
||||||
let mut window = cfg.window.clone();
|
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
|
// https://developer.apple.com/documentation/appkit/nswindowcollectionbehavior/nswindowcollectionbehaviormanaged
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
@@ -485,6 +488,9 @@ impl WebviewInstance {
|
|||||||
provide_context(history_provider);
|
provide_context(history_provider);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Request an initial redraw
|
||||||
|
desktop_context.window.request_redraw();
|
||||||
|
|
||||||
WebviewInstance {
|
WebviewInstance {
|
||||||
dom,
|
dom,
|
||||||
edits,
|
edits,
|
||||||
|
|||||||
Reference in new issue
Block a user