webview.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. use crate::desktop_context::{EditQueue, EventData};
  2. use crate::protocol;
  3. use crate::{desktop_context::UserWindowEvent, Config};
  4. use std::sync::Arc;
  5. use std::sync::Mutex;
  6. use tao::event_loop::{EventLoopProxy, EventLoopWindowTarget};
  7. pub use wry;
  8. pub use wry::application as tao;
  9. use wry::application::window::Window;
  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, EditQueue) {
  16. let builder = cfg.window.clone();
  17. let window = builder.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 edit_queue = EditQueue::default();
  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("dioxus".into(), {
  47. let edit_queue = edit_queue.clone();
  48. move |r, responder| {
  49. protocol::desktop_handler(
  50. &r,
  51. responder,
  52. custom_head.clone(),
  53. index_file.clone(),
  54. &root_name,
  55. &edit_queue,
  56. )
  57. }
  58. })
  59. .with_file_drop_handler(move |window, evet| {
  60. file_handler
  61. .as_ref()
  62. .map(|handler| handler(window, evet))
  63. .unwrap_or_default()
  64. })
  65. .with_web_context(&mut web_context);
  66. #[cfg(windows)]
  67. {
  68. // Windows has a platform specific settings to disable the browser shortcut keys
  69. use wry::webview::WebViewBuilderExtWindows;
  70. webview = webview.with_browser_accelerator_keys(false);
  71. }
  72. if let Some(color) = cfg.background_color {
  73. webview = webview.with_background_color(color);
  74. }
  75. // These are commented out because wry is currently broken in wry
  76. // let mut web_context = WebContext::new(cfg.data_dir.clone());
  77. // .with_web_context(&mut web_context);
  78. for (name, handler) in cfg.protocols.drain(..) {
  79. webview = webview.with_custom_protocol(name, handler)
  80. }
  81. if cfg.disable_context_menu {
  82. // in release mode, we don't want to show the dev tool or reload menus
  83. webview = webview.with_initialization_script(
  84. r#"
  85. if (document.addEventListener) {
  86. document.addEventListener('contextmenu', function(e) {
  87. e.preventDefault();
  88. }, false);
  89. } else {
  90. document.attachEvent('oncontextmenu', function() {
  91. window.event.returnValue = false;
  92. });
  93. }
  94. "#,
  95. )
  96. } else {
  97. // in debug, we are okay with the reload menu showing and dev tool
  98. webview = webview.with_devtools(true);
  99. }
  100. (webview.build().unwrap(), web_context, edit_queue)
  101. }