webview.rs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. use std::rc::Rc;
  2. use crate::desktop_context::EventData;
  3. use crate::protocol;
  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::webview::{WebContext, WebView, WebViewBuilder};
  10. pub fn build(
  11. cfg: &mut Config,
  12. event_loop: &EventLoopWindowTarget<UserWindowEvent>,
  13. proxy: EventLoopProxy<UserWindowEvent>,
  14. ) -> (Rc<WebView>, WebContext) {
  15. let builder = cfg.window.clone();
  16. let window = builder.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 mut webview = WebViewBuilder::new(window)
  34. .unwrap()
  35. .with_transparent(cfg.window.window.transparent)
  36. .with_url("dioxus://index.html/")
  37. .unwrap()
  38. .with_ipc_handler(move |window: &Window, payload: String| {
  39. // defer the event to the main thread
  40. if let Ok(message) = serde_json::from_str(&payload) {
  41. _ = proxy.send_event(UserWindowEvent(EventData::Ipc(message), window.id()));
  42. }
  43. })
  44. .with_custom_protocol(String::from("dioxus"), move |r| {
  45. protocol::desktop_handler(r, custom_head.clone(), index_file.clone(), &root_name)
  46. })
  47. .with_file_drop_handler(move |window, evet| {
  48. file_handler
  49. .as_ref()
  50. .map(|handler| handler(window, evet))
  51. .unwrap_or_default()
  52. })
  53. .with_web_context(&mut web_context);
  54. for (name, handler) in cfg.protocols.drain(..) {
  55. webview = webview.with_custom_protocol(name, handler)
  56. }
  57. if cfg.disable_context_menu {
  58. // in release mode, we don't want to show the dev tool or reload menus
  59. webview = webview.with_initialization_script(
  60. r#"
  61. if (document.addEventListener) {
  62. document.addEventListener('contextmenu', function(e) {
  63. e.preventDefault();
  64. }, false);
  65. } else {
  66. document.attachEvent('oncontextmenu', function() {
  67. window.event.returnValue = false;
  68. });
  69. }
  70. "#,
  71. )
  72. } else {
  73. // in debug, we are okay with the reload menu showing and dev tool
  74. webview = webview.with_devtools(true);
  75. }
  76. (Rc::new(webview.build().unwrap()), web_context)
  77. }