create global shortcut handler for dioxus desktop

This commit is contained in:
Evan Almloff committed 2023-03-01 13:23:15 -06:00
1 parent 49c5a5043a
commit cf2a79c173
4 files changed
+370

No files matched your search

+9
View File
@@ -9,6 +9,7 @@ mod escape;
mod eval;
mod events;
mod protocol;
mod shortcut;
mod waker;
mod webview;
@@ -21,6 +22,8 @@ use dioxus_core::*;
use dioxus_html::HtmlEvent;
pub use eval::{use_eval, EvalResult};
use futures_util::{pin_mut, FutureExt};
use shortcut::ShortcutRegistry;
pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError};
use std::collections::HashMap;
use std::rc::Rc;
use std::task::Waker;
@@ -139,6 +142,8 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
let queue = WebviewQueue::default();
let shortcut_manager = ShortcutRegistry::new(&event_loop);
// By default, we'll create a new window when the app starts
queue.borrow_mut().push(create_new_window(
cfg,
@@ -147,6 +152,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
VirtualDom::new_with_props(root, props),
&queue,
&event_handlers,
shortcut_manager.clone(),
));
event_loop.run(move |window_event, event_loop, control_flow| {
@@ -260,6 +266,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
_ => {}
},
Event::GlobalShortcutEvent(id) => shortcut_manager.call_handlers(id),
_ => {}
}
})
@@ -272,6 +279,7 @@ fn create_new_window(
dom: VirtualDom,
queue: &WebviewQueue,
event_handlers: &WindowEventHandlers,
shortcut_manager: ShortcutRegistry,
) -> WebviewHandler {
let webview = webview::build(&mut cfg, event_loop, proxy.clone());
@@ -281,6 +289,7 @@ fn create_new_window(
event_loop.clone(),
queue.clone(),
event_handlers.clone(),
shortcut_manager,
));
let id = webview.window().id();