Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements the media_context feature across the Craft engine and its Flutter adapter. It introduces the MediaContext class, which quantizes surface dimensions into logical size classes, orientation, platform, and reduced-motion preferences. It adds the standard function atLeast for comparing size classes, integrates media references into the evaluator and runtime, and updates CraftView in Flutter to automatically resolve and update the media context from layout constraints or window queries. Comprehensive unit, widget, and conformance tests have been added to validate the implementation. I have no feedback to provide.
b8d3022 to
382122f
Compare
382122f to
896a28b
Compare
896a28b to
d2ea5f6
Compare
d2ea5f6 to
8a20283
Compare
8a20283 to
550ed6c
Compare
550ed6c to
663745b
Compare
ditman
left a comment
There was a problem hiding this comment.
🤖 Clean, well-specified media context layer — the quantized size-class model (classes rather than pixels), single-frame resize expansion with deferred listener notification, and 29 conformance cases are very solid.
A few items to tighten before (or cleanly alongside) landing:
- Post-stack barrel tightening reminder (
lib/a2ui_craft.dart): Per our agreement on #23, please includesrc/media_context.dartwhen switchinglib/a2ui_craft.dartto explicitshowlists (export 'src/media_context.dart';exposes internal engine plumbingreadMediaandmediaFunctionsalongside the public model, and#39still leaves it as a wildcard). - Avoid unconditional
MediaQuery.sizeOf(context)inMediaSource.layout(craft_view.dart): ReadingMediaQuery.sizeOf(context)unconditionally insideLayoutBuildersubscribesCraftViewto window resizes even when its own layout constraints are bounded and unchanged, and throws if there is noMediaQueryancestor. Only queryMediaQueryfor unbounded axes (or usemaybeSizeOf). - Guard
_notifyingMediain the post-frame callback (craft_view.dart): Ifwidget.runtimeis swapped or the view is disposed before the post-frame callback runs, setting_notifyingMedia = truearoundoldRuntime.refresh()can mask notifications from the new runtime.
(Also noted that MediaSource.layout intrinsic-size handling is already addressed in draft #39 via SpaceBuilder, so I left that out of this review.)
| export 'src/evaluator.dart'; | ||
| export 'src/expansion.dart'; | ||
| export 'src/functions.dart'; | ||
| export 'src/media_context.dart'; |
There was a problem hiding this comment.
Reminder for the post-stack barrel-tightening pass agreed in #23: please include src/media_context.dart when switching lib/a2ui_craft.dart to explicit show lists so internal plumbing stays private:
export 'src/media_context.dart'
show HostPlatform, HeightClass, MediaContext, SurfaceOrientation, WidthClass;Right now export 'src/media_context.dart'; also exposes internal engine plumbing (readMedia and mediaFunctions) on package:a2ui_craft/a2ui_craft.dart (and draft #39 still leaves this line as a wildcard export).
| return LayoutBuilder( | ||
| builder: (BuildContext context, BoxConstraints constraints) { | ||
| final Size window = MediaQuery.sizeOf(context); | ||
| _setMedia( | ||
| context, | ||
| Size( | ||
| constraints.hasBoundedWidth | ||
| ? constraints.maxWidth | ||
| : window.width, | ||
| constraints.hasBoundedHeight | ||
| ? constraints.maxHeight | ||
| : window.height, | ||
| ), | ||
| ); | ||
| return _renderRoot(); | ||
| }, | ||
| ); |
There was a problem hiding this comment.
Calling MediaQuery.sizeOf(context) unconditionally inside LayoutBuilder has two downsides when both constraints.hasBoundedWidth and constraints.hasBoundedHeight are true:
- It subscribes
CraftViewto every window-size change (e.g., soft keyboard or unrelated split-pane resize) even when its own bounded layout constraints did not change. - It throws if
CraftViewis hosted in a subtree without aMediaQueryancestor (unlikeMediaQuery.maybeDisableAnimationsOf(context)in_setMedia, which handles absence gracefully).
Only calling MediaQuery.sizeOf(context) when !constraints.hasBoundedWidth || !constraints.hasBoundedHeight avoids both issues when constraints are bounded:
final Size? window =
constraints.hasBoundedWidth && constraints.hasBoundedHeight
? null
: MediaQuery.sizeOf(context);
_setMedia(
context,
Size(
constraints.hasBoundedWidth ? constraints.maxWidth : window!.width,
constraints.hasBoundedHeight ? constraints.maxHeight : window!.height,
),
);(Note: when #39 replaces LayoutBuilder with SpaceBuilder, MediaQuery.maybeSizeOf(context) is still worth considering if missing-MediaQuery safety is desired.)
Drop-in regression test for craft/packages/a2ui_craft_flutter/test/craft_view_test.dart:
testWidgets('bounded layout CraftView renders without a MediaQuery ancestor', (
WidgetTester tester,
) async {
final _Harness h = _Harness('''
import core;
widget root = Text(text: media.width);
''');
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Align(
alignment: Alignment.topLeft,
child: SizedBox(
width: 400,
height: 300,
child: CraftView(
runtime: h.runtime,
libraries: <LibraryName, PrimitiveWidgetLibrary>{
coreLibrary: _testWidgets,
},
),
),
),
),
);
expect(tester.takeException(), isNull);
expect(find.text('compact'), findsOneWidget);
});| SchedulerBinding.instance.addPostFrameCallback((Duration _) { | ||
| _mediaNotificationScheduled = false; | ||
| _notifyingMedia = true; | ||
| try { | ||
| runtime.refresh(); | ||
| } finally { | ||
| _notifyingMedia = false; | ||
| } | ||
| }); |
There was a problem hiding this comment.
🤖 If CraftView is updated with a different widget.runtime (or disposed) before this post-frame callback runs, runtime.refresh() still executes with _notifyingMedia = true. If a listener on the old runtime mutates the new widget.runtime, _changed() on the new runtime would be swallowed.
Checking final bool same = mounted && identical(runtime, widget.runtime); and only setting _notifyingMedia = same (while still notifying runtime.refresh()) avoids masking notifications across a runtime swap.
Drop-in regression test for craft/packages/a2ui_craft_flutter/test/media_test.dart:
testWidgets('swapping runtime before media post-frame does not mask changes', (
WidgetTester tester,
) async {
_largeSurface(tester);
final _Harness h1 = _Harness('''
import core;
widget root = Text(text: media.width);
''');
final _Harness h2 = _Harness('''
import core;
widget root = Text(text: data.v);
''');
h2.runtime.data = <String, Object?>{'v': 'initial'};
h1.runtime.addListener(() {
h2.runtime.data = <String, Object?>{'v': 'updated'};
});
CraftRuntime current = h1.runtime;
late StateSetter setHostState;
SchedulerBinding.instance.addPostFrameCallback((Duration _) {
setHostState(() => current = h2.runtime);
tester.binding.buildOwner!.buildScope(tester.binding.rootElement!);
});
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
setHostState = setState;
return _sized(
CraftView(
runtime: current,
libraries: <LibraryName, PrimitiveWidgetLibrary>{
coreLibrary: _widgets,
},
media: MediaSource.window,
),
width: 400,
height: 600,
window: const Size(400, 600),
);
},
),
);
await tester.pump();
expect(find.text('updated'), findsOneWidget);
});ff4c36c to
a510a81
Compare
Adds the craft_media_context feature: what a host tells templates about
the surface they render on, as classes rather than pixels, so that one
template adapts to a phone, a tablet or a desktop with `switch
media.width { ... }` or `atLeast(a: media.width, b: "expanded")`.
- craft/blueprints/features/craft_media_context.blueprint.md: the
properties (width class compact/medium/expanded/large/extraLarge,
height class compact/medium/expanded, orientation, optional platform,
reducedMotion) and their media map; the size classes of a size in
logical pixels (width breakpoints 600/840/1200/1600 as Material 3's
window size classes, height 480/900 as Android's; landscape when wider
than tall; negative or NaN refused); media references read the media
map, absent for anything else and without a media context, in the
evaluator and the runtime; the standard function atLeast, absent for
names that are no size class; the runtime's media context, which
notifies only when it changes (or, when the host asks, not at all, for
it to notify later) and keeps state, arguments and data.
- craft/conformance/craft/craft_media_context.yaml: 29 cases (media maps
of sizes, values through the evaluator, runtime scenarios with media
changes and notifications), run by
test/conformance/media_context_test.dart; its runtime scenarios use
the expand_template action of test/conformance/runtime_engine.dart.
- packages/a2ui_craft: lib/src/media_context.dart (MediaContext,
WidthClass, HeightClass, SurfaceOrientation, HostPlatform, readMedia,
mediaFunctions); Evaluator.evaluate, TemplateRegistry.evaluate and
CraftRuntime take a media context; TemplateRegistry adds atLeast to the
function tables.
- packages/a2ui_craft_flutter: CraftView.media (MediaSource.layout by
default, window, host) sets the runtime's media context from the view's
layout constraints or the MediaQuery size, with the platform and the
reduced-motion preference, before it expands, so a resize across a
class renders in the same frame, and notifies the runtime's other
listeners after that frame (CraftRuntime.setMedia(notify: false) and
refresh), so a listening host never rebuilds during a build; widget
tests.
- functions, runtime and craft_template_registry blueprints, the module
table and the codebase blueprints updated for media references and
atLeast.
- Conformance: the media_map, media_sizes and evaluate_media actions,
expand_template's media context, and the F-CRAFT-MEDIA-01 to 05 feature
IDs.
Based on the Craft prototype's media_context.dart and the atLeast
function. Changed from it: the media context belongs to the runtime and
is observable through the evaluator and expansions; atLeast is absent for
unknown names instead of reading them as compact; the form factor,
capabilities and their flags, the rendering engine and the text scale are
left out; the Flutter adapter measures the space itself.
Co-authored-by: Yegor <yegor.jbanov@gmail.com>
a510a81 to
7007ef5
Compare
Description
Adds the media context, so a template can adapt to the surface it renders on:
switch media.width { "compact": ..., default: ... }. Stacked on #31. Same three parts as the other engine PRs: a blueprint, conformance cases and the code.craft/blueprints/features/craft_media_context.blueprint.md.width:compactbelow 600,mediumbelow 840,expandedbelow 1200,largebelow 1600,extraLargeabove (Material 3 window size classes);height:compactbelow 480,mediumbelow 900,expandedabove;orientation,platformandreducedMotion.media.<parts>evaluates in the evaluator and the runtime. Without a context, or for unknown parts, it is absent, so aswitchfalls to its default. The new standard functionatLeast(a, b)compares classes. A media change re-expands the template and keeps its state.CraftViewderives the context from the space the view is given (LayoutBuilder), from the window (MediaQuery), or leaves it to the host. A resize across a class re-renders in the same frame with one expansion; a resize within a class does nothing. Other listeners on the runtime are notified after the frame.craft/conformance/craft/craft_media_context.yaml, plus unit and widget tests.theme.<parts>) stay undefined; design tokens come with the building blocks.media_context.dartandatLeaststarted in yjbanov/a2ui_craft ataece53f, hence the co-author. An independent review checked the blueprint against the tests and the code, including mutations.Pre-launch Checklist
One time:
For this PR:
python3 ./scripts/fix_licenses.py --check)../scripts/fix_format.sh --check).