Skip to content

Commit bad85a1

Browse files
authored
feat(build): find .ico in config instead of default icons/icon.ico (#4115)
1 parent 45c4525 commit bad85a1

12 files changed

Lines changed: 140 additions & 555 deletions

File tree

.changes/find-icon.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+
Search `tauri.conf.json > tauri > bundle > icons` for a `.ico` file for the window icon instead of simple default `icons/icon.ico` when `WindowsAttributes::window_icon_path` is not set.

core/tauri-build/src/lib.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,15 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {
6060

6161
/// Attributes used on Windows.
6262
#[allow(dead_code)]
63-
#[derive(Debug)]
63+
#[derive(Debug, Default)]
6464
pub struct WindowsAttributes {
65-
window_icon_path: PathBuf,
65+
window_icon_path: Option<PathBuf>,
6666
/// The path to the sdk location. This can be a absolute or relative path. If not supplied
6767
/// this defaults to whatever `winres` crate determines is the best. See the
6868
/// [winres documentation](https://docs.rs/winres/*/winres/struct.WindowsResource.html#method.set_toolkit_path)
6969
sdk_dir: Option<PathBuf>,
7070
}
7171

72-
impl Default for WindowsAttributes {
73-
fn default() -> Self {
74-
Self {
75-
window_icon_path: PathBuf::from("icons/icon.ico"),
76-
sdk_dir: None,
77-
}
78-
}
79-
}
80-
8172
impl WindowsAttributes {
8273
/// Creates the default attribute set.
8374
pub fn new() -> Self {
@@ -88,7 +79,9 @@ impl WindowsAttributes {
8879
/// It must be in `ico` format. Defaults to `icons/icon.ico`.
8980
#[must_use]
9081
pub fn window_icon_path<P: AsRef<Path>>(mut self, window_icon_path: P) -> Self {
91-
self.window_icon_path = window_icon_path.as_ref().into();
82+
self
83+
.window_icon_path
84+
.replace(window_icon_path.as_ref().into());
9285
self
9386
}
9487

@@ -230,16 +223,16 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
230223
.parent()
231224
.unwrap();
232225

233-
if let Some(paths) = config.tauri.bundle.external_bin {
226+
if let Some(paths) = &config.tauri.bundle.external_bin {
234227
copy_binaries(
235-
ResourcePaths::new(external_binaries(&paths, &target_triple).as_slice(), true),
228+
ResourcePaths::new(external_binaries(paths, &target_triple).as_slice(), true),
236229
&target_triple,
237230
target_dir,
238231
)?;
239232
}
240233

241234
#[allow(unused_mut)]
242-
let mut resources = config.tauri.bundle.resources.unwrap_or_default();
235+
let mut resources = config.tauri.bundle.resources.clone().unwrap_or_default();
243236
#[cfg(target_os = "linux")]
244237
if let Some(tray) = config.tauri.system_tray {
245238
resources.push(tray.icon_path.display().to_string());
@@ -259,13 +252,24 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
259252
use semver::Version;
260253
use winres::{VersionInfo, WindowsResource};
261254

262-
let icon_path_string = attributes
255+
fn find_icon<F: Fn(&&String) -> bool>(config: &Config, predicate: F, default: &str) -> PathBuf {
256+
let icon_path = config
257+
.tauri
258+
.bundle
259+
.icon
260+
.iter()
261+
.find(|i| predicate(i))
262+
.cloned()
263+
.unwrap_or_else(|| default.to_string());
264+
icon_path.into()
265+
}
266+
267+
let window_icon_path = attributes
263268
.windows_attributes
264269
.window_icon_path
265-
.to_string_lossy()
266-
.into_owned();
270+
.unwrap_or_else(|| find_icon(&config, |i| i.ends_with(".ico"), "icons/icon.ico"));
267271

268-
if attributes.windows_attributes.window_icon_path.exists() {
272+
if window_icon_path.exists() {
269273
let mut res = WindowsResource::new();
270274
if let Some(sdk_dir) = &attributes.windows_attributes.sdk_dir {
271275
if let Some(sdk_dir_str) = sdk_dir.to_str() {
@@ -289,17 +293,17 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
289293
res.set("ProductName", product_name);
290294
res.set("FileDescription", product_name);
291295
}
292-
res.set_icon_with_id(&icon_path_string, "32512");
296+
res.set_icon_with_id(&window_icon_path.display().to_string(), "32512");
293297
res.compile().with_context(|| {
294298
format!(
295299
"failed to compile `{}` into a Windows Resource file during tauri-build",
296-
icon_path_string
300+
window_icon_path.display()
297301
)
298302
})?;
299303
} else {
300304
return Err(anyhow!(format!(
301305
"`{}` not found; required for generating a Windows Resource file during tauri-build",
302-
icon_path_string
306+
window_icon_path.display()
303307
)));
304308
}
305309
}

core/tests/app-updater/build.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use tauri_build::{try_build, Attributes, WindowsAttributes};
6-
75
fn main() {
8-
if let Err(error) = try_build(Attributes::new().windows_attributes(
9-
WindowsAttributes::new().window_icon_path("../../../examples/.icons/icon.ico"),
10-
)) {
11-
panic!("error found during tauri-build: {:#?}", error);
12-
}
6+
tauri_build::build()
137
}

0 commit comments

Comments
 (0)