Skip to content

Commit

Permalink
refactor(build): allow setting window icon path on try_build (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 3, 2021
1 parent 1d6f418 commit c91105f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .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.
1 change: 1 addition & 0 deletions core/tauri-build/src/codegen/context.rs
Expand Up @@ -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.
Expand Down
76 changes: 69 additions & 7 deletions core/tauri-build/src/lib.rs
Expand Up @@ -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<P: AsRef<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:
Expand All @@ -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
)));
}
}

Expand Down
10 changes: 9 additions & 1 deletion core/tauri-codegen/src/context.rs
Expand Up @@ -44,7 +44,15 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE

// handle default window icons for Windows targets
let default_window_icon = if cfg!(windows) {
let icon_path = config_parent.join("icons/icon.ico").display().to_string();
let icon_path = config
.tauri
.bundle
.icon
.iter()
.find(|i| i.ends_with(".ico"))
.cloned()
.unwrap_or_else(|| "icons/icon.ico".to_string());
let icon_path = config_parent.join(icon_path).display().to_string();
quote!(Some(include_bytes!(#icon_path).to_vec()))
} else {
quote!(None)
Expand Down
8 changes: 7 additions & 1 deletion core/tauri-utils/src/config.rs
Expand Up @@ -306,12 +306,16 @@ impl CliConfig {
pub struct BundleConfig {
/// The bundle identifier.
pub identifier: String,
/// The bundle icons.
#[serde(default)]
pub icon: Vec<String>,
}

impl Default for BundleConfig {
fn default() -> Self {
Self {
identifier: String::from(""),
icon: Vec::default(),
}
}
}
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -843,6 +848,7 @@ mod test {
}],
bundle: BundleConfig {
identifier: String::from(""),
icon: Vec::new(),
},
cli: None,
updater: UpdaterConfig {
Expand Down

0 comments on commit c91105f

Please sign in to comment.