diff --git a/.github/scripts/conventions.sh b/.github/scripts/conventions.sh index 3a36bf7..b32eb63 100755 --- a/.github/scripts/conventions.sh +++ b/.github/scripts/conventions.sh @@ -18,7 +18,8 @@ # image or color, every image it names exists, and no catalog folder provides # a namespace (names would gain a "Folder/" prefix); # 5. no color built from components (red:, white:, hue:, gray:, .sRGB, hex, -# #colorLiteral) outside Color+DashUI.swift, where tokens are defined; +# #colorLiteral), Color+DashUI.swift included, where tokens are read from +# colorsets with dashAsset("Name"); # 6. every NSLocalizedString call, and every String(localized:) / # AttributedString(localized:) call with a literal key, passes the package # bundle; @@ -51,10 +52,10 @@ scripts=$(cd "$(dirname "$0")" && pwd) src=Sources/DashUIKit catalog=$src/Resources/Media.xcassets icons=$src/Foundation/Icon_DashUI.swift -colors=$src/Foundation/Color+DashUI.swift +tokens=$src/Foundation/Color+DashUI.swift command -v jq > /dev/null || { echo "conventions.sh needs jq" >&2; exit 2; } -for required in Package.swift "$catalog" "$icons" "$colors"; do +for required in Package.swift "$catalog" "$icons" "$tokens"; do [ -e "$required" ] || { echo "conventions.sh: ${required} not found; run from the repository root" >&2; exit 2; } done @@ -99,7 +100,7 @@ list_sets colorset > "$colorsets" scan=$work/scan # shellcheck disable=SC2016 # awk fields, not shell variables find "$src" -name '*.swift' -print0 | sort -z | - xargs -0 awk -v exempt="$colors" -v icons="$icons" -v ios_floor="$ios_floor" -v mac_floor="$mac_floor" ' + xargs -0 awk -v tokens_file="$tokens" -v icons="$icons" -v ios_floor="$ios_floor" -v mac_floor="$mac_floor" ' # mask(s): string contents become spaces, so a match in M is always code. # Interpolated code ("\(x)") stays visible, its "\(" written as " (". function mask(s, i, c, instr, n, pd, out) { @@ -202,13 +203,14 @@ find "$src" -name '*.swift' -print0 | sort -z | if (amask[1] ~ /^[[:space:]]*(let|var)[[:space:]]/ || before ~ /(^|[^A-Za-z0-9_])case([[:space:]]+(let|var))?[[:space:]]*$/) continue # A name that is not one literal, looked up in the package bundle, is not # checked against the catalog. Icon_DashUI.swift and Color+DashUI.swift are - # exempt: they hold the resolvers for DashIcon cases and Color.dash tokens, - # whose names rules 1 and 2 check where they are declared. In this package - # only DashIconSource has a .custom case that takes a bundle. A literal - # with an escape counts as computed: its name is not read below. + # exempt from this check only: they hold the resolvers for DashIcon cases + # and Color.dash tokens, whose names rules 1 and 2 check where they are + # declared. In this package only DashIconSource has a .custom case that + # takes a bundle. A literal with an escape counts as computed: its name is + # not read below. k = arg(label) computed = (amask[1] !~ /^[[:space:]]*"[[:space:]]*"[[:space:]]*$/ || index(atop[1], "\\")) - if (k && pkg(amask[k]) && computed && file != icons && file != exempt) + if (k && pkg(amask[k]) && computed && file != icons && file != tokens_file) out(li, "asset name is not one literal, so the catalog check cannot follow it; use " (kind == "colorset" ? "a Color.dash token" : "a DashIcon case (DashIcon...source or .image)")) # The literal names in the call, each once. "a" + b is an expression, not # a name. @@ -244,9 +246,8 @@ find "$src" -name '*.swift' -print0 | sort -z | # Rule 5: colors from components. function colors(li, ml, base, open, first, k, hit) { - if (file == exempt) return ml = M[li]; base = 0 - if (ml ~ /#colorLiteral/) out(li, "hardcoded color: add a token to Foundation/Color+DashUI.swift backed by a colorset and use Color.dash.") + if (ml ~ /#colorLiteral/) out(li, "hardcoded color: add a colorset and a token for it in Foundation/Color+DashUI.swift (dashAsset(\"Name\")), and use Color.dash.") while (match(substr(ml, base + 1), /((^|[^A-Za-z0-9_])(Color|UIColor|NSColor|CGColor|CIColor)[[:space:]]*(\.init)?|\.init)[[:space:]]*\(/)) { open = base + RSTART + RLENGTH - 1 base = open @@ -255,7 +256,7 @@ find "$src" -name '*.swift' -print0 | sort -z | for (k = 1; k <= nargs; k++) if (alabel[k] ~ /^(red|white|hue|gray|displayP3Red|srgbRed|hex|calibratedRed|calibratedWhite|calibratedHue|deviceRed|deviceWhite|deviceHue|genericGamma22White)$/) hit = 1 if (hit) - out(li, "hardcoded color: add a token to Foundation/Color+DashUI.swift backed by a colorset and use Color.dash.") + out(li, "hardcoded color: add a colorset and a token for it in Foundation/Color+DashUI.swift (dashAsset(\"Name\")), and use Color.dash.") } } @@ -321,7 +322,7 @@ find "$src" -name '*.swift' -print0 | sort -z | # A token whose name is not one literal is unchecked, like a computed name # in rule 3. function tokens(li, ml, base, open, s, before) { - if (file != exempt) return + if (file != tokens_file) return ml = M[li]; base = 0 while (match(substr(ml, base + 1), /(^|[^A-Za-z0-9_])dashAsset[[:space:]]*\(/)) { before = lead(li, substr(ml, 1, base + RSTART - 1)) diff --git a/.github/scripts/tests/conventions-test.sh b/.github/scripts/tests/conventions-test.sh index e58dc44..bfad3cc 100755 --- a/.github/scripts/tests/conventions-test.sh +++ b/.github/scripts/tests/conventions-test.sh @@ -128,6 +128,10 @@ expect "no imagesets at all" fail 'no imageset found' fresh; dir=$(first_set colorset); cp -R "$dir" "${dir%/*}/OrphanColor.colorset" expect "orphan colorset" fail 'colorset "OrphanColor" is not referenced' +fresh; echo 'let fixtureShadow = Color(red: 0.72, green: 0.76, blue: 0.8, opacity: 0.1)' >> "$work/tree/$src/Foundation/Color+DashUI.swift" +expect "component color in Color+DashUI.swift" fail \ + "Color+DashUI.swift:$(wc -l < "$work/tree/$src/Foundation/Color+DashUI.swift" | tr -d ' '): hardcoded color" + fresh; echo 'let fixtureToken = dashAsset( "NoSuchToken" )' >> "$work/tree/$src/Foundation/Color+DashUI.swift" expect "token without colorset" fail 'Color+DashUI.swift:' 'no colorset named "NoSuchToken"' diff --git a/Sources/DashUIKit/Foundation/Color+DashUI.swift b/Sources/DashUIKit/Foundation/Color+DashUI.swift index bc25f84..2b290fa 100644 --- a/Sources/DashUIKit/Foundation/Color+DashUI.swift +++ b/Sources/DashUIKit/Foundation/Color+DashUI.swift @@ -16,9 +16,6 @@ // import SwiftUI -#if canImport(UIKit) -import UIKit -#endif public extension Color { static var dash: DashColors.Type { DashColors.self } @@ -289,22 +286,8 @@ public enum DashColors { // MARK: Shadow - public static var shadow: Color { - #if canImport(UIKit) - Color( - UIColor { traitCollection in - switch traitCollection.userInterfaceStyle { - case .dark: - return .clear - default: - return UIColor(red: 0.72, green: 0.76, blue: 0.8, alpha: 0.1) - } - } - ) - #else - .clear - #endif - } + /// Subtle on light, clear on dark. + public static var shadow: Color { dashAsset("Shadow") } } private extension DashColors { diff --git a/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Contents.json b/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Shadow.colorset/Contents.json b/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Shadow.colorset/Contents.json new file mode 100644 index 0000000..e078252 --- /dev/null +++ b/Sources/DashUIKit/Resources/Media.xcassets/Colors/Shadow/Shadow.colorset/Contents.json @@ -0,0 +1,38 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "0.100", + "blue" : "0.800", + "green" : "0.760", + "red" : "0.720" + } + }, + "idiom" : "universal" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "color" : { + "color-space" : "srgb", + "components" : { + "alpha" : "0.000", + "blue" : "0.800", + "green" : "0.760", + "red" : "0.720" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/docs/foundation.md b/docs/foundation.md index 3da3974..910e78b 100644 --- a/docs/foundation.md +++ b/docs/foundation.md @@ -37,7 +37,7 @@ Token groups (see the source for the full list): - **Raw tokens:** `blue` (+ `blueAlpha5…90`), `black`/`black1000Alpha5…90`/`black800/900`, `gray50…500` (+ alpha variants), `green`/`greenAlpha10`, `red`/`redAlpha5/10`, `white`/`whiteAlpha5…90`, `orange`, `yellow`, `lightBlue`, `purple`, `topper`, `uphold` -- **`shadow`** — a code-defined adaptive color (subtle on light, `.clear` on dark); +- **`shadow`** — the `Shadow` colorset (subtle on light, clear on dark); used by `MenuViewModifier`. Adding a color: add the color set to the catalog, then add a matching diff --git a/docs/navigation-and-containers.md b/docs/navigation-and-containers.md index 4aaa394..79cde30 100644 --- a/docs/navigation-and-containers.md +++ b/docs/navigation-and-containers.md @@ -229,4 +229,4 @@ VStack(spacing: 0) { /* rows */ } .modifier(MenuViewModifier(shadowRadius: 10, innerPadding: 6)) ``` -The shadow uses `Color.dash.shadow`, which is adaptive (subtle on light, `.clear` on dark). +The shadow uses `Color.dash.shadow`, the `Shadow` colorset (subtle on light, clear on dark).