Repository navigation
feat(swift): add A2UICardStyle, A2UIButtonStyle, and Surface.makeWithBasicCatalogs - #4024
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a customizable styling system for A2UI components in SwiftUI, refactoring A2UIButton and A2UICard to delegate rendering to style protocols (A2UIButtonStyle and A2UICardStyle) and providing default, elevated, and outlined style implementations. It also adds environment-based catalog and theme propagation utilities alongside comprehensive unit tests. The review feedback suggests preserving the existing default catalog ID in a2uiCatalogs when not explicitly overridden, using configuration.isDisabled instead of !configuration.node.isValid for button opacity calculations, and replacing iOS 17+ specific .rect shapes with RoundedRectangle to maintain backward compatibility and consistency.
|
cc @pinieb for review when you get a chance! |
5347608 to
919316c
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces customizable styling capabilities for A2UI SwiftUI components, specifically adding A2UIButtonStyle and A2UICardStyle protocols along with several default implementations. It also adds environment modifiers to easily register catalogs, themes, and styles. The feedback focuses on improving environment integration and backward compatibility: specifically, A2UIButton should respect the ancestor isEnabled state, and the code should avoid using the @entry macro and newer shape/overlay APIs (like .rect and overlay(_:in:)) to maintain compatibility with Swift 5.9 and older OS versions (iOS 15/16).
919316c to
147c0eb
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces customizable styling capabilities for A2UI SwiftUI components by defining A2UIButtonStyle and A2UICardStyle protocols, along with their default, elevated, and outlined implementations. It also updates Surface to support environment-based resolution of catalogs, themes, and default catalog IDs. The feedback highlights critical compilation and backward compatibility issues (iOS 15/16) in the style definitions due to the use of .rect(cornerRadius:) and .stroke(lineWidth:) inside overlays, recommending a fallback to RoundedRectangle and .strokeBorder. Additionally, it suggests caching basic catalogs to avoid performance bottlenecks from redundant recreations during view initialization.
|
All automated review feedback has been addressed — ready for your review whenever you get a chance, @pinieb! |
53750ae to
f60b3de
Compare
|
I need a bit more time with this PR. I'm working to resolve how some of these new APIs interact the blueprint's requirements and haven't quite settled on that yet. It is on my radar though, and I have started poking at it. |
f60b3de to
37becc3
Compare
|
From triage bot: Triage suggestion: For humansSummary: Why flagged for triage: Suggestion:
To adjust and apply this suggestion, run in your a2ui checkout: tools/internal/triage_new/triage_ui.sh https://github.com/a2ui-project/a2ui/pull/4024For software🤖 Agent-generated |
pinieb
left a comment
There was a problem hiding this comment.
Thank for you bearing with me, I went back and forth for a while about what to do with the Environment pieces of this PR.
The tl;dr: is that I think we should not adopt the Environment changes in this PR, but I do want to take the custom component styles.
The longer story is that I do see value in potentially letting clients populate catalogs through the Environment, but I'm not sure what the right way to reconcile that with the other API goals is at the moment. Because of that, I would rather skip the Environment changes in his PR and spend some time thinking about where the API surface should head.
This PR brought my attention to some pitfalls in the current intended usage (e.g. the Surface initializer defaults its catalog dictionary to [:], and a2uiCatalogs is publicly writable above/outside of the Surface hierarchy`) that I would like to shore up as well.
From the standpoint of allowing clients to customize the appearance of Basic Catalog components, I think styles are the right approach. theme has been replaced on the v1.0 Catalog schema, and outright removed from the Basic Catalog schema. While its replacement could be used to deliver theming data in other catalog implementations, the Basic Catalog starting in v1.0 does not.
37becc3 to
8ea67cf
Compare
|
Thanks for taking the time to think this through, @pinieb, and great catch on Stripping out the I also left a quick note on #4195 with the use case that prompted the environment modifier idea in the first place so we don't lose track of it. |
Signed-off-by: Peter Friese <peter@peterfriese.de>
…ck in makeWithBasicCatalogs Signed-off-by: Peter Friese <peter@peterfriese.de>
8ea67cf to
7dd9900
Compare
pinieb
left a comment
There was a problem hiding this comment.
Thanks again for coming along with me on this one, and thank you for the PR!
…#4361) ## Problem #3257 asks for a way to restyle the SwiftUI Basic Catalog components. #4024 added `A2UIButtonStyle` and `A2UICardStyle`, and the issue stays open for `Text`, `TextField` and `Image`. `A2UITextField` still hard-codes its look: 10 pt padding, a gray fill, an 8 pt corner radius, and a border and label that turn red while a check fails. To change that, an app has to replace the whole component, copying the data binding, the variants and the accessibility modifiers. Part of #3257. This PR covers `TextField` only. `Text` and `Image` are also changed by the open #4123. ## Changes - Adds `A2UITextFieldStyle`, `A2UITextFieldStyleConfiguration` and `View.a2uiTextFieldStyle(_:)` to `BasicCatalogSwiftUI`, built the same way as `A2UIButtonStyle` in #4024. The protocol refines `DynamicProperty` and the style is resolved through a generic wrapper view, so a style's `@Environment` values (such as `\.colorScheme`) resolve. - The configuration has: - `label`: the label text, or `nil` when the `label` property is empty. It stays hidden from assistive technologies, as before. - `field`: the input control for the variant (`SecureField` for `obscured`, a vertical `TextField` for `longText`, the decimal pad on iOS for `number`), bound to `value` and carrying the accessibility identifier, label and hint. - `variant` (`shortText`, `longText`, `number`, `obscured`; unknown values resolve to `shortText`), `errorMessage` (the first failing `error` check) with `hasError`, `isDisabled` (from the environment's `isEnabled`), and `node`. - `DefaultA2UITextFieldStyle` (`.automatic`) draws what `A2UITextField` drew before, with the same values. - `A2UITextField` still renders `A2UICheckMessage` below the style's view, so errors and warning/info hints look the same under any style. The accessibility modifiers moved from the padded box onto the input control, so a custom style can't drop them. They still reach the same native text field. - The style is read in a private host view inside `A2UITextField.body`, so tests that call `.body` outside a hosted view don't read the environment. - New tests in `ComponentStyleTests`: - a custom style gets the label, variant, error message, disabled state and node; - an unknown variant resolves to `shortText`, and an ancestor `.disabled(true)` sets `isDisabled`; - a style resolves `\.colorScheme` in Dark Mode inside a `Surface`; - the default style renders every variant with a failing check; - under a custom style, the `a2ui-<id>` element in the platform accessibility tree has the explicit accessibility label and hint, or the `label` property when there is no explicit label. The existing `A2UITextField`, check message, accessibility and adapter conformance tests pass unchanged. - A "Styling Basic Catalog components" section in `swift/README.md` with a text field style example, and a `swift/CHANGELOG.md` entry. Verified on macOS (Xcode toolchain, Swift 6.1 package): - `swift build` - `swift test --enable-swift-testing --no-parallel --filter A2UISwiftUITests`: 203 tests. All pass except `A2UIModalTests.keyboardActivationOfButtonTriggerRunsActionOnceAndPresentsSheet`, which also fails on `main` when run headless. - `swift test --enable-swift-testing --no-parallel --skip A2UISwiftUITests`: 809 tests pass. - `scripts/fix_format.sh --check --only-swift` and `swift-format lint --strict` on the changed files. ## Pre-launch Checklist One time: - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I read the [Style Guide]. - [ ] I have built and run at least one [sample]. For this PR: - [x] I have updated the relevant CHANGELOG.md file. - [x] I updated/added relevant documentation. - [x] My code changes (if any) have tests. - [ ] If my branch is on a fork, I have verified that [scripts/e2e_test.sh](https://github.com/a2ui-project/a2ui/blob/main/scripts/e2e_test.sh) passes. If you need help, consider asking for advice on the [discussion board]. <!-- Links --> [CLA]: https://cla.developers.google.com/ [Contributors Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md [discussion board]: https://github.com/a2ui-project/a2ui/discussions [Style Guide]: https://github.com/a2ui-project/a2ui/blob/main/CONTRIBUTING.md#coding-style [sample]: https://github.com/a2ui-project/a2ui/blob/main/samples/README.md#samples --------- Co-authored-by: Polina Cherkasova <polina.c@live.com>
Description
Adds SwiftUI component style protocols (
A2UICardStyle,A2UIButtonStyle) andSurface.makeWithBasicCatalogs(viewModel:)toBasicCatalogSwiftUI, and requires an explicitcatalogsargument onSurface.init(viewModel:catalogs:defaultCatalogID:), addressing #3257.Changes
SurfaceCatalog Configuration:= []default argument onSurface.init(viewModel:catalogs:defaultCatalogID:)inA2UISwiftUIso passing an empty catalog array is an explicit choice rather than an accidental omission.Surface.makeWithBasicCatalogs(viewModel:)inBasicCatalogSwiftUI(Surface+BasicCatalog.swift) to create aSurfacepreconfigured withBasicCatalogImplementation.allCatalogs(v0.9,v0.9.1, andv1.0), falling back toBasicCatalog.catalogURI(version: viewModel.protocolVersion)whenviewModel.defaultCatalogIDisnil.BasicCatalogSwiftUI):A2UICardStyle(refiningDynamicProperty, Sendable) andA2UICardStyleConfiguration, with built-in.automatic(DefaultA2UICardStyle),.elevated(ElevatedA2UICardStyle), and.outlined(OutlinedA2UICardStyle) styles, applied via.a2uiCardStyle(_:).A2UIButtonStyle(refiningDynamicProperty, Sendable) andA2UIButtonStyleConfiguration(exposingvariant,isPressed,isDisabled, andnode), with.automatic(DefaultA2UIButtonStyle), applied via.a2uiButtonStyle(_:).ComponentStyleTestscovering customA2UICardStyle/A2UIButtonStyleconfigurations,@Environmentresolution (e.g.\.colorSchemein Dark Mode) viaDynamicProperty, and built-in card/button styles.makeWithBasicCatalogsConfiguresAllVersionsAndProtocolFallbackinSurfaceTestsand updatesSurfaceTestsfor explicitcatalogs: [].Related to #3257 and #4195.
Pre-launch Checklist
One time:
For this PR: