Add back ondestroy hook

This commit is contained in:
Jonathan Kelley committed 2024-01-15 14:27:20 -08:00
1 parent 6134a2ce24
commit bb6aa9e792
8 files changed
+121 -146

No files matched your search

+45 -47
View File
@@ -1,17 +1,21 @@
use std::rc::Rc;
use crate::{
assets::*, ipc::UserWindowEvent, shortcut::IntoAccelerator, window, DesktopContext,
ShortcutHandle, ShortcutRegistryError, WryEventHandler,
};
use dioxus_core::use_hook;
use dioxus_core::{
prelude::{consume_context, current_scope_id},
use_hook,
};
use dioxus_hooks::use_on_drop;
use tao::{event::Event, event_loop::EventLoopWindowTarget};
use wry::RequestAsyncResponder;
// /// Get an imperative handle to the current window
// pub fn use_window() -> &DesktopContext {
// cx.use_hook(|| cx.consume_context::<DesktopContext>())
// .as_ref()
// .unwrap()
// }
/// Get an imperative handle to the current window
pub fn use_window() -> DesktopContext {
use_hook(|| consume_context::<DesktopContext>()).unwrap()
}
/// Get a closure that executes any JavaScript in the WebView context.
pub fn use_wry_event_handler(
@@ -29,48 +33,42 @@ pub fn use_wry_event_handler(
})
}
// /// Provide a callback to handle asset loading yourself.
// ///
// /// 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(
// ,
// name: &str,
// handler: impl Fn(AssetRequest, RequestAsyncResponder) + 'static,
// ) {
// cx.use_hook(|| {
// crate::window().asset_handlers.register_handler(
// name.to_string(),
// Box::new(handler),
// cx.scope_id(),
// );
/// Provide a callback to handle asset loading yourself.
///
/// 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(
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(),
);
// Handler(name.to_string())
// });
Rc::new(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);
// }
// }
// }
use_on_drop(move || {
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
});
}
// /// Get a closure that executes any JavaScript in the WebView context.
// pub fn use_global_shortcut(
// ,
// accelerator: impl IntoAccelerator,
// handler: impl FnMut() + 'static,
// ) -> &Result<ShortcutHandle, ShortcutRegistryError> {
// cx.use_hook(move || {
// let desktop = window();
/// Get a closure that executes any JavaScript in the WebView context.
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);
let id = desktop.create_shortcut(accelerator.accelerator(), handler);
// Ok(ShortcutHandle {
// desktop,
// shortcut_id: id?,
// })
// })
// }
Ok(ShortcutHandle {
desktop,
shortcut_id: id?,
})
})
}
+1 -1
View File
@@ -43,7 +43,7 @@ pub use config::{Config, WindowCloseBehaviour};
pub use desktop_context::{
window, DesktopContext, DesktopService, WryEventHandler, WryEventHandlerId,
};
// pub use hooks::{use_asset_handler, use_global_shortcut, use_window, use_wry_event_handler};
pub use hooks::{use_asset_handler, use_global_shortcut, use_window, use_wry_event_handler};
pub use shortcut::{ShortcutHandle, ShortcutId, ShortcutRegistryError};
pub use wry::RequestAsyncResponder;