cfg.rs 5.0 KB

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