Fix non tokio builds for desktop

This commit is contained in:
Jonathan Kelley committed 2024-01-18 04:07:28 -08:00
1 parent 1b65ee8501
commit 94b17cc8ca
15 files changed
+274 -437

No files matched your search

+27 -34
View File
@@ -8,29 +8,23 @@ use dioxus_core::{
prelude::{consume_context, current_scope_id},
use_hook,
};
use dioxus_hooks::use_on_drop;
use dioxus_hooks::use_hook_with_cleanup;
use tao::{event::Event, event_loop::EventLoopWindowTarget};
use wry::RequestAsyncResponder;
/// Get an imperative handle to the current window
pub fn use_window() -> DesktopContext {
use_hook(|| consume_context::<DesktopContext>())
use_hook(consume_context::<DesktopContext>)
}
/// Get a closure that executes any JavaScript in the WebView context.
pub fn use_wry_event_handler(
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
) -> WryEventHandler {
use_hook(move || {
let desktop = window();
let id = desktop.create_wry_event_handler(handler);
WryEventHandler {
handlers: desktop.shared.event_handlers.clone(),
id,
}
})
use_hook_with_cleanup(
move || window().create_wry_event_handler(handler),
move |handler| handler.remove(),
)
}
/// Provide a callback to handle asset loading yourself.
@@ -41,19 +35,20 @@ pub fn use_asset_handler(
name: &str,
handler: impl Fn(AssetRequest, RequestAsyncResponder) + 'static,
) {
let name = use_hook(|| {
crate::window().asset_handlers.register_handler(
name.to_string(),
Box::new(handler),
current_scope_id().unwrap(),
);
use_hook_with_cleanup(
|| {
crate::window().asset_handlers.register_handler(
name.to_string(),
Box::new(handler),
current_scope_id().unwrap(),
);
Rc::new(name.to_string())
});
use_on_drop(move || {
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
});
Rc::new(name.to_string())
},
move |name| {
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
},
);
}
/// Get a closure that executes any JavaScript in the WebView context.
@@ -61,14 +56,12 @@ pub fn use_global_shortcut(
accelerator: impl IntoAccelerator,
handler: impl FnMut() + 'static,
) -> Result<ShortcutHandle, ShortcutRegistryError> {
use_hook(move || {
let desktop = window();
let id = desktop.create_shortcut(accelerator.accelerator(), handler);
Ok(ShortcutHandle {
desktop,
shortcut_id: id?,
})
})
use_hook_with_cleanup(
move || window().create_shortcut(accelerator.accelerator(), handler),
|handle| {
if let Ok(handle) = handle {
handle.remove();
}
},
)
}