Collapse objects into single shared object
This commit is contained in:
7 files changed
+203
-218
No files matched your search
+50
-35
@@ -7,8 +7,9 @@ use crate::{
|
|||||||
ipc::IpcMessage,
|
ipc::IpcMessage,
|
||||||
query::QueryResult,
|
query::QueryResult,
|
||||||
shortcut::{GlobalHotKeyEvent, ShortcutRegistry},
|
shortcut::{GlobalHotKeyEvent, ShortcutRegistry},
|
||||||
webview::WebviewHandler,
|
webview::WebviewInstance,
|
||||||
};
|
};
|
||||||
|
use crossbeam_channel::Receiver;
|
||||||
use dioxus_core::{Component, ElementId, VirtualDom};
|
use dioxus_core::{Component, ElementId, VirtualDom};
|
||||||
use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent, MountedData};
|
use dioxus_html::{native_bind::NativeFileEngine, FormData, HtmlEvent, MountedData};
|
||||||
use futures_util::{pin_mut, FutureExt};
|
use futures_util::{pin_mut, FutureExt};
|
||||||
@@ -21,20 +22,28 @@ use tao::{
|
|||||||
|
|
||||||
pub(crate) struct App<P> {
|
pub(crate) struct App<P> {
|
||||||
// move the props into a cell so we can pop it out later to create the first window
|
// move the props into a cell so we can pop it out later to create the first window
|
||||||
// iOS panics if we create a window before the event loop is started
|
// iOS panics if we create a window before the event loop is started, so we toss them into a cell
|
||||||
pub(crate) props: Rc<Cell<Option<P>>>,
|
pub(crate) props: Cell<Option<P>>,
|
||||||
pub(crate) cfg: Rc<Cell<Option<Config>>>,
|
pub(crate) cfg: Cell<Option<Config>>,
|
||||||
|
|
||||||
pub(crate) root: Component<P>,
|
// Stuff we need mutable access to
|
||||||
pub(crate) webviews: HashMap<WindowId, WebviewHandler>,
|
|
||||||
pub(crate) event_handlers: WindowEventHandlers,
|
|
||||||
pub(crate) queue: WebviewQueue,
|
|
||||||
pub(crate) shortcut_manager: ShortcutRegistry,
|
|
||||||
pub(crate) global_hotkey_channel: crossbeam_channel::Receiver<GlobalHotKeyEvent>,
|
|
||||||
pub(crate) proxy: EventLoopProxy<UserWindowEvent>,
|
|
||||||
pub(crate) window_behavior: WindowCloseBehaviour,
|
|
||||||
pub(crate) control_flow: ControlFlow,
|
pub(crate) control_flow: ControlFlow,
|
||||||
pub(crate) is_visible_before_start: bool,
|
pub(crate) is_visible_before_start: bool,
|
||||||
|
pub(crate) root: Component<P>,
|
||||||
|
pub(crate) webviews: HashMap<WindowId, WebviewInstance>,
|
||||||
|
pub(crate) window_behavior: WindowCloseBehaviour,
|
||||||
|
|
||||||
|
pub(crate) shared: SharedContext,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SharedContext {
|
||||||
|
pub(crate) event_handlers: WindowEventHandlers,
|
||||||
|
pub(crate) pending_webviews: WebviewQueue,
|
||||||
|
pub(crate) shortcut_manager: ShortcutRegistry,
|
||||||
|
pub(crate) global_hotkey_channel: Receiver<GlobalHotKeyEvent>,
|
||||||
|
pub(crate) proxy: EventLoopProxy<UserWindowEvent>,
|
||||||
|
pub(crate) target: EventLoopWindowTarget<UserWindowEvent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P: 'static> App<P> {
|
impl<P: 'static> App<P> {
|
||||||
@@ -46,14 +55,17 @@ impl<P: 'static> App<P> {
|
|||||||
window_behavior: cfg.last_window_close_behaviour,
|
window_behavior: cfg.last_window_close_behaviour,
|
||||||
is_visible_before_start: true,
|
is_visible_before_start: true,
|
||||||
webviews: HashMap::new(),
|
webviews: HashMap::new(),
|
||||||
event_handlers: WindowEventHandlers::default(),
|
|
||||||
queue: WebviewQueue::default(),
|
|
||||||
shortcut_manager: ShortcutRegistry::new(),
|
|
||||||
global_hotkey_channel: GlobalHotKeyEvent::receiver().clone(),
|
|
||||||
proxy: event_loop.create_proxy(),
|
|
||||||
props: Rc::new(Cell::new(Some(props))),
|
|
||||||
cfg: Rc::new(Cell::new(Some(cfg))),
|
|
||||||
control_flow: ControlFlow::Wait,
|
control_flow: ControlFlow::Wait,
|
||||||
|
props: Cell::new(Some(props)),
|
||||||
|
cfg: Cell::new(Some(cfg)),
|
||||||
|
shared: SharedContext {
|
||||||
|
event_handlers: WindowEventHandlers::default(),
|
||||||
|
pending_webviews: WebviewQueue::default(),
|
||||||
|
shortcut_manager: ShortcutRegistry::new(),
|
||||||
|
global_hotkey_channel: GlobalHotKeyEvent::receiver().clone(),
|
||||||
|
proxy: event_loop.create_proxy(),
|
||||||
|
target: event_loop.clone(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(all(feature = "hot-reload", debug_assertions))]
|
#[cfg(all(feature = "hot-reload", debug_assertions))]
|
||||||
@@ -69,18 +81,21 @@ impl<P: 'static> App<P> {
|
|||||||
) {
|
) {
|
||||||
self.control_flow = ControlFlow::Wait;
|
self.control_flow = ControlFlow::Wait;
|
||||||
|
|
||||||
self.event_handlers.apply_event(window_event, event_loop);
|
self.shared
|
||||||
|
.event_handlers
|
||||||
|
.apply_event(window_event, event_loop);
|
||||||
|
|
||||||
_ = self
|
_ = self
|
||||||
|
.shared
|
||||||
.global_hotkey_channel
|
.global_hotkey_channel
|
||||||
.try_recv()
|
.try_recv()
|
||||||
.map(|event| self.shortcut_manager.call_handlers(event));
|
.map(|event| self.shared.shortcut_manager.call_handlers(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(feature = "hot-reload", debug_assertions))]
|
#[cfg(all(feature = "hot-reload", debug_assertions))]
|
||||||
pub fn connect_hotreload(&mut self) {
|
pub fn connect_hotreload(&mut self) {
|
||||||
let proxy = self.proxy.clone();
|
|
||||||
dioxus_hot_reload::connect({
|
dioxus_hot_reload::connect({
|
||||||
|
let proxy = self.shared.proxy.clone();
|
||||||
move |template| {
|
move |template| {
|
||||||
let _ = proxy.send_event(UserWindowEvent(
|
let _ = proxy.send_event(UserWindowEvent(
|
||||||
EventData::HotReloadEvent(template),
|
EventData::HotReloadEvent(template),
|
||||||
@@ -92,10 +107,13 @@ impl<P: 'static> App<P> {
|
|||||||
|
|
||||||
//
|
//
|
||||||
pub fn handle_new_window(&mut self) {
|
pub fn handle_new_window(&mut self) {
|
||||||
for handler in self.queue.borrow_mut().drain(..) {
|
for handler in self.shared.pending_webviews.borrow_mut().drain(..) {
|
||||||
let id = handler.desktop_context.window.id();
|
let id = handler.desktop_context.window.id();
|
||||||
self.webviews.insert(id, handler);
|
self.webviews.insert(id, handler);
|
||||||
_ = self.proxy.send_event(UserWindowEvent(EventData::Poll, id));
|
_ = self
|
||||||
|
.shared
|
||||||
|
.proxy
|
||||||
|
.send_event(UserWindowEvent(EventData::Poll, id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,28 +154,25 @@ impl<P: 'static> App<P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_start_cause_init(&mut self, target: &EventLoopWindowTarget<UserWindowEvent>) {
|
pub fn handle_start_cause_init(&mut self) {
|
||||||
let props = self.props.take().unwrap();
|
let props = self.props.take().unwrap();
|
||||||
let cfg = self.cfg.take().unwrap();
|
let cfg = self.cfg.take().unwrap();
|
||||||
|
|
||||||
let dom = VirtualDom::new_with_props(self.root, props);
|
|
||||||
|
|
||||||
self.is_visible_before_start = cfg.window.window.visible;
|
self.is_visible_before_start = cfg.window.window.visible;
|
||||||
|
|
||||||
let handler = crate::webview::create_new_window(
|
let handler = WebviewInstance::new(
|
||||||
cfg,
|
cfg,
|
||||||
dom,
|
VirtualDom::new_with_props(self.root, props),
|
||||||
target,
|
self.shared.clone(),
|
||||||
&self.proxy,
|
|
||||||
&self.queue,
|
|
||||||
&self.event_handlers,
|
|
||||||
self.shortcut_manager.clone(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let id = handler.desktop_context.window.id();
|
let id = handler.desktop_context.window.id();
|
||||||
self.webviews.insert(id, handler);
|
self.webviews.insert(id, handler);
|
||||||
|
|
||||||
_ = self.proxy.send_event(UserWindowEvent(EventData::Poll, id));
|
_ = self
|
||||||
|
.shared
|
||||||
|
.proxy
|
||||||
|
.send_event(UserWindowEvent(EventData::Poll, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_browser_open(&mut self, msg: IpcMessage) {
|
pub fn handle_browser_open(&mut self, msg: IpcMessage) {
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use crate::ipc::IpcMessage;
|
|
||||||
use crate::query::QueryEngine;
|
|
||||||
use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError};
|
use crate::shortcut::{HotKey, ShortcutId, ShortcutRegistry, ShortcutRegistryError};
|
||||||
use crate::AssetHandler;
|
use crate::AssetHandler;
|
||||||
use crate::Config;
|
use crate::Config;
|
||||||
|
use crate::{app::SharedContext, ipc::IpcMessage};
|
||||||
use crate::{assets::AssetFuture, edits::WebviewQueue};
|
use crate::{assets::AssetFuture, edits::WebviewQueue};
|
||||||
use crate::{assets::AssetHandlerRegistry, edits::EditQueue};
|
use crate::{assets::AssetHandlerRegistry, edits::EditQueue};
|
||||||
|
use crate::{query::QueryEngine, webview::WebviewInstance};
|
||||||
use dioxus_core::Mutations;
|
use dioxus_core::Mutations;
|
||||||
use dioxus_core::VirtualDom;
|
use dioxus_core::VirtualDom;
|
||||||
use dioxus_interpreter_js::binary_protocol::Channel;
|
use dioxus_interpreter_js::binary_protocol::Channel;
|
||||||
@@ -52,21 +52,18 @@ pub struct DesktopService {
|
|||||||
/// The tao window itself
|
/// The tao window itself
|
||||||
pub window: Window,
|
pub window: Window,
|
||||||
|
|
||||||
/// The proxy to the event loop
|
|
||||||
pub proxy: EventLoopProxy<UserWindowEvent>,
|
|
||||||
|
|
||||||
/// The receiver for queries about the current window
|
/// The receiver for queries about the current window
|
||||||
pub(super) query: QueryEngine,
|
pub(super) query: QueryEngine,
|
||||||
|
|
||||||
pub(super) pending_windows: WebviewQueue,
|
pub(crate) shared: SharedContext,
|
||||||
|
|
||||||
pub(crate) event_loop: EventLoopWindowTarget<UserWindowEvent>,
|
// pub(crate) event_handlers: WindowEventHandlers,
|
||||||
|
// pub(super) pending_windows: WebviewQueue,
|
||||||
pub(crate) event_handlers: WindowEventHandlers,
|
// pub(crate) shortcut_manager: ShortcutRegistry,
|
||||||
|
|
||||||
pub(crate) shortcut_manager: ShortcutRegistry,
|
|
||||||
|
|
||||||
|
// pub(crate) event_loop: EventLoopWindowTarget<UserWindowEvent>,
|
||||||
pub(crate) edit_queue: EditQueue,
|
pub(crate) edit_queue: EditQueue,
|
||||||
|
|
||||||
pub(crate) templates: RefCell<FxHashMap<String, u16>>,
|
pub(crate) templates: RefCell<FxHashMap<String, u16>>,
|
||||||
pub(crate) max_template_count: AtomicU16,
|
pub(crate) max_template_count: AtomicU16,
|
||||||
|
|
||||||
@@ -90,28 +87,20 @@ impl DesktopService {
|
|||||||
pub(crate) fn new(
|
pub(crate) fn new(
|
||||||
window: Window,
|
window: Window,
|
||||||
webview: WebView,
|
webview: WebView,
|
||||||
proxy: EventLoopProxy<UserWindowEvent>,
|
shared: SharedContext,
|
||||||
event_loop: EventLoopWindowTarget<UserWindowEvent>,
|
|
||||||
webviews: WebviewQueue,
|
|
||||||
event_handlers: WindowEventHandlers,
|
|
||||||
shortcut_manager: ShortcutRegistry,
|
|
||||||
edit_queue: EditQueue,
|
edit_queue: EditQueue,
|
||||||
asset_handlers: AssetHandlerRegistry,
|
asset_handlers: AssetHandlerRegistry,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
window,
|
window,
|
||||||
webview,
|
webview,
|
||||||
proxy,
|
shared,
|
||||||
event_loop,
|
|
||||||
query: Default::default(),
|
|
||||||
pending_windows: webviews,
|
|
||||||
event_handlers,
|
|
||||||
shortcut_manager,
|
|
||||||
edit_queue,
|
edit_queue,
|
||||||
|
asset_handlers,
|
||||||
|
query: Default::default(),
|
||||||
templates: Default::default(),
|
templates: Default::default(),
|
||||||
max_template_count: Default::default(),
|
max_template_count: Default::default(),
|
||||||
channel: Default::default(),
|
channel: Default::default(),
|
||||||
asset_handlers,
|
|
||||||
#[cfg(target_os = "ios")]
|
#[cfg(target_os = "ios")]
|
||||||
views: Default::default(),
|
views: Default::default(),
|
||||||
}
|
}
|
||||||
@@ -136,27 +125,21 @@ impl DesktopService {
|
|||||||
///
|
///
|
||||||
/// 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<DesktopService> {
|
pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> Weak<DesktopService> {
|
||||||
let window = crate::webview::create_new_window(
|
let window = WebviewInstance::new(cfg, dom, self.shared.clone());
|
||||||
cfg,
|
|
||||||
dom,
|
|
||||||
&self.event_loop,
|
|
||||||
&self.proxy,
|
|
||||||
&self.pending_windows,
|
|
||||||
&self.event_handlers,
|
|
||||||
self.shortcut_manager.clone(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let cx = window.desktop_context.clone();
|
let cx = window.desktop_context.clone();
|
||||||
|
|
||||||
self.proxy
|
self.shared
|
||||||
|
.proxy
|
||||||
.send_event(UserWindowEvent(EventData::NewWindow, cx.id()))
|
.send_event(UserWindowEvent(EventData::NewWindow, cx.id()))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.proxy
|
self.shared
|
||||||
|
.proxy
|
||||||
.send_event(UserWindowEvent(EventData::Poll, cx.id()))
|
.send_event(UserWindowEvent(EventData::Poll, cx.id()))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.pending_windows.borrow_mut().push(window);
|
self.shared.pending_webviews.borrow_mut().push(window);
|
||||||
|
|
||||||
Rc::downgrade(&cx)
|
Rc::downgrade(&cx)
|
||||||
}
|
}
|
||||||
@@ -188,6 +171,7 @@ impl DesktopService {
|
|||||||
/// close window
|
/// close window
|
||||||
pub fn close(&self) {
|
pub fn close(&self) {
|
||||||
let _ = self
|
let _ = self
|
||||||
|
.shared
|
||||||
.proxy
|
.proxy
|
||||||
.send_event(UserWindowEvent(EventData::CloseWindow, self.id()));
|
.send_event(UserWindowEvent(EventData::CloseWindow, self.id()));
|
||||||
}
|
}
|
||||||
@@ -195,6 +179,7 @@ impl DesktopService {
|
|||||||
/// close window
|
/// close window
|
||||||
pub fn close_window(&self, id: WindowId) {
|
pub fn close_window(&self, id: WindowId) {
|
||||||
let _ = self
|
let _ = self
|
||||||
|
.shared
|
||||||
.proxy
|
.proxy
|
||||||
.send_event(UserWindowEvent(EventData::CloseWindow, id));
|
.send_event(UserWindowEvent(EventData::CloseWindow, id));
|
||||||
}
|
}
|
||||||
@@ -237,12 +222,12 @@ impl DesktopService {
|
|||||||
&self,
|
&self,
|
||||||
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
handler: impl FnMut(&Event<UserWindowEvent>, &EventLoopWindowTarget<UserWindowEvent>) + 'static,
|
||||||
) -> WryEventHandlerId {
|
) -> WryEventHandlerId {
|
||||||
self.event_handlers.add(self.id(), handler)
|
self.shared.event_handlers.add(self.window.id(), handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a wry event handler created with [`DesktopContext::create_wry_event_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: WryEventHandlerId) {
|
||||||
self.event_handlers.remove(id)
|
self.shared.event_handlers.remove(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a global shortcut
|
/// Create a global shortcut
|
||||||
@@ -253,18 +238,19 @@ impl DesktopService {
|
|||||||
hotkey: HotKey,
|
hotkey: HotKey,
|
||||||
callback: impl FnMut() + 'static,
|
callback: impl FnMut() + 'static,
|
||||||
) -> Result<ShortcutId, ShortcutRegistryError> {
|
) -> Result<ShortcutId, ShortcutRegistryError> {
|
||||||
self.shortcut_manager
|
self.shared
|
||||||
|
.shortcut_manager
|
||||||
.add_shortcut(hotkey, Box::new(callback))
|
.add_shortcut(hotkey, Box::new(callback))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a global shortcut
|
/// Remove a global shortcut
|
||||||
pub fn remove_shortcut(&self, id: ShortcutId) {
|
pub fn remove_shortcut(&self, id: ShortcutId) {
|
||||||
self.shortcut_manager.remove_shortcut(id)
|
self.shared.shortcut_manager.remove_shortcut(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove all global shortcuts
|
/// Remove all global shortcuts
|
||||||
pub fn remove_all_shortcuts(&self) {
|
pub fn remove_all_shortcuts(&self) {
|
||||||
self.shortcut_manager.remove_all()
|
self.shared.shortcut_manager.remove_all()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Provide a callback to handle asset loading yourself.
|
/// Provide a callback to handle asset loading yourself.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::webview::WebviewHandler;
|
use crate::webview::WebviewInstance;
|
||||||
use dioxus_core::Mutations;
|
use dioxus_core::Mutations;
|
||||||
use dioxus_core::{BorrowedAttributeValue, Template, TemplateAttribute, TemplateNode};
|
use dioxus_core::{BorrowedAttributeValue, Template, TemplateAttribute, TemplateNode};
|
||||||
use dioxus_html::event_bubbles;
|
use dioxus_html::event_bubbles;
|
||||||
@@ -14,7 +14,7 @@ use std::{
|
|||||||
|
|
||||||
use wry::RequestAsyncResponder;
|
use wry::RequestAsyncResponder;
|
||||||
|
|
||||||
pub(crate) type WebviewQueue = Rc<RefCell<Vec<WebviewHandler>>>;
|
pub(crate) type WebviewQueue = Rc<RefCell<Vec<WebviewInstance>>>;
|
||||||
|
|
||||||
/// This handles communication between the requests that the webview makes and the interpreter. The interpreter
|
/// This handles communication between the requests that the webview makes and the interpreter. The interpreter
|
||||||
/// constantly makes long running requests to the webview to get any edits that should be made to the DOM almost like
|
/// constantly makes long running requests to the webview to get any edits that should be made to the DOM almost like
|
||||||
|
|||||||
@@ -23,7 +23,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.clone(),
|
handlers: desktop.shared.event_handlers.clone(),
|
||||||
id,
|
id,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,13 +1,15 @@
|
|||||||
//! Convert a serialized event to an event trigger
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// A message struct that manages the communication between the webview and the eventloop code
|
||||||
|
///
|
||||||
|
/// This needs to be serializable across the JS boundary, so the method names and structs are sensitive.
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
pub struct IpcMessage {
|
pub struct IpcMessage {
|
||||||
method: String,
|
method: String,
|
||||||
params: serde_json::Value,
|
params: serde_json::Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A set of known messages that we need to respond to
|
||||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
pub enum IpcMethod<'a> {
|
pub enum IpcMethod<'a> {
|
||||||
FileDialog,
|
FileDialog,
|
||||||
@@ -21,7 +23,7 @@ pub enum IpcMethod<'a> {
|
|||||||
impl IpcMessage {
|
impl IpcMessage {
|
||||||
pub(crate) fn method(&self) -> IpcMethod {
|
pub(crate) fn method(&self) -> IpcMethod {
|
||||||
match self.method.as_str() {
|
match self.method.as_str() {
|
||||||
// todo: this is a misspelling
|
// todo: this is a misspelling, needs to be fixed
|
||||||
"file_diolog" => IpcMethod::FileDialog,
|
"file_diolog" => IpcMethod::FileDialog,
|
||||||
"user_event" => IpcMethod::UserEvent,
|
"user_event" => IpcMethod::UserEvent,
|
||||||
"query" => IpcMethod::Query,
|
"query" => IpcMethod::Query,
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ pub fn launch_with_props_blocking<P: 'static>(root: Component<P>, props: P, cfg:
|
|||||||
app.tick(&window_event, event_loop);
|
app.tick(&window_event, event_loop);
|
||||||
|
|
||||||
match window_event {
|
match window_event {
|
||||||
Event::NewEvents(StartCause::Init) => app.handle_start_cause_init(event_loop),
|
Event::NewEvents(StartCause::Init) => app.handle_start_cause_init(),
|
||||||
Event::WindowEvent {
|
Event::WindowEvent {
|
||||||
event, window_id, ..
|
event, window_id, ..
|
||||||
} => match event {
|
} => match event {
|
||||||
|
|||||||
+118
-136
@@ -1,26 +1,18 @@
|
|||||||
use std::{rc::Rc, task::Waker};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assets::AssetHandlerRegistry, desktop_context::UserWindowEvent, waker::tao_waker, Config,
|
app::SharedContext,
|
||||||
DesktopContext,
|
assets::AssetHandlerRegistry,
|
||||||
};
|
desktop_context::{EventData, UserWindowEvent},
|
||||||
use crate::{
|
edits::EditQueue,
|
||||||
desktop_context::{EventData, WindowEventHandlers},
|
|
||||||
shortcut::ShortcutRegistry,
|
|
||||||
};
|
|
||||||
use crate::{
|
|
||||||
edits::{EditQueue, WebviewQueue},
|
|
||||||
eval::DesktopEvalProvider,
|
eval::DesktopEvalProvider,
|
||||||
};
|
|
||||||
use crate::{
|
|
||||||
protocol::{self},
|
protocol::{self},
|
||||||
DesktopService,
|
waker::tao_waker,
|
||||||
|
Config, DesktopContext, DesktopService,
|
||||||
};
|
};
|
||||||
use dioxus_core::VirtualDom;
|
use dioxus_core::VirtualDom;
|
||||||
use tao::event_loop::{EventLoopProxy, EventLoopWindowTarget};
|
use std::{rc::Rc, task::Waker};
|
||||||
use wry::{WebContext, WebViewBuilder};
|
use wry::{WebContext, WebViewBuilder};
|
||||||
|
|
||||||
pub struct WebviewHandler {
|
pub struct WebviewInstance {
|
||||||
pub dom: VirtualDom,
|
pub dom: VirtualDom,
|
||||||
pub desktop_context: DesktopContext,
|
pub desktop_context: DesktopContext,
|
||||||
pub waker: Waker,
|
pub waker: Waker,
|
||||||
@@ -30,107 +22,100 @@ pub struct WebviewHandler {
|
|||||||
_web_context: WebContext,
|
_web_context: WebContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_new_window(
|
impl WebviewInstance {
|
||||||
mut cfg: Config,
|
pub fn new(mut cfg: Config, dom: VirtualDom, shared: SharedContext) -> WebviewInstance {
|
||||||
dom: VirtualDom,
|
let window = cfg.window.clone().build(&shared.target).unwrap();
|
||||||
event_loop: &EventLoopWindowTarget<UserWindowEvent>,
|
|
||||||
proxy: &EventLoopProxy<UserWindowEvent>,
|
|
||||||
queue: &WebviewQueue,
|
|
||||||
event_handlers: &WindowEventHandlers,
|
|
||||||
shortcut_manager: ShortcutRegistry,
|
|
||||||
) -> WebviewHandler {
|
|
||||||
let window = cfg.window.clone().build(event_loop).unwrap();
|
|
||||||
|
|
||||||
// TODO: allow users to specify their own menubars, again :/
|
// TODO: allow users to specify their own menubars, again :/
|
||||||
if cfg.enable_default_menu_bar {
|
if cfg.enable_default_menu_bar {
|
||||||
use crate::menubar::*;
|
use crate::menubar::*;
|
||||||
build_menu_bar(build_default_menu_bar(), &window);
|
build_menu_bar(build_default_menu_bar(), &window);
|
||||||
}
|
}
|
||||||
|
|
||||||
let window_id = window.id();
|
let window_id = window.id();
|
||||||
let file_handler = cfg.file_drop_handler.take();
|
let file_handler = cfg.file_drop_handler.take();
|
||||||
let custom_head = cfg.custom_head.clone();
|
let custom_head = cfg.custom_head.clone();
|
||||||
let index_file = cfg.custom_index.clone();
|
let index_file = cfg.custom_index.clone();
|
||||||
let root_name = cfg.root_name.clone();
|
let root_name = cfg.root_name.clone();
|
||||||
|
|
||||||
// We assume that if the icon is None in cfg, then the user just didnt set it
|
// We assume that if the icon is None in cfg, then the user just didnt set it
|
||||||
if cfg.window.window.window_icon.is_none() {
|
if cfg.window.window.window_icon.is_none() {
|
||||||
window.set_window_icon(Some(
|
window.set_window_icon(Some(
|
||||||
tao::window::Icon::from_rgba(
|
tao::window::Icon::from_rgba(
|
||||||
include_bytes!("./assets/default_icon.bin").to_vec(),
|
include_bytes!("./assets/default_icon.bin").to_vec(),
|
||||||
460,
|
460,
|
||||||
460,
|
460,
|
||||||
)
|
)
|
||||||
.expect("image parse failed"),
|
.expect("image parse failed"),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut web_context = WebContext::new(cfg.data_dir.clone());
|
let mut web_context = WebContext::new(cfg.data_dir.clone());
|
||||||
let edit_queue = EditQueue::default();
|
let edit_queue = EditQueue::default();
|
||||||
let headless = !cfg.window.window.visible;
|
let headless = !cfg.window.window.visible;
|
||||||
let asset_handlers = AssetHandlerRegistry::new();
|
let asset_handlers = AssetHandlerRegistry::new();
|
||||||
let asset_handlers_ref = asset_handlers.clone();
|
let asset_handlers_ref = asset_handlers.clone();
|
||||||
|
|
||||||
let mut webview = WebViewBuilder::new(&window)
|
let mut webview = WebViewBuilder::new(&window)
|
||||||
.with_transparent(cfg.window.window.transparent)
|
.with_transparent(cfg.window.window.transparent)
|
||||||
.with_url("dioxus://index.html/")
|
.with_url("dioxus://index.html/")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.with_ipc_handler({
|
.with_ipc_handler({
|
||||||
let proxy = proxy.clone();
|
let proxy = shared.proxy.clone();
|
||||||
move |payload: String| {
|
move |payload: String| {
|
||||||
// defer the event to the main thread
|
// defer the event to the main thread
|
||||||
if let Ok(message) = serde_json::from_str(&payload) {
|
if let Ok(message) = serde_json::from_str(&payload) {
|
||||||
_ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window_id));
|
_ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window_id));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
})
|
.with_asynchronous_custom_protocol(String::from("dioxus"), {
|
||||||
.with_asynchronous_custom_protocol(String::from("dioxus"), {
|
|
||||||
let edit_queue = edit_queue.clone();
|
|
||||||
move |request, responder| {
|
|
||||||
let custom_head = custom_head.clone();
|
|
||||||
let index_file = index_file.clone();
|
|
||||||
let root_name = root_name.clone();
|
|
||||||
let asset_handlers_ref = asset_handlers_ref.clone();
|
|
||||||
let edit_queue = edit_queue.clone();
|
let edit_queue = edit_queue.clone();
|
||||||
tokio::spawn(async move {
|
move |request, responder| {
|
||||||
protocol::desktop_handler(
|
let custom_head = custom_head.clone();
|
||||||
request,
|
let index_file = index_file.clone();
|
||||||
custom_head.clone(),
|
let root_name = root_name.clone();
|
||||||
index_file.clone(),
|
let asset_handlers_ref = asset_handlers_ref.clone();
|
||||||
&root_name,
|
let edit_queue = edit_queue.clone();
|
||||||
&asset_handlers_ref,
|
tokio::spawn(async move {
|
||||||
&edit_queue,
|
protocol::desktop_handler(
|
||||||
headless,
|
request,
|
||||||
responder,
|
custom_head.clone(),
|
||||||
)
|
index_file.clone(),
|
||||||
.await;
|
&root_name,
|
||||||
});
|
&asset_handlers_ref,
|
||||||
}
|
&edit_queue,
|
||||||
})
|
headless,
|
||||||
.with_file_drop_handler(move |event| {
|
responder,
|
||||||
file_handler
|
)
|
||||||
.as_ref()
|
.await;
|
||||||
.map(|handler| handler(window_id, event))
|
});
|
||||||
.unwrap_or_default()
|
}
|
||||||
})
|
})
|
||||||
.with_web_context(&mut web_context);
|
.with_file_drop_handler(move |event| {
|
||||||
|
file_handler
|
||||||
|
.as_ref()
|
||||||
|
.map(|handler| handler(window_id, event))
|
||||||
|
.unwrap_or_default()
|
||||||
|
})
|
||||||
|
.with_web_context(&mut web_context);
|
||||||
|
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
// Windows has a platform specific settings to disable the browser shortcut keys
|
// Windows has a platform specific settings to disable the browser shortcut keys
|
||||||
use wry::webview::WebViewBuilderExtWindows;
|
use wry::webview::WebViewBuilderExtWindows;
|
||||||
webview = webview.with_browser_accelerator_keys(false);
|
webview = webview.with_browser_accelerator_keys(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(color) = cfg.background_color {
|
if let Some(color) = cfg.background_color {
|
||||||
webview = webview.with_background_color(color);
|
webview = webview.with_background_color(color);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (name, handler) in cfg.protocols.drain(..) {
|
for (name, handler) in cfg.protocols.drain(..) {
|
||||||
webview = webview.with_custom_protocol(name, move |r| handler(r))
|
webview = webview.with_custom_protocol(name, move |r| handler(r))
|
||||||
}
|
}
|
||||||
|
|
||||||
const INITIALIZATION_SCRIPT: &str = r#"
|
const INITIALIZATION_SCRIPT: &str = r#"
|
||||||
if (document.addEventListener) {
|
if (document.addEventListener) {
|
||||||
document.addEventListener('contextmenu', function(e) {
|
document.addEventListener('contextmenu', function(e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -142,40 +127,37 @@ pub fn create_new_window(
|
|||||||
}
|
}
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
if cfg.disable_context_menu {
|
if cfg.disable_context_menu {
|
||||||
// in release mode, we don't want to show the dev tool or reload menus
|
// in release mode, we don't want to show the dev tool or reload menus
|
||||||
webview = webview.with_initialization_script(INITIALIZATION_SCRIPT)
|
webview = webview.with_initialization_script(INITIALIZATION_SCRIPT)
|
||||||
} else {
|
} else {
|
||||||
// in debug, we are okay with the reload menu showing and dev tool
|
// in debug, we are okay with the reload menu showing and dev tool
|
||||||
webview = webview.with_devtools(true);
|
webview = webview.with_devtools(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
let webview = webview.build().unwrap();
|
let webview = webview.build().unwrap();
|
||||||
|
|
||||||
let desktop_context = Rc::from(DesktopService::new(
|
let desktop_context = Rc::from(DesktopService::new(
|
||||||
window,
|
window,
|
||||||
webview,
|
webview,
|
||||||
proxy.clone(),
|
shared.clone(),
|
||||||
event_loop.clone(),
|
edit_queue,
|
||||||
queue.clone(),
|
asset_handlers,
|
||||||
event_handlers.clone(),
|
));
|
||||||
shortcut_manager,
|
|
||||||
edit_queue,
|
|
||||||
asset_handlers,
|
|
||||||
));
|
|
||||||
|
|
||||||
// Provide the desktop context to the virtualdom
|
// Provide the desktop context to the virtualdom
|
||||||
dom.base_scope().provide_context(desktop_context.clone());
|
dom.base_scope().provide_context(desktop_context.clone());
|
||||||
|
|
||||||
// Also set up its eval provider
|
// Also set up its eval provider
|
||||||
dom.base_scope()
|
dom.base_scope()
|
||||||
.provide_context(Rc::new(DesktopEvalProvider::new(desktop_context.clone())));
|
.provide_context(Rc::new(DesktopEvalProvider::new(desktop_context.clone())));
|
||||||
|
|
||||||
WebviewHandler {
|
WebviewInstance {
|
||||||
// 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
|
||||||
waker: tao_waker(proxy.clone(), desktop_context.window.id()),
|
waker: tao_waker(shared.proxy.clone(), desktop_context.window.id()),
|
||||||
desktop_context,
|
desktop_context,
|
||||||
dom,
|
dom,
|
||||||
_web_context: web_context,
|
_web_context: web_context,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user