webview.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. use std::sync::Arc;
  2. use crate::desktop_context::EventData;
  3. use crate::protocol::{self, AssetHandlerRegistry};
  4. use crate::{desktop_context::UserWindowEvent, Config};
  5. use tao::event_loop::{EventLoopProxy, EventLoopWindowTarget};
  6. pub use wry;
  7. pub use wry::application as tao;
  8. use wry::application::window::Window;
  9. use wry::http::Response;
  10. use wry::webview::{WebContext, WebView, WebViewBuilder};
  11. pub fn build(
  12. cfg: &mut Config,
  13. event_loop: &EventLoopWindowTarget<UserWindowEvent>,
  14. proxy: EventLoopProxy<UserWindowEvent>,
  15. ) -> (WebView, WebContext, AssetHandlerRegistry) {
  16. let builder = cfg.window.clone();
  17. let window = builder.with_visible(false).build(event_loop).unwrap();
  18. let file_handler = cfg.file_drop_handler.take();
  19. let custom_head = cfg.custom_head.clone();
  20. let index_file = cfg.custom_index.clone();
  21. let root_name = cfg.root_name.clone();
  22. // We assume that if the icon is None in cfg, then the user just didnt set it
  23. if cfg.window.window.window_icon.is_none() {
  24. window.set_window_icon(Some(
  25. tao::window::Icon::from_rgba(
  26. include_bytes!("./assets/default_icon.bin").to_vec(),
  27. 460,
  28. 460,
  29. )
  30. .expect("image parse failed"),
  31. ));
  32. }
  33. let mut web_context = WebContext::new(cfg.data_dir.clone());
  34. let asset_handlers = AssetHandlerRegistry::new();
  35. let asset_handlers_ref = asset_handlers.clone();
  36. let mut webview = WebViewBuilder::new(window)
  37. .unwrap()
  38. .with_transparent(cfg.window.window.transparent)
  39. .with_url("dioxus://index.html/")
  40. .unwrap()
  41. .with_ipc_handler(move |window: &Window, payload: String| {
  42. // defer the event to the main thread
  43. if let Ok(message) = serde_json::from_str(&payload) {
  44. _ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window.id()));
  45. }
  46. })
  47. .with_asynchronous_custom_protocol(String::from("dioxus"), move |request, responder| {
  48. let custom_head = custom_head.clone();
  49. let index_file = index_file.clone();
  50. let root_name = root_name.clone();
  51. let asset_handlers_ref = asset_handlers_ref.clone();
  52. tokio::spawn(async move {
  53. let response_res = protocol::desktop_handler(
  54. &request,
  55. custom_head.clone(),
  56. index_file.clone(),
  57. &root_name,
  58. &asset_handlers_ref,
  59. )
  60. .await;
  61. let response = response_res.unwrap_or_else(|err| {
  62. tracing::error!("Error: {}", err);
  63. Response::builder()
  64. .status(500)
  65. .body(err.to_string().into_bytes().into())
  66. .unwrap()
  67. });
  68. responder.respond(response);
  69. });
  70. })
  71. .with_file_drop_handler(move |window, evet| {
  72. file_handler
  73. .as_ref()
  74. .map(|handler| handler(window, evet))
  75. .unwrap_or_default()
  76. })
  77. .with_web_context(&mut web_context);
  78. #[cfg(windows)]
  79. {
  80. // Windows has a platform specific settings to disable the browser shortcut keys
  81. use wry::webview::WebViewBuilderExtWindows;
  82. webview = webview.with_browser_accelerator_keys(false);
  83. }
  84. if let Some(color) = cfg.background_color {
  85. webview = webview.with_background_color(color);
  86. }
  87. // These are commented out because wry is currently broken in wry
  88. // let mut web_context = WebContext::new(cfg.data_dir.clone());
  89. // .with_web_context(&mut web_context);
  90. for (name, handler) in cfg.protocols.drain(..) {
  91. webview = webview.with_custom_protocol(name, move |r| match handler(&r) {
  92. Ok(response) => response,
  93. Err(err) => {
  94. tracing::error!("Error: {}", err);
  95. Response::builder()
  96. .status(500)
  97. .body(err.to_string().into_bytes().into())
  98. .unwrap()
  99. }
  100. })
  101. }
  102. if cfg.disable_context_menu {
  103. // in release mode, we don't want to show the dev tool or reload menus
  104. webview = webview.with_initialization_script(
  105. r#"
  106. if (document.addEventListener) {
  107. document.addEventListener('contextmenu', function(e) {
  108. e.preventDefault();
  109. }, false);
  110. } else {
  111. document.attachEvent('oncontextmenu', function() {
  112. window.event.returnValue = false;
  113. });
  114. }
  115. "#,
  116. )
  117. } else {
  118. // in debug, we are okay with the reload menu showing and dev tool
  119. webview = webview.with_devtools(true);
  120. }
  121. (webview.build().unwrap(), web_context, asset_handlers)
  122. }