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

New imageicon interface #132

Merged
merged 6 commits into from
Sep 20, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,325 changes: 1,058 additions & 267 deletions Sources/Charcoal/Generated/Assets.swift

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions Sources/Charcoal/SwiftUI/Images/Image.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import SwiftUI

extension Image {
@inlinable init(charocalIcon: CharcoalAsset.Images) {
self.init(asset: charocalIcon.imageAsset)
}
}

// MARK: Development View

fileprivate struct ImageView: View {
var body: some View {
VStack {
Image(charocalIcon: .add24)
Image(charocalIcon: .delete32)
Image(charocalIcon: .addInline)
Image(charocalIcon: .codes24)
}
}
}

struct ImageView_Previews: PreviewProvider {
static var previews: some View {
ImageView()
}
}
51 changes: 33 additions & 18 deletions stencils/assets-template.stencil
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// swiftlint:disable all
// Generated using SwiftGen — https://github.com/SwiftGen/SwiftGen
{% import "macros/enum-cases.stencil" %}
{% import "macros/enum-cases-color.stencil" %}
{% import "macros/enum-cases-image.stencil" %}
{% import "macros/enum-cases-flat-images.stencil" %}
{% if catalogs %}
{% macro hasValuesBlock assets filter %}
{%- for asset in assets -%}
Expand All @@ -11,6 +14,14 @@
{%- endif -%}
{%- endfor -%}
{% endmacro %}

{% macro hasValuesNoRecursionBlock assets filter %}
{%- for asset in assets -%}
{%- if asset.type == filter -%}
1
{%- endif -%}
{%- endfor -%}
{% endmacro %}
{% set enumName %}{{param.enumName|default:"Asset"}}{% endset %}
{% set arResourceGroupType %}{{param.arResourceGroupTypeName|default:"ARResourceGroupAsset"}}{% endset %}
{% set colorType %}{{param.colorTypeName|default:"ColorAsset"}}{% endset %}
Expand Down Expand Up @@ -53,14 +64,12 @@
// MARK: - Asset Catalogs

{% macro enumBlock assets %}
{% call casesBlock assets %}
{% call casesBlock assets assets.name %}

{% call enumCasesBlock assets %}
{% set assetsHasGroup %}{% call hasValuesNoRecursionBlock assets "group" %}{% endset %}

