diff --git a/.changes/tauri-build-icon-path.md b/.changes/tauri-build-icon-path.md new file mode 100644 index 00000000000..8f46aa00279 --- /dev/null +++ b/.changes/tauri-build-icon-path.md @@ -0,0 +1,5 @@ +--- +"tauri-build": patch +--- + +The `try_build` method now has a `Attributes` argument to allow specifying the window icon path used on Windows. diff --git a/core/tauri-build/src/codegen/context.rs b/core/tauri-build/src/codegen/context.rs index 53a17be814e..7eb2bc3f081 100644 --- a/core/tauri-build/src/codegen/context.rs +++ b/core/tauri-build/src/codegen/context.rs @@ -11,6 +11,7 @@ use std::{ }; use tauri_codegen::{context_codegen, ContextData}; +// TODO docs /// A builder for generating a Tauri application context during compile time. /// /// Meant to be used with [`tauri::include_codegen_context!`] inside your application code. diff --git a/core/tauri-build/src/lib.rs b/core/tauri-build/src/lib.rs index 4ff1aa607e6..b8d60ed55aa 100644 --- a/core/tauri-build/src/lib.rs +++ b/core/tauri-build/src/lib.rs @@ -6,12 +6,62 @@ pub use anyhow::Result; +use std::path::{Path, PathBuf}; + #[cfg(feature = "codegen")] mod codegen; #[cfg(feature = "codegen")] pub use codegen::context::CodegenContext; +/// Attributes used on Windows. +#[allow(dead_code)] +pub struct WindowsAttributes { + window_icon_path: PathBuf, +} + +impl Default for WindowsAttributes { + fn default() -> Self { + Self { + window_icon_path: PathBuf::from("icons/icon.ico"), + } + } +} + +impl WindowsAttributes { + /// Creates the default attribute set. + pub fn new() -> Self { + Self::default() + } + + /// Sets the icon to use on the window. Currently only used on Windows. + /// It must be in `ico` format. Defaults to `icons/icon.ico`. + pub fn window_icon_path>(mut self, window_icon_path: P) -> Self { + self.window_icon_path = window_icon_path.as_ref().into(); + self + } +} + +/// The attributes used on the build. +#[derive(Default)] +pub struct Attributes { + #[allow(dead_code)] + windows_attributes: WindowsAttributes, +} + +impl Attributes { + /// Creates the default attribute set. + pub fn new() -> Self { + Self::default() + } + + /// Sets the icon to use on the window. Currently only used on Windows. + pub fn windows_attributes(mut self, windows_attributes: WindowsAttributes) -> Self { + self.windows_attributes = windows_attributes; + self + } +} + /// Run all build time helpers for your Tauri Application. /// /// The current helpers include the following: @@ -32,27 +82,39 @@ pub use codegen::context::CodegenContext; /// If any of the build time helpers fail, they will [`std::panic!`] with the related error message. /// This is typically desirable when running inside a build script; see [`try_build`] for no panics. pub fn build() { - if let Err(error) = try_build() { + if let Err(error) = try_build(Attributes::default()) { panic!("error found during tauri-build: {}", error); } } /// Non-panicking [`build()`]. -pub fn try_build() -> Result<()> { +#[allow(unused_variables)] +pub fn try_build(attributes: Attributes) -> Result<()> { #[cfg(windows)] { use anyhow::{anyhow, Context}; - use std::path::Path; use winres::WindowsResource; - if Path::new("icons/icon.ico").exists() { + let icon_path_string = attributes + .windows_attributes + .window_icon_path + .to_string_lossy() + .into_owned(); + + if attributes.windows_attributes.window_icon_path.exists() { let mut res = WindowsResource::new(); - res.set_icon_with_id("icons/icon.ico", "32512"); + res.set_icon_with_id(&icon_path_string, "32512"); res.compile().with_context(|| { - "failed to compile icons/icon.ico into a Windows Resource file during tauri-build" + format!( + "failed to compile `{}` into a Windows Resource file during tauri-build", + icon_path_string + ) })?; } else { - return Err(anyhow!("no icons/icon.ico file found; required for generating a Windows Resource file during tauri-build")); + return Err(anyhow!(format!( + "`{}` not found; required for generating a Windows Resource file during tauri-build", + icon_path_string + ))); } } diff --git a/core/tauri-codegen/src/context.rs b/core/tauri-codegen/src/context.rs index f4b8b7fbe39..a0d369e7010 100644 --- a/core/tauri-codegen/src/context.rs +++ b/core/tauri-codegen/src/context.rs @@ -44,7 +44,15 @@ pub fn context_codegen(data: ContextData) -> Result, } impl Default for BundleConfig { fn default() -> Self { Self { identifier: String::from(""), + icon: Vec::default(), } } } @@ -725,8 +729,9 @@ mod build { impl ToTokens for BundleConfig { fn to_tokens(&self, tokens: &mut TokenStream) { let identifier = str_lit(&self.identifier); + let icon = vec_lit(&self.icon, str_lit); - literal_struct!(tokens, BundleConfig, identifier); + literal_struct!(tokens, BundleConfig, identifier, icon); } } @@ -843,6 +848,7 @@ mod test { }], bundle: BundleConfig { identifier: String::from(""), + icon: Vec::new(), }, cli: None, updater: UpdaterConfig {