mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 20:53:06 +00:00
remove rc around webview and add type for desktopcontext
This commit is contained in:
parent
64575eaee2
commit
d6b9d8f5b6
4 changed files with 30 additions and 29 deletions
|
@ -31,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) -> &Rc<DesktopContext> {
|
pub fn use_window(cx: &ScopeState) -> &DesktopContext {
|
||||||
cx.use_hook(|| cx.consume_context::<Rc<DesktopContext>>())
|
cx.use_hook(|| cx.consume_context::<DesktopContext>())
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
@ -51,10 +51,9 @@ pub(crate) type WebviewQueue = Rc<RefCell<Vec<WebviewHandler>>>;
|
||||||
/// ```rust, ignore
|
/// ```rust, ignore
|
||||||
/// let desktop = cx.consume_context::<DesktopContext>().unwrap();
|
/// let desktop = cx.consume_context::<DesktopContext>().unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone)]
|
pub struct DesktopService {
|
||||||
pub struct DesktopContext {
|
|
||||||
/// The wry/tao proxy to the current window
|
/// The wry/tao proxy to the current window
|
||||||
pub webview: Rc<WebView>,
|
pub webview: WebView,
|
||||||
|
|
||||||
/// The proxy to the event loop
|
/// The proxy to the event loop
|
||||||
pub proxy: ProxyType,
|
pub proxy: ProxyType,
|
||||||
|
@ -74,8 +73,10 @@ pub struct DesktopContext {
|
||||||
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
|
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type DesktopContext = Rc<DesktopService>;
|
||||||
|
|
||||||
/// A smart pointer to the current window.
|
/// A smart pointer to the current window.
|
||||||
impl std::ops::Deref for DesktopContext {
|
impl std::ops::Deref for DesktopService {
|
||||||
type Target = Window;
|
type Target = Window;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
|
@ -83,9 +84,9 @@ impl std::ops::Deref for DesktopContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DesktopContext {
|
impl DesktopService {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
webview: Rc<WebView>,
|
webview: WebView,
|
||||||
proxy: ProxyType,
|
proxy: ProxyType,
|
||||||
event_loop: EventLoopWindowTarget<UserWindowEvent>,
|
event_loop: EventLoopWindowTarget<UserWindowEvent>,
|
||||||
webviews: WebviewQueue,
|
webviews: WebviewQueue,
|
||||||
|
@ -112,7 +113,7 @@ 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) -> Weak<DesktopContext> {
|
pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> Weak<DesktopService> {
|
||||||
let window = create_new_window(
|
let window = create_new_window(
|
||||||
cfg,
|
cfg,
|
||||||
&self.event_loop,
|
&self.event_loop,
|
||||||
|
@ -126,10 +127,10 @@ impl DesktopContext {
|
||||||
let desktop_context = window
|
let desktop_context = window
|
||||||
.dom
|
.dom
|
||||||
.base_scope()
|
.base_scope()
|
||||||
.consume_context::<Rc<DesktopContext>>()
|
.consume_context::<Rc<DesktopService>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let id = window.webview.window().id();
|
let id = window.desktop_context.webview.window().id();
|
||||||
|
|
||||||
self.proxy
|
self.proxy
|
||||||
.send_event(UserWindowEvent(EventData::NewWindow, id))
|
.send_event(UserWindowEvent(EventData::NewWindow, id))
|
||||||
|
|
|
@ -15,9 +15,11 @@ mod webview;
|
||||||
|
|
||||||
pub use cfg::Config;
|
pub use cfg::Config;
|
||||||
pub use desktop_context::{
|
pub use desktop_context::{
|
||||||
use_window, use_wry_event_handler, DesktopContext, WryEventHandler, WryEventHandlerId,
|
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_core::*;
|
||||||
use dioxus_html::HtmlEvent;
|
use dioxus_html::HtmlEvent;
|
||||||
pub use eval::{use_eval, EvalResult};
|
pub use eval::{use_eval, EvalResult};
|
||||||
|
@ -186,7 +188,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
|
||||||
Event::NewEvents(StartCause::Init)
|
Event::NewEvents(StartCause::Init)
|
||||||
| Event::UserEvent(UserWindowEvent(EventData::NewWindow, _)) => {
|
| Event::UserEvent(UserWindowEvent(EventData::NewWindow, _)) => {
|
||||||
for handler in queue.borrow_mut().drain(..) {
|
for handler in queue.borrow_mut().drain(..) {
|
||||||
let id = handler.webview.window().id();
|
let id = handler.desktop_context.webview.window().id();
|
||||||
webviews.insert(id, handler);
|
webviews.insert(id, handler);
|
||||||
_ = proxy.send_event(UserWindowEvent(EventData::Poll, id));
|
_ = proxy.send_event(UserWindowEvent(EventData::Poll, id));
|
||||||
}
|
}
|
||||||
|
@ -232,12 +234,12 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
|
||||||
view.dom
|
view.dom
|
||||||
.handle_event(&evt.name, evt.data.into_any(), evt.element, evt.bubbles);
|
.handle_event(&evt.name, evt.data.into_any(), evt.element, evt.bubbles);
|
||||||
|
|
||||||
send_edits(view.dom.render_immediate(), &view.webview);
|
send_edits(view.dom.render_immediate(), &view.desktop_context.webview);
|
||||||
}
|
}
|
||||||
|
|
||||||
EventData::Ipc(msg) if msg.method() == "initialize" => {
|
EventData::Ipc(msg) if msg.method() == "initialize" => {
|
||||||
let view = webviews.get_mut(&event.1).unwrap();
|
let view = webviews.get_mut(&event.1).unwrap();
|
||||||
send_edits(view.dom.rebuild(), &view.webview);
|
send_edits(view.dom.rebuild(), &view.desktop_context.webview);
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the webview chirps back with the result of the eval, we send it to the active receiver
|
// When the webview chirps back with the result of the eval, we send it to the active receiver
|
||||||
|
@ -248,7 +250,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
|
||||||
webviews[&event.1]
|
webviews[&event.1]
|
||||||
.dom
|
.dom
|
||||||
.base_scope()
|
.base_scope()
|
||||||
.consume_context::<Rc<DesktopContext>>()
|
.consume_context::<DesktopContext>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.eval
|
.eval
|
||||||
.send(msg.params())
|
.send(msg.params())
|
||||||
|
@ -284,8 +286,8 @@ fn create_new_window(
|
||||||
shortcut_manager: ShortcutRegistry,
|
shortcut_manager: ShortcutRegistry,
|
||||||
) -> WebviewHandler {
|
) -> WebviewHandler {
|
||||||
let webview = webview::build(&mut cfg, event_loop, proxy.clone());
|
let webview = webview::build(&mut cfg, event_loop, proxy.clone());
|
||||||
let desktop_context = Rc::from(DesktopContext::new(
|
let desktop_context = Rc::from(DesktopService::new(
|
||||||
webview.clone(),
|
webview,
|
||||||
proxy.clone(),
|
proxy.clone(),
|
||||||
event_loop.clone(),
|
event_loop.clone(),
|
||||||
queue.clone(),
|
queue.clone(),
|
||||||
|
@ -295,12 +297,12 @@ fn create_new_window(
|
||||||
|
|
||||||
dom.base_scope().provide_context(desktop_context.clone());
|
dom.base_scope().provide_context(desktop_context.clone());
|
||||||
|
|
||||||
let id = webview.window().id();
|
let id = desktop_context.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,
|
desktop_context,
|
||||||
dom,
|
dom,
|
||||||
waker: waker::tao_waker(proxy, id),
|
waker: waker::tao_waker(proxy, id),
|
||||||
}
|
}
|
||||||
|
@ -308,7 +310,7 @@ fn create_new_window(
|
||||||
|
|
||||||
struct WebviewHandler {
|
struct WebviewHandler {
|
||||||
dom: VirtualDom,
|
dom: VirtualDom,
|
||||||
webview: Rc<wry::webview::WebView>,
|
desktop_context: DesktopContext,
|
||||||
waker: Waker,
|
waker: Waker,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -331,7 +333,7 @@ fn poll_vdom(view: &mut WebviewHandler) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
send_edits(view.dom.render_immediate(), &view.webview);
|
send_edits(view.dom.render_immediate(), &view.desktop_context.webview);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ use wry::application::{
|
||||||
keyboard::{KeyCode, ModifiersState},
|
keyboard::{KeyCode, ModifiersState},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{use_window, DesktopContext};
|
use crate::{desktop_context::DesktopContext, use_window};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct ShortcutRegistry {
|
pub(crate) struct ShortcutRegistry {
|
||||||
|
@ -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: Rc<DesktopContext>,
|
desktop: DesktopContext,
|
||||||
/// The id of the shortcut
|
/// The id of the shortcut
|
||||||
pub shortcut_id: ShortcutId,
|
pub shortcut_id: ShortcutId,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use crate::desktop_context::EventData;
|
use crate::desktop_context::EventData;
|
||||||
use crate::protocol;
|
use crate::protocol;
|
||||||
use crate::{desktop_context::UserWindowEvent, Config};
|
use crate::{desktop_context::UserWindowEvent, Config};
|
||||||
|
@ -13,7 +11,7 @@ pub fn build(
|
||||||
cfg: &mut Config,
|
cfg: &mut Config,
|
||||||
event_loop: &EventLoopWindowTarget<UserWindowEvent>,
|
event_loop: &EventLoopWindowTarget<UserWindowEvent>,
|
||||||
proxy: EventLoopProxy<UserWindowEvent>,
|
proxy: EventLoopProxy<UserWindowEvent>,
|
||||||
) -> Rc<WebView> {
|
) -> WebView {
|
||||||
let builder = cfg.window.clone();
|
let builder = cfg.window.clone();
|
||||||
let window = builder.build(event_loop).unwrap();
|
let window = builder.build(event_loop).unwrap();
|
||||||
let file_handler = cfg.file_drop_handler.take();
|
let file_handler = cfg.file_drop_handler.take();
|
||||||
|
@ -82,5 +80,5 @@ pub fn build(
|
||||||
webview = webview.with_devtools(true);
|
webview = webview.with_devtools(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rc::new(webview.build().unwrap())
|
webview.build().unwrap()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue