Quellcode durchsuchen

feat(cli): add `--ios-color` option to set iOS icon background color (#6247)

Lucas Fernandes Nogueira vor 2 Jahren
Ursprung
Commit
677554257e
22 geänderte Dateien mit 44 neuen und 7 gelöschten Zeilen
  1. 6 0
      .changes/ios-icon-color.md
  2. 7 0
      tooling/cli/Cargo.lock
  3. 1 0
      tooling/cli/Cargo.toml
  4. 30 7
      tooling/cli/src/icon.rs
  5. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@1x.png
  6. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@2x-1.png
  7. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@2x.png
  8. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@3x.png
  9. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@1x.png
  10. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@2x-1.png
  11. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@2x.png
  12. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@3x.png
  13. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@1x.png
  14. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@2x-1.png
  15. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@2x.png
  16. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@3x.png
  17. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-512@2x.png
  18. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-60x60@2x.png
  19. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-60x60@3x.png
  20. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-76x76@1x.png
  21. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-76x76@2x.png
  22. BIN
      tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-83.5x83.5@2x.png

+ 6 - 0
.changes/ios-icon-color.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+Added `--ios-color` option to the `tauri icon` command.

+ 7 - 0
tooling/cli/Cargo.lock

@@ -642,6 +642,12 @@ dependencies = [
  "typenum",
 ]
 
+[[package]]
+name = "css-color"
+version = "0.2.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d101c65424c856131a3cb818da2ddde03500dc3656972269cdf79f018ef77eb4"
+
 [[package]]
 name = "cssparser"
 version = "0.27.2"
@@ -3810,6 +3816,7 @@ dependencies = [
  "clap",
  "colored 2.0.0",
  "common-path",
+ "css-color",
  "ctrlc",
  "dialoguer",
  "env_logger",

+ 1 - 0
tooling/cli/Cargo.toml

@@ -85,6 +85,7 @@ kuchiki = "0.8"
 tokio = { version = "1", features = [ "macros", "sync" ] }
 common-path = "1"
 local-ip-address = "0.4"
+css-color = "0.2"
 
 [target."cfg(windows)".dependencies]
 winapi = { version = "0.3", features = [ "handleapi", "processenv", "winbase", "wincon", "winnt" ] }

+ 30 - 7
tooling/cli/src/icon.rs

@@ -9,6 +9,7 @@ use std::{
   fs::{create_dir_all, File},
   io::{BufWriter, Write},
   path::{Path, PathBuf},
+  str::FromStr,
 };
 
 use anyhow::Context;
@@ -20,7 +21,7 @@ use image::{
     png::{CompressionType, FilterType as PngFilterType, PngEncoder},
   },
   imageops::FilterType,
-  open, ColorType, DynamicImage, ImageEncoder,
+  open, ColorType, DynamicImage, ImageBuffer, ImageEncoder, Rgba,
 };
 use serde::Deserialize;
 
@@ -48,11 +49,25 @@ pub struct Options {
   /// Default: 'icons' directory next to the tauri.conf.json file.
   #[clap(short, long)]
   output: Option<PathBuf>,
+  /// The background color of the iOS icon - string as defined in the W3C's CSS Color Module Level 4 <https://www.w3.org/TR/css-color-4/>.
+  #[clap(long, default_value = "#fff")]
+  ios_color: String,
 }
 
 pub fn command(options: Options) -> Result<()> {
   let input = options.input;
   let out_dir = options.output.unwrap_or_else(|| tauri_dir().join("icons"));
+  let ios_color = css_color::Srgb::from_str(&options.ios_color)
+    .map(|color| {
+      Rgba([
+        (color.red * 255.) as u8,
+        (color.green * 255.) as u8,
+        (color.blue * 255.) as u8,
+        (color.alpha * 255.) as u8,
+      ])
+    })
+    .map_err(|_| anyhow::anyhow!("failed to parse iOS color"))?;
+
   create_dir_all(&out_dir).context("Can't create output directory")?;
 
   // Try to read the image as a DynamicImage, convert it to rgba8 and turn it into a DynamicImage again.
@@ -73,7 +88,7 @@ pub fn command(options: Options) -> Result<()> {
 
   ico(&source, &out_dir).context("Failed to generate .ico file")?;
 
-  png(&source, &out_dir).context("Failed to generate png icons")?;
+  png(&source, &out_dir, ios_color).context("Failed to generate png icons")?;
 
   Ok(())
 }
@@ -161,7 +176,7 @@ fn ico(source: &DynamicImage, out_dir: &Path) -> Result<()> {
 
 // Generate .png files in 32x32, 128x128, 256x256, 512x512 (icon.png)
 // Main target: Linux & Android
-fn png(source: &DynamicImage, out_dir: &Path) -> Result<()> {
+fn png(source: &DynamicImage, out_dir: &Path, ios_color: Rgba<u8>) -> Result<()> {
   fn desktop_entries(out_dir: &Path) -> Vec<PngEntry> {
     let mut entries = Vec::new();
 
@@ -350,24 +365,32 @@ fn png(source: &DynamicImage, out_dir: &Path) -> Result<()> {
     create_dir_all(&out).context("Can't create iOS output directory")?;
     out
   };
-  entries.extend(ios_entries(&out)?);
 
   for entry in entries {
     log::info!(action = "PNG"; "Creating {}", entry.name);
     resize_and_save_png(source, entry.size, &entry.out_path)?;
   }
 
+  let source_rgba8 = source.as_rgba8().expect("unexpected image type");
+  let mut img = ImageBuffer::from_fn(source_rgba8.width(), source_rgba8.height(), |_, _| {
+    ios_color
+  });
+  image::imageops::overlay(&mut img, source_rgba8, 0, 0);
+  let image = DynamicImage::ImageRgba8(img);
+
+  for entry in ios_entries(&out)? {
+    log::info!(action = "iOS"; "Creating {}", entry.name);
+    resize_and_save_png(&image, entry.size, &entry.out_path)?;
+  }
+
   Ok(())
 }
 
 // Resize image and save it to disk.
 fn resize_and_save_png(source: &DynamicImage, size: u32, file_path: &Path) -> Result<()> {
   let image = source.resize_exact(size, size, FilterType::Lanczos3);
-
   let mut out_file = BufWriter::new(File::create(file_path)?);
-
   write_png(image.as_bytes(), &mut out_file, size)?;
-
   Ok(out_file.flush()?)
 }
 

BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@1x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@2x-1.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-20x20@3x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@1x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@2x-1.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-29x29@3x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@1x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@2x-1.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-40x40@3x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-512@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-60x60@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-60x60@3x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-76x76@1x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-76x76@2x.png


BIN
tooling/cli/templates/mobile/ios/Assets.xcassets/AppIcon.appiconset/AppIcon-83.5x83.5@2x.png