From ad294d274dd81d2ef91ed73af9163b6e9b8eb964 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Mon, 16 Sep 2024 23:03:25 +0300 Subject: [PATCH] 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 Co-authored-by: Lucas Fernandes Nogueira --- .changes/window-config-create.md | 6 ++++++ crates/tauri-bundler/src/bundle/windows/msi/wix.rs | 2 +- crates/tauri-cli/config.schema.json | 7 ++++++- .../tauri-schema-generator/schemas/config.schema.json | 7 ++++++- crates/tauri-utils/src/config.rs | 11 ++++++++++- crates/tauri/src/app.rs | 4 ++-- 6 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 .changes/window-config-create.md diff --git a/.changes/window-config-create.md b/.changes/window-config-create.md new file mode 100644 index 00000000000..47d16fd4c28 --- /dev/null +++ b/.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. diff --git a/crates/tauri-bundler/src/bundle/windows/msi/wix.rs b/crates/tauri-bundler/src/bundle/windows/msi/wix.rs index 982d009864b..db053e28d99 100644 --- a/crates/tauri-bundler/src/bundle/windows/msi/wix.rs +++ b/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())); diff --git a/crates/tauri-cli/config.schema.json b/crates/tauri-cli/config.schema.json index 3b92885794a..c0eee7a0cf9 100644 --- a/crates/tauri-cli/config.schema.json +++ b/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", diff --git a/crates/tauri-schema-generator/schemas/config.schema.json b/crates/tauri-schema-generator/schemas/config.schema.json index 3b92885794a..c0eee7a0cf9 100644 --- a/crates/tauri-schema-generator/schemas/config.schema.json +++ b/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", diff --git a/crates/tauri-utils/src/config.rs b/crates/tauri-utils/src/config.rs index 13a067e2bc0..51bab4a5c1a 100644 --- a/crates/tauri-utils/src/config.rs +++ b/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, /// 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, diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index c5e8ae8564f..afe7918086f 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -2014,8 +2014,8 @@ impl HasDisplayHandle for App { fn setup(app: &mut App) -> 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);