diff --git a/examples/shortcut.rs b/examples/shortcut.rs index 0d8ffae81..53c3aace3 100644 --- a/examples/shortcut.rs +++ b/examples/shortcut.rs @@ -1,5 +1,4 @@ use dioxus::prelude::*; -use dioxus_desktop::tao::keyboard::ModifiersState; use dioxus_desktop::use_global_shortcut; fn main() { @@ -8,6 +7,7 @@ fn main() { fn app(cx: Scope) -> Element { let toggled = use_state(cx, || false); + use_global_shortcut(cx, "ctrl+s", { to_owned![toggled]; move || toggled.modify(|t| !*t) diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index a82a6cee8..1caa0da64 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -72,6 +72,7 @@ pub struct DesktopService { pub(crate) views: Rc>>, } +/// A handle to the [`DesktopService`] that can be passed around. pub type DesktopContext = Rc; /// A smart pointer to the current window. diff --git a/packages/desktop/src/element.rs b/packages/desktop/src/element.rs index 94db28f0b..0de5304ba 100644 --- a/packages/desktop/src/element.rs +++ b/packages/desktop/src/element.rs @@ -1,20 +1,17 @@ -use std::rc::Rc; - use dioxus_core::ElementId; use dioxus_html::{geometry::euclid::Rect, MountedResult, RenderedElementBacking}; -use wry::webview::WebView; -use crate::query::QueryEngine; +use crate::{desktop_context::DesktopContext, query::QueryEngine}; /// A mounted element passed to onmounted events pub struct DesktopElement { id: ElementId, - webview: Rc, + webview: DesktopContext, query: QueryEngine, } impl DesktopElement { - pub(crate) fn new(id: ElementId, webview: Rc, query: QueryEngine) -> Self { + pub(crate) fn new(id: ElementId, webview: DesktopContext, query: QueryEngine) -> Self { Self { id, webview, query } } } @@ -37,7 +34,7 @@ impl RenderedElementBacking for DesktopElement { let fut = self .query - .new_query::>>(&script, &self.webview) + .new_query::>>(&script, &self.webview.webview) .resolve(); Box::pin(async move { match fut.await { @@ -64,7 +61,7 @@ impl RenderedElementBacking for DesktopElement { let fut = self .query - .new_query::(&script, &self.webview) + .new_query::(&script, &self.webview.webview) .resolve(); Box::pin(async move { match fut.await { @@ -90,7 +87,7 @@ impl RenderedElementBacking for DesktopElement { let fut = self .query - .new_query::(&script, &self.webview) + .new_query::(&script, &self.webview.webview) .resolve(); Box::pin(async move { diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index fc574dfc0..779122b9c 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -18,12 +18,11 @@ mod webview; use crate::query::QueryResult; pub use cfg::Config; +pub use desktop_context::DesktopContext; pub use desktop_context::{ use_window, use_wry_event_handler, DesktopService, WryEventHandler, WryEventHandlerId, }; -use desktop_context::{ - DesktopContext, EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers, -}; +use desktop_context::{EventData, UserWindowEvent, WebviewQueue, WindowEventHandlers}; use dioxus_core::*; use dioxus_html::MountedData; use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent}; @@ -253,9 +252,11 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) .base_scope() .consume_context::() .unwrap() - .query; + .query + .clone(); - let element = DesktopElement::new(element, view.webview.clone(), query); + let element = + DesktopElement::new(element, view.desktop_context.clone(), query); Rc::new(MountedData::new(element)) } else { @@ -278,7 +279,8 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) .base_scope() .consume_context::() .unwrap() - .query; + .query + .clone(); query.send(result); } @@ -324,7 +326,7 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) view.dom.handle_event(event_name, data, id, event_bubbles); } - send_edits(view.dom.render_immediate(), &view.webview); + send_edits(view.dom.render_immediate(), &view.desktop_context.webview); } }