Skip to content
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
27 changes: 14 additions & 13 deletions .github/scripts/conventions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.<Group>.<case>.source or .image)"))
# The literal names in the call, each once. "a" + b is an expression, not
# a name.
Expand Down Expand Up @@ -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.<token>")
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.<token>")
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
Expand All @@ -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.<token>")
out(li, "hardcoded color: add a colorset and a token for it in Foundation/Color+DashUI.swift (dashAsset(\"Name\")), and use Color.dash.<token>")
}
}

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions .github/scripts/tests/conventions-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"'

Expand Down
21 changes: 2 additions & 19 deletions Sources/DashUIKit/Foundation/Color+DashUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
//

import SwiftUI
#if canImport(UIKit)
import UIKit
#endif

public extension Color {
static var dash: DashColors.Type { DashColors.self }
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion docs/foundation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/navigation-and-containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Loading