webview.rs 3.0 KB

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