@@ -192,21 +192,23 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
192192 // handle default window icons for Windows targets
193193 #[ cfg( windows) ]
194194 let default_window_icon = {
195- let mut icon_path = find_icon (
195+ let icon_path = find_icon (
196196 & config,
197197 & config_parent,
198198 |i| i. ends_with ( ".ico" ) ,
199199 "icons/icon.ico" ,
200200 ) ;
201- if !icon_path. exists ( ) {
202- icon_path = find_icon (
201+ if icon_path. exists ( ) {
202+ ico_icon ( & root, & out_dir, icon_path) ?
203+ } else {
204+ let icon_path = find_icon (
203205 & config,
204206 & config_parent,
205207 |i| i. ends_with ( ".png" ) ,
206208 "icons/icon.png" ,
207209 ) ;
210+ png_icon ( & root, & out_dir, icon_path) ?
208211 }
209- ico_icon ( & root, & out_dir, icon_path) ?
210212 } ;
211213 #[ cfg( target_os = "linux" ) ]
212214 let default_window_icon = {
@@ -221,6 +223,29 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
221223 #[ cfg( not( any( windows, target_os = "linux" ) ) ) ]
222224 let default_window_icon = quote ! ( None ) ;
223225
226+ #[ cfg( target_os = "macos" ) ]
227+ let app_icon = if dev {
228+ let mut icon_path = find_icon (
229+ & config,
230+ & config_parent,
231+ |i| i. ends_with ( ".icns" ) ,
232+ "icons/icon.png" ,
233+ ) ;
234+ if !icon_path. exists ( ) {
235+ icon_path = find_icon (
236+ & config,
237+ & config_parent,
238+ |i| i. ends_with ( ".png" ) ,
239+ "icons/icon.png" ,
240+ ) ;
241+ }
242+ raw_icon ( & out_dir, icon_path) ?
243+ } else {
244+ quote ! ( None )
245+ } ;
246+ #[ cfg( not( target_os = "macos" ) ) ]
247+ let app_icon = quote ! ( None ) ;
248+
224249 let package_name = if let Some ( product_name) = & config. package . product_name {
225250 quote ! ( #product_name. to_string( ) )
226251 } else {
@@ -353,6 +378,7 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
353378 #config,
354379 :: std:: sync:: Arc :: new( #assets) ,
355380 #default_window_icon,
381+ #app_icon,
356382 #system_tray_icon,
357383 #package_info,
358384 #info_plist,
@@ -403,6 +429,35 @@ fn ico_icon<P: AsRef<Path>>(
403429 Ok ( icon)
404430}
405431
432+ #[ cfg( target_os = "macos" ) ]
433+ fn raw_icon < P : AsRef < Path > > ( out_dir : & Path , path : P ) -> Result < TokenStream , EmbeddedAssetsError > {
434+ use std:: fs:: File ;
435+ use std:: io:: Write ;
436+
437+ let path = path. as_ref ( ) ;
438+ let bytes = std:: fs:: read ( & path)
439+ . unwrap_or_else ( |_| panic ! ( "failed to read icon {}" , path. display( ) ) )
440+ . to_vec ( ) ;
441+
442+ let out_path = out_dir. join ( path. file_name ( ) . unwrap ( ) ) ;
443+ let mut out_file = File :: create ( & out_path) . map_err ( |error| EmbeddedAssetsError :: AssetWrite {
444+ path : out_path. clone ( ) ,
445+ error,
446+ } ) ?;
447+
448+ out_file
449+ . write_all ( & bytes)
450+ . map_err ( |error| EmbeddedAssetsError :: AssetWrite {
451+ path : path. to_owned ( ) ,
452+ error,
453+ } ) ?;
454+
455+ let out_path = out_path. display ( ) . to_string ( ) ;
456+
457+ let icon = quote ! ( Some ( include_bytes!( #out_path) . to_vec( ) ) ) ;
458+ Ok ( icon)
459+ }
460+
406461fn png_icon < P : AsRef < Path > > (
407462 root : & TokenStream ,
408463 out_dir : & Path ,
@@ -445,7 +500,6 @@ fn png_icon<P: AsRef<Path>>(
445500 Ok ( icon)
446501}
447502
448- #[ cfg( any( windows, target_os = "linux" ) ) ]
449503fn find_icon < F : Fn ( & & String ) -> bool > (
450504 config : & Config ,
451505 config_parent : & Path ,
0 commit comments