Merge branch 'master' into master

This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-01-04 09:57:26 -08:00
commit 601627d46e
128 files changed
+2793 -790

No files matched your search

+40 -7
View File
@@ -1,19 +1,20 @@
use crate::desktop_context::EventData;
use crate::protocol;
use crate::protocol::{self, AssetHandlerRegistry};
use crate::{desktop_context::UserWindowEvent, Config};
use tao::event_loop::{EventLoopProxy, EventLoopWindowTarget};
pub use wry;
pub use wry::application as tao;
use wry::application::menu::{MenuBar, MenuItem};
use wry::application::window::Window;
use wry::http::Response;
use wry::webview::{WebContext, WebView, WebViewBuilder};
pub fn build(
cfg: &mut Config,
event_loop: &EventLoopWindowTarget<UserWindowEvent>,
proxy: EventLoopProxy<UserWindowEvent>,
) -> (WebView, WebContext) {
let mut builder = cfg.window.clone();
) -> (WebView, WebContext, AssetHandlerRegistry) {
let window = builder.with_visible(false).build(event_loop).unwrap();
let file_handler = cfg.file_drop_handler.take();
let custom_head = cfg.custom_head.clone();
let index_file = cfg.custom_index.clone();
@@ -38,6 +39,8 @@ pub fn build(
}
let mut web_context = WebContext::new(cfg.data_dir.clone());
let asset_handlers = AssetHandlerRegistry::new();
let asset_handlers_ref = asset_handlers.clone();
let mut webview = WebViewBuilder::new(window)
.unwrap()
@@ -50,8 +53,29 @@ pub fn build(
_ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window.id()));
}
})
.with_custom_protocol(String::from("dioxus"), move |r| {
protocol::desktop_handler(r, custom_head.clone(), index_file.clone(), &root_name)
.with_asynchronous_custom_protocol(String::from("dioxus"), 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();
tokio::spawn(async move {
let response_res = protocol::desktop_handler(
request,
custom_head.clone(),
index_file.clone(),
&root_name,
&asset_handlers_ref,
)
.await;
let response = response_res.unwrap_or_else(|err| {
tracing::error!("Error: {}", err);
Response::builder()
.status(500)
.body(err.to_string().into_bytes().into())
.unwrap()
});
responder.respond(response);
});
})
.with_file_drop_handler(move |window, evet| {
file_handler
@@ -77,7 +101,16 @@ pub fn build(
// .with_web_context(&mut web_context);
for (name, handler) in cfg.protocols.drain(..) {
webview = webview.with_custom_protocol(name, handler)
webview = webview.with_custom_protocol(name, move |r| match handler(&r) {
Ok(response) => response,
Err(err) => {
tracing::error!("Error: {}", err);
Response::builder()
.status(500)
.body(err.to_string().into_bytes().into())
.unwrap()
}
})
}
if cfg.disable_context_menu {
@@ -100,7 +133,7 @@ pub fn build(
webview = webview.with_devtools(true);
}
(webview.build().unwrap(), web_context)
(webview.build().unwrap(), web_context, asset_handlers)
}
/// Builds a standard menu bar depending on the users platform. It may be used as a starting point