{% set catalogHasColor %}{% call hasValuesBlock assets "color" %}{% endset %}
{# Generate Charcoral Color Asset Enum #}
{% if catalogHasColor %}
{% call enumCasesColorBlock assets %}
{% if assetsHasGroup %}
{% filter indent:2," ",true %}{% call enumCasesFlatImagesBlock assets %}{% endfilter %}
{% endif %}

{% if param.allValues %}
Expand Down Expand Up @@ -104,33 +113,39 @@
// swiftlint:enable trailing_comma
{% endif %}
{% endmacro %}
{% macro casesBlock assets %}
{% macro casesBlock assets assetsGroupName %}
{% for asset in assets %}
{% if asset.type == "arresourcegroup" %}
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{arResourceGroupType}}(name: "{{asset.value}}")
{% elif asset.type == "color" %}
static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}Asset = {{colorType}}(name: "{{asset.value}}")
static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord}}Asset = {{colorType}}(name: "{{asset.value}}")
{% elif asset.type == "data" %}
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{dataType}}(name: "{{asset.value}}")
{% elif asset.type == "image" %}
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{imageType}}(name: "{{asset.value}}")
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord}}Asset = {{imageType}}(name: "{{asset.value}}")
{% elif asset.type == "symbol" %}
{{accessModifier}} static let {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}} = {{symbolType}}(name: "{{asset.value}}")
{% elif asset.items and ( forceNamespaces == "true" or asset.isNamespaced == "true" ) %}
{{accessModifier}} enum {{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{% filter indent:2," ",true %}{% call casesBlock asset.items %}{% endfilter %}
{{accessModifier}} enum {{asset.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} : CaseIterable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ライブラリを使う側の人にとってはCaseIterableは不要だと思うので、ここでは削っていただけると 🙏
CaseIterableを付けたいと思ったのはSample側の話でした

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extensionでCaseIterableを書く場合、allCasesを自分で実装する必要があったため、やはりこの方針でいくのが良いとなった

{% filter indent:2," ",true %}{% call casesBlock asset.items asset.name %}{% endfilter %}
}
{% elif asset.items %}
{% call casesBlock asset.items %}
{% endif %}
{% endfor %}
{% endmacro %}
{% macro enumCasesBlock assets %}
{% for asset in assets %}
{% if asset.type == "color" %}
case {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}

{% set catalogHasColor %}{% call hasValuesNoRecursionBlock assets "color" %}{% endset %}
{# Generate Charcoral Color Asset Enum #}
{% if catalogHasColor %}
{% call enumCasesBlock assets %}
{% call enumCasesColorBlock assets %}
{% endif %}
{% endfor %}

{# {% set catalogHasImage %}{% call hasValuesNoRecursionBlock assets "image" %}{% endset %} #}
{# Generate Charcoral Image Asset Enum #}
{# {% if catalogHasImage and assetsGroupName %} #}
{# {% call enumCasesImageBlock assets assetsGroupName %} #}
{# {% endif %} #}
{% endmacro %}
{% macro allValuesBlock assets filter prefix %}
{% for asset in assets %}
Expand All @@ -148,7 +163,7 @@
{{accessModifier}} enum {{enumName}} {
{% if catalogs.count > 1 or param.forceFileNameEnum %}
{% for catalog in catalogs %}
{{accessModifier}} enum {{catalog.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} {
{{accessModifier}} enum {{catalog.name|swiftIdentifier:"pretty"|escapeReservedKeywords}} : CaseIterable {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここもCaseIterable無くて大丈夫です

{% if catalog.assets %}
{% filter indent:2," ",true %}{% call enumBlock catalog.assets %}{% endfilter %}
{% endif %}
Expand Down
30 changes: 30 additions & 0 deletions stencils/macros/enum-cases-flat-images.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% macro enumCasesFlatImagesBlock assets %}
{% for asset in assets %}
{% if asset.items %}
{% for item in asset.items %}
case {{item.name | swiftIdentifier:"pretty"|lowerFirstWord}}{{asset.name}}
{% endfor %}
{% endif %}
{% endfor %}

{% set groupHasImage %}{% call hasValuesBlock assets "image" %}{% endset %}
{% if groupHasImage %}
{{accessModifier}} var imageAsset: ImageAsset {
switch (self) {
{% for asset in assets %}
{% if asset.items %}
{% for item in asset.items %}
{% if item.type == "image" %}
case .{{item.name|swiftIdentifier:"pretty"|lowerFirstWord}}{{asset.name}}:
return {{enumName}}.{{catalog.name|swiftIdentifier:"pretty"}}.{{asset.name|swiftIdentifier:"pretty"}}.{{item.name|swiftIdentifier:"pretty"|lowerFirstWord}}Asset
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
}
}
{{accessModifier}} var image: ImageAsset.Image {
return imageAsset.image
}
{% endif %}
{% endmacro %}
15 changes: 15 additions & 0 deletions stencils/macros/enum-cases-image.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro enumCasesImageBlock assets assetsGroupName%}
{{accessModifier}} var imageAsset: ImageAsset {
switch (self) {
{% for asset in assets %}
{% if asset.type == "image" %}
case .{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord}}:
return {{enumName}}.{{catalog.name|swiftIdentifier:"pretty"}}.{{assetsGroupName|swiftIdentifier:"pretty"}}.{{asset.name|swiftIdentifier:"pretty"|lowerFirstWord}}Asset
{% endif %}
{% endfor %}
}
}
{{accessModifier}} var image: ImageAsset.Image {
return imageAsset.image
}
{% endmacro %}
7 changes: 7 additions & 0 deletions stencils/macros/enum-cases.stencil
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% macro enumCasesBlock assets %}
{% for asset in assets %}
{% if not asset.items %}
case {{asset.name|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}
{% endif %}
{% endfor %}
{% endmacro %}