aggressively clean up desktop with new inline poll

This commit is contained in:
Jonathan Kelley committed 2022-12-30 01:08:25 -05:00
1 parent 7bc8150da7
commit 633bf1f834
7 files changed
+325 -382

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
use crate::events::parse_ipc_message;
use crate::protocol;
use crate::{desktop_context::UserWindowEvent, Config};
pub use wry;
pub use wry::application as tao;
use wry::application::window::Window;
use wry::webview::WebViewBuilder;
pub fn build(
cfg: &mut Config,
event_loop: &tao::event_loop::EventLoopWindowTarget<UserWindowEvent>,
proxy: tao::event_loop::EventLoopProxy<UserWindowEvent>,
) -> wry::webview::WebView {
let builder = cfg.window.clone();
let window = builder.build(event_loop).unwrap();
let file_handler = cfg.file_drop_handler.take();
let custom_head = cfg.custom_head.clone();
let resource_dir = cfg.resource_dir.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"),
));
}
let mut webview = WebViewBuilder::new(window)
.unwrap()
.with_transparent(cfg.window.window.transparent)
.with_url("dioxus://index.html/")
.unwrap()
.with_ipc_handler(move |_window: &Window, payload: String| {
let message = match parse_ipc_message(&payload) {
Some(message) => message,
None => {
log::error!("Failed to parse IPC message: {}", payload);
return;
}
};
match message.method() {
"eval_result" => {
let _ = proxy.send_event(UserWindowEvent::EvalResult(message.params()));
}
"user_event" => {
let _ = proxy.send_event(UserWindowEvent::UserEvent(message.params()));
}
"initialize" => {
let _ = proxy.send_event(UserWindowEvent::Initialize);
}
"browser_open" => match message.params().as_object() {
Some(temp) if temp.contains_key("href") => {
let open = webbrowser::open(temp["href"].as_str().unwrap());
if let Err(e) = open {
log::error!("Open Browser error: {:?}", e);
}
}
_ => (),
},
_ => (),
}
})
.with_custom_protocol(String::from("dioxus"), move |r| {
protocol::desktop_handler(
r,
resource_dir.clone(),
custom_head.clone(),
index_file.clone(),
&root_name,
)
})
.with_file_drop_handler(move |window, evet| {
file_handler
.as_ref()
.map(|handler| handler(window, evet))
.unwrap_or_default()
});
for (name, handler) in cfg.protocols.drain(..) {
webview = webview.with_custom_protocol(name, handler)
}
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(
r#"
if (document.addEventListener) {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
}, false);
} else {
document.attachEvent('oncontextmenu', function() {
window.event.returnValue = false;
});
}
"#,
)
} else {
// in debug, we are okay with the reload menu showing and dev tool
webview = webview.with_devtools(true);
}
webview.build().unwrap()
}