cfg.rs 7.0 KB

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