|
5 | 5 | use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError}; |
6 | 6 | use proc_macro2::TokenStream; |
7 | 7 | use quote::quote; |
8 | | -use std::path::PathBuf; |
| 8 | +use std::path::{Path, PathBuf}; |
9 | 9 | use tauri_utils::config::{AppUrl, Config, WindowUrl}; |
10 | 10 |
|
11 | 11 | /// Necessary data needed by [`context_codegen`] to generate code for a Tauri application context. |
@@ -67,15 +67,20 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE |
67 | 67 |
|
68 | 68 | // handle default window icons for Windows targets |
69 | 69 | let default_window_icon = if cfg!(windows) { |
70 | | - let icon_path = config |
71 | | - .tauri |
72 | | - .bundle |
73 | | - .icon |
74 | | - .iter() |
75 | | - .find(|i| i.ends_with(".ico")) |
76 | | - .cloned() |
77 | | - .unwrap_or_else(|| "icons/icon.ico".to_string()); |
78 | | - let icon_path = config_parent.join(icon_path).display().to_string(); |
| 70 | + let icon_path = find_icon( |
| 71 | + &config, |
| 72 | + &config_parent, |
| 73 | + |i| i.ends_with(".ico"), |
| 74 | + "icons/icon.ico", |
| 75 | + ); |
| 76 | + quote!(Some(include_bytes!(#icon_path).to_vec())) |
| 77 | + } else if cfg!(target_os = "linux") { |
| 78 | + let icon_path = find_icon( |
| 79 | + &config, |
| 80 | + &config_parent, |
| 81 | + |i| i.ends_with(".png"), |
| 82 | + "icons/icon.png", |
| 83 | + ); |
79 | 84 | quote!(Some(include_bytes!(#icon_path).to_vec())) |
80 | 85 | } else { |
81 | 86 | quote!(None) |
@@ -148,3 +153,20 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE |
148 | 153 | #package_info, |
149 | 154 | ))) |
150 | 155 | } |
| 156 | + |
| 157 | +fn find_icon<F: Fn(&&String) -> bool>( |
| 158 | + config: &Config, |
| 159 | + config_parent: &Path, |
| 160 | + predicate: F, |
| 161 | + default: &str, |
| 162 | +) -> String { |
| 163 | + let icon_path = config |
| 164 | + .tauri |
| 165 | + .bundle |
| 166 | + .icon |
| 167 | + .iter() |
| 168 | + .find(|i| predicate(i)) |
| 169 | + .cloned() |
| 170 | + .unwrap_or_else(|| default.to_string()); |
| 171 | + config_parent.join(icon_path).display().to_string() |
| 172 | +} |
0 commit comments