Skip to content

Commit

Permalink
fix: Added support for AdaptiveIcon to icons.
Browse files Browse the repository at this point in the history
  • Loading branch information
mathrunet committed Oct 3, 2023
1 parent b6c8f69 commit 57d66b7
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
129 changes: 129 additions & 0 deletions packages/katana_cli/lib/action/app/icon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ final _sizeList = {
"web/icons/Icon-maskable-512.png": 512,
};

final _sizeListAdaptiveBackground = {
"android/app/src/main/res/mipmap-hdpi/ic_launcher_background.png": 162,
"android/app/src/main/res/mipmap-mdpi/ic_launcher_background.png": 108,
"android/app/src/main/res/mipmap-xhdpi/ic_launcher_background.png": 216,
"android/app/src/main/res/mipmap-xxhdpi/ic_launcher_background.png": 324,
"android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_background.png": 432,
};
final _sizeListAdaptiveForeground = {
"android/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png": 162,
"android/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png": 108,
"android/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png": 216,
"android/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png": 324,
"android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png": 432,
};

final _faviconSize = [
16,
32,
Expand Down Expand Up @@ -90,6 +105,10 @@ class AppIconCliAction extends CliCommand with CliActionMixin {
);
return;
}
final adaptive = icon.getAsMap("adaptive_icon");
final adaptiveBackground = adaptive.get("background", "");
final adaptiveForeground = adaptive.get("foreground", "");
label("Load $adaptiveBackground $adaptiveForeground");
label("Load a file from $path");
final iconFile = File(path);
if (!iconFile.existsSync()) {
Expand Down Expand Up @@ -119,6 +138,72 @@ class AppIconCliAction extends CliCommand with CliActionMixin {
);
await file.writeAsBytes(encodePng(resized, level: 9));
}
if (adaptiveBackground.isNotEmpty && adaptiveForeground.isNotEmpty) {
label("Load a file from $adaptiveBackground");
final backgroundIconFile = File(adaptiveBackground);
if (!backgroundIconFile.existsSync()) {
error("Icon file not found in $adaptiveBackground.");
return;
}
label("Load a file from $adaptiveForeground");
final foregroundIconFile = File(adaptiveForeground);
if (!foregroundIconFile.existsSync()) {
error("Icon file not found in $adaptiveForeground.");
return;
}
final backgroundIconImage =
decodeImage(backgroundIconFile.readAsBytesSync())!;
if (backgroundIconImage.width != 1024 ||
backgroundIconImage.height != 1024) {
error("Icon files should be 1024 x 1024.");
return;
}
final foregroundIconImage =
decodeImage(foregroundIconFile.readAsBytesSync())!;
if (foregroundIconImage.width != 1024 ||
foregroundIconImage.height != 1024) {
error("Icon files should be 1024 x 1024.");
return;
}
for (final tmp in _sizeListAdaptiveBackground.entries) {
label("Resize & Save to ${tmp.key}");
final dir = Directory(tmp.key.parentPath());
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
final file = File(tmp.key);
if (file.existsSync()) {
await file.delete();
}
final resized = copyResize(
backgroundIconImage,
height: tmp.value,
width: tmp.value,
interpolation: Interpolation.average,
);
await file.writeAsBytes(encodePng(resized, level: 9));
}
for (final tmp in _sizeListAdaptiveForeground.entries) {
label("Resize & Save to ${tmp.key}");
final dir = Directory(tmp.key.parentPath());
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
final file = File(tmp.key);
if (file.existsSync()) {
await file.delete();
}
final resized = copyResize(
foregroundIconImage,
height: tmp.value,
width: tmp.value,
interpolation: Interpolation.average,
);
await file.writeAsBytes(encodePng(resized, level: 9));
}
label("Create a ic_launcher.xml");
await const IcLauncherCliCode().generateFile("ic_launcher.xml");
}
final icoFile = File("web/favicon.ico");
if (icoFile.existsSync()) {
await icoFile.delete();
Expand All @@ -136,3 +221,47 @@ class AppIconCliAction extends CliCommand with CliActionMixin {
);
}
}

/// Contents of ic_launcher.xml.
///
/// ic_launcher.xmlの中身。
class IcLauncherCliCode extends CliCode {
/// Contents of launch.json.
///
/// launch.jsonの中身。
const IcLauncherCliCode();

@override
String get name => "ic_launcher";

@override
String get prefix => "ic_launcher";

@override
String get directory => "android/app/src/main/res/mipmap-anydpi-v26";

@override
String get description =>
"Create ic_launcher.xml for adaptive icons. アダプティブアイコン用のic_launcher.xmlを作成します。";

@override
String import(String path, String baseName, String className) {
return "";
}

@override
String header(String path, String baseName, String className) {
return "";
}

@override
String body(String path, String baseName, String className) {
return r"""
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_background"/>
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
""";
}
}
5 changes: 5 additions & 0 deletions packages/katana_cli/lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ app:
# The application icon is automatically set based on the image file specified in [path].
# Image files should be created as 1024 x 1024 png files.
# You can create an AdaptiveIcon for Android by specifying [background] and [foreground] in [adaptive_icon].
# [path]に指定された画像ファイルを元にアプリのアイコンを自動設定します。
# 画像ファイルは1024×1024のpngファイルで作成してください。
# [adaptive_icon]で[background]と[foreground]を指定するとAndroid用のAdaptiveIconを作成できます。
icon:
enable: false
path: assets/icon.png
adaptive_icon:
foreground:
background:
# Describe the application information.
# For each language code, put the normal title in [title] and a short title for the app in [short_title]. Provide an overview of the app in [overview].
Expand Down
8 changes: 4 additions & 4 deletions packages/katana_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ packages:
dependency: "direct main"
description:
name: archive
sha256: "20071638cbe4e5964a427cfa0e86dce55d060bc7d82d56f3554095d7239a8765"
sha256: "06a96f1249f38a00435b3b0c9a3246d934d7dbc8183fc7c9e56989860edb99d4"
url: "https://pub.dev"
source: hosted
version: "3.4.2"
version: "3.4.4"
args:
dependency: transitive
description:
Expand Down Expand Up @@ -125,10 +125,10 @@ packages:
dependency: "direct main"
description:
name: image
sha256: "93d1141eb65fe62414d5492de96ca2bff97eba0ca9218a89ad407a6f201fcadf"
sha256: "028f61960d56f26414eb616b48b04eb37d700cbe477b7fb09bf1d7ce57fd9271"
url: "https://pub.dev"
source: hosted
version: "4.1.2"
version: "4.1.3"
import_sorter:
dependency: "direct dev"
description:
Expand Down

0 comments on commit 57d66b7

Please sign in to comment.