Skip to content

Commit

Permalink
feat(build): find .ico in config instead of default icons/icon.ico (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 13, 2022
1 parent 45c4525 commit bad85a1
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 555 deletions.
5 changes: 5 additions & 0 deletions .changes/find-icon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-build": patch
---

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.
48 changes: 26 additions & 22 deletions core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,15 @@ fn copy_resources(resources: ResourcePaths<'_>, path: &Path) -> Result<()> {

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

impl Default for WindowsAttributes {
fn default() -> Self {
Self {
window_icon_path: PathBuf::from("icons/icon.ico"),
sdk_dir: None,
}
}
}

impl WindowsAttributes {
/// Creates the default attribute set.
pub fn new() -> Self {
Expand All @@ -88,7 +79,9 @@ impl WindowsAttributes {
/// It must be in `ico` format. Defaults to `icons/icon.ico`.
#[must_use]
pub fn window_icon_path<P: AsRef<Path>>(mut self, window_icon_path: P) -> Self {
self.window_icon_path = window_icon_path.as_ref().into();
self
.window_icon_path
.replace(window_icon_path.as_ref().into());
self
}

Expand Down Expand Up @@ -230,16 +223,16 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
.parent()
.unwrap();

if let Some(paths) = config.tauri.bundle.external_bin {
if let Some(paths) = &config.tauri.bundle.external_bin {
copy_binaries(
ResourcePaths::new(external_binaries(&paths, &target_triple).as_slice(), true),
ResourcePaths::new(external_binaries(paths, &target_triple).as_slice(), true),
&target_triple,
target_dir,
)?;
}

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

let icon_path_string = attributes
fn find_icon<F: Fn(&&String) -> bool>(config: &Config, predicate: F, default: &str) -> PathBuf {
let icon_path = config
.tauri
.bundle
.icon
.iter()
.find(|i| predicate(i))
.cloned()
.unwrap_or_else(|| default.to_string());
icon_path.into()
}

let window_icon_path = attributes
.windows_attributes
.window_icon_path
.to_string_lossy()
.into_owned();
.unwrap_or_else(|| find_icon(&config, |i| i.ends_with(".ico"), "icons/icon.ico"));

if attributes.windows_attributes.window_icon_path.exists() {
if window_icon_path.exists() {
let mut res = WindowsResource::new();
if let Some(sdk_dir) = &attributes.windows_attributes.sdk_dir {
if let Some(sdk_dir_str) = sdk_dir.to_str() {
Expand All @@ -289,17 +293,17 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
res.set("ProductName", product_name);
res.set("FileDescription", product_name);
}
res.set_icon_with_id(&icon_path_string, "32512");
res.set_icon_with_id(&window_icon_path.display().to_string(), "32512");
res.compile().with_context(|| {
format!(
"failed to compile `{}` into a Windows Resource file during tauri-build",
icon_path_string
window_icon_path.display()
)
})?;
} else {
return Err(anyhow!(format!(
"`{}` not found; required for generating a Windows Resource file during tauri-build",
icon_path_string
window_icon_path.display()
)));
}
}
Expand Down
8 changes: 1 addition & 7 deletions core/tests/app-updater/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use tauri_build::{try_build, Attributes, WindowsAttributes};

fn main() {
if let Err(error) = try_build(Attributes::new().windows_attributes(
WindowsAttributes::new().window_icon_path("../../../examples/.icons/icon.ico"),
)) {
panic!("error found during tauri-build: {:#?}", error);
}
tauri_build::build()
}

0 comments on commit bad85a1

Please sign in to comment.