Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/macos-support #407

Merged
merged 6 commits into from Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 32 additions & 14 deletions README.md
Expand Up @@ -31,6 +31,9 @@ flutter_icons:
generate: true
image_path: "path/to/image.png"
icon_size: 48 # min:48, max:256, default: 48
macos:
generate: true
image_path: "path/to/image.png"
```

If you name your configuration file something other than `flutter_launcher_icons.yaml` or `pubspec.yaml` you will need to specify
Expand Down Expand Up @@ -61,42 +64,57 @@ with the icon located in the image path specified above and given the name "laun

Shown below is the full list of attributes which you can specify within your Flutter Launcher Icons configuration.

- `android`/`ios`
### Global

- `image_path`: The location of the icon image file which you want to use as the app launcher icon.

### Android

- `android`
- `true`: Override the default existing Flutter launcher icon for the platform specified
- `false`: Ignore making launcher icons for this platform
- `icon/path/here.png`: This will generate a new launcher icons for the platform with the name you specify, without removing the old default existing Flutter launcher icon.

- `image_path`: The location of the icon image file which you want to use as the app launcher icon

- `image_path_android`: The location of the icon image file specific for Android platform (optional - if not defined then the image_path is used)
- `min_sdk_android`: Specify android min sdk value
**The next two attributes are only used when generating Android launcher icon**

- `image_path_ios`: The location of the icon image file specific for iOS platform (optional - if not defined then the image_path is used)
- `adaptive_icon_background`: The color (E.g. `"#ffffff"`) or image asset (E.g. `"assets/images/christmas-background.png"`) which will
be used to fill out the background of the adaptive icon.
- `adaptive_icon_foreground`: The image asset which will be used for the icon foreground of the adaptive icon
*Note: Adaptive Icons will only be generated when both adaptive_icon_background and adaptive_icon_foreground are specified. (the image_path is not automatically taken as foreground)*

- `min_sdk_android`: Specify android min sdk value
### IOS

- `ios`
- `true`: Override the default existing Flutter launcher icon for the platform specified
- `false`: Ignore making launcher icons for this platform
- `icon/path/here.png`: This will generate a new launcher icons for the platform with the name you specify, without removing the old default existing Flutter launcher icon.
- `image_path_ios`: The location of the icon image file specific for iOS platform (optional - if not defined then the image_path is used)
- `remove_alpha_ios`: Removes alpha channel for IOS icons

### Web

- `web`: Add web related configs
- `generate`: Specifies weather to generate icons for this platform or not
- `image_path`: Path to web icon.png
- `background_color`: Updates *background_color* in `web/manifest.json`
- `theme_color`: Updates *theme_color* in `web/manifest.json`

### Windows

- `windows`: Add Windows related configs
- `generate`: Specifies weather to generate icons for Windows platform or not
- `image_path`: Path to web icon.png
- `icon_size`: Windows app icon size. Icon size should be within this constrains *48<=icon_size<=256, defaults to 48*

### MacOS

*Note: iOS icons should [fill the entire image](https://stackoverflow.com/questions/26014461/black-border-on-my-ios-icon) and not contain transparent borders.*

The next two attributes are only used when generating Android launcher icon
- `macos`: Add MacOS related configs
- `generate`: Specifies weather to generate icons for MacOS platform or not
- `image_path`: Path to macos icon.png file

- `adaptive_icon_background`: The color (E.g. `"#ffffff"`) or image asset (E.g. `"assets/images/christmas-background.png"`) which will
be used to fill out the background of the adaptive icon.

- `adaptive_icon_foreground`: The image asset which will be used for the icon foreground of the adaptive icon

Note: Adaptive Icons will only be generated when both adaptive_icon_background and adaptive_icon_foreground are specified. (the image_path is not automatically taken as foreground)
*Note: iOS icons should [fill the entire image](https://stackoverflow.com/questions/26014461/black-border-on-my-ios-icon) and not contain transparent borders.*

## Flavor support

Expand Down
7 changes: 5 additions & 2 deletions example/default_example/pubspec.yaml
Expand Up @@ -25,13 +25,16 @@ flutter_icons:
remove_alpha_ios: true
web:
generate: true
image_path: "path/to/image.png"
image_path: "assets/images/icon-1024x1024.png"
background_color: "#hexcode"
theme_color: "#hexcode"
windows:
generate: true
image_path: "path/to/image.png"
image_path: "assets/images/icon-1024x1024.png"
icon_size: 48 # min:48, max:256, default: 48
macos:
generate: true
image_path: "assets/images/icon-1024x1024.png"

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions lib/abs/icon_generator.dart
Expand Up @@ -58,6 +58,9 @@ class IconGeneratorContext {

/// Shortcut for `config.windowsConfig`
WindowsConfig? get windowsConfig => config.windowsConfig;

/// Shortcut for `config.macOSConfig`
MacOSConfig? get macOSConfig => config.macOSConfig;
}

/// Generates Icon for given platforms
Expand Down
11 changes: 11 additions & 0 deletions lib/constants.dart
Expand Up @@ -63,6 +63,17 @@ String windowsIconFilePath = path.join(windowsResourcesDirPath, 'app_icon.ico');
///
const int windowsDefaultIconSize = 48;

// MacOS

/// Relative path to macos folder
final macOSDirPath = path.join('macos');

/// Relative path to macos icons folder
final macOSIconsDirPath = path.join(macOSDirPath, 'Runner', 'Assets.xcassets', 'AppIcon.appiconset');

/// Relative path to macos contents.json
final macOSContentsFilePath = path.join(macOSIconsDirPath, 'Contents.json');

const String errorMissingImagePath =
'Missing "image_path" or "image_path_android" + "image_path_ios" within configuration';
const String errorMissingPlatform = 'No platform specified within config to generate icons for.';
Expand Down
100 changes: 66 additions & 34 deletions lib/flutter_launcher_icons_config.dart
Expand Up @@ -43,7 +43,7 @@ class FlutterLauncherIconsConfig {
final String? adaptiveIconBackground;

/// Android min_sdk_android
@JsonKey(name: 'min_sdk_android', defaultValue: constants.androidDefaultAndroidMinSDK)
@JsonKey(name: 'min_sdk_android')
final int minSdkAndroid;

/// IOS remove_alpha_ios
Expand All @@ -58,6 +58,10 @@ class FlutterLauncherIconsConfig {
@JsonKey(name: 'windows')
final WindowsConfig? windowsConfig;

/// MacOS platform config
@JsonKey(name: 'macos')
final MacOSConfig? macOSConfig;

/// Creates an instance of [FlutterLauncherIconsConfig]
const FlutterLauncherIconsConfig({
this.imagePath,
Expand All @@ -71,11 +75,48 @@ class FlutterLauncherIconsConfig {
this.removeAlphaIOS = false,
this.webConfig,
this.windowsConfig,
this.macOSConfig,
});

/// Creates [FlutterLauncherIconsConfig] icons from [json]
factory FlutterLauncherIconsConfig.fromJson(Map json) => _$FlutterLauncherIconsConfigFromJson(json);

bool get hasAndroidAdaptiveConfig =>
isNeedingNewAndroidIcon && adaptiveIconForeground != null && adaptiveIconBackground != null;

/// Checks if contains any platform config
bool get hasPlatformConfig {
return ios != false || android != false || webConfig != null || windowsConfig != null || macOSConfig != null;
}

/// Check to see if specified Android config is a string or bool
/// String - Generate new launcher icon with the string specified
/// bool - override the default flutter project icon
bool get isCustomAndroidFile => android is String;

bool get isNeedingNewAndroidIcon => android != false;

bool get isNeedingNewIOSIcon => ios != false;

/// Method for the retrieval of the Android icon path
/// If image_path_android is found, this will be prioritised over the image_path
/// value.
String? getImagePathAndroid() => imagePathAndroid ?? imagePath;
// todo: refactor after Android & iOS configs will be refactored to the new schema
// https://github.com/fluttercommunity/flutter_launcher_icons/issues/394
String? getImagePathIOS() => imagePathIOS ?? imagePath;

/// Converts config to [Map]
Map<String, dynamic> toJson() => _$FlutterLauncherIconsConfigToJson(this);

@override
String toString() => 'FlutterLauncherIconsConfig: ${toJson()}';

/// Creates [FlutterLauncherIconsConfig] for given [flavor] and [prefixPath]
static FlutterLauncherIconsConfig? loadConfigFromFlavor(String flavor, String prefixPath) {
return FlutterLauncherIconsConfig.loadConfigFromPath(utils.flavorConfigFile(flavor), prefixPath);
}

/// Loads flutter launcher icons configs from given [filePath]
static FlutterLauncherIconsConfig? loadConfigFromPath(String filePath, String prefixPath) {
final configFile = File(path.join(prefixPath, filePath));
Expand Down Expand Up @@ -125,45 +166,36 @@ class FlutterLauncherIconsConfig {
rethrow;
}
}
}

/// Creates [FlutterLauncherIconsConfig] for given [flavor] and [prefixPath]
static FlutterLauncherIconsConfig? loadConfigFromFlavor(String flavor, String prefixPath) {
return FlutterLauncherIconsConfig.loadConfigFromPath(utils.flavorConfigFile(flavor), prefixPath);
}

/// Converts config to [Map]
Map<String, dynamic> toJson() => _$FlutterLauncherIconsConfigToJson(this);

@override
String toString() => 'FlutterLauncherIconsConfig: ${toJson()}';
/// A Configs for Windows
@JsonSerializable(
anyMap: true,
checked: true,
)
class MacOSConfig {
/// Specifies weather to generate icons for macos
@JsonKey()
final bool generate;

bool get isNeedingNewIOSIcon => ios != false;
bool get isNeedingNewAndroidIcon => android != false;
bool get hasAndroidAdaptiveConfig =>
isNeedingNewAndroidIcon &&
adaptiveIconForeground != null &&
adaptiveIconBackground != null;
/// Image path for macos
@JsonKey(name: 'image_path')
final String? imagePath;

// todo: refactor after Android & iOS configs will be refactored to the new schema
// https://github.com/fluttercommunity/flutter_launcher_icons/issues/394
String? getImagePathIOS() => imagePathIOS ?? imagePath;
/// Creates a instance of [MacOSConfig]
const MacOSConfig({
this.generate = false,
this.imagePath,
});

/// Method for the retrieval of the Android icon path
/// If image_path_android is found, this will be prioritised over the image_path
/// value.
String? getImagePathAndroid() => imagePathAndroid ?? imagePath;
/// Creates [WebConfig] from [json]
factory MacOSConfig.fromJson(Map json) => _$MacOSConfigFromJson(json);

/// Check to see if specified Android config is a string or bool
/// String - Generate new launcher icon with the string specified
/// bool - override the default flutter project icon
bool get isCustomAndroidFile => android is String;
/// Creates [Map] from [WebConfig]
Map<String, dynamic> toJson() => _$MacOSConfigToJson(this);

/// Checks if contains any platform config
bool get hasPlatformConfig =>
ios != false ||
android != false ||
webConfig != null ||
windowsConfig != null;
@override
String toString() => '$runtimeType: ${toJson()}';
}

/// Parse `web` config from `flutter_launcher_icons.yaml`
Expand Down
29 changes: 26 additions & 3 deletions lib/flutter_launcher_icons_config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.