turn desktop context into Rc<DesktopContext>

This commit is contained in:
Andrew Collins committed 2023-04-11 11:03:46 -04:00
1 parent a5c46b0e5c
commit 0e1f015b78
3 files changed
+24 -19

No files matched your search

+13 -6
View File
@@ -1,5 +1,6 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc; use std::rc::Rc;
use std::rc::Weak;
use crate::create_new_window; use crate::create_new_window;
use crate::eval::EvalResult; use crate::eval::EvalResult;
@@ -30,8 +31,8 @@ use wry::webview::WebView;
pub type ProxyType = EventLoopProxy<UserWindowEvent>; pub type ProxyType = EventLoopProxy<UserWindowEvent>;
/// Get an imperative handle to the current window /// Get an imperative handle to the current window
pub fn use_window(cx: &ScopeState) -> &DesktopContext { pub fn use_window(cx: &ScopeState) -> &Rc<DesktopContext> {
cx.use_hook(|| cx.consume_context::<DesktopContext>()) cx.use_hook(|| cx.consume_context::<Rc<DesktopContext>>())
.as_ref() .as_ref()
.unwrap() .unwrap()
} }
@@ -111,8 +112,8 @@ impl DesktopContext {
/// You can use this to control other windows from the current window. /// You can use this to control other windows from the current window.
/// ///
/// Be careful to not create a cycle of windows, or you might leak memory. /// Be careful to not create a cycle of windows, or you might leak memory.
pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> DesktopContext { pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> Weak<DesktopContext> {
let (window, desktop_context) = create_new_window( let window = create_new_window(
cfg, cfg,
&self.event_loop, &self.event_loop,
&self.proxy, &self.proxy,
@@ -122,6 +123,12 @@ impl DesktopContext {
self.shortcut_manager.clone(), self.shortcut_manager.clone(),
); );
let desktop_context = window
.dom
.base_scope()
.consume_context::<Rc<DesktopContext>>()
.unwrap();
let id = window.webview.window().id(); let id = window.webview.window().id();
self.proxy self.proxy
@@ -134,7 +141,7 @@ impl DesktopContext {
self.pending_windows.borrow_mut().push(window); self.pending_windows.borrow_mut().push(window);
desktop_context Rc::downgrade(&desktop_context)
} }
/// trigger the drag-window event /// trigger the drag-window event
@@ -420,7 +427,7 @@ pub fn use_wry_event_handler(
let id = desktop.create_wry_event_handler(handler); let id = desktop.create_wry_event_handler(handler);
WryEventHandler { WryEventHandler {
handlers: desktop.event_handlers, handlers: desktop.event_handlers.clone(),
id, id,
} }
}) })
+10 -12
View File
@@ -144,7 +144,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
let shortcut_manager = ShortcutRegistry::new(&event_loop); let shortcut_manager = ShortcutRegistry::new(&event_loop);
let (web_view, _) = create_new_window( let web_view = create_new_window(
cfg, cfg,
&event_loop, &event_loop,
&proxy, &proxy,
@@ -282,30 +282,28 @@ fn create_new_window(
queue: &WebviewQueue, queue: &WebviewQueue,
event_handlers: &WindowEventHandlers, event_handlers: &WindowEventHandlers,
shortcut_manager: ShortcutRegistry, shortcut_manager: ShortcutRegistry,
) -> (WebviewHandler, DesktopContext) { ) -> WebviewHandler {
let webview = webview::build(&mut cfg, event_loop, proxy.clone()); let webview = webview::build(&mut cfg, event_loop, proxy.clone());
let desktop_context = DesktopContext::new( let desktop_context = Rc::from(DesktopContext::new(
webview.clone(), webview.clone(),
proxy.clone(), proxy.clone(),
event_loop.clone(), event_loop.clone(),
queue.clone(), queue.clone(),
event_handlers.clone(), event_handlers.clone(),
shortcut_manager, shortcut_manager,
); ));
dom.base_scope().provide_context(desktop_context.clone()); dom.base_scope().provide_context(desktop_context.clone());
let id = webview.window().id(); let id = webview.window().id();
// We want to poll the virtualdom and the event loop at the same time, so the waker will be connected to both // We want to poll the virtualdom and the event loop at the same time, so the waker will be connected to both
(
WebviewHandler { WebviewHandler {
webview, webview,
dom, dom,
waker: waker::tao_waker(proxy, id), waker: waker::tao_waker(proxy, id),
}, }
desktop_context,
)
} }
struct WebviewHandler { struct WebviewHandler {
+1 -1
View File
@@ -146,7 +146,7 @@ pub struct ShortcutId {
/// A global shortcut. This will be automatically removed when it is dropped. /// A global shortcut. This will be automatically removed when it is dropped.
pub struct ShortcutHandle { pub struct ShortcutHandle {
desktop: DesktopContext, desktop: Rc<DesktopContext>,
/// The id of the shortcut /// The id of the shortcut
pub shortcut_id: ShortcutId, pub shortcut_id: ShortcutId,
} }