|
@@ -13,8 +13,8 @@ use tauri_runtime::{
|
|
|
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
|
|
|
DetachedWindow, PendingWindow, WindowEvent,
|
|
|
},
|
|
|
- Dispatch, Error, Icon, Params, Result, RunEvent, RunIteration, Runtime, RuntimeHandle,
|
|
|
- UserAttentionType,
|
|
|
+ Dispatch, Error, GlobalShortcutManager, Icon, Params, Result, RunEvent, RunIteration, Runtime,
|
|
|
+ RuntimeHandle, UserAttentionType,
|
|
|
};
|
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
@@ -32,6 +32,7 @@ use tauri_utils::config::WindowConfig;
|
|
|
use uuid::Uuid;
|
|
|
use wry::{
|
|
|
application::{
|
|
|
+ accelerator::{Accelerator, AcceleratorId},
|
|
|
dpi::{
|
|
|
LogicalPosition as WryLogicalPosition, LogicalSize as WryLogicalSize,
|
|
|
PhysicalPosition as WryPhysicalPosition, PhysicalSize as WryPhysicalSize,
|
|
@@ -40,6 +41,7 @@ use wry::{
|
|
|
event::{Event, WindowEvent as WryWindowEvent},
|
|
|
event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget},
|
|
|
monitor::MonitorHandle,
|
|
|
+ platform::global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
|
|
|
window::{
|
|
|
Fullscreen, Icon as WindowIcon, UserAttentionType as WryUserAttentionType, Window,
|
|
|
WindowBuilder as WryWindowBuilder, WindowId,
|
|
@@ -76,6 +78,118 @@ type CreateWebviewHandler =
|
|
|
Box<dyn FnOnce(&EventLoopWindowTarget<Message>) -> Result<WebviewWrapper> + Send>;
|
|
|
type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
|
|
|
type WindowEventListeners = Arc<Mutex<HashMap<Uuid, WindowEventHandler>>>;
|
|
|
+type GlobalShortcutListeners = Arc<Mutex<HashMap<AcceleratorId, Box<dyn Fn() + Send>>>>;
|
|
|
+
|
|
|
+macro_rules! dispatcher_getter {
|
|
|
+ ($self: ident, $message: expr) => {{
|
|
|
+ if current_thread().id() == $self.context.main_thread_id
|
|
|
+ && !$self.context.is_event_loop_running.load(Ordering::Relaxed)
|
|
|
+ {
|
|
|
+ panic!("This API cannot be called when the event loop is not running");
|
|
|
+ }
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ $self
|
|
|
+ .context
|
|
|
+ .proxy
|
|
|
+ .send_event(Message::Window($self.window_id, $message(tx)))
|
|
|
+ .map_err(|_| Error::FailedToSendMessage)?;
|
|
|
+ rx.recv().unwrap()
|
|
|
+ }};
|
|
|
+}
|
|
|
+
|
|
|
+macro_rules! shortcut_getter {
|
|
|
+ ($self: ident, $rx: expr, $message: expr) => {{
|
|
|
+ if current_thread().id() == $self.context.main_thread_id
|
|
|
+ && !$self.context.is_event_loop_running.load(Ordering::Relaxed)
|
|
|
+ {
|
|
|
+ panic!("This API cannot be called when the event loop is not running");
|
|
|
+ }
|
|
|
+ $self
|
|
|
+ .context
|
|
|
+ .proxy
|
|
|
+ .send_event($message)
|
|
|
+ .map_err(|_| Error::FailedToSendMessage)?;
|
|
|
+ $rx.recv().unwrap()
|
|
|
+ }};
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Debug, Clone)]
|
|
|
+struct GlobalShortcutWrapper(GlobalShortcut);
|
|
|
+
|
|
|
+unsafe impl Send for GlobalShortcutWrapper {}
|
|
|
+
|
|
|
+#[derive(Clone)]
|
|
|
+struct GlobalShortcutManagerContext {
|
|
|
+ main_thread_id: ThreadId,
|
|
|
+ is_event_loop_running: Arc<AtomicBool>,
|
|
|
+ proxy: EventLoopProxy<Message>,
|
|
|
+}
|
|
|
+
|
|
|
+/// Wrapper around [`WryShortcutManager`].
|
|
|
+#[derive(Clone)]
|
|
|
+pub struct GlobalShortcutManagerHandle {
|
|
|
+ context: GlobalShortcutManagerContext,
|
|
|
+ shortcuts: HashMap<String, (AcceleratorId, GlobalShortcutWrapper)>,
|
|
|
+ listeners: GlobalShortcutListeners,
|
|
|
+}
|
|
|
+
|
|
|
+impl GlobalShortcutManager for GlobalShortcutManagerHandle {
|
|
|
+ fn is_registered(&self, accelerator: &str) -> Result<bool> {
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ Ok(shortcut_getter!(
|
|
|
+ self,
|
|
|
+ rx,
|
|
|
+ Message::GlobalShortcut(GlobalShortcutMessage::IsRegistered(
|
|
|
+ accelerator.parse().expect("invalid accelerator"),
|
|
|
+ tx
|
|
|
+ ))
|
|
|
+ ))
|
|
|
+ }
|
|
|
+
|
|
|
+ fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()> {
|
|
|
+ let wry_accelerator: Accelerator = accelerator.parse().expect("invalid accelerator");
|
|
|
+ let id = wry_accelerator.clone().id();
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ let shortcut = shortcut_getter!(
|
|
|
+ self,
|
|
|
+ rx,
|
|
|
+ Message::GlobalShortcut(GlobalShortcutMessage::Register(wry_accelerator, tx))
|
|
|
+ )?;
|
|
|
+
|
|
|
+ self.listeners.lock().unwrap().insert(id, Box::new(handler));
|
|
|
+ self.shortcuts.insert(accelerator.into(), (id, shortcut));
|
|
|
+
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ fn unregister_all(&mut self) -> Result<()> {
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ shortcut_getter!(
|
|
|
+ self,
|
|
|
+ rx,
|
|
|
+ Message::GlobalShortcut(GlobalShortcutMessage::UnregisterAll(tx))
|
|
|
+ )?;
|
|
|
+ self.listeners.lock().unwrap().clear();
|
|
|
+ self.shortcuts.clear();
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+
|
|
|
+ fn unregister(&mut self, accelerator: &str) -> Result<()> {
|
|
|
+ if let Some((accelerator_id, shortcut)) = self.shortcuts.remove(accelerator) {
|
|
|
+ let (tx, rx) = channel();
|
|
|
+ shortcut_getter!(
|
|
|
+ self,
|
|
|
+ rx,
|
|
|
+ Message::GlobalShortcut(GlobalShortcutMessage::Unregister(
|
|
|
+ GlobalShortcutWrapper(shortcut.0),
|
|
|
+ tx
|
|
|
+ ))
|
|
|
+ )?;
|
|
|
+ self.listeners.lock().unwrap().remove(&accelerator_id);
|
|
|
+ }
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
/// Wrapper around a [`wry::application::window::Icon`] that can be created from an [`Icon`].
|
|
|
pub struct WryIcon(WindowIcon);
|
|
@@ -267,7 +381,7 @@ pub struct WindowBuilderWrapper {
|
|
|
inner: WryWindowBuilder,
|
|
|
center: bool,
|
|
|
#[cfg(feature = "menu")]
|
|
|
- menu_items: HashMap<u32, WryCustomMenuItem>,
|
|
|
+ menu_items: HashMap<u16, WryCustomMenuItem>,
|
|
|
}
|
|
|
|
|
|
// safe since `menu_items` are read only here
|
|
@@ -510,7 +624,7 @@ enum WindowMessage {
|
|
|
SetSkipTaskbar(bool),
|
|
|
DragWindow,
|
|
|
#[cfg(feature = "menu")]
|
|
|
- UpdateMenuItem(u32, menu::MenuUpdate),
|
|
|
+ UpdateMenuItem(u16, menu::MenuUpdate),
|
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
@@ -522,12 +636,20 @@ enum WebviewMessage {
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
#[derive(Clone)]
|
|
|
pub(crate) enum TrayMessage {
|
|
|
- UpdateItem(u32, menu::MenuUpdate),
|
|
|
+ UpdateItem(u16, menu::MenuUpdate),
|
|
|
UpdateIcon(Icon),
|
|
|
#[cfg(windows)]
|
|
|
Remove,
|
|
|
}
|
|
|
|
|
|
+#[derive(Clone)]
|
|
|
+pub(crate) enum GlobalShortcutMessage {
|
|
|
+ IsRegistered(Accelerator, Sender<bool>),
|
|
|
+ Register(Accelerator, Sender<Result<GlobalShortcutWrapper>>),
|
|
|
+ Unregister(GlobalShortcutWrapper, Sender<Result<()>>),
|
|
|
+ UnregisterAll(Sender<Result<()>>),
|
|
|
+}
|
|
|
+
|
|
|
#[derive(Clone)]
|
|
|
pub(crate) enum Message {
|
|
|
Task(MainTask),
|
|
@@ -536,6 +658,7 @@ pub(crate) enum Message {
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
Tray(TrayMessage),
|
|
|
CreateWebview(Arc<Mutex<Option<CreateWebviewHandler>>>, Sender<WindowId>),
|
|
|
+ GlobalShortcut(GlobalShortcutMessage),
|
|
|
}
|
|
|
|
|
|
#[derive(Clone)]
|
|
@@ -555,23 +678,6 @@ pub struct WryDispatcher {
|
|
|
context: DispatcherContext,
|
|
|
}
|
|
|
|
|
|
-macro_rules! dispatcher_getter {
|
|
|
- ($self: ident, $message: expr) => {{
|
|
|
- if current_thread().id() == $self.context.main_thread_id
|
|
|
- && !$self.context.is_event_loop_running.load(Ordering::Relaxed)
|
|
|
- {
|
|
|
- panic!("This API cannot be called when the event loop is not running");
|
|
|
- }
|
|
|
- let (tx, rx) = channel();
|
|
|
- $self
|
|
|
- .context
|
|
|
- .proxy
|
|
|
- .send_event(Message::Window($self.window_id, $message(tx)))
|
|
|
- .map_err(|_| Error::FailedToSendMessage)?;
|
|
|
- rx.recv().unwrap()
|
|
|
- }};
|
|
|
-}
|
|
|
-
|
|
|
impl Dispatch for WryDispatcher {
|
|
|
type Runtime = Wry;
|
|
|
type WindowBuilder = WindowBuilderWrapper;
|
|
@@ -959,7 +1065,7 @@ impl Dispatch for WryDispatcher {
|
|
|
}
|
|
|
|
|
|
#[cfg(feature = "menu")]
|
|
|
- fn update_menu_item(&self, id: u32, update: menu::MenuUpdate) -> Result<()> {
|
|
|
+ fn update_menu_item(&self, id: u16, update: menu::MenuUpdate) -> Result<()> {
|
|
|
self
|
|
|
.context
|
|
|
.proxy
|
|
@@ -983,7 +1089,7 @@ struct WebviewWrapper {
|
|
|
label: String,
|
|
|
inner: WebView,
|
|
|
#[cfg(feature = "menu")]
|
|
|
- menu_items: HashMap<u32, WryCustomMenuItem>,
|
|
|
+ menu_items: HashMap<u16, WryCustomMenuItem>,
|
|
|
#[cfg(feature = "menu")]
|
|
|
is_menu_visible: AtomicBool,
|
|
|
}
|
|
@@ -991,6 +1097,8 @@ struct WebviewWrapper {
|
|
|
/// A Tauri [`Runtime`] wrapper around wry.
|
|
|
pub struct Wry {
|
|
|
main_thread_id: ThreadId,
|
|
|
+ global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
|
|
+ global_shortcut_manager_handle: GlobalShortcutManagerHandle,
|
|
|
is_event_loop_running: Arc<AtomicBool>,
|
|
|
event_loop: EventLoop<Message>,
|
|
|
webviews: Arc<Mutex<HashMap<WindowId, WebviewWrapper>>>,
|
|
@@ -1050,14 +1158,30 @@ impl RuntimeHandle for WryHandle {
|
|
|
impl Runtime for Wry {
|
|
|
type Dispatcher = WryDispatcher;
|
|
|
type Handle = WryHandle;
|
|
|
+ type GlobalShortcutManager = GlobalShortcutManagerHandle;
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
type TrayHandler = SystemTrayHandle;
|
|
|
|
|
|
fn new() -> Result<Self> {
|
|
|
let event_loop = EventLoop::<Message>::with_user_event();
|
|
|
+ let global_shortcut_manager = WryShortcutManager::new(&event_loop);
|
|
|
+ let global_shortcut_listeners = GlobalShortcutListeners::default();
|
|
|
+ let proxy = event_loop.create_proxy();
|
|
|
+ let main_thread_id = current_thread().id();
|
|
|
+ let is_event_loop_running = Arc::new(AtomicBool::default());
|
|
|
Ok(Self {
|
|
|
- main_thread_id: current_thread().id(),
|
|
|
- is_event_loop_running: Default::default(),
|
|
|
+ main_thread_id,
|
|
|
+ global_shortcut_manager: Arc::new(Mutex::new(global_shortcut_manager)),
|
|
|
+ global_shortcut_manager_handle: GlobalShortcutManagerHandle {
|
|
|
+ context: GlobalShortcutManagerContext {
|
|
|
+ main_thread_id,
|
|
|
+ is_event_loop_running: is_event_loop_running.clone(),
|
|
|
+ proxy,
|
|
|
+ },
|
|
|
+ shortcuts: Default::default(),
|
|
|
+ listeners: global_shortcut_listeners,
|
|
|
+ },
|
|
|
+ is_event_loop_running,
|
|
|
event_loop,
|
|
|
webviews: Default::default(),
|
|
|
window_event_listeners: Default::default(),
|
|
@@ -1081,6 +1205,10 @@ impl Runtime for Wry {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ fn global_shortcut_manager(&self) -> Self::GlobalShortcutManager {
|
|
|
+ self.global_shortcut_manager_handle.clone()
|
|
|
+ }
|
|
|
+
|
|
|
fn create_window<P: Params<Runtime = Self>>(
|
|
|
&self,
|
|
|
pending: PendingWindow<P>,
|
|
@@ -1168,6 +1296,8 @@ impl Runtime for Wry {
|
|
|
let menu_event_listeners = self.menu_event_listeners.clone();
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
let tray_context = self.tray_context.clone();
|
|
|
+ let global_shortcut_manager = self.global_shortcut_manager.clone();
|
|
|
+ let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone();
|
|
|
|
|
|
let mut iteration = RunIteration::default();
|
|
|
|
|
@@ -1186,6 +1316,8 @@ impl Runtime for Wry {
|
|
|
callback: &callback,
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
+ global_shortcut_manager: global_shortcut_manager.clone(),
|
|
|
+ global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_event_listeners: menu_event_listeners.clone(),
|
|
|
#[cfg(feature = "system-tray")]
|
|
@@ -1206,6 +1338,8 @@ impl Runtime for Wry {
|
|
|
let menu_event_listeners = self.menu_event_listeners.clone();
|
|
|
#[cfg(feature = "system-tray")]
|
|
|
let tray_context = self.tray_context;
|
|
|
+ let global_shortcut_manager = self.global_shortcut_manager.clone();
|
|
|
+ let global_shortcut_manager_handle = self.global_shortcut_manager_handle.clone();
|
|
|
|
|
|
self.event_loop.run(move |event, event_loop, control_flow| {
|
|
|
handle_event_loop(
|
|
@@ -1216,6 +1350,8 @@ impl Runtime for Wry {
|
|
|
callback: &callback,
|
|
|
webviews: webviews.lock().expect("poisoned webview collection"),
|
|
|
window_event_listeners: window_event_listeners.clone(),
|
|
|
+ global_shortcut_manager: global_shortcut_manager.clone(),
|
|
|
+ global_shortcut_manager_handle: global_shortcut_manager_handle.clone(),
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_event_listeners: menu_event_listeners.clone(),
|
|
|
#[cfg(feature = "system-tray")]
|
|
@@ -1230,6 +1366,8 @@ struct EventLoopIterationContext<'a> {
|
|
|
callback: &'a (dyn Fn(RunEvent) + 'static),
|
|
|
webviews: MutexGuard<'a, HashMap<WindowId, WebviewWrapper>>,
|
|
|
window_event_listeners: WindowEventListeners,
|
|
|
+ global_shortcut_manager: Arc<Mutex<WryShortcutManager>>,
|
|
|
+ global_shortcut_manager_handle: GlobalShortcutManagerHandle,
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_event_listeners: MenuEventListeners,
|
|
|
#[cfg(feature = "system-tray")]
|
|
@@ -1246,6 +1384,8 @@ fn handle_event_loop(
|
|
|
callback,
|
|
|
mut webviews,
|
|
|
window_event_listeners,
|
|
|
+ global_shortcut_manager,
|
|
|
+ global_shortcut_manager_handle,
|
|
|
#[cfg(feature = "menu")]
|
|
|
menu_event_listeners,
|
|
|
#[cfg(feature = "system-tray")]
|
|
@@ -1254,6 +1394,13 @@ fn handle_event_loop(
|
|
|
*control_flow = ControlFlow::Wait;
|
|
|
|
|
|
match event {
|
|
|
+ Event::GlobalShortcutEvent(accelerator_id) => {
|
|
|
+ for (id, handler) in &*global_shortcut_manager_handle.listeners.lock().unwrap() {
|
|
|
+ if accelerator_id == *id {
|
|
|
+ handler();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
#[cfg(feature = "menu")]
|
|
|
Event::MenuEvent {
|
|
|
menu_id,
|
|
@@ -1504,6 +1651,44 @@ fn handle_event_loop(
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
+ Message::GlobalShortcut(message) => match message {
|
|
|
+ GlobalShortcutMessage::IsRegistered(accelerator, tx) => tx
|
|
|
+ .send(
|
|
|
+ global_shortcut_manager
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .is_registered(&accelerator),
|
|
|
+ )
|
|
|
+ .unwrap(),
|
|
|
+ GlobalShortcutMessage::Register(accelerator, tx) => tx
|
|
|
+ .send(
|
|
|
+ global_shortcut_manager
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .register(accelerator)
|
|
|
+ .map(GlobalShortcutWrapper)
|
|
|
+ .map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
+ )
|
|
|
+ .unwrap(),
|
|
|
+ GlobalShortcutMessage::Unregister(shortcut, tx) => tx
|
|
|
+ .send(
|
|
|
+ global_shortcut_manager
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .unregister(shortcut.0)
|
|
|
+ .map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
+ )
|
|
|
+ .unwrap(),
|
|
|
+ GlobalShortcutMessage::UnregisterAll(tx) => tx
|
|
|
+ .send(
|
|
|
+ global_shortcut_manager
|
|
|
+ .lock()
|
|
|
+ .unwrap()
|
|
|
+ .unregister_all()
|
|
|
+ .map_err(|e| Error::GlobalShortcut(Box::new(e))),
|
|
|
+ )
|
|
|
+ .unwrap(),
|
|
|
+ },
|
|
|
},
|
|
|
_ => (),
|
|
|
}
|