cfg.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use std::path::PathBuf;
  2. use wry::application::window::Icon;
  3. use wry::{
  4. application::window::{Window, WindowBuilder},
  5. http::{Request as HttpRequest, Response as HttpResponse},
  6. webview::FileDropEvent,
  7. Result as WryResult,
  8. };
  9. // pub(crate) type DynEventHandlerFn = dyn Fn(&mut EventLoop<()>, &mut WebView);
  10. /// The configuration for the desktop application.
  11. pub struct Config {
  12. pub(crate) window: WindowBuilder,
  13. pub(crate) file_drop_handler: Option<DropHandler>,
  14. pub(crate) protocols: Vec<WryProtocol>,
  15. pub(crate) pre_rendered: Option<String>,
  16. // pub(crate) event_handler: Option<Box<DynEventHandlerFn>>,
  17. pub(crate) disable_context_menu: bool,
  18. pub(crate) resource_dir: Option<PathBuf>,
  19. pub(crate) custom_head: Option<String>,
  20. pub(crate) custom_index: Option<String>,
  21. pub(crate) root_name: String,
  22. }
  23. type DropHandler = Box<dyn Fn(&Window, FileDropEvent) -> bool>;
  24. pub(crate) type WryProtocol = (
  25. String,
  26. Box<dyn Fn(&HttpRequest<Vec<u8>>) -> WryResult<HttpResponse<Vec<u8>>> + 'static>,
  27. );
  28. impl Config {
  29. /// Initializes a new `WindowBuilder` with default values.
  30. #[inline]
  31. pub fn new() -> Self {
  32. let window = WindowBuilder::new().with_title("Dioxus app");
  33. Self {
  34. // event_handler: None,
  35. window,
  36. protocols: Vec::new(),
  37. file_drop_handler: None,
  38. pre_rendered: None,
  39. disable_context_menu: !cfg!(debug_assertions),
  40. resource_dir: None,
  41. custom_head: None,
  42. custom_index: None,
  43. root_name: "main".to_string(),
  44. }
  45. }
  46. /// set the directory from which assets will be searched in release mode
  47. pub fn with_resource_directory(mut self, path: impl Into<PathBuf>) -> Self {
  48. self.resource_dir = Some(path.into());
  49. self
  50. }
  51. /// Set whether or not the right-click context menu should be disabled.
  52. pub fn with_disable_context_menu(mut self, disable: bool) -> Self {
  53. self.disable_context_menu = disable;
  54. self
  55. }
  56. /// Set the pre-rendered HTML content
  57. pub fn with_prerendered(mut self, content: String) -> Self {
  58. self.pre_rendered = Some(content);
  59. self
  60. }
  61. /// Set the configuration for the window.
  62. pub fn with_window(mut self, window: WindowBuilder) -> Self {
  63. // gots to do a swap because the window builder only takes itself as muy self
  64. // I wish more people knew about returning &mut Self
  65. self.window = window;
  66. self
  67. }
  68. // /// Set a custom event handler
  69. // pub fn with_event_handler(
  70. // mut self,
  71. // handler: impl Fn(&mut EventLoop<()>, &mut WebView) + 'static,
  72. // ) -> Self {
  73. // self.event_handler = Some(Box::new(handler));
  74. // self
  75. // }
  76. /// Set a file drop handler
  77. pub fn with_file_drop_handler(
  78. mut self,
  79. handler: impl Fn(&Window, FileDropEvent) -> bool + 'static,
  80. ) -> Self {
  81. self.file_drop_handler = Some(Box::new(handler));
  82. self
  83. }
  84. /// Set a custom protocol
  85. pub fn with_custom_protocol<F>(mut self, name: String, handler: F) -> Self
  86. where
  87. F: Fn(&HttpRequest<Vec<u8>>) -> WryResult<HttpResponse<Vec<u8>>> + 'static,
  88. {
  89. self.protocols.push((name, Box::new(handler)));
  90. self
  91. }
  92. /// Set a custom icon for this application
  93. pub fn with_icon(mut self, icon: Icon) -> Self {
  94. self.window.window.window_icon = Some(icon);
  95. self
  96. }
  97. /// Inject additional content into the document's HEAD.
  98. ///
  99. /// This is useful for loading CSS libraries, JS libraries, etc.
  100. pub fn with_custom_head(mut self, head: String) -> Self {
  101. self.custom_head = Some(head);
  102. self
  103. }
  104. /// Use a custom index.html instead of the default Dioxus one.
  105. ///
  106. /// Make sure your index.html is valid HTML.
  107. ///
  108. /// Dioxus injects some loader code into the closing body tag. Your document
  109. /// must include a body element!
  110. pub fn with_custom_index(mut self, index: String) -> Self {
  111. self.custom_index = Some(index);
  112. self
  113. }
  114. /// Set the name of the element that Dioxus will use as the root.
  115. ///
  116. /// This is akint to calling React.render() on the element with the specified name.
  117. pub fn with_root_name(mut self, name: impl Into<String>) -> Self {
  118. self.root_name = name.into();
  119. self
  120. }
  121. }
  122. impl Default for Config {
  123. fn default() -> Self {
  124. Self::new()
  125. }
  126. }
  127. // dirty trick, avoid introducing `image` at runtime
  128. // TODO: use serde when `Icon` impl serde
  129. //
  130. // This function should only be enabled when generating new icons.
  131. //
  132. // #[test]
  133. // #[ignore]
  134. // fn prepare_default_icon() {
  135. // use image::io::Reader as ImageReader;
  136. // use image::ImageFormat;
  137. // use std::fs::File;
  138. // use std::io::Cursor;
  139. // use std::io::Write;
  140. // use std::path::PathBuf;
  141. // let png: &[u8] = include_bytes!("default_icon.png");
  142. // let mut reader = ImageReader::new(Cursor::new(png));
  143. // reader.set_format(ImageFormat::Png);
  144. // let icon = reader.decode().unwrap();
  145. // let bin = PathBuf::from(file!())
  146. // .parent()
  147. // .unwrap()
  148. // .join("default_icon.bin");
  149. // println!("{:?}", bin);
  150. // let mut file = File::create(bin).unwrap();
  151. // file.write_all(icon.as_bytes()).unwrap();
  152. // println!("({}, {})", icon.width(), icon.height())
  153. // }