Skip to content

Commit 40b717e

Browse files
authored
feat(core): set window icon on Linux, closes #1922 (#1937)
1 parent df21ffc commit 40b717e

6 files changed

Lines changed: 57 additions & 10 deletions

File tree

.changes/icon-png-support.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": patch
3+
---
4+
5+
Adds support to PNG icons.

.changes/linux-window-icon.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-codegen": patch
3+
"tauri": patch
4+
---
5+
6+
Read `tauri.conf.json > tauri > bundle > icons` and use the first `.png` icon as window icon on Linux. Defaults to `icon/icon.png` if a PNG icon is not configured.

core/tauri-codegen/src/context.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError};
66
use proc_macro2::TokenStream;
77
use quote::quote;
8-
use std::path::PathBuf;
8+
use std::path::{Path, PathBuf};
99
use tauri_utils::config::{AppUrl, Config, WindowUrl};
1010

1111
/// 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
6767

6868
// handle default window icons for Windows targets
6969
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+
);
7984
quote!(Some(include_bytes!(#icon_path).to_vec()))
8085
} else {
8186
quote!(None)
@@ -148,3 +153,20 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
148153
#package_info,
149154
)))
150155
}
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+
}

core/tauri-runtime-wry/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ infer = "0.4"
2222
ico = "0.1"
2323
winapi = "0.3"
2424

25+
[target."cfg(target_os = \"linux\")".dependencies]
26+
png = "0.16"
27+
2528
[features]
2629
dox = [ "wry/dox" ]
2730
menu = [ "wry/menu", "tauri-runtime/menu" ]

core/tauri-runtime-wry/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ impl TryFrom<Icon> for WryIcon {
9999
.map_err(icon_err)?;
100100
Ok(Self(icon))
101101
}
102+
#[cfg(target_os = "linux")]
103+
"png" => {
104+
let decoder = png::Decoder::new(std::io::Cursor::new(image_bytes));
105+
let (info, mut reader) = decoder.read_info().map_err(icon_err)?;
106+
let mut buffer = Vec::new();
107+
while let Ok(Some(row)) = reader.next_row() {
108+
buffer.extend(row);
109+
}
110+
let icon = WindowIcon::from_rgba(buffer, info.width, info.height).map_err(icon_err)?;
111+
Ok(Self(icon))
112+
}
102113
_ => panic!(
103114
"image `{}` extension not supported; please file a Tauri feature request",
104115
extension
2.34 KB
Loading

0 commit comments

Comments
 (0)