From d1575b40d1648768ce80e621e1c75f6ffcd428b8 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 4 Sep 2023 08:24:38 -0500 Subject: [PATCH] fix mobile build --- packages/desktop/Cargo.toml | 1 + packages/desktop/src/desktop_context.rs | 4 +- packages/desktop/src/lib.rs | 11 +--- packages/desktop/src/mobile_shortcut.rs | 69 +++++++++++++++---------- packages/desktop/src/shortcut.rs | 22 ++++---- 5 files changed, 57 insertions(+), 50 deletions(-) diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index 311e224bb..a17b52b1a 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -37,6 +37,7 @@ slab = { workspace = true } futures-util = { workspace = true } urlencoding = "2.1.2" async-trait = "0.1.68" +crossbeam-channel = "0.5.8" [target.'cfg(any(target_os = "windows",target_os = "macos",target_os = "linux",target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))'.dependencies] diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index 5393fe113..c5f9f5ca5 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -5,7 +5,7 @@ use std::rc::Weak; use crate::create_new_window; use crate::events::IpcMessage; use crate::query::QueryEngine; -use crate::shortcut::{ShortcutId, ShortcutRegistry, ShortcutRegistryError}; +use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError}; use crate::Config; use crate::WebviewHandler; use dioxus_core::ScopeState; @@ -230,7 +230,7 @@ impl DesktopService { /// Linux: Only works on x11. See [this issue](https://github.com/tauri-apps/tao/issues/331) for more information. pub fn create_shortcut( &self, - hotkey: global_hotkey::hotkey::HotKey, + hotkey: HotKey, callback: impl FnMut() + 'static, ) -> Result { self.shortcut_manager diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index 130170b6f..a96cd5abf 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -19,6 +19,7 @@ mod waker; mod webview; use crate::query::QueryResult; +use crate::shortcut::GlobalHotKeyEvent; pub use cfg::{Config, WindowCloseBehaviour}; pub use desktop_context::DesktopContext; pub use desktop_context::{ @@ -31,16 +32,6 @@ use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent}; use element::DesktopElement; use eval::init_eval; use futures_util::{pin_mut, FutureExt}; -#[cfg(any( - target_os = "windows", - target_os = "macos", - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] -use global_hotkey::GlobalHotKeyEvent; use shortcut::ShortcutRegistry; pub use shortcut::{use_global_shortcut, ShortcutHandle, ShortcutId, ShortcutRegistryError}; use std::cell::Cell; diff --git a/packages/desktop/src/mobile_shortcut.rs b/packages/desktop/src/mobile_shortcut.rs index b4919dfdf..c6144344b 100644 --- a/packages/desktop/src/mobile_shortcut.rs +++ b/packages/desktop/src/mobile_shortcut.rs @@ -1,38 +1,51 @@ #![allow(unused)] use super::*; -use wry::application::accelerator::Accelerator; +use std::str::FromStr; use wry::application::event_loop::EventLoopWindowTarget; -pub struct HotKey(); +use dioxus_html::input_data::keyboard_types::Modifiers; + +#[derive(Clone, Debug)] +pub struct Accelerator; + +#[derive(Clone, Copy)] +pub struct HotKey; + +impl HotKey { + pub fn new(mods: Option, key: Code) -> Self { + Self + } + + pub fn id(&self) -> u32 { + 0 + } +} impl FromStr for HotKey { type Err = (); fn from_str(s: &str) -> Result { - Ok(HotKey()) + Ok(HotKey) } } -pub struct ShortcutManager(); +pub struct GlobalHotKeyManager(); -impl ShortcutManager { - pub fn new(target: &EventLoopWindowTarget) -> Self { - Self() +impl GlobalHotKeyManager { + pub fn new() -> Result { + Ok(Self()) } - pub fn register( - &mut self, - accelerator: Accelerator, - ) -> Result { - Ok(GlobalShortcut()) + pub fn register(&mut self, accelerator: HotKey) -> Result { + Ok(HotKey) } - pub fn unregister(&mut self, id: ShortcutId) -> Result<(), ShortcutManagerError> { + pub fn unregister(&mut self, id: HotKey) -> Result<(), HotkeyError> { Ok(()) } - pub fn unregister_all(&mut self) -> Result<(), ShortcutManagerError> { + pub fn unregister_all(&mut self, _: &[HotKey]) -> Result<(), HotkeyError> { Ok(()) } } @@ -42,33 +55,35 @@ use std::{error, fmt}; /// An error whose cause the `ShortcutManager` to fail. #[non_exhaustive] #[derive(Debug)] -pub enum ShortcutManagerError { +pub enum HotkeyError { AcceleratorAlreadyRegistered(Accelerator), AcceleratorNotRegistered(Accelerator), - InvalidAccelerator(String), + HotKeyParseError(String), } -impl error::Error for ShortcutManagerError {} -impl fmt::Display for ShortcutManagerError { +impl error::Error for HotkeyError {} +impl fmt::Display for HotkeyError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { - ShortcutManagerError::AcceleratorAlreadyRegistered(e) => { + HotkeyError::AcceleratorAlreadyRegistered(e) => { f.pad(&format!("hotkey already registered: {:?}", e)) } - ShortcutManagerError::AcceleratorNotRegistered(e) => { + HotkeyError::AcceleratorNotRegistered(e) => { f.pad(&format!("hotkey not registered: {:?}", e)) } - ShortcutManagerError::InvalidAccelerator(e) => e.fmt(f), + HotkeyError::HotKeyParseError(e) => e.fmt(f), } } } -struct HotkeyError; - -struct GlobalHotKeyEvent { - id: u32, +pub struct GlobalHotKeyEvent { + pub id: u32, } -pub(crate) type Code = dioxus::prelude::Code; +impl GlobalHotKeyEvent { + pub fn receiver() -> crossbeam_channel::Receiver { + crossbeam_channel::unbounded().1 + } +} -struct GlobalHotKeyManager {} +pub(crate) type Code = dioxus_html::input_data::keyboard_types::Code; diff --git a/packages/desktop/src/shortcut.rs b/packages/desktop/src/shortcut.rs index 9696fa520..66ab4ba7a 100644 --- a/packages/desktop/src/shortcut.rs +++ b/packages/desktop/src/shortcut.rs @@ -7,7 +7,16 @@ use wry::application::keyboard::ModifiersState; use crate::{desktop_context::DesktopContext, use_window}; -use global_hotkey::{ +#[cfg(any( + target_os = "windows", + target_os = "macos", + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" +))] +pub use global_hotkey::{ hotkey::{Code, HotKey}, Error as HotkeyError, GlobalHotKeyEvent, GlobalHotKeyManager, }; @@ -61,7 +70,7 @@ impl ShortcutRegistry { pub(crate) fn add_shortcut( &self, - hotkey: global_hotkey::hotkey::HotKey, + hotkey: HotKey, callback: Box, ) -> Result { let accelerator_id = hotkey.clone().id(); @@ -168,15 +177,6 @@ impl IntoAccelerator for &str { } } -#[cfg(any( - target_os = "windows", - target_os = "macos", - target_os = "linux", - target_os = "dragonfly", - target_os = "freebsd", - target_os = "netbsd", - target_os = "openbsd" -))] /// Get a closure that executes any JavaScript in the WebView context. pub fn use_global_shortcut( cx: &ScopeState,