Fix non tokio builds for desktop
This commit is contained in:
15 files changed
+274
-437
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user