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

+31
View File
@@ -5,6 +5,11 @@ use std::rc::Weak;
use crate::create_new_window;
use crate::eval::EvalResult;
use crate::events::IpcMessage;
use crate::shortcut::IntoKeyCode;
use crate::shortcut::IntoModifersState;
use crate::shortcut::ShortcutId;
use crate::shortcut::ShortcutRegistry;
use crate::shortcut::ShortcutRegistryError;
use crate::Config;
use crate::WebviewHandler;
use dioxus_core::ScopeState;
@@ -63,6 +68,8 @@ pub struct DesktopContext {
pub(crate) event_handlers: WindowEventHandlers,
pub(crate) shortcut_manager: ShortcutRegistry,
#[cfg(target_os = "ios")]
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
}
@@ -83,6 +90,7 @@ impl DesktopContext {
event_loop: EventLoopWindowTarget<UserWindowEvent>,
webviews: WebviewQueue,
event_handlers: WindowEventHandlers,
shortcut_manager: ShortcutRegistry,
) -> Self {
Self {
webview,
@@ -91,6 +99,7 @@ impl DesktopContext {
eval: tokio::sync::broadcast::channel(8).0,
pending_windows: webviews,
event_handlers,
shortcut_manager,
#[cfg(target_os = "ios")]
views: Default::default(),
}
@@ -111,6 +120,7 @@ impl DesktopContext {
dom,
&self.pending_windows,
&self.event_handlers,
self.shortcut_manager.clone(),
);
let id = window.webview.window().id();
@@ -240,6 +250,27 @@ impl DesktopContext {
self.event_handlers.remove(id)
}
/// Create a global shortcut
///
/// Linux: Only works on x11. See [this issue](https://github.com/tauri-apps/tao/issues/331) for more information.
pub fn create_shortcut(
&self,
key: impl IntoKeyCode,
modifiers: impl IntoModifersState,
callback: impl FnMut() + 'static,
) -> Result<ShortcutId, ShortcutRegistryError> {
self.shortcut_manager.add_shortcut(
modifiers.into_modifiers_state(),
key.into_key_code(),
Box::new(callback),
)
}
/// Remove a global shortcut
pub fn remove_shortcut(&self, id: ShortcutId) {
self.shortcut_manager.remove_shortcut(id)
}
/// Push an objc view to the window
#[cfg(target_os = "ios")]
pub fn push_view(&self, view: objc_id::ShareId<objc::runtime::Object>) {