66
77pub use anyhow:: Result ;
88
9+ use std:: path:: { Path , PathBuf } ;
10+
911#[ cfg( feature = "codegen" ) ]
1012mod codegen;
1113
1214#[ cfg( feature = "codegen" ) ]
1315pub 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.
3484pub 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
0 commit comments