Skip to content

refactor(native): Swift themes write colors with SwiftUI's own initializer - #8437

Merged
cixzhang merged 1 commit into
mainfrom
native/swift-color-sdk-initializers
Oct 25, 2026
Merged

cixzhang merged 1 commit into
mainfrom
native/swift-color-sdk-initializers

Conversation

@cixzhang

Copy link
Copy Markdown
Contributor

Based on main. #8420 (CSS reader) and #8422 (diagnostics) stack on this PR.

User need

An iOS developer using Astryx sees two color initializers of Astryx's own, Color(astryxRGBA:) and Color(astryxHex:), next to SwiftUI's. They do exactly what SwiftUI's Color(.sRGB, red:green:blue:opacity:) already does, so they add API to learn and keep, and they make generated theme code read as Astryx-specific when it is plain SwiftUI.

After this change:

  • Generated themes are plain SwiftUI: every color is Color(.sRGB, red: …, green: …, blue: …, opacity: …), readable by anyone who knows SwiftUI.
  • The Astryx module has no color initializers of its own: two public initializers are removed. Nothing else in the public API changes (.astryxTheme, \.astryxTheme, and \.astryx stay).

Problem and solution fit

  • Theme.swift: removes Color(astryxRGBA:) and Color(astryxHex:), the only Astryx-prefixed Color initializers or helpers in the package (searched every .swift file under packages/native/apple for init(astryx, func astryx, and var astryx; the other matches are view modifiers, environment values, and internal font and text helpers, none about color).
  • Generator: one helper, swiftColor(space, hex), writes Color(.sRGB, red:green:blue:opacity:). Each 8-bit channel is 0, 1, or the exact decimal of its step over 255.
  • All eight generated themes regenerated; Tokens.swift is unchanged.
  • Tests: Swift tests build colors with Color(.sRGB, …) directly; the two tests of the removed initializers become one test that the generated default accent equals Color(.sRGB, red: 0, green: 100 / 255, blue: 224 / 255, opacity: 1).
  • Spec: G7 says how a color is written; Current state says the module adds no Color initializer.

Non-test Sources/ usages changed: none. The removed initializers were used only by the generated theme files (regenerated) and by tests.

Current specification

packages/native/specs/swift/library/theme-generator.md (draft) owns how the generator writes values (G7); this PR amends G7 and Current state there.

Public delta and caller burden

Removed (public, experimental canary-only package):

// before
Color(astryxRGBA: 0x0064E0FF)
Color(astryxHex: "#0064E0")

// after: SwiftUI's own
Color(.sRGB, red: 0, green: 100 / 255, blue: 224 / 255, opacity: 1)

Generated theme files, before and after:

- accent: Color(astryxRGBA: 0x0064E0FF),
+ accent: Color(.sRGB, red: 0, green: 0.39215686274509803, blue: 0.8784313725490196, opacity: 1),

A caller who wrote Color(astryxRGBA:) or Color(astryxHex:) switches to Color(.sRGB, …). A product theme regenerates with the same command and needs no other change.

Design and alternatives

  • Exact decimals, not n / 255. Each decimal is the shortest form of n / 255, so it is the same Double Swift computes for 100 / 255 (the Swift test above compares them with ==). With n / 255 in every channel, Swift's type checker took about 1.9 s per theme initializer (measured with -warn-long-expression-type-checking), about 3.5× the module's total type-check time; with decimals it is about 1.5× the old 0xRRGGBBAA form. Rejected: n / 255 literals, for that build-time cost in every app that builds Astryx.
  • No replacement helper. A hex initializer is a convenience an app can write in two lines; the design system does not need to own it.

Existing authority searched

Writing rubric results

R1 narration: none; G7 and Current state describe how colors are written, in the present tense.
R2 research: none in the record; the type-check timing lives only in this PR body.
R3 evidence: the G7 acceptance row cites a concrete Swift test, no PRs.
R4 rejected alternatives: none in the record.
R5 change references: none (searched for #digits, /pull/, /issues/).
R6 open questions: none.

Evidence

  • Drift test regenerates every theme byte-identically to the committed files.
  • swiftc -typecheck passes for the package, tests, and templates.
  • Generated colors equal the old ones channel for channel: the Swift test above compares the generated default accent with Color(.sRGB, red: 0, green: 100 / 255, blue: 224 / 255, opacity: 1) with ==, which is how the removed initializer built it.

Scope

  • One capability is changed.
  • Unrelated fixes, cleanup, and policy changes were removed or split.
  • Consumer docs, focused tests, and migration evidence ship with the change.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm vitest run --project node packages/native --testTimeout=120000   # all pass
pnpm -F @astryxdesign/native generate:swift-ios                       # regenerated; drift test green
pnpm -F @astryxdesign/native typecheck                                # pass
swiftc -typecheck (package, tests, templates)                         # pass
pnpm check:knowledge                                                  # Knowledge templates and records are aligned.

@vercel

vercel Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Oct 25, 2026 7:25pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 25, 2026
@astracat-bot

astracat-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Oct 25, 2026, 7:48 PM UTC

@github-actions

github-actions Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Previews ready: Storybook and Sandbox were built with this PR's docsite. CI evidence will be added when available.

View Storybook for this PR · View Sandbox for this PR

@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec 0998e62

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approval waits on an engineering owner's decision on the new draft requirement in packages/native/specs/swift/library/theme-generator.md G7 (generated colors written as Color(.sRGB, …) with exact decimals, and no color initializer of the package's own): an engineering owner records it by commenting /approve-spec <full head SHA> on this pull request once it is rebased, and the author may do so if they are an engineering owner. Independently, the branch conflicts with main, and two calls added on main still use the removed initializer:

  • In packages/native/apple/Tests/AstryxTests/CardTests.swift, anInsetRingDrawsInsideTheDefaultCardsBorder (lines 141 and 143 on main) calls Color(astryxRGBA:); that file merges without a conflict, so switch both calls to Color(.sRGB, …) during the rebase or the Swift tests stop compiling.
  • Resolve the acceptance-table conflict in theme-generator.md, keeping main's inset-ring sentence, so CI, including the macOS Swift job, runs on the new head.

[Automated review]


extension Color {
/// A color from `0xRRGGBBAA` in sRGB, the form the generated themes use.
public init(astryxRGBA value: UInt32) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

main gained two more callers after this branch was cut: anInsetRingDrawsInsideTheDefaultCardsBorder in Tests/AstryxTests/CardTests.swift (lines 141 and 143 on main) calls Color(astryxRGBA: 0xFF00FFFF) and Color(astryxRGBA: 0x00FFFFFF). That file merges without a conflict, so during the rebase please switch them to Color(.sRGB, red: 1, green: 0, blue: 1, opacity: 1) and Color(.sRGB, red: 0, green: 1, blue: 1, opacity: 1), or the Swift test target won't compile.

…lizer

Generated themes write every color as SwiftUI's
Color(.sRGB, red:green:blue:opacity:), each channel 0, 1, or the exact
decimal of its 8-bit step over 255. The package's own Color(astryxRGBA:)
and Color(astryxHex:) initializers, which duplicated SwiftUI, are removed;
no other public API changes. Every theme is regenerated, Swift tests build
colors with Color(.sRGB, ...) directly, and G7 says how a color is written.
@cixzhang
cixzhang force-pushed the native/swift-color-sdk-initializers branch from 0998e62 to f2862cc Compare October 25, 2026 19:16
@cixzhang

Copy link
Copy Markdown
Contributor Author

/approve-spec f2862cc

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is ready for approval: the draft G7 requirement in packages/native/specs/swift/library/theme-generator.md carries an engineering owner's /approve-spec for this head, every regenerated theme color keeps its exact value, and no caller of the removed Color(astryxRGBA:) or Color(astryxHex:) remains. No changes are needed; the Swift package builds and its tests pass on macOS and the iOS Simulator.

[Automated review]

@cixzhang
cixzhang merged commit 9cd399d into main Oct 25, 2026
71 checks passed
@github-actions
github-actions Bot deleted the native/swift-color-sdk-initializers branch October 26, 2026 06:31

This branch was successfully deployed

1 active deployment
Preview — f2862cc5 Deployed Oct 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant