Skip to content

Commit

Permalink
feat(core): set window icon on Linux, closes #1922 (#1937)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Jun 1, 2021
1 parent df21ffc commit 40b717e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/icon-png-support.md
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch
---

Adds support to PNG icons.
6 changes: 6 additions & 0 deletions .changes/linux-window-icon.md
@@ -0,0 +1,6 @@
---
"tauri-codegen": patch
"tauri": patch
---

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.
42 changes: 32 additions & 10 deletions core/tauri-codegen/src/context.rs
Expand Up @@ -5,7 +5,7 @@
use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError};
use proc_macro2::TokenStream;
use quote::quote;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tauri_utils::config::{AppUrl, Config, WindowUrl};

/// Necessary data needed by [`context_codegen`] to generate code for a Tauri application context.
Expand Down Expand Up @@ -67,15 +67,20 @@ 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
.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();
let icon_path = find_icon(
&config,
&config_parent,
|i| i.ends_with(".ico"),
"icons/icon.ico",
);
quote!(Some(include_bytes!(#icon_path).to_vec()))
} else if cfg!(target_os = "linux") {
let icon_path = find_icon(
&config,
&config_parent,
|i| i.ends_with(".png"),
"icons/icon.png",
);
quote!(Some(include_bytes!(#icon_path).to_vec()))
} else {
quote!(None)
Expand Down Expand Up @@ -148,3 +153,20 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
#package_info,
)))
}

fn find_icon<F: Fn(&&String) -> bool>(
config: &Config,
config_parent: &Path,
predicate: F,
default: &str,
) -> String {
let icon_path = config
.tauri
.bundle
.icon
.iter()
.find(|i| predicate(i))
.cloned()
.unwrap_or_else(|| default.to_string());
config_parent.join(icon_path).display().to_string()
}
3 changes: 3 additions & 0 deletions core/tauri-runtime-wry/Cargo.toml
Expand Up @@ -22,6 +22,9 @@ infer = "0.4"
ico = "0.1"
winapi = "0.3"

[target."cfg(target_os = \"linux\")".dependencies]
png = "0.16"

[features]
dox = [ "wry/dox" ]
menu = [ "wry/menu", "tauri-runtime/menu" ]
Expand Down
11 changes: 11 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -99,6 +99,17 @@ impl TryFrom<Icon> for WryIcon {
.map_err(icon_err)?;
Ok(Self(icon))
}
#[cfg(target_os = "linux")]
"png" => {
let decoder = png::Decoder::new(std::io::Cursor::new(image_bytes));
let (info, mut reader) = decoder.read_info().map_err(icon_err)?;
let mut buffer = Vec::new();
while let Ok(Some(row)) = reader.next_row() {
buffer.extend(row);
}
let icon = WindowIcon::from_rgba(buffer, info.width, info.height).map_err(icon_err)?;
Ok(Self(icon))
}
_ => panic!(
"image `{}` extension not supported; please file a Tauri feature request",
extension
Expand Down
Binary file added core/tauri/test/fixture/src-tauri/icons/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40b717e

Please sign in to comment.