config.rs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. use std::borrow::Cow;
  2. use std::path::PathBuf;
  3. use dioxus_core::prelude::Component;
  4. use tao::window::{Icon, WindowBuilder, WindowId};
  5. use wry::{
  6. http::{Request as HttpRequest, Response as HttpResponse},
  7. FileDropEvent,
  8. };
  9. /// The behaviour of the application when the last window is closed.
  10. #[derive(Copy, Clone, Eq, PartialEq)]
  11. pub enum WindowCloseBehaviour {
  12. /// Default behaviour, closing the last window exits the app
  13. LastWindowExitsApp,
  14. /// Closing the last window will not actually close it, just hide it
  15. LastWindowHides,
  16. /// Closing the last window will close it but the app will keep running so that new windows can be opened
  17. CloseWindow,
  18. }
  19. /// The configuration for the desktop application.
  20. pub struct Config {
  21. pub(crate) window: WindowBuilder,
  22. pub(crate) file_drop_handler: Option<DropHandler>,
  23. pub(crate) protocols: Vec<WryProtocol>,
  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_behaviour: WindowCloseBehaviour,
  33. pub(crate) enable_default_menu_bar: bool,
  34. }
  35. type DropHandler = Box<dyn Fn(WindowId, FileDropEvent) -> bool>;
  36. pub(crate) type WryProtocol = (
  37. String,
  38. Box<dyn Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static>,
  39. );
  40. impl Config {
  41. /// Initializes a new `WindowBuilder` with default values.
  42. #[inline]
  43. pub fn new() -> Self {
  44. let window = WindowBuilder::new().with_title("Dioxus app");
  45. Self {
  46. // event_handler: None,
  47. window,
  48. protocols: Vec::new(),
  49. file_drop_handler: None,
  50. pre_rendered: None,
  51. disable_context_menu: !cfg!(debug_assertions),
  52. resource_dir: None,
  53. data_dir: None,
  54. custom_head: None,
  55. custom_index: None,
  56. root_name: "main".to_string(),
  57. background_color: None,
  58. last_window_close_behaviour: WindowCloseBehaviour::LastWindowExitsApp,
  59. enable_default_menu_bar: true,
  60. }
  61. }
  62. /// Launch a Dioxus app using the given component and config
  63. ///
  64. /// See the [`crate::launch::launch`] function for more details.
  65. pub fn launch(self, root: Component<()>) {
  66. crate::launch::launch_cfg(root, self)
  67. }
  68. /// Launch a Dioxus app using the given component, config, and props
  69. ///
  70. /// See the [`crate::launch::launch_with_props`] function for more details.
  71. pub fn launch_with_props<P: 'static>(self, root: Component<P>, props: P) {
  72. crate::launch::launch_with_props(root, props, self)
  73. }
  74. /// Set whether the default menu bar should be enabled.
  75. ///
  76. /// > Note: `enable` is `true` by default. To disable the default menu bar pass `false`.
  77. pub fn with_default_menu_bar(mut self, enable: bool) -> Self {
  78. self.enable_default_menu_bar = enable;
  79. self
  80. }
  81. /// set the directory from which assets will be searched in release mode
  82. pub fn with_resource_directory(mut self, path: impl Into<PathBuf>) -> Self {
  83. self.resource_dir = Some(path.into());
  84. self
  85. }
  86. /// set the directory where data will be stored in release mode.
  87. ///
  88. /// > Note: This **must** be set when bundling on Windows.
  89. pub fn with_data_directory(mut self, path: impl Into<PathBuf>) -> Self {
  90. self.data_dir = Some(path.into());
  91. self
  92. }
  93. /// Set whether or not the right-click context menu should be disabled.
  94. pub fn with_disable_context_menu(mut self, disable: bool) -> Self {
  95. self.disable_context_menu = disable;
  96. self
  97. }
  98. /// Set the pre-rendered HTML content
  99. pub fn with_prerendered(mut self, content: String) -> Self {
  100. self.pre_rendered = Some(content);
  101. self
  102. }
  103. /// Set the configuration for the window.
  104. pub fn with_window(mut self, window: WindowBuilder) -> Self {
  105. // gots to do a swap because the window builder only takes itself as muy self
  106. // I wish more people knew about returning &mut Self
  107. self.window = window;
  108. self
  109. }
  110. /// Sets the behaviour of the application when the last window is closed.
  111. pub fn with_close_behaviour(mut self, behaviour: WindowCloseBehaviour) -> Self {
  112. self.last_window_close_behaviour = behaviour;
  113. self
  114. }
  115. /// Set a file drop handler
  116. pub fn with_file_drop_handler(
  117. mut self,
  118. handler: impl Fn(WindowId, FileDropEvent) -> bool + 'static,
  119. ) -> Self {
  120. self.file_drop_handler = Some(Box::new(handler));
  121. self
  122. }
  123. /// Set a custom protocol
  124. pub fn with_custom_protocol<F>(mut self, name: String, handler: F) -> Self
  125. where
  126. F: Fn(HttpRequest<Vec<u8>>) -> HttpResponse<Cow<'static, [u8]>> + 'static,
  127. {
  128. self.protocols.push((name, Box::new(handler)));
  129. self
  130. }
  131. /// Set a custom icon for this application
  132. pub fn with_icon(mut self, icon: Icon) -> Self {
  133. self.window.window.window_icon = Some(icon);
  134. self
  135. }
  136. /// Inject additional content into the document's HEAD.
  137. ///
  138. /// This is useful for loading CSS libraries, JS libraries, etc.
  139. pub fn with_custom_head(mut self, head: String) -> Self {
  140. self.custom_head = Some(head);
  141. self
  142. }
  143. /// Use a custom index.html instead of the default Dioxus one.
  144. ///
  145. /// Make sure your index.html is valid HTML.
  146. ///
  147. /// Dioxus injects some loader code into the closing body tag. Your document
  148. /// must include a body element!
  149. pub fn with_custom_index(mut self, index: String) -> Self {
  150. self.custom_index = Some(index);
  151. self
  152. }
  153. /// Set the name of the element that Dioxus will use as the root.
  154. ///
  155. /// This is akint to calling React.render() on the element with the specified name.
  156. pub fn with_root_name(mut self, name: impl Into<String>) -> Self {
  157. self.root_name = name.into();
  158. self
  159. }
  160. /// Sets the background color of the WebView.
  161. /// This will be set before the HTML is rendered and can be used to prevent flashing when the page loads.
  162. /// Accepts a color in RGBA format
  163. pub fn with_background_color(mut self, color: (u8, u8, u8, u8)) -> Self {
  164. self.background_color = Some(color);
  165. self
  166. }
  167. }
  168. impl Default for Config {
  169. fn default() -> Self {
  170. Self::new()
  171. }
  172. }
  173. // dirty trick, avoid introducing `image` at runtime
  174. // TODO: use serde when `Icon` impl serde
  175. //
  176. // This function should only be enabled when generating new icons.
  177. //
  178. // #[test]
  179. // #[ignore]
  180. // fn prepare_default_icon() {
  181. // use image::io::Reader as ImageReader;
  182. // use image::ImageFormat;
  183. // use std::fs::File;
  184. // use std::io::Cursor;
  185. // use std::io::Write;
  186. // use std::path::PathBuf;
  187. // let png: &[u8] = include_bytes!("default_icon.png");
  188. // let mut reader = ImageReader::new(Cursor::new(png));
  189. // reader.set_format(ImageFormat::Png);
  190. // let icon = reader.decode().unwrap();
  191. // let bin = PathBuf::from(file!())
  192. // .parent()
  193. // .unwrap()
  194. // .join("default_icon.bin");
  195. // println!("{:?}", bin);
  196. // let mut file = File::create(bin).unwrap();
  197. // file.write_all(icon.as_bytes()).unwrap();
  198. // println!("({}, {})", icon.width(), icon.height())
  199. // }