webview.rs 4.9 KB

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