Collapse objects into single shared object

This commit is contained in:
Jonathan Kelley committed 2024-01-04 20:17:38 -08:00
1 parent 2e9737ef57
commit 4adc912189
7 files changed
+203 -218

No files matched your search

+118 -136
View File
@@ -1,26 +1,18 @@
use std::{rc::Rc, task::Waker};
use crate::{
assets::AssetHandlerRegistry, desktop_context::UserWindowEvent, waker::tao_waker, Config,
DesktopContext,
};
use crate::{
desktop_context::{EventData, WindowEventHandlers},
shortcut::ShortcutRegistry,
};
use crate::{
edits::{EditQueue, WebviewQueue},
app::SharedContext,
assets::AssetHandlerRegistry,
desktop_context::{EventData, UserWindowEvent},
edits::EditQueue,
eval::DesktopEvalProvider,
};
use crate::{
protocol::{self},
DesktopService,
waker::tao_waker,
Config, DesktopContext, DesktopService,
};
use dioxus_core::VirtualDom;
use tao::event_loop::{EventLoopProxy, EventLoopWindowTarget};
use std::{rc::Rc, task::Waker};
use wry::{WebContext, WebViewBuilder};
pub struct WebviewHandler {
pub struct WebviewInstance {
pub dom: VirtualDom,
pub desktop_context: DesktopContext,
pub waker: Waker,
@@ -30,107 +22,100 @@ pub struct WebviewHandler {
_web_context: WebContext,
}
pub fn create_new_window(
mut cfg: Config,
dom: VirtualDom,
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();
impl WebviewInstance {
pub fn new(mut cfg: Config, dom: VirtualDom, shared: SharedContext) -> WebviewInstance {
let window = cfg.window.clone().build(&shared.target).unwrap();
// TODO: allow users to specify their own menubars, again :/
if cfg.enable_default_menu_bar {
use crate::menubar::*;
build_menu_bar(build_default_menu_bar(), &window);
}
// TODO: allow users to specify their own menubars, again :/
if cfg.enable_default_menu_bar {
use crate::menubar::*;
build_menu_bar(build_default_menu_bar(), &window);
}
let window_id = window.id();
let file_handler = cfg.file_drop_handler.take();
let custom_head = cfg.custom_head.clone();
let index_file = cfg.custom_index.clone();
let root_name = cfg.root_name.clone();
let window_id = window.id();
let file_handler = cfg.file_drop_handler.take();
let custom_head = cfg.custom_head.clone();
let index_file = cfg.custom_index.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
if cfg.window.window.window_icon.is_none() {
window.set_window_icon(Some(
tao::window::Icon::from_rgba(
include_bytes!("./assets/default_icon.bin").to_vec(),
460,
460,
)
.expect("image parse failed"),
));
}
// 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() {
window.set_window_icon(Some(
tao::window::Icon::from_rgba(
include_bytes!("./assets/default_icon.bin").to_vec(),
460,
460,
)
.expect("image parse failed"),
));
}
let mut web_context = WebContext::new(cfg.data_dir.clone());
let edit_queue = EditQueue::default();
let headless = !cfg.window.window.visible;
let asset_handlers = AssetHandlerRegistry::new();
let asset_handlers_ref = asset_handlers.clone();
let mut web_context = WebContext::new(cfg.data_dir.clone());
let edit_queue = EditQueue::default();
let headless = !cfg.window.window.visible;
let asset_handlers = AssetHandlerRegistry::new();
let asset_handlers_ref = asset_handlers.clone();
let mut webview = WebViewBuilder::new(&window)
.with_transparent(cfg.window.window.transparent)
.with_url("dioxus://index.html/")
.unwrap()
.with_ipc_handler({
let proxy = proxy.clone();
move |payload: String| {
// defer the event to the main thread
if let Ok(message) = serde_json::from_str(&payload) {
_ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window_id));
let mut webview = WebViewBuilder::new(&window)
.with_transparent(cfg.window.window.transparent)
.with_url("dioxus://index.html/")
.unwrap()
.with_ipc_handler({
let proxy = shared.proxy.clone();
move |payload: String| {
// defer the event to the main thread
if let Ok(message) = serde_json::from_str(&payload) {
_ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window_id));
}
}
}
})
.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();
})
.with_asynchronous_custom_protocol(String::from("dioxus"), {
let edit_queue = edit_queue.clone();
tokio::spawn(async move {
protocol::desktop_handler(
request,
custom_head.clone(),
index_file.clone(),
&root_name,
&asset_handlers_ref,
&edit_queue,
headless,
responder,
)
.await;
});
}
})
.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);
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();
tokio::spawn(async move {
protocol::desktop_handler(
request,
custom_head.clone(),
index_file.clone(),
&root_name,
&asset_handlers_ref,
&edit_queue,
headless,
responder,
)
.await;
});
}
})
.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)]
{
// Windows has a platform specific settings to disable the browser shortcut keys
use wry::webview::WebViewBuilderExtWindows;
webview = webview.with_browser_accelerator_keys(false);
}
#[cfg(windows)]
{
// Windows has a platform specific settings to disable the browser shortcut keys
use wry::webview::WebViewBuilderExtWindows;
webview = webview.with_browser_accelerator_keys(false);
}
if let Some(color) = cfg.background_color {
webview = webview.with_background_color(color);
}
if let Some(color) = cfg.background_color {
webview = webview.with_background_color(color);
}
for (name, handler) in cfg.protocols.drain(..) {
webview = webview.with_custom_protocol(name, move |r| handler(r))
}
for (name, handler) in cfg.protocols.drain(..) {
webview = webview.with_custom_protocol(name, move |r| handler(r))
}
const INITIALIZATION_SCRIPT: &str = r#"
const INITIALIZATION_SCRIPT: &str = r#"
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
@@ -142,40 +127,37 @@ pub fn create_new_window(
}
"#;
if cfg.disable_context_menu {
// in release mode, we don't want to show the dev tool or reload menus
webview = webview.with_initialization_script(INITIALIZATION_SCRIPT)
} else {
// in debug, we are okay with the reload menu showing and dev tool
webview = webview.with_devtools(true);
}
if cfg.disable_context_menu {
// in release mode, we don't want to show the dev tool or reload menus
webview = webview.with_initialization_script(INITIALIZATION_SCRIPT)
} else {
// in debug, we are okay with the reload menu showing and dev tool
webview = webview.with_devtools(true);
}
let webview = webview.build().unwrap();
let webview = webview.build().unwrap();
let desktop_context = Rc::from(DesktopService::new(
window,
webview,
proxy.clone(),
event_loop.clone(),
queue.clone(),
event_handlers.clone(),
shortcut_manager,
edit_queue,
asset_handlers,
));
let desktop_context = Rc::from(DesktopService::new(
window,
webview,
shared.clone(),
edit_queue,
asset_handlers,
));
// Provide the desktop context to the virtualdom
dom.base_scope().provide_context(desktop_context.clone());
// Provide the desktop context to the virtualdom
dom.base_scope().provide_context(desktop_context.clone());
// Also set up its eval provider
dom.base_scope()
.provide_context(Rc::new(DesktopEvalProvider::new(desktop_context.clone())));
// Also set up its eval provider
dom.base_scope()
.provide_context(Rc::new(DesktopEvalProvider::new(desktop_context.clone())));
WebviewHandler {
// 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()),
desktop_context,
dom,
_web_context: web_context,
WebviewInstance {
// 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(shared.proxy.clone(), desktop_context.window.id()),
desktop_context,
dom,
_web_context: web_context,
}
}
}