Fix non tokio builds for desktop
This commit is contained in:
15 files changed
+274
-437
No files matched your search
@@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
config::{Config, WindowCloseBehaviour},
|
||||
desktop_context::WindowEventHandlers,
|
||||
element::DesktopElement,
|
||||
event_handlers::WindowEventHandlers,
|
||||
file_upload::FileDialogRequest,
|
||||
ipc::IpcMessage,
|
||||
ipc::{EventData, UserWindowEvent},
|
||||
|
||||
@@ -4,17 +4,16 @@ use crate::{
|
||||
edits::EditQueue,
|
||||
ipc::{EventData, UserWindowEvent},
|
||||
query::QueryEngine,
|
||||
shortcut::{HotKey, ShortcutId, ShortcutRegistryError},
|
||||
shortcut::{HotKey, ShortcutHandle, ShortcutRegistryError},
|
||||
webview::WebviewInstance,
|
||||
AssetRequest, Config,
|
||||
AssetRequest, Config, WryEventHandler,
|
||||
};
|
||||
use dioxus_core::{
|
||||
prelude::{current_scope_id, ScopeId},
|
||||
use_hook, VirtualDom,
|
||||
VirtualDom,
|
||||
};
|
||||
use dioxus_interpreter_js::MutationState;
|
||||
use slab::Slab;
|
||||
use std::{cell::RefCell, fmt::Debug, rc::Rc, rc::Weak};
|
||||
use std::{cell::RefCell, rc::Rc, rc::Weak};
|
||||
use tao::{
|
||||
event::Event,
|
||||
event_loop::EventLoopWindowTarget,
|
||||
@@ -208,12 +207,12 @@ impl DesktopService {
|
||||
pub fn create_wry_event_handler(
|
||||
&self,
|
||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> WryEventHandlerId {
|
||||
) -> WryEventHandler {
|
||||
self.shared.event_handlers.add(self.window.id(), handler)
|
||||
}
|
||||
|
||||
/// Remove a wry event handler created with [`DesktopContext::create_wry_event_handler`]
|
||||
pub fn remove_wry_event_handler(&self, id: WryEventHandlerId) {
|
||||
pub fn remove_wry_event_handler(&self, id: WryEventHandler) {
|
||||
self.shared.event_handlers.remove(id)
|
||||
}
|
||||
|
||||
@@ -224,14 +223,14 @@ impl DesktopService {
|
||||
&self,
|
||||
hotkey: HotKey,
|
||||
callback: impl FnMut() + 'static,
|
||||
) -> Result<ShortcutId, ShortcutRegistryError> {
|
||||
) -> Result<ShortcutHandle, ShortcutRegistryError> {
|
||||
self.shared
|
||||
.shortcut_manager
|
||||
.add_shortcut(hotkey, Box::new(callback))
|
||||
}
|
||||
|
||||
/// Remove a global shortcut
|
||||
pub fn remove_shortcut(&self, id: ShortcutId) {
|
||||
pub fn remove_shortcut(&self, id: ShortcutHandle) {
|
||||
self.shared.shortcut_manager.remove_shortcut(id)
|
||||
}
|
||||
|
||||
@@ -250,12 +249,12 @@ impl DesktopService {
|
||||
pub fn register_asset_handler(
|
||||
&self,
|
||||
name: String,
|
||||
f: Box<dyn Fn(AssetRequest, RequestAsyncResponder) + 'static>,
|
||||
handler: Box<dyn Fn(AssetRequest, RequestAsyncResponder) + 'static>,
|
||||
scope: Option<ScopeId>,
|
||||
) {
|
||||
self.asset_handlers.register_handler(
|
||||
name,
|
||||
f,
|
||||
handler,
|
||||
scope.unwrap_or(current_scope_id().unwrap_or(ScopeId(0))),
|
||||
)
|
||||
}
|
||||
@@ -313,106 +312,3 @@ fn is_main_thread() -> bool {
|
||||
let result: BOOL = unsafe { msg_send![cls, isMainThread] };
|
||||
result != NO
|
||||
}
|
||||
|
||||
/// The unique identifier of a window event handler. This can be used to later remove the handler.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct WryEventHandlerId(usize);
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct WindowEventHandlers {
|
||||
handlers: Rc<RefCell<Slab<WryWindowEventHandlerInner>>>,
|
||||
}
|
||||
|
||||
impl WindowEventHandlers {
|
||||
pub(crate) fn add(
|
||||
&self,
|
||||
window_id: WindowId,
|
||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> WryEventHandlerId {
|
||||
WryEventHandlerId(
|
||||
self.handlers
|
||||
.borrow_mut()
|
||||
.insert(WryWindowEventHandlerInner {
|
||||
window_id,
|
||||
handler: Box::new(handler),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn remove(&self, id: WryEventHandlerId) {
|
||||
self.handlers.borrow_mut().try_remove(id.0);
|
||||
}
|
||||
|
||||
pub(crate) fn apply_event(
|
||||
&self,
|
||||
event: &Event<UserWindowEvent>,
|
||||
target: &EventLoopWindowTarget<UserWindowEvent>,
|
||||
) {
|
||||
for (_, handler) in self.handlers.borrow_mut().iter_mut() {
|
||||
handler.apply_event(event, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct WryWindowEventHandlerInner {
|
||||
window_id: WindowId,
|
||||
handler: WryEventHandlerCallback,
|
||||
}
|
||||
|
||||
type WryEventHandlerCallback =
|
||||
Box<dyn FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static>;
|
||||
|
||||
impl WryWindowEventHandlerInner {
|
||||
fn apply_event(
|
||||
&mut self,
|
||||
event: &Event<UserWindowEvent>,
|
||||
target: &EventLoopWindowTarget<UserWindowEvent>,
|
||||
) {
|
||||
// if this event does not apply to the window this listener cares about, return
|
||||
if let Event::WindowEvent { window_id, .. } = event {
|
||||
if *window_id != self.window_id {
|
||||
return;
|
||||
}
|
||||
}
|
||||
(self.handler)(event, target)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a closure that executes any JavaScript in the WebView context.
|
||||
pub fn use_wry_event_handler(
|
||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> WryEventHandler {
|
||||
use_hook(move || {
|
||||
let desktop = window();
|
||||
|
||||
let id = desktop.create_wry_event_handler(handler);
|
||||
|
||||
WryEventHandler {
|
||||
handlers: desktop.shared.event_handlers.clone(),
|
||||
id,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// A wry event handler that is scoped to the current component and window. The event handler will only receive events for the window it was created for and global events.
|
||||
///
|
||||
/// This will automatically be removed when the component is unmounted.
|
||||
#[derive(Clone)]
|
||||
pub struct WryEventHandler {
|
||||
pub(crate) handlers: WindowEventHandlers,
|
||||
/// The unique identifier of the event handler.
|
||||
pub id: WryEventHandlerId,
|
||||
}
|
||||
|
||||
impl WryEventHandler {
|
||||
/// Remove the event handler.
|
||||
pub fn remove(&self) {
|
||||
self.handlers.remove(self.id);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WryEventHandler {
|
||||
fn drop(&mut self) {
|
||||
self.handlers.remove(self.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
use crate::{ipc::UserWindowEvent, window};
|
||||
use slab::Slab;
|
||||
use std::cell::RefCell;
|
||||
use tao::{event::Event, event_loop::EventLoopWindowTarget, window::WindowId};
|
||||
|
||||
/// The unique identifier of a window event handler. This can be used to later remove the handler.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct WryEventHandler(pub(crate) usize);
|
||||
|
||||
impl WryEventHandler {
|
||||
/// Unregister this event handler from the window
|
||||
pub fn remove(&self) {
|
||||
window().shared.event_handlers.remove(*self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WindowEventHandlers {
|
||||
handlers: RefCell<Slab<WryWindowEventHandlerInner>>,
|
||||
}
|
||||
|
||||
struct WryWindowEventHandlerInner {
|
||||
window_id: WindowId,
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
handler:
|
||||
Box<dyn FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static>,
|
||||
}
|
||||
|
||||
impl WindowEventHandlers {
|
||||
pub(crate) fn add(
|
||||
&self,
|
||||
window_id: WindowId,
|
||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> WryEventHandler {
|
||||
WryEventHandler(
|
||||
self.handlers
|
||||
.borrow_mut()
|
||||
.insert(WryWindowEventHandlerInner {
|
||||
window_id,
|
||||
handler: Box::new(handler),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn remove(&self, id: WryEventHandler) {
|
||||
self.handlers.borrow_mut().try_remove(id.0);
|
||||
}
|
||||
|
||||
pub fn apply_event(
|
||||
&self,
|
||||
event: &Event<UserWindowEvent>,
|
||||
target: &EventLoopWindowTarget<UserWindowEvent>,
|
||||
) {
|
||||
for (_, handler) in self.handlers.borrow_mut().iter_mut() {
|
||||
// if this event does not apply to the window this listener cares about, return
|
||||
if let Event::WindowEvent { window_id, .. } = event {
|
||||
if *window_id != handler.window_id {
|
||||
return;
|
||||
}
|
||||
}
|
||||
(handler.handler)(event, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,29 +8,23 @@ use dioxus_core::{
|
||||
prelude::{consume_context, current_scope_id},
|
||||
use_hook,
|
||||
};
|
||||
use dioxus_hooks::use_on_drop;
|
||||
use dioxus_hooks::use_hook_with_cleanup;
|
||||
use tao::{event::Event, event_loop::EventLoopWindowTarget};
|
||||
use wry::RequestAsyncResponder;
|
||||
|
||||
/// Get an imperative handle to the current window
|
||||
pub fn use_window() -> DesktopContext {
|
||||
use_hook(|| consume_context::<DesktopContext>())
|
||||
use_hook(consume_context::<DesktopContext>)
|
||||
}
|
||||
|
||||
/// Get a closure that executes any JavaScript in the WebView context.
|
||||
pub fn use_wry_event_handler(
|
||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||
) -> WryEventHandler {
|
||||
use_hook(move || {
|
||||
let desktop = window();
|
||||
|
||||
let id = desktop.create_wry_event_handler(handler);
|
||||
|
||||
WryEventHandler {
|
||||
handlers: desktop.shared.event_handlers.clone(),
|
||||
id,
|
||||
}
|
||||
})
|
||||
use_hook_with_cleanup(
|
||||
move || window().create_wry_event_handler(handler),
|
||||
move |handler| handler.remove(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Provide a callback to handle asset loading yourself.
|
||||
@@ -41,19 +35,20 @@ pub fn use_asset_handler(
|
||||
name: &str,
|
||||
handler: impl Fn(AssetRequest, RequestAsyncResponder) + 'static,
|
||||
) {
|
||||
let name = use_hook(|| {
|
||||
crate::window().asset_handlers.register_handler(
|
||||
name.to_string(),
|
||||
Box::new(handler),
|
||||
current_scope_id().unwrap(),
|
||||
);
|
||||
use_hook_with_cleanup(
|
||||
|| {
|
||||
crate::window().asset_handlers.register_handler(
|
||||
name.to_string(),
|
||||
Box::new(handler),
|
||||
current_scope_id().unwrap(),
|
||||
);
|
||||
|
||||
Rc::new(name.to_string())
|
||||
});
|
||||
|
||||
use_on_drop(move || {
|
||||
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
|
||||
});
|
||||
Rc::new(name.to_string())
|
||||
},
|
||||
move |name| {
|
||||
_ = crate::window().asset_handlers.remove_handler(name.as_ref());
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Get a closure that executes any JavaScript in the WebView context.
|
||||
@@ -61,14 +56,12 @@ pub fn use_global_shortcut(
|
||||
accelerator: impl IntoAccelerator,
|
||||
handler: impl FnMut() + 'static,
|
||||
) -> Result<ShortcutHandle, ShortcutRegistryError> {
|
||||
use_hook(move || {
|
||||
let desktop = window();
|
||||
|
||||
let id = desktop.create_shortcut(accelerator.accelerator(), handler);
|
||||
|
||||
Ok(ShortcutHandle {
|
||||
desktop,
|
||||
shortcut_id: id?,
|
||||
})
|
||||
})
|
||||
use_hook_with_cleanup(
|
||||
move || window().create_shortcut(accelerator.accelerator(), handler),
|
||||
|handle| {
|
||||
if let Ok(handle) = handle {
|
||||
handle.remove();
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -70,5 +70,5 @@ pub fn launch(
|
||||
}));
|
||||
|
||||
#[cfg(not(feature = "tokio"))]
|
||||
launch_with_props_blocking(config, platform_config)
|
||||
launch_with_props_blocking(virtual_dom, platform_config)
|
||||
}
|
||||
@@ -10,6 +10,7 @@ mod desktop_context;
|
||||
mod edits;
|
||||
mod element;
|
||||
mod eval;
|
||||
mod event_handlers;
|
||||
mod events;
|
||||
mod file_upload;
|
||||
mod hooks;
|
||||
@@ -38,11 +39,8 @@ pub use wry;
|
||||
// Public exports
|
||||
pub use assets::AssetRequest;
|
||||
pub use config::{Config, WindowCloseBehaviour};
|
||||
pub use desktop_context::{
|
||||
window, DesktopContext, DesktopService, WryEventHandler, WryEventHandlerId,
|
||||
};
|
||||
pub use desktop_context::{window, DesktopContext, DesktopService};
|
||||
pub use event_handlers::WryEventHandler;
|
||||
pub use hooks::{use_asset_handler, use_global_shortcut, use_window, use_wry_event_handler};
|
||||
pub use shortcut::{ShortcutHandle, ShortcutId, ShortcutRegistryError};
|
||||
pub use shortcut::{ShortcutHandle, ShortcutRegistryError};
|
||||
pub use wry::RequestAsyncResponder;
|
||||
|
||||
pub use hooks::*;
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use crate::DesktopContext;
|
||||
use futures_util::StreamExt;
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
use serde_json::Value;
|
||||
use slab::Slab;
|
||||
use thiserror::Error;
|
||||
use tokio::sync::broadcast::error::RecvError;
|
||||
|
||||
const DIOXUS_CODE: &str = r#"
|
||||
let dioxus = {
|
||||
@@ -64,8 +64,8 @@ impl<T> Default for SharedSlab<T> {
|
||||
}
|
||||
|
||||
struct QueryEntry {
|
||||
channel_sender: tokio::sync::mpsc::UnboundedSender<Value>,
|
||||
return_sender: Option<tokio::sync::oneshot::Sender<Value>>,
|
||||
channel_sender: futures_channel::mpsc::UnboundedSender<Value>,
|
||||
return_sender: Option<futures_channel::oneshot::Sender<Value>>,
|
||||
}
|
||||
|
||||
const QUEUE_NAME: &str = "__msg_queues";
|
||||
@@ -83,8 +83,8 @@ impl QueryEngine {
|
||||
script: &str,
|
||||
context: DesktopContext,
|
||||
) -> Query<V> {
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
|
||||
let (return_tx, return_rx) = tokio::sync::oneshot::channel();
|
||||
let (tx, rx) = futures_channel::mpsc::unbounded();
|
||||
let (return_tx, return_rx) = futures_channel::oneshot::channel();
|
||||
let request_id = self.active_requests.slab.borrow_mut().insert(QueryEntry {
|
||||
channel_sender: tx,
|
||||
return_sender: Some(return_tx),
|
||||
@@ -99,14 +99,14 @@ impl QueryEngine {
|
||||
if (!window.{QUEUE_NAME}) {{
|
||||
window.{QUEUE_NAME} = [];
|
||||
}}
|
||||
|
||||
|
||||
let _request_id = {request_id};
|
||||
|
||||
|
||||
if (!window.{QUEUE_NAME}[{request_id}]) {{
|
||||
window.{QUEUE_NAME}[{request_id}] = [];
|
||||
}}
|
||||
let _message_queue = window.{QUEUE_NAME}[{request_id}];
|
||||
|
||||
|
||||
{script}
|
||||
}})().then((result)=>{{
|
||||
let returned_value = {{
|
||||
@@ -150,7 +150,7 @@ impl QueryEngine {
|
||||
let _ = sender.send(data);
|
||||
}
|
||||
} else {
|
||||
let _ = entry.channel_sender.send(data);
|
||||
let _ = entry.channel_sender.unbounded_send(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,8 +159,8 @@ impl QueryEngine {
|
||||
pub(crate) struct Query<V: DeserializeOwned> {
|
||||
desktop: DesktopContext,
|
||||
slab: SharedSlab<QueryEntry>,
|
||||
receiver: tokio::sync::mpsc::UnboundedReceiver<Value>,
|
||||
return_receiver: Option<tokio::sync::oneshot::Receiver<Value>>,
|
||||
receiver: futures_channel::mpsc::UnboundedReceiver<Value>,
|
||||
return_receiver: Option<futures_channel::oneshot::Receiver<Value>>,
|
||||
id: usize,
|
||||
phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
@@ -200,18 +200,13 @@ impl<V: DeserializeOwned> Query<V> {
|
||||
|
||||
/// Receive a message from the query
|
||||
pub async fn recv(&mut self) -> Result<Value, QueryError> {
|
||||
self.receiver
|
||||
.recv()
|
||||
.await
|
||||
.ok_or(QueryError::Recv(RecvError::Closed))
|
||||
self.receiver.next().await.ok_or(QueryError::Recv)
|
||||
}
|
||||
|
||||
/// Receive the result of the query
|
||||
pub async fn result(&mut self) -> Result<Value, QueryError> {
|
||||
match self.return_receiver.take() {
|
||||
Some(receiver) => receiver
|
||||
.await
|
||||
.map_err(|_| QueryError::Recv(RecvError::Closed)),
|
||||
Some(receiver) => receiver.await.map_err(|_| QueryError::Recv),
|
||||
None => Err(QueryError::Finished),
|
||||
}
|
||||
}
|
||||
@@ -238,8 +233,8 @@ impl<V: DeserializeOwned> Drop for Query<V> {
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum QueryError {
|
||||
#[error("Error receiving query result: {0}")]
|
||||
Recv(RecvError),
|
||||
#[error("Error receiving query result.")]
|
||||
Recv,
|
||||
#[error("Error sending message to query: {0}")]
|
||||
Send(String),
|
||||
#[error("Error deserializing query result: {0}")]
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc, str::FromStr};
|
||||
|
||||
use dioxus_html::input_data::keyboard_types::Modifiers;
|
||||
use slab::Slab;
|
||||
use tao::keyboard::ModifiersState;
|
||||
|
||||
use crate::desktop_context::DesktopContext;
|
||||
|
||||
#[cfg(any(
|
||||
target_os = "windows",
|
||||
target_os = "macos",
|
||||
@@ -23,101 +15,23 @@ pub use global_hotkey::{
|
||||
#[cfg(any(target_os = "ios", target_os = "android"))]
|
||||
pub use crate::mobile_shortcut::*;
|
||||
|
||||
pub(crate) struct ShortcutRegistry {
|
||||
manager: GlobalHotKeyManager,
|
||||
shortcuts: RefCell<HashMap<u32, Shortcut>>,
|
||||
use crate::window;
|
||||
use dioxus_html::input_data::keyboard_types::Modifiers;
|
||||
use slab::Slab;
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc, str::FromStr};
|
||||
use tao::keyboard::ModifiersState;
|
||||
|
||||
/// An global id for a shortcut.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ShortcutHandle {
|
||||
id: u32,
|
||||
number: usize,
|
||||
}
|
||||
|
||||
struct Shortcut {
|
||||
#[allow(unused)]
|
||||
shortcut: HotKey,
|
||||
callbacks: Slab<Box<dyn FnMut()>>,
|
||||
}
|
||||
|
||||
impl Shortcut {
|
||||
fn insert(&mut self, callback: Box<dyn FnMut()>) -> usize {
|
||||
self.callbacks.insert(callback)
|
||||
}
|
||||
|
||||
fn remove(&mut self, id: usize) {
|
||||
let _ = self.callbacks.remove(id);
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.callbacks.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl ShortcutRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
manager: GlobalHotKeyManager::new().unwrap(),
|
||||
shortcuts: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn call_handlers(&self, id: GlobalHotKeyEvent) {
|
||||
if let Some(Shortcut { callbacks, .. }) = self.shortcuts.borrow_mut().get_mut(&id.id) {
|
||||
for (_, callback) in callbacks.iter_mut() {
|
||||
(callback)();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_shortcut(
|
||||
&self,
|
||||
hotkey: HotKey,
|
||||
callback: Box<dyn FnMut()>,
|
||||
) -> Result<ShortcutId, ShortcutRegistryError> {
|
||||
let accelerator_id = hotkey.clone().id();
|
||||
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
|
||||
if let Some(callbacks) = shortcuts.get_mut(&accelerator_id) {
|
||||
return Ok(ShortcutId {
|
||||
id: accelerator_id,
|
||||
number: callbacks.insert(callback),
|
||||
});
|
||||
};
|
||||
|
||||
self.manager.register(hotkey).map_err(|e| match e {
|
||||
HotkeyError::HotKeyParseError(shortcut) => {
|
||||
ShortcutRegistryError::InvalidShortcut(shortcut)
|
||||
}
|
||||
err => ShortcutRegistryError::Other(Rc::new(err)),
|
||||
})?;
|
||||
|
||||
let mut shortcut = Shortcut {
|
||||
shortcut: hotkey,
|
||||
callbacks: Slab::new(),
|
||||
};
|
||||
|
||||
let id = shortcut.callbacks.insert(callback);
|
||||
|
||||
shortcuts.insert(accelerator_id, shortcut);
|
||||
|
||||
Ok(ShortcutId {
|
||||
id: accelerator_id,
|
||||
number: id,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn remove_shortcut(&self, id: ShortcutId) {
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
if let Some(callbacks) = shortcuts.get_mut(&id.id) {
|
||||
callbacks.remove(id.number);
|
||||
if callbacks.is_empty() {
|
||||
if let Some(_shortcut) = shortcuts.remove(&id.id) {
|
||||
let _ = self.manager.unregister(_shortcut.shortcut);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove_all(&self) {
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
let hotkeys: Vec<_> = shortcuts.drain().map(|(_, v)| v.shortcut).collect();
|
||||
let _ = self.manager.unregister_all(&hotkeys);
|
||||
impl ShortcutHandle {
|
||||
/// Remove the shortcut.
|
||||
pub fn remove(&self) {
|
||||
window().remove_shortcut(*self);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,19 +45,88 @@ pub enum ShortcutRegistryError {
|
||||
Other(Rc<dyn std::error::Error>),
|
||||
}
|
||||
|
||||
/// An global id for a shortcut.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct ShortcutId {
|
||||
id: u32,
|
||||
number: usize,
|
||||
pub(crate) struct ShortcutRegistry {
|
||||
manager: GlobalHotKeyManager,
|
||||
shortcuts: RefCell<HashMap<u32, ShortcutInner>>,
|
||||
}
|
||||
|
||||
/// A global shortcut. This will be automatically removed when it is dropped.
|
||||
#[derive(Clone)]
|
||||
pub struct ShortcutHandle {
|
||||
pub(crate) desktop: DesktopContext,
|
||||
/// The id of the shortcut
|
||||
pub shortcut_id: ShortcutId,
|
||||
struct ShortcutInner {
|
||||
#[allow(unused)]
|
||||
shortcut: HotKey,
|
||||
callbacks: Slab<Box<dyn FnMut()>>,
|
||||
}
|
||||
|
||||
impl ShortcutRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
manager: GlobalHotKeyManager::new().unwrap(),
|
||||
shortcuts: RefCell::new(HashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn call_handlers(&self, id: GlobalHotKeyEvent) {
|
||||
if let Some(ShortcutInner { callbacks, .. }) = self.shortcuts.borrow_mut().get_mut(&id.id) {
|
||||
for (_, callback) in callbacks.iter_mut() {
|
||||
(callback)();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn add_shortcut(
|
||||
&self,
|
||||
hotkey: HotKey,
|
||||
callback: Box<dyn FnMut()>,
|
||||
) -> Result<ShortcutHandle, ShortcutRegistryError> {
|
||||
let accelerator_id = hotkey.clone().id();
|
||||
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
|
||||
if let Some(callbacks) = shortcuts.get_mut(&accelerator_id) {
|
||||
return Ok(ShortcutHandle {
|
||||
id: accelerator_id,
|
||||
number: callbacks.callbacks.insert(callback),
|
||||
});
|
||||
};
|
||||
|
||||
self.manager.register(hotkey).map_err(|e| match e {
|
||||
HotkeyError::HotKeyParseError(shortcut) => {
|
||||
ShortcutRegistryError::InvalidShortcut(shortcut)
|
||||
}
|
||||
err => ShortcutRegistryError::Other(Rc::new(err)),
|
||||
})?;
|
||||
|
||||
let mut shortcut = ShortcutInner {
|
||||
shortcut: hotkey,
|
||||
callbacks: Slab::new(),
|
||||
};
|
||||
|
||||
let id = shortcut.callbacks.insert(callback);
|
||||
|
||||
shortcuts.insert(accelerator_id, shortcut);
|
||||
|
||||
Ok(ShortcutHandle {
|
||||
id: accelerator_id,
|
||||
number: id,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn remove_shortcut(&self, id: ShortcutHandle) {
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
if let Some(callbacks) = shortcuts.get_mut(&id.id) {
|
||||
let _ = callbacks.callbacks.remove(id.number);
|
||||
if callbacks.callbacks.is_empty() {
|
||||
if let Some(_shortcut) = shortcuts.remove(&id.id) {
|
||||
let _ = self.manager.unregister(_shortcut.shortcut);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn remove_all(&self) {
|
||||
let mut shortcuts = self.shortcuts.borrow_mut();
|
||||
let hotkeys: Vec<_> = shortcuts.drain().map(|(_, v)| v.shortcut).collect();
|
||||
let _ = self.manager.unregister_all(&hotkeys);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoAccelerator {
|
||||
@@ -174,19 +157,6 @@ impl IntoAccelerator for &str {
|
||||
}
|
||||
}
|
||||
|
||||
impl ShortcutHandle {
|
||||
/// Remove the shortcut.
|
||||
pub fn remove(&self) {
|
||||
self.desktop.remove_shortcut(self.shortcut_id);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ShortcutHandle {
|
||||
fn drop(&mut self) {
|
||||
self.remove()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoModifersState {
|
||||
fn into_modifiers_state(self) -> Modifiers;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user