Skip to content

Commit c91105f

Browse files
authored
refactor(build): allow setting window icon path on try_build (#1686)
1 parent 1d6f418 commit c91105f

5 files changed

Lines changed: 91 additions & 9 deletions

File tree

.changes/tauri-build-icon-path.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-build": patch
3+
---
4+
5+
The `try_build` method now has a `Attributes` argument to allow specifying the window icon path used on Windows.

core/tauri-build/src/codegen/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::{
1111
};
1212
use tauri_codegen::{context_codegen, ContextData};
1313

14+
// TODO docs
1415
/// A builder for generating a Tauri application context during compile time.
1516
///
1617
/// Meant to be used with [`tauri::include_codegen_context!`] inside your application code.

core/tauri-build/src/lib.rs

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,62 @@
66

77
pub use anyhow::Result;
88

9+
use std::path::{Path, PathBuf};
10+
911
#[cfg(feature = "codegen")]
1012
mod codegen;
1113

1214
#[cfg(feature = "codegen")]
1315
pub use codegen::context::CodegenContext;
1416

17+
/// Attributes used on Windows.
18+
#[allow(dead_code)]
19+
pub struct WindowsAttributes {
20+
window_icon_path: PathBuf,
21+
}
22+
23+
impl Default for WindowsAttributes {
24+
fn default() -> Self {
25+
Self {
26+
window_icon_path: PathBuf::from("icons/icon.ico"),
27+
}
28+
}
29+
}
30+
31+
impl WindowsAttributes {
32+
/// Creates the default attribute set.
33+
pub fn new() -> Self {
34+
Self::default()
35+
}
36+
37+
/// Sets the icon to use on the window. Currently only used on Windows.
38+
/// It must be in `ico` format. Defaults to `icons/icon.ico`.
39+
pub fn window_icon_path<P: AsRef<Path>>(mut self, window_icon_path: P) -> Self {
40+
self.window_icon_path = window_icon_path.as_ref().into();
41+
self
42+
}
43+
}
44+
45+
/// The attributes used on the build.
46+
#[derive(Default)]
47+
pub struct Attributes {
48+
#[allow(dead_code)]
49+
windows_attributes: WindowsAttributes,
50+
}
51+
52+
impl Attributes {
53+
/// Creates the default attribute set.
54+
pub fn new() -> Self {
55+
Self::default()
56+
}
57+
58+
/// Sets the icon to use on the window. Currently only used on Windows.
59+
pub fn windows_attributes(mut self, windows_attributes: WindowsAttributes) -> Self {
60+
self.windows_attributes = windows_attributes;
61+
self
62+
}
63+
}
64+
1565
/// Run all build time helpers for your Tauri Application.
1666
///
1767
/// The current helpers include the following:
@@ -32,27 +82,39 @@ pub use codegen::context::CodegenContext;
3282
/// If any of the build time helpers fail, they will [`std::panic!`] with the related error message.
3383
/// This is typically desirable when running inside a build script; see [`try_build`] for no panics.
3484
pub fn build() {
35-
if let Err(error) = try_build() {
85+
if let Err(error) = try_build(Attributes::default()) {
3686
panic!("error found during tauri-build: {}", error);
3787
}
3888
}
3989

4090
/// Non-panicking [`build()`].
41-
pub fn try_build() -> Result<()> {
91+
#[allow(unused_variables)]
92+
pub fn try_build(attributes: Attributes) -> Result<()> {
4293
#[cfg(windows)]
4394
{
4495
use anyhow::{anyhow, Context};
45-
use std::path::Path;
4696
use winres::WindowsResource;
4797

48-
if Path::new("icons/icon.ico").exists() {
98+
let icon_path_string = attributes
99+
.windows_attributes
100+
.window_icon_path
101+
.to_string_lossy()
102+
.into_owned();
103+
104+
if attributes.windows_attributes.window_icon_path.exists() {
49105
let mut res = WindowsResource::new();
50-
res.set_icon_with_id("icons/icon.ico", "32512");
106+
res.set_icon_with_id(&icon_path_string, "32512");
51107
res.compile().with_context(|| {
52-
"failed to compile icons/icon.ico into a Windows Resource file during tauri-build"
108+
format!(
109+
"failed to compile `{}` into a Windows Resource file during tauri-build",
110+
icon_path_string
111+
)
53112
})?;
54113
} else {
55-
return Err(anyhow!("no icons/icon.ico file found; required for generating a Windows Resource file during tauri-build"));
114+
return Err(anyhow!(format!(
115+
"`{}` not found; required for generating a Windows Resource file during tauri-build",
116+
icon_path_string
117+
)));
56118
}
57119
}
58120

core/tauri-codegen/src/context.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
4444

4545
// handle default window icons for Windows targets
4646
let default_window_icon = if cfg!(windows) {
47-
let icon_path = config_parent.join("icons/icon.ico").display().to_string();
47+
let icon_path = config
48+
.tauri
49+
.bundle
50+
.icon
51+
.iter()
52+
.find(|i| i.ends_with(".ico"))
53+
.cloned()
54+
.unwrap_or_else(|| "icons/icon.ico".to_string());
55+
let icon_path = config_parent.join(icon_path).display().to_string();
4856
quote!(Some(include_bytes!(#icon_path).to_vec()))
4957
} else {
5058
quote!(None)

core/tauri-utils/src/config.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,16 @@ impl CliConfig {
306306
pub struct BundleConfig {
307307
/// The bundle identifier.
308308
pub identifier: String,
309+
/// The bundle icons.
310+
#[serde(default)]
311+
pub icon: Vec<String>,
309312
}
310313

311314
impl Default for BundleConfig {
312315
fn default() -> Self {
313316
Self {
314317
identifier: String::from(""),
318+
icon: Vec::default(),
315319
}
316320
}
317321
}
@@ -725,8 +729,9 @@ mod build {
725729
impl ToTokens for BundleConfig {
726730
fn to_tokens(&self, tokens: &mut TokenStream) {
727731
let identifier = str_lit(&self.identifier);
732+
let icon = vec_lit(&self.icon, str_lit);
728733

729-
literal_struct!(tokens, BundleConfig, identifier);
734+
literal_struct!(tokens, BundleConfig, identifier, icon);
730735
}
731736
}
732737

@@ -843,6 +848,7 @@ mod test {
843848
}],
844849
bundle: BundleConfig {
845850
identifier: String::from(""),
851+
icon: Vec::new(),
846852
},
847853
cli: None,
848854
updater: UpdaterConfig {

0 commit comments

Comments
 (0)