Skip to content

feat(craft): preview polish: field labels, labelled options, empty catalogs, Wasm tests - #81

Open
innkuika wants to merge 1 commit into
craft/studio-previewfrom
craft/polish
Open

innkuika wants to merge 1 commit into
craft/studio-previewfrom
craft/polish

Conversation

@innkuika

@innkuika innkuika commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

From writing the examples and the getting-started guide:

  • Building blocks: TextInput, Select and DatePicker draw their label above
    the field (label style, disabled color, tapping it focuses the field; the
    field keeps its semantics name, and its text and focus when the label
    comes or goes). Select options may be {label, value} pairs. The example
    app and the docs drop their Text workarounds.
  • templateCatalog reports a catalog made with no components, with the reason
    (emptyCatalogReason: no template library yet, not a template library, no
    exported declaration, none available): to onEmpty, else to
    FlutterError.reportError and a failing assertion in debug builds.
  • Parser: positions at a line break, and at the end of a source that is
    empty or ends with one, are at column 1 instead of 0; specified in
    craft_text_format, conformance cases pin the columns.
  • TemplateRegistry keeps an unmodifiable deep copy of each template library
    (open nit from feat(craft): add the template registry #28), switch keys included, and gives back a new copy of it
    with templateLibrary, so source positions set on what is read back don't
    move the positions it reports; templateLibraryNames lists them.
  • The conformance harness loads its files under dart2wasm (Node's fs via
    process.getBuiltinModule); CI runs the engine tests compiled to Wasm, and
    the building blocks tests compiled to Wasm in Chrome. No engine case is
    skipped there; web_skips.dart would list one, with its reason.
  • dart2wasm 3.14 traps when a switch over an Object? matches string
    constants against a value of another type: primitiveArg (metadata such
    as false) and decodeExtent (every numeric size) test for a String
    first.
  • fix_format.sh passes dart format's page width and trailing comma style, so
    it no longer reads analysis_options.yaml and warns about unresolved
    package: includes when no pub get ran.
  • Example app: a dark theme; cards have no opaque background, captions use
    the host's text color, and errors use it too next to a red icon, since no
    one red reaches 4.5:1 on both themes; badge colors darkened to 4.5:1. A
    test checks every text (4.5:1, 3:1 when large) and icon (3:1) in both
    themes, for every flight status.

@innkuika
innkuika requested a review from ditman December 3, 2026 04:08
@innkuika
innkuika changed the base branch from craft/examples to craft/studio-preview December 3, 2026 04:09
@innkuika
innkuika added this pull request to stack #51 December 3, 2026 04:09

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces several enhancements to the Craft engine and A2UI integration, including visible field labels for input controls, support for labelled options in Select blocks, and improved reporting for empty catalogs. It also updates the template registry to use deep copies of template libraries to prevent external modifications, refines line/column error reporting to be 1-based, and adds dark mode contrast tests. The review feedback highlights opportunities to simplify pattern matching in selectOptions and recommends defensive null-checks in the newly added dark_mode_test.dart to prevent potential NullCheckError crashes during testing.

Comment on lines +519 to +537
Map<String, String> selectOptions(Object? options) {
final Map<String, String> result = <String, String>{};
if (options is! List<Object?>) return result;
for (final Object? option in options) {
switch (option) {
case final String value:
result.putIfAbsent(value, () => value);
case {'value': final String value}:
result.putIfAbsent(
value,
() => switch (option['label']) {
final String label => label,
_ => value,
},
);
}
}
return result;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

We can simplify the pattern matching in selectOptions by matching both 'value' and 'label' directly in the map pattern. This avoids the nested switch expression and makes the code more concise and readable.

Map<String, String> selectOptions(Object? options) {
  final Map<String, String> result = <String, String>{};
  if (options is! List<Object?>) return result;
  for (final Object? option in options) {
    switch (option) {
      case final String value:
        result.putIfAbsent(value, () => value);
      case {'value': final String value, 'label': final String label}:
        result.putIfAbsent(value, () => label);
      case {'value': final String value}:
        result.putIfAbsent(value, () => value);
    }
  }
  return result;
}

Comment on lines +56 to +60
bool _isLarge(TextStyle style) {
final double size = style.fontSize!;
final FontWeight weight = style.fontWeight ?? FontWeight.normal;
return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

In Dart, TextStyle.fontSize and TextStyle.fontWeight are nullable. To prevent potential NullCheckError runtime exceptions during widget tests, we should defensively handle null values for style and its properties.

Suggested change
bool _isLarge(TextStyle style) {
final double size = style.fontSize!;
final FontWeight weight = style.fontWeight ?? FontWeight.normal;
return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value);
}
bool _isLarge(TextStyle? style) {
final double? size = style?.fontSize;
if (size == null) return false;
final FontWeight weight = style?.fontWeight ?? FontWeight.normal;
return size >= 24 || (size >= 18.66 && weight.value >= FontWeight.bold.value);
}

Comment on lines +64 to +86
void _checkTexts(WidgetTester tester, String where) {
int checked = 0;
for (final Element e
in find
.descendant(
of: find.byType(A2uiSurface),
matching: find.byType(RichText),
)
.evaluate()) {
final TextSpan span = (e.renderObject! as RenderParagraph).text as TextSpan;
final String text = span.toPlainText();
if (text.trim().isEmpty) continue;
final TextStyle style = span.style!;
final bool icon = e.findAncestorWidgetOfExactType<Icon>() != null;
expect(
_contrast(style.color!, _background(e)),
greaterThanOrEqualTo(icon || _isLarge(style) ? 3 : 4.5),
reason: '$where: "$text" in ${style.color}, ${style.fontSize} px',
);
checked += 1;
}
expect(checked, greaterThan(5), reason: where);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

In _checkTexts, span.style and style.color are nullable. If a text span does not have an explicit style or color, calling span.style! or style.color! will throw a NullCheckError and crash the test. We should defensively check for nulls.

void _checkTexts(WidgetTester tester, String where) {
  int checked = 0;
  for (final Element e
      in find
          .descendant(
            of: find.byType(A2uiSurface),
            matching: find.byType(RichText),
          )
          .evaluate()) {
    final TextSpan span = (e.renderObject! as RenderParagraph).text as TextSpan;
    final String text = span.toPlainText();
    if (text.trim().isEmpty) continue;
    final TextStyle? style = span.style;
    final Color? color = style?.color;
    if (color == null) continue;
    final bool icon = e.findAncestorWidgetOfExactType<Icon>() != null;
    expect(
      _contrast(color, _background(e)),
      greaterThanOrEqualTo(icon || _isLarge(style) ? 3 : 4.5),
      reason: '$where: "$text" in $color, ${style?.fontSize} px',
    );
    checked += 1;
  }
  expect(checked, greaterThan(5), reason: where);
}

…talogs, Wasm tests

From writing the examples and the getting-started guide:

- Building blocks: TextInput, Select and DatePicker draw their `label` above
  the field (label style, disabled color, tapping it focuses the field; the
  field keeps its semantics name, and its text and focus when the label
  comes or goes). Select options may be `{label, value}` pairs. The example
  app and the docs drop their `Text` workarounds.
- templateCatalog reports a catalog made with no components, with the reason
  (`emptyCatalogReason`: no template library yet, not a template library, no
  exported declaration, none available): to `onEmpty`, else to
  FlutterError.reportError and a failing assertion in debug builds.
- Parser: positions at a line break, and at the end of a source that is
  empty or ends with one, are at column 1 instead of 0; specified in
  craft_text_format, conformance cases pin the columns.
- TemplateRegistry keeps an unmodifiable deep copy of each template library
  (open nit from #28), switch keys included, and gives back a new copy of it
  with `templateLibrary`, so source positions set on what is read back don't
  move the positions it reports; `templateLibraryNames` lists them.
- The conformance harness loads its files under dart2wasm (Node's fs via
  process.getBuiltinModule); CI runs the engine tests compiled to Wasm, and
  the building blocks tests compiled to Wasm in Chrome. No engine case is
  skipped there; web_skips.dart would list one, with its reason.
- dart2wasm 3.14 traps when a switch over an `Object?` matches string
  constants against a value of another type: `primitiveArg` (metadata such
  as `false`) and `decodeExtent` (every numeric size) test for a String
  first.
- fix_format.sh passes dart format's page width and trailing comma style, so
  it no longer reads analysis_options.yaml and warns about unresolved
  `package:` includes when no `pub get` ran.
- Example app: a dark theme; cards have no opaque background, captions use
  the host's text color, and errors use it too next to a red icon, since no
  one red reaches 4.5:1 on both themes; badge colors darkened to 4.5:1. A
  test checks every text (4.5:1, 3:1 when large) and icon (3:1) in both
  themes, for every flight status.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant