config.rs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. use std::borrow::Cow;
  2. use std::path::PathBuf;
  3. use tao::window::{Icon, WindowBuilder};
  4. use wry::http::{Request as HttpRequest, Response as HttpResponse};
  5. use wry::RequestAsyncResponder;
  6. use crate::menubar::{default_menu_bar, DioxusMenu};
  7. /// The behaviour of the application when the last window is closed.
  8. #[derive(Copy, Clone, Eq, PartialEq)]
  9. #[non_exhaustive]
  10. pub enum WindowCloseBehaviour {
  11. /// Default behaviour, closing the last window exits the app
  12. LastWindowExitsApp,
  13. /// Closing the last window will not actually close it, just hide it
  14. LastWindowHides,
  15. /// Closing the last window will close it but the app will keep running so that new windows can be opened
  16. CloseWindow,
  17. }
  18. /// The configuration for the desktop application.
  19. pub struct Config {
  20. pub(crate) window: WindowBuilder,
  21. pub(crate) menu: Option<DioxusMenu>,
  22. pub(crate) protocols: Vec<WryProtocol>,
  23. pub(crate) asynchronous_protocols: Vec<AsyncWryProtocol>,
  24. pub(crate) pre_rendered: Option<String>,
  25. pub(crate) disable_context_menu: bool,
  26. pub(crate) resource_dir: Option<PathBuf>,
  27. pub(crate) data_dir: Option<PathBuf>,
  28. pub(crate) custom_head: Option<String>,
  29. pub(crate) custom_index: Option<String>,
  30. pub(crate) root_name: String,
  31. pub(crate) background_color: Option<(u8, u8, u8, u8)>,
  32. pub(crate) last_window_close_behavior: WindowCloseBehaviour,
  33. }
  34. pub(crate) type WryProtocol = (
  35. String,
  36. Box<dyn Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static>,
  37. );
  38. pub(crate) type AsyncWryProtocol = (
  39. String,
  40. Box<dyn Fn(HttpRequest<Vec<u8>>, RequestAsyncResponder) + 'static>,
  41. );
  42. impl Config {
  43. /// Initializes a new `WindowBuilder` with default values.
  44. #[inline]
  45. pub fn new() -> Self {
  46. let window: WindowBuilder = WindowBuilder::new()
  47. .with_title(
  48. dioxus_cli_config::CURRENT_CONFIG
  49. .as_ref()
  50. .map(|c| c.dioxus_config.application.name.clone())
  51. .unwrap_or("Dioxus App".to_string()),
  52. )
  53. // During development we want the window to be on top so we can see it while we work
  54. .with_always_on_top(cfg!(debug_assertions));
  55. Self {
  56. window,
  57. menu: Some(default_menu_bar()),
  58. protocols: Vec::new(),
  59. asynchronous_protocols: Vec::new(),
  60. pre_rendered: None,
  61. disable_context_menu: !cfg!(debug_assertions),
  62. resource_dir: None,
  63. data_dir: None,
  64. custom_head: None,
  65. custom_index: None,
  66. root_name: "main".to_string(),
  67. background_color: None,
  68. last_window_close_behavior: WindowCloseBehaviour::LastWindowExitsApp,
  69. }
  70. }
  71. /// set the directory from which assets will be searched in release mode
  72. pub fn with_resource_directory(mut self, path: impl Into<PathBuf>) -> Self {
  73. self.resource_dir = Some(path.into());
  74. self
  75. }
  76. /// set the directory where data will be stored in release mode.
  77. ///
  78. /// > Note: This **must** be set when bundling on Windows.
  79. pub fn with_data_directory(mut self, path: impl Into<PathBuf>) -> Self {
  80. self.data_dir = Some(path.into());
  81. self
  82. }
  83. /// Set whether or not the right-click context menu should be disabled.
  84. pub fn with_disable_context_menu(mut self, disable: bool) -> Self {
  85. self.disable_context_menu = disable;
  86. self
  87. }
  88. /// Set the pre-rendered HTML content
  89. pub fn with_prerendered(mut self, content: String) -> Self {
  90. self.pre_rendered = Some(content);
  91. self
  92. }
  93. /// Set the configuration for the window.
  94. pub fn with_window(mut self, window: WindowBuilder) -> Self {
  95. // gots to do a swap because the window builder only takes itself as muy self
  96. // I wish more people knew about returning &mut Self
  97. self.window = window;
  98. self
  99. }
  100. /// Sets the behaviour of the application when the last window is closed.
  101. pub fn with_close_behaviour(mut self, behaviour: WindowCloseBehaviour) -> Self {
  102. self.last_window_close_behavior = behaviour;
  103. self
  104. }
  105. /// Set a custom protocol
  106. pub fn with_custom_protocol<F>(mut self, name: impl ToString, handler: F) -> Self
  107. where
  108. F: Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static,
  109. {
  110. self.protocols.push((name.to_string(), Box::new(handler)));
  111. self
  112. }
  113. /// Set an asynchronous custom protocol
  114. ///
  115. /// **Example Usage**
  116. /// ```rust
  117. /// # use wry::http::response::Response as HTTPResponse;
  118. /// # use std::borrow::Cow;
  119. /// # use dioxus_desktop::Config;
  120. /// #
  121. /// # fn main() {
  122. /// let cfg = Config::new()
  123. /// .with_asynchronous_custom_protocol("asset", |request, responder| {
  124. /// tokio::spawn(async move {
  125. /// responder.respond(
  126. /// HTTPResponse::builder()
  127. /// .status(404)
  128. /// .body(Cow::Borrowed("404 - Not Found".as_bytes()))
  129. /// .unwrap()
  130. /// );
  131. /// });
  132. /// });
  133. /// # }
  134. /// ```
  135. /// note a key difference between Dioxus and Wry, the protocol name doesn't explicitly need to be a
  136. /// [`String`], but needs to implement [`ToString`].
  137. ///
  138. /// See [`wry`](wry::WebViewBuilder::with_asynchronous_custom_protocol) for more details on implementation
  139. pub fn with_asynchronous_custom_protocol<F>(mut self, name: impl ToString, handler: F) -> Self
  140. where
  141. F: Fn(HttpRequest<Vec<u8>>, RequestAsyncResponder) + 'static,
  142. {
  143. self.asynchronous_protocols
  144. .push((name.to_string(), Box::new(handler)));
  145. self
  146. }
  147. /// Set a custom icon for this application
  148. pub fn with_icon(mut self, icon: Icon) -> Self {
  149. self.window.window.window_icon = Some(icon);
  150. self
  151. }
  152. /// Inject additional content into the document's HEAD.
  153. ///
  154. /// This is useful for loading CSS libraries, JS libraries, etc.
  155. pub fn with_custom_head(mut self, head: String) -> Self {
  156. self.custom_head = Some(head);
  157. self
  158. }
  159. /// Use a custom index.html instead of the default Dioxus one.
  160. ///
  161. /// Make sure your index.html is valid HTML.
  162. ///
  163. /// Dioxus injects some loader code into the closing body tag. Your document
  164. /// must include a body element!
  165. pub fn with_custom_index(mut self, index: String) -> Self {
  166. self.custom_index = Some(index);
  167. self
  168. }
  169. /// Set the name of the element that Dioxus will use as the root.
  170. ///
  171. /// This is akin to calling React.render() on the element with the specified name.
  172. pub fn with_root_name(mut self, name: impl Into<String>) -> Self {
  173. self.root_name = name.into();
  174. self
  175. }
  176. /// Sets the background color of the WebView.
  177. /// This will be set before the HTML is rendered and can be used to prevent flashing when the page loads.
  178. /// Accepts a color in RGBA format
  179. pub fn with_background_color(mut self, color: (u8, u8, u8, u8)) -> Self {
  180. self.background_color = Some(color);
  181. self
  182. }
  183. /// Sets the menu the window will use. This will override the default menu bar.
  184. ///
  185. /// > Note: A default menu bar will be enabled unless the menu is overridden or set to `None`.
  186. #[allow(unused)]
  187. pub fn with_menu(mut self, menu: impl Into<Option<DioxusMenu>>) -> Self {
  188. #[cfg(not(any(target_os = "ios", target_os = "android")))]
  189. {
  190. self.menu = menu.into();
  191. }
  192. self
  193. }
  194. }
  195. impl Default for Config {
  196. fn default() -> Self {
  197. Self::new()
  198. }
  199. }
  200. // dirty trick, avoid introducing `image` at runtime
  201. // TODO: use serde when `Icon` impl serde
  202. //
  203. // This function should only be enabled when generating new icons.
  204. //
  205. // #[test]
  206. // #[ignore]
  207. // fn prepare_default_icon() {
  208. // use image::io::Reader as ImageReader;
  209. // use image::ImageFormat;
  210. // use std::fs::File;
  211. // use std::io::Cursor;
  212. // use std::io::Write;
  213. // use std::path::PathBuf;
  214. // let png: &[u8] = include_bytes!("default_icon.png");
  215. // let mut reader = ImageReader::new(Cursor::new(png));
  216. // reader.set_format(ImageFormat::Png);
  217. // let icon = reader.decode().unwrap();
  218. // let bin = PathBuf::from(file!())
  219. // .parent()
  220. // .unwrap()
  221. // .join("default_icon.bin");
  222. // println!("{:?}", bin);
  223. // let mut file = File::create(bin).unwrap();
  224. // file.write_all(icon.as_bytes()).unwrap();
  225. // println!("({}, {})", icon.width(), icon.height())
  226. // }