浏览代码

fix mobile build

Evan Almloff 1 年之前
父节点
当前提交
d1575b40d1

+ 1 - 0
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]

+ 2 - 2
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<ShortcutId, ShortcutRegistryError> {
         self.shortcut_manager

+ 1 - 10
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;

+ 42 - 27
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<Modifiers>, key: Code) -> Self {
+        Self
+    }
+
+    pub fn id(&self) -> u32 {
+        0
+    }
+}
 
 impl FromStr for HotKey {
     type Err = ();
 
     fn from_str(s: &str) -> Result<Self, Self::Err> {
-        Ok(HotKey())
+        Ok(HotKey)
     }
 }
 
-pub struct ShortcutManager();
+pub struct GlobalHotKeyManager();
 
-impl ShortcutManager {
-    pub fn new<T>(target: &EventLoopWindowTarget<T>) -> Self {
-        Self()
+impl GlobalHotKeyManager {
+    pub fn new() -> Result<Self, HotkeyError> {
+        Ok(Self())
     }
 
-    pub fn register(
-        &mut self,
-        accelerator: Accelerator,
-    ) -> Result<GlobalShortcut, ShortcutManagerError> {
-        Ok(GlobalShortcut())
+    pub fn register(&mut self, accelerator: HotKey) -> Result<HotKey, HotkeyError> {
+        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<GlobalHotKeyEvent> {
+        crossbeam_channel::unbounded().1
+    }
+}
 
-struct GlobalHotKeyManager {}
+pub(crate) type Code = dioxus_html::input_data::keyboard_types::Code;

+ 11 - 11
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<dyn FnMut()>,
     ) -> Result<ShortcutId, ShortcutRegistryError> {
         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,