Sfoglia il codice sorgente

feat(core): set window icon on Linux, closes #1922 (#1937)

Lucas Fernandes Nogueira 4 anni fa
parent
commit
40b717edc5

+ 5 - 0
.changes/icon-png-support.md

@@ -0,0 +1,5 @@
+---
+"tauri-runtime-wry": patch
+---
+
+Adds support to PNG icons.

+ 6 - 0
.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.

+ 32 - 10
core/tauri-codegen/src/context.rs

@@ -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.
@@ -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)
@@ -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 - 0
core/tauri-runtime-wry/Cargo.toml

@@ -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" ]

+ 11 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -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

BIN
core/tauri/test/fixture/src-tauri/icons/icon.png