Call handlers from context of a runtime and scope
This commit is contained in:
10 files changed
+265
-277
No files matched your search
@@ -3,8 +3,8 @@ use crate::{
|
||||
ShortcutHandle, ShortcutRegistryError, WryEventHandler,
|
||||
};
|
||||
use dioxus_core::ScopeState;
|
||||
use std::rc::Rc;
|
||||
use tao::{event::Event, event_loop::EventLoopWindowTarget};
|
||||
use wry::RequestAsyncResponder;
|
||||
|
||||
/// Get an imperative handle to the current window
|
||||
pub fn use_window(cx: &ScopeState) -> &DesktopContext {
|
||||
@@ -34,24 +34,28 @@ pub fn use_wry_event_handler(
|
||||
///
|
||||
/// The callback takes a path as requested by the web view, and it should return `Some(response)`
|
||||
/// if you want to load the asset, and `None` if you want to fallback on the default behavior.
|
||||
pub fn use_asset_handler<F: AssetFuture>(
|
||||
pub fn use_asset_handler(
|
||||
cx: &ScopeState,
|
||||
handler: impl AssetHandler<F>,
|
||||
) -> &AssetHandlerHandle {
|
||||
name: &str,
|
||||
handler: impl Fn(AssetRequest, RequestAsyncResponder) + 'static,
|
||||
) {
|
||||
cx.use_hook(|| {
|
||||
let desktop = crate::window();
|
||||
let handler_id = Rc::new(tokio::sync::OnceCell::new());
|
||||
let handler_id_ref = Rc::clone(&handler_id);
|
||||
let desktop_ref = Rc::clone(&desktop);
|
||||
cx.push_future(async move {
|
||||
let id = desktop.asset_handlers.register_handler(handler).await;
|
||||
handler_id.set(id).unwrap();
|
||||
});
|
||||
AssetHandlerHandle {
|
||||
desktop: desktop_ref,
|
||||
handler_id: handler_id_ref,
|
||||
crate::window().asset_handlers.register_handler(
|
||||
name.to_string(),
|
||||
Box::new(handler),
|
||||
cx.scope_id(),
|
||||
);
|
||||
|
||||
Handler(name.to_string())
|
||||
});
|
||||
|
||||
// todo: can we just put ondrop in core?
|
||||
struct Handler(String);
|
||||
impl Drop for Handler {
|
||||
fn drop(&mut self) {
|
||||
_ = crate::window().asset_handlers.remove_handler(&self.0);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a closure that executes any JavaScript in the WebView context.
|
||||
|
||||
Reference in new issue
Block a user