mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 12:43:08 +00:00
Fix shortcut by wrapping callback
This commit is contained in:
parent
f2ec5c5cb8
commit
2ade193a5b
2 changed files with 17 additions and 6 deletions
|
@ -9,6 +9,7 @@ use dioxus_core::{
|
||||||
use_hook,
|
use_hook,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use dioxus_hooks::use_callback;
|
||||||
use tao::{event::Event, event_loop::EventLoopWindowTarget};
|
use tao::{event::Event, event_loop::EventLoopWindowTarget};
|
||||||
use wry::RequestAsyncResponder;
|
use wry::RequestAsyncResponder;
|
||||||
|
|
||||||
|
@ -56,8 +57,11 @@ pub fn use_global_shortcut(
|
||||||
accelerator: impl IntoAccelerator,
|
accelerator: impl IntoAccelerator,
|
||||||
handler: impl FnMut() + 'static,
|
handler: impl FnMut() + 'static,
|
||||||
) -> Result<ShortcutHandle, ShortcutRegistryError> {
|
) -> Result<ShortcutHandle, ShortcutRegistryError> {
|
||||||
|
// wrap the user's handler in something that will carry the scope/runtime with it
|
||||||
|
let mut cb = use_callback(handler);
|
||||||
|
|
||||||
use_hook_with_cleanup(
|
use_hook_with_cleanup(
|
||||||
move || window().create_shortcut(accelerator.accelerator(), handler),
|
move || window().create_shortcut(accelerator.accelerator(), move || cb.call()),
|
||||||
|handle| {
|
|handle| {
|
||||||
if let Ok(handle) = handle {
|
if let Ok(handle) = handle {
|
||||||
handle.remove();
|
handle.remove();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use dioxus_core::prelude::use_hook;
|
use dioxus_core::prelude::{current_scope_id, use_hook, Runtime};
|
||||||
use dioxus_signals::CopyValue;
|
use dioxus_signals::CopyValue;
|
||||||
use dioxus_signals::Writable;
|
use dioxus_signals::Writable;
|
||||||
|
|
||||||
|
@ -18,10 +18,17 @@ pub fn use_callback<O>(f: impl FnMut() -> O + 'static) -> UseCallback<O> {
|
||||||
inner.set(Some(f));
|
inner.set(Some(f));
|
||||||
|
|
||||||
// And then wrap that callback in a boxed callback so we're blind to the size of the actual callback
|
// And then wrap that callback in a boxed callback so we're blind to the size of the actual callback
|
||||||
use_hook(|| UseCallback {
|
use_hook(|| {
|
||||||
inner: CopyValue::new(Box::new(move || {
|
let cur_scope = current_scope_id().unwrap();
|
||||||
inner.with_mut(|f: &mut Option<_>| f.as_mut().unwrap()())
|
let rt = Runtime::current().unwrap();
|
||||||
})),
|
|
||||||
|
UseCallback {
|
||||||
|
inner: CopyValue::new(Box::new(move || {
|
||||||
|
// run this callback in the context of the scope it was created in.
|
||||||
|
let run_callback = || inner.with_mut(|f: &mut Option<_>| f.as_mut().unwrap()());
|
||||||
|
rt.on_scope(cur_scope, run_callback)
|
||||||
|
})),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue