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

+13
View File
@@ -867,6 +867,11 @@ impl IntoVNode for VNode {
self
}
}
impl IntoVNode for &VNode {
fn into_vnode(self) -> VNode {
self.clone()
}
}
impl IntoVNode for Element {
fn into_vnode(self) -> VNode {
match self {
@@ -875,6 +880,14 @@ impl IntoVNode for Element {
}
}
}
impl IntoVNode for &Element {
fn into_vnode(self) -> VNode {
match self {
Some(val) => val.into_vnode(),
_ => VNode::empty().unwrap(),
}
}
}
// Note that we're using the E as a generic but this is never crafted anyways.
pub struct FromNodeIterator;
+1
View File
@@ -44,6 +44,7 @@ infer = "0.11.0"
dunce = "1.0.2"
slab = { workspace = true }
rustc-hash = { workspace = true }
dioxus-hooks = { workspace = true }
futures-util = { workspace = true }
urlencoding = "2.1.2"
+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;
+2 -2
View File
@@ -57,8 +57,8 @@ macro_rules! to_owned {
// pub mod computed;
// mod use_on_destroy;
// pub use use_on_destroy::*;
mod use_on_destroy;
pub use use_on_destroy::*;
// mod use_const;
// pub use use_const::*;
+29 -12
View File
@@ -1,9 +1,11 @@
use dioxus_core::use_hook;
#[deprecated(
note = "Use `use_on_destroy` instead, which has the same functionality. \
This is deprecated because of the introduction of `use_on_create` which is better mirrored by `use_on_destroy`. \
The reason why `use_on_create` is not `use_on_mount` is because of potential confusion with `dioxus::events::onmounted`."
)]
pub fn use_on_unmount<D: FnOnce() + 'static>(cx: &dioxus_core::ScopeState, destroy: D) {
pub fn use_on_unmount<D: FnOnce() + 'static>(destroy: D) {
use_on_destroy(destroy);
}
@@ -67,19 +69,34 @@ pub fn use_on_unmount<D: FnOnce() + 'static>(cx: &dioxus_core::ScopeState, destr
/// }
/// }
/// ```
pub fn use_on_destroy<D: FnOnce() + 'static>(cx: &dioxus_core::ScopeState, destroy: D) {
cx.use_hook(|| LifeCycle {
pub fn use_on_destroy<D: FnOnce() + 'static>(destroy: D) {
struct LifeCycle<D: FnOnce()> {
/// Wrap the closure in an option so that we can take it out on drop.
ondestroy: Option<D>,
}
/// On drop, we want to run the closure.
impl<D: FnOnce()> Drop for LifeCycle<D> {
fn drop(&mut self) {
if let Some(f) = self.ondestroy.take() {
f();
}
}
}
// We need to impl clone for the lifecycle, but we don't want the drop handler for the closure to be called twice.
impl<D: FnOnce()> Clone for LifeCycle<D> {
fn clone(&self) -> Self {
Self { ondestroy: None }
}
}
use_hook(|| LifeCycle {
ondestroy: Some(destroy),
});
}
struct LifeCycle<D: FnOnce()> {
ondestroy: Option<D>,
}
impl<D: FnOnce()> Drop for LifeCycle<D> {
fn drop(&mut self) {
let f = self.ondestroy.take().unwrap();
f();
}
/// Creates a callback that will be run before the component is dropped
pub fn use_on_drop<D: FnOnce() + 'static>(ondrop: D) {
use_on_destroy(ondrop);
}