Browse Source

feat(core): add `app > windows > create` option to disable window creation at startup (#11032)

* feat(core): add `app > windows > create` option to disable window creation at startup

closes #10950

* clippy

* clippy

* update docs

* Update .changes/window-config-create.md

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
Amr Bashir 10 months ago
parent
commit
ad294d274d

+ 6 - 0
.changes/window-config-create.md

@@ -0,0 +1,6 @@
+---
+"tauri": "patch:feat"
+"tauri-utils": "patch:feat"
+---
+
+Add `app > windows > create` option to choose whether to create this window at app startup or not.

+ 1 - 1
crates/tauri-bundler/src/bundle/windows/msi/wix.rs

@@ -520,7 +520,7 @@ pub fn build_wix_app_installer(
   data.insert("upgrade_code", to_json(upgrade_code.as_str()));
   let product_code = Uuid::new_v5(
     &Uuid::NAMESPACE_DNS,
-    &settings.bundle_identifier().as_bytes(),
+    settings.bundle_identifier().as_bytes(),
   )
   .to_string();
   data.insert("product_code", to_json(product_code.as_str()));

+ 6 - 1
crates/tauri-cli/config.schema.json

@@ -160,7 +160,7 @@
       "type": "object",
       "properties": {
         "windows": {
-          "description": "The windows configuration.",
+          "description": "The app windows configuration.",
           "default": [],
           "type": "array",
           "items": {
@@ -225,6 +225,11 @@
           "default": "main",
           "type": "string"
         },
+        "create": {
+          "description": "Whether Tauri should create this window at app startup or not.\n\n When this is set to `false` you must manually grab the config object via `app.config().app.windows`\n and create it with [`WebviewWindowBuilder::from_config`](https://docs.rs/tauri/2.0.0-rc/tauri/webview/struct.WebviewWindowBuilder.html#method.from_config).",
+          "default": true,
+          "type": "boolean"
+        },
         "url": {
           "description": "The window webview URL.",
           "default": "index.html",

+ 6 - 1
crates/tauri-schema-generator/schemas/config.schema.json

@@ -160,7 +160,7 @@
       "type": "object",
       "properties": {
         "windows": {
-          "description": "The windows configuration.",
+          "description": "The app windows configuration.",
           "default": [],
           "type": "array",
           "items": {
@@ -225,6 +225,11 @@
           "default": "main",
           "type": "string"
         },
+        "create": {
+          "description": "Whether Tauri should create this window at app startup or not.\n\n When this is set to `false` you must manually grab the config object via `app.config().app.windows`\n and create it with [`WebviewWindowBuilder::from_config`](https://docs.rs/tauri/2.0.0-rc/tauri/webview/struct.WebviewWindowBuilder.html#method.from_config).",
+          "default": true,
+          "type": "boolean"
+        },
         "url": {
           "description": "The window webview URL.",
           "default": "index.html",

+ 10 - 1
crates/tauri-utils/src/config.rs

@@ -1259,6 +1259,12 @@ pub struct WindowConfig {
   /// The window identifier. It must be alphanumeric.
   #[serde(default = "default_window_label")]
   pub label: String,
+  /// Whether Tauri should create this window at app startup or not.
+  ///
+  /// When this is set to `false` you must manually grab the config object via `app.config().app.windows`
+  /// and create it with [`WebviewWindowBuilder::from_config`](https://docs.rs/tauri/2.0.0-rc/tauri/webview/struct.WebviewWindowBuilder.html#method.from_config).
+  #[serde(default = "default_true")]
+  pub create: bool,
   /// The window webview URL.
   #[serde(default)]
   pub url: WebviewUrl,
@@ -1455,6 +1461,7 @@ impl Default for WindowConfig {
     Self {
       label: default_window_label(),
       url: WebviewUrl::default(),
+      create: true,
       user_agent: None,
       drag_drop_enabled: true,
       center: false,
@@ -1835,7 +1842,7 @@ impl Default for PatternKind {
 #[cfg_attr(feature = "schema", derive(JsonSchema))]
 #[serde(rename_all = "camelCase", deny_unknown_fields)]
 pub struct AppConfig {
-  /// The windows configuration.
+  /// The app windows configuration.
   #[serde(default)]
   pub windows: Vec<WindowConfig>,
   /// Security configuration.
@@ -2423,6 +2430,7 @@ mod build {
   impl ToTokens for WindowConfig {
     fn to_tokens(&self, tokens: &mut TokenStream) {
       let label = str_lit(&self.label);
+      let create = &self.create;
       let url = &self.url;
       let user_agent = opt_str_lit(self.user_agent.as_ref());
       let drag_drop_enabled = self.drag_drop_enabled;
@@ -2469,6 +2477,7 @@ mod build {
         ::tauri::utils::config::WindowConfig,
         label,
         url,
+        create,
         user_agent,
         drag_drop_enabled,
         center,

+ 2 - 2
crates/tauri/src/app.rs

@@ -2014,8 +2014,8 @@ impl<R: Runtime> HasDisplayHandle for App<R> {
 fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
   app.ran_setup = true;
 
-  for window_config in app.config().app.windows.clone() {
-    WebviewWindowBuilder::from_config(app.handle(), &window_config)?.build()?;
+  for window_config in app.config().app.windows.iter().filter(|w| w.create) {
+    WebviewWindowBuilder::from_config(app.handle(), window_config)?.build()?;
   }
 
   app.manager.assets.setup(app);