nandi/frqpublic Fork 0
32dfa6e7f898369d116106e25c4202474502f104
Commits
Clone
git clone https://git.rickub.com/nandi/frq.git
git clone ssh://git@rickub.com/nandi/frq.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

nim_renderer.dart · 674 lines · 25.2 KBDart Blame HistoryRaw
The data model, in Nim d333b6f nandi 23h ago1/// The renderer: a Nim widget tree, walked into Flutter widgets.
2///
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago3/// This knows the tag vocabulary and nothing else — no screens, no state, no
4/// idea what "connect" means. Nim decides what the screen is; this decides
5/// what a `vbox` looks like.
The data model, in Nim d333b6f nandi 23h ago6///
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago7/// The measure of whether the split is honest is how boring this file is. If a
8/// feature ever needs a change here AND in Nim, the boundary is in the wrong
9/// place. The treatments are `flutter/src/frq/hiccup.cljd`'s, so a tree from
10/// Nim paints the way the same tree painted under ClojureDart.
11library;
12
The data model, in Nim d333b6f nandi 23h ago13import 'dart:async';
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago14import 'dart:io';
The data model, in Nim d333b6f nandi 23h ago15
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago16import 'package:flutter/gestures.dart';
17
The data model, in Nim d333b6f nandi 23h ago18import 'package:flutter/material.dart';
19import 'package:frq_core/frq_core.dart' as core;
20
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago21import 'nim_theme.dart' as t;
22
The data model, in Nim d333b6f nandi 23h ago23class NimApp extends StatefulWidget {
24 const NimApp({super.key});
25 @override
26 State<NimApp> createState() => _NimAppState();
27}
28
29class _NimAppState extends State<NimApp> {
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago30 late core.UiFrame _frame = core.renderFrame();
31 core.UiNode get _tree => _frame.tree;
The data model, in Nim d333b6f nandi 23h ago32 Timer? _poll;
33
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago34 // One controller and one focus node per keyed entry, kept across rebuilds.
35 //
36 // This is why `:key` is on every entry in both the Clojure and the Nim: a
37 // controller identified by position instead of name meant the host field and
38 // the port field shared one and both showed the port. The focus node is the
39 // same bug one layer up — the field is rebuilt from a fresh tree on every
40 // keystroke, so without a node held per key the caret goes nowhere after the
41 // first line.
42 final _controllers = <String, TextEditingController>{};
43 final _focus = <String, FocusNode>{};
44
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago45 // One tap recogniser per link URL, kept across rebuilds and disposed with
46 // the state. A recogniser made during build and dropped on the next frame
47 // leaks, and this tree is rebuilt on every keystroke.
48 final _linkTaps = <String, TapGestureRecognizer>{};
49
The data model, in Nim d333b6f nandi 23h ago50 @override
51 void initState() {
52 super.initState();
53 // Polling, because the socket lives on a Nim thread and there is no
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago54 // callback into Dart. At ~70µs a render a 100ms timer costs nothing.
The data model, in Nim d333b6f nandi 23h ago55 _poll = Timer.periodic(const Duration(milliseconds: 100), (_) {
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago56 // Compared as the JSON Nim already produced, and decoded only when it
57 // differs. Stringifying both trees to answer "did anything change" was
58 // ~1MB of string churn per poll in a busy room, ten times a second, for
59 // an answer that is almost always no.
60 final next = core.pollIfChanged(_frame.json);
61 if (next != null) setState(() => _frame = next);
The data model, in Nim d333b6f nandi 23h ago62 });
63 }
64
65 void _send(String id, [String value = '']) {
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago66 if (id.isEmpty) return;
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago67 setState(() => _frame = core.dispatchFrame(id, value));
The data model, in Nim d333b6f nandi 23h ago68 if (id == 'send') _focus['draft']?.requestFocus();
69 }
70
71 @override
72 void dispose() {
73 _poll?.cancel();
74 for (final c in _controllers.values) {
75 c.dispose();
76 }
77 for (final f in _focus.values) {
78 f.dispose();
79 }
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago80 for (final r in _linkTaps.values) {
81 r.dispose();
82 }
The data model, in Nim d333b6f nandi 23h ago83 super.dispose();
84 }
85
86 @override
87 Widget build(BuildContext context) => MaterialApp(
88 title: 'frq',
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago89 debugShowCheckedModeBanner: false,
90 theme: ThemeData(
91 useMaterial3: true,
92 brightness: Brightness.dark,
93 scaffoldBackgroundColor: t.bg,
94 colorScheme: const ColorScheme.dark(
95 primary: t.accent,
96 onPrimary: t.onAccent,
97 surface: t.bg,
98 onSurface: t.onBg,
99 error: t.destructive,
100 ),
The data model, in Nim d333b6f nandi 23h ago101 ),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago102 home: Scaffold(backgroundColor: t.bg, body: SafeArea(child: _build(_tree))),
The data model, in Nim d333b6f nandi 23h ago103 );
104
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago105 // ---------------------------------------------------------------- helpers
106
107 TextStyle _style(double size, Color color) =>
108 TextStyle(fontSize: size, color: color, height: 1.35);
109
110 double _d(dynamic v, double fallback) =>
111 v is num ? v.toDouble() : fallback;
112
113 /// Gaps between children, as real widgets rather than a `spacing:` — the
114 /// same layout on every Flutter version this might be built against.
115 List<Widget> _spaced(List<Widget> kids, double gap, {required bool vertical}) {
116 if (gap <= 0 || kids.length < 2) return kids;
117 final out = <Widget>[];
118 for (var i = 0; i < kids.length; i++) {
119 if (i > 0) {
120 out.add(vertical ? SizedBox(height: gap) : SizedBox(width: gap));
121 }
122 out.add(kids[i]);
123 }
124 return out;
125 }
126
127 /// A source that may be a bundled asset, a file on disk, or a URL — the
128 /// three the screens hand over, named apart by an `asset:` prefix so they
129 /// stay one property.
130 ImageProvider? _imageProvider(String src) {
131 if (src.isEmpty) return null;
132 if (src.startsWith('asset:')) return AssetImage(src.substring(6));
133 if (src.startsWith('http://') || src.startsWith('https://')) {
134 return NetworkImage(src);
135 }
136 return FileImage(File(src));
137 }
138
139 Widget _wrapTap(String onClick, Widget child, {BorderRadius? radius}) {
140 if (onClick.isEmpty) return child;
141 return InkWell(
142 onTap: () => _send(onClick),
143 borderRadius: radius,
144 child: child,
145 );
146 }
147
148 // ------------------------------------------------------------------ build
149
Expanded only where a Flex can hold it f1e99b4 nandi 20h ago150 /// The axis of the widget a node is being built *into*, because `Expanded`
151 /// is only legal inside a Flex and there is no way to ask Flutter after the
152 /// fact.
153 ///
154 /// Getting this wrong is what "Cannot hit test a render box that has never
155 /// been laid out" means, in a pile: an `Expanded` inside a `Wrap` fails the
156 /// layout, and every box under it is then asked to hit-test without ever
157 /// having been laid out. The chats screen did exactly that — two unsized
158 /// entries in an `hbox`, which is a Wrap.
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago159 /// What an unsized entry or a stranded scroll falls back to.
160 ///
161 /// Both are only reachable when the tree has put one outside a Flex, which
162 /// is a tree bug rather than a rendering choice. The numbers exist so that
163 /// bug renders as something a person can see and a test can catch, not so
164 /// that it renders correctly.
165 static const _unsizedEntry = 320.0;
166 static const _strandedScroll = 400.0;
167
Expanded only where a Flex can hold it f1e99b4 nandi 20h ago168 static const _noAxis = '';
169 static const _row = 'row';
170 static const _column = 'column';
171
172 Widget _build(core.UiNode n, [String axis = _noAxis]) {
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago173 final spacing = _d(n.props['spacing'], 0);
Expanded only where a Flex can hold it f1e99b4 nandi 20h ago174 final flex = axis == _row || axis == _column;
175
176 // What this node's own children are being built into.
177 final childAxis = switch (n.tag) {
178 'page' || 'vbox' || 'card' || 'scroll' || 'dialog' => _column,
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago179 // Wrapping unless the row says otherwise. Flipping this default was
180 // tried and reverted: only 4 of 15 `hbox` call sites state `wrap` at
181 // all, so the other 11 became Rows and overflowed — the tree's habit is
182 // to wrap, and the default has to match it.
Expanded only where a Flex can hold it f1e99b4 nandi 20h ago183 'hbox' => n.prop('wrap', true) ? _noAxis : _row,
184 _ => _noAxis,
185 };
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago186 // A paragraph's children are spans, not widgets — building them as
187 // widgets and throwing them away is what the `inline` special case did.
188 final kids = n.tag == 'paragraph'
189 ? const <Widget>[]
190 : n.children.map((c) => _build(c, childAxis)).toList();
191
192 // One rule for "take the remaining main-axis extent", stated by the node
193 // that expands. It used to be three: a vbox prop, a scroll with no
194 // height, and a row peering at its children's props to infer it.
195 Widget expanded(Widget w) =>
196 (n.prop('expand', false) && flex) ? Expanded(child: w) : w;
The data model, in Nim d333b6f nandi 23h ago197
198 switch (n.tag) {
199 case 'page':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago200 return SingleChildScrollView(
201 child: Center(
202 child: ConstrainedBox(
203 constraints:
204 BoxConstraints(maxWidth: _d(n.props['maxWidth'], 520)),
205 child: Padding(
206 padding: const EdgeInsets.all(t.spaceM),
207 child: Column(
208 crossAxisAlignment: CrossAxisAlignment.start,
209 children: _spaced(kids, spacing, vertical: true)),
210 ),
The data model, in Nim d333b6f nandi 23h ago211 ),
212 ),
213 );
214
215 case 'vbox':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago216 {
217 Widget col = Column(
218 crossAxisAlignment: CrossAxisAlignment.start,
219 mainAxisSize: MainAxisSize.min,
220 children: _spaced(kids, spacing, vertical: true),
221 );
222 col = _margins(n, col);
223 final w = _d(n.props['widthRequest'], 0);
224 if (w > 0) col = SizedBox(width: w, child: col);
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago225 return expanded(col);
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago226 }
The data model, in Nim d333b6f nandi 23h ago227
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago228 // Prose with links in it. NOT a Wrap: children of a Wrap are given
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago229 // unbounded width, so a long URL or a long word can never wrap — it
230 // overflows, the layout fails, and every box under it is then hit-tested
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago231 // having never been laid out. Spans in one RichText wrap properly.
232 case 'paragraph':
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago233 return Text.rich(
234 TextSpan(children: n.children.map(_span).toList()),
235 softWrap: true,
236 );
237
The data model, in Nim d333b6f nandi 23h ago238 case 'hbox':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago239 {
240 // Wrap and not Row: `:hbox` in the screens means "these go together
241 // across", not "these fit". The head row of the chat screen asks for
242 // more than 360 points has, and a Row answers that with an overflow
243 // rather than a second line.
244 final wrapping = n.prop('wrap', true);
245 final align = n.prop('align', 'center');
246 if (!wrapping) {
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago247 // An expanding row stretches on its cross axis, which is where a
248 // child's height comes from — Expanded in a Row is about width.
249 // Stretch needs a bounded height, and `expanded()` below is what
250 // gives the row one; without it the stretch resolves to infinity.
251 final fills = n.prop('expand', false);
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago252 final row = Row(
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago253 crossAxisAlignment: fills
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago254 ? CrossAxisAlignment.stretch
255 : (align == 'end'
256 ? CrossAxisAlignment.end
257 : CrossAxisAlignment.center),
258 children: _spaced(kids, spacing, vertical: false),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago259 );
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago260 return expanded(_margins(n, row));
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago261 }
262 return _margins(
263 n,
264 Wrap(
265 spacing: spacing,
266 runSpacing: spacing,
267 crossAxisAlignment: align == 'end'
268 ? WrapCrossAlignment.end
269 : WrapCrossAlignment.center,
270 children: kids,
The data model, in Nim d333b6f nandi 23h ago271 ),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago272 );
273 }
The data model, in Nim d333b6f nandi 23h ago274
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago275 // Container::Card in the Clojure: padding 12, fills its width.
The data model, in Nim d333b6f nandi 23h ago276 case 'card':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago277 return Container(
278 width: double.infinity,
279 margin: const EdgeInsets.symmetric(vertical: t.spaceXxxs),
280 padding: const EdgeInsets.all(t.spaceXs),
281 decoration: BoxDecoration(
282 color: t.card,
283 borderRadius: BorderRadius.circular(t.radiusS),
The data model, in Nim d333b6f nandi 23h ago284 ),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago285 child: Column(
286 crossAxisAlignment: CrossAxisAlignment.start,
287 children: _spaced(kids, spacing > 0 ? spacing : t.spaceXxs,
288 vertical: true)),
The data model, in Nim d333b6f nandi 23h ago289 );
290
291 case 'title':
292 return Text(n.prop('label', ''),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago293 style: _style(t.textTitle3, t.onBg)
294 .copyWith(fontWeight: FontWeight.bold));
The data model, in Nim d333b6f nandi 23h ago295
296 case 'title-2':
297 return Padding(
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago298 padding: const EdgeInsets.only(top: t.spaceXxs, bottom: t.spaceXxxs),
The data model, in Nim d333b6f nandi 23h ago299 child: Text(n.prop('label', ''),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago300 style: _style(t.textTitle4, t.onBg)
301 .copyWith(fontWeight: FontWeight.w600)),
The data model, in Nim d333b6f nandi 23h ago302 );
303
304 case 'label':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago305 return Text(n.prop('label', ''), style: _style(t.textBody, t.onBg));
The data model, in Nim d333b6f nandi 23h ago306
307 case 'dim-label':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago308 return Text(n.prop('label', ''), style: _style(t.textCaption, t.dim));
309
310 /// Prose, as opposed to a label: this is what a message is, and it
311 /// wraps. Kept apart from `label` because a wrapping label in a row
312 /// lays out against the row's width rather than the column's.
313 case 'text':
314 return Text(n.prop('text', ''), style: _style(t.textBody, t.onBg));
315
316 case 'link':
317 return _wrapTap(
318 n.prop('onClick', ''),
319 Text(
320 n.prop('label', ''),
321 style: _style(t.textBody, t.accent)
322 .copyWith(decoration: TextDecoration.underline,
323 decorationColor: t.accent),
324 ),
325 );
326
327 case 'separator':
328 return const Divider(height: 1, thickness: 1, color: t.divider);
329
330 case 'spacer':
331 {
332 final s = _d(n.props['size'], t.spaceXxs);
333 return SizedBox(width: s, height: s);
334 }
The data model, in Nim d333b6f nandi 23h ago335
336 case 'spinner':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago337 return Row(
338 mainAxisSize: MainAxisSize.min,
339 children: [
340 const SizedBox(
341 width: 16,
342 height: 16,
343 child: CircularProgressIndicator(
344 strokeWidth: 2, color: t.accent)),
345 if (n.prop('label', '').isNotEmpty) ...[
346 const SizedBox(width: t.spaceXxs),
347 Text(n.prop('label', ''), style: _style(t.textCaption, t.dim)),
348 ],
349 ],
350 );
351
352 /// A dot that says whether the thing is live, and the words beside it.
353 case 'status':
354 return Row(
355 mainAxisSize: MainAxisSize.min,
356 children: [
357 Container(
358 width: 8,
359 height: 8,
360 decoration: BoxDecoration(
361 color: n.prop('live', false) ? t.success : t.dim,
362 borderRadius: BorderRadius.circular(t.radiusXs),
363 ),
364 ),
365 const SizedBox(width: 6),
366 Text(n.prop('label', ''), style: _style(t.textCaption, t.dim)),
367 ],
368 );
The data model, in Nim d333b6f nandi 23h ago369
370 case 'button':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago371 {
372 final onClick = n.prop('onClick', '');
373 final kind = n.prop('kind', 'default');
374 final label = Text(n.prop('label', ''));
375 if (kind == 'primary') {
376 return FilledButton(
377 onPressed: () => _send(onClick), child: label);
378 }
A face that opens someone 9bb81a1 nandi 13h ago379 // A sender's name: a way in to who someone is, but it sits in the
380 // middle of a line and must not look like a control. Text that
381 // takes a press, with no chrome at all.
382 if (kind == 'plain') {
383 return InkWell(
384 onTap: () => _send(onClick),
385 child: Text(n.prop('label', ''),
386 style: _style(t.textBody, t.onBg)),
387 );
388 }
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago389 if (kind == 'destructive') {
390 return FilledButton(
391 style: FilledButton.styleFrom(
392 backgroundColor: t.destructive,
393 foregroundColor: t.onDestructive),
394 onPressed: () => _send(onClick),
395 child: label,
396 );
397 }
398 return OutlinedButton(onPressed: () => _send(onClick), child: label);
399 }
The data model, in Nim d333b6f nandi 23h ago400
401 case 'checkbutton':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago402 {
403 // The label is part of the target. 20 logical pixels is a fine tick
404 // on a desktop pointer and a miss on a thumb, so the whole row taps.
405 final onToggled = n.prop('onToggled', '');
406 return InkWell(
407 onTap: () => _send(onToggled),
408 child: Row(
409 mainAxisSize: MainAxisSize.min,
410 children: [
411 Checkbox(
412 value: n.prop('active', false),
413 onChanged: (_) => _send(onToggled),
414 ),
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago415 // Flexible, because the label is prose and the row is as wide
416 // as the window: "Hide join/part messages" beside a checkbox
417 // overflows a phone otherwise.
418 Flexible(
419 child: Text(n.prop('label', ''),
420 style: _style(t.textBody, t.onBg)),
421 ),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago422 ],
423 ),
424 );
425 }
426
427 case 'emoji':
428 return _wrapTap(
429 n.prop('onClick', ''),
430 Text(n.prop('glyph', n.prop('emoji', '')),
431 style: TextStyle(fontSize: _d(n.props['size'], 16))),
432 );
433
434 /// A reaction pill: the glyph, and the tally beside it where there is
435 /// one to show. The same shape whether it is a reaction under a message,
436 /// a swatch in the picker, or a chip on the sender's row — which is the
437 /// point: what you press to react and what appears once you have should
438 /// look like one family.
439 ///
440 /// A count of zero is no count. The picker passes 0 for every swatch,
441 /// and a grid of little grey zeroes is noise where a reader is scanning
442 /// for a face. `mine` is the accent, because the only thing a pill has
443 /// to say at a glance is whether pressing it again takes yours off.
444 case 'reaction':
445 {
446 final size = _d(n.props['size'], 14);
447 final count = n.prop('count', 0);
448 final mine = n.prop('mine', false);
449 final pad = (0.25 * size).clamp(2.0, 8.0);
450 return _wrapTap(
451 n.prop('onClick', ''),
452 Container(
453 padding: EdgeInsets.symmetric(horizontal: pad, vertical: pad / 2),
454 decoration: BoxDecoration(
455 color: mine ? t.accent : t.component,
456 borderRadius: BorderRadius.circular(t.radiusS),
457 ),
458 child: Row(
459 mainAxisSize: MainAxisSize.min,
460 children: [
461 Text(n.prop('emoji', ''), style: TextStyle(fontSize: size)),
462 if (count > 0) ...[
463 const SizedBox(width: 4),
464 Text('$count',
465 style: _style(t.textCaption,
466 mine ? t.onAccent : t.dim)),
467 ],
468 ],
469 ),
470 ),
471 );
472 }
473
474 /// A face is a way in to who someone is, so it takes the press that
475 /// opens their profile. A picture that will not load is a face that
476 /// stays its initial and nothing else.
477 case 'avatar':
478 {
479 final size = _d(n.props['size'], 32);
480 final provider = _imageProvider(n.prop('url', ''));
481 final fallback = n.prop('fallback', '');
482 final face = CircleAvatar(
483 radius: size / 2,
484 backgroundColor: t.component,
485 backgroundImage: provider,
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago486 onBackgroundImageError: provider == null ? null : (_, _) {},
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago487 child: provider == null
488 ? Text(
489 fallback.isNotEmpty
490 ? fallback.substring(0, 1).toUpperCase()
491 : '?',
492 style: _style(t.textBody, t.onBg))
493 : null,
494 );
495 final onClick = n.prop('onClick', '');
496 if (onClick.isEmpty) return face;
497 return InkWell(
498 onTap: () => _send(onClick),
499 customBorder: const CircleBorder(),
500 child: face,
501 );
502 }
503
504 case 'image':
505 {
506 final provider = _imageProvider(n.prop('src', ''));
507 if (provider == null) return const SizedBox.shrink();
508 final maxW = _d(n.props['maxWidth'], 0);
509 final maxH = _d(n.props['maxHeight'], 0);
510 Widget img = Image(
511 image: provider,
512 fit: BoxFit.contain,
513 // A half-written cache file, or one deleted under us: the decoder
514 // throws during the build, and an exception in a build is a red
515 // screen for the whole conversation rather than a gap where one
516 // picture was.
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago517 errorBuilder: (_, _, _) => const SizedBox.shrink(),
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago518 );
519 if (maxW > 0 || maxH > 0) {
520 img = ConstrainedBox(
521 constraints: BoxConstraints(
522 maxWidth: maxW > 0 ? maxW : double.infinity,
523 maxHeight: maxH > 0 ? maxH : double.infinity,
524 ),
525 child: img,
526 );
527 }
528 return _wrapTap(n.prop('onClick', ''), img);
529 }
The data model, in Nim d333b6f nandi 23h ago530
531 case 'entry':
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago532 {
533 final key = n.prop('key', '');
534 final value = n.prop('text', '');
535 final c = _controllers.putIfAbsent(
536 key, () => TextEditingController(text: value));
537 // Only when it actually differs: assigning unconditionally moves the
538 // caret to the end on every keystroke.
539 if (c.text != value) {
540 c.value = c.value.copyWith(
541 text: value,
542 selection: TextSelection.collapsed(offset: value.length),
543 );
544 }
545 final field = TextField(
546 controller: c,
547 focusNode: _focus.putIfAbsent(key, FocusNode.new),
548 style: _style(t.textBody, t.onBg),
549 decoration: InputDecoration(
550 hintText: n.prop('placeholder', ''),
551 hintStyle: _style(t.textBody, t.dim),
552 isDense: true,
553 filled: true,
554 fillColor: t.component,
555 border: OutlineInputBorder(
556 borderRadius: BorderRadius.circular(t.radiusS),
557 borderSide: BorderSide.none,
558 ),
559 ),
560 onChanged: (v) => _send(n.prop('onChange', ''), v),
561 onSubmitted: (_) => _send(n.prop('onSubmit', '')),
562 );
563 final w = _d(n.props['widthRequest'], 0);
Expanded only where a Flex can hold it f1e99b4 nandi 20h ago564 if (w > 0) return SizedBox(width: w, child: field);
565 // No width asked for: take the rest of the row where there is a row
566 // to take it from, and otherwise a definite width. NOT Expanded
567 // unconditionally — a TextField has no intrinsic width, so in a Wrap
568 // it is both illegal and unmeasurable, and that combination is what
569 // took the whole screen down rather than one field.
570 return axis == _row
571 ? Expanded(child: field)
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago572 : SizedBox(width: _unsizedEntry, child: field);
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago573 }
574
575 case 'scroll':
576 {
577 Widget body = SingleChildScrollView(
578 // The backlog reads from the bottom; a settings list from the top.
579 reverse: n.prop('stickToBottom', false),
580 child: Column(
581 crossAxisAlignment: CrossAxisAlignment.start,
582 children: _spaced(kids, spacing, vertical: true)),
The data model, in Nim d333b6f nandi 23h ago583 );
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago584 body = Scrollbar(child: body);
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago585 // A scroll takes what the column has left. Outside a Flex there is
586 // nothing to take, and the tree is malformed — `_strandedScroll` is
587 // a visible size rather than a correct one, so the layout tests see
588 // a screen instead of an exception.
589 return flex
590 ? Expanded(child: body)
591 : const SizedBox(height: _strandedScroll);
The data model, in Nim d333b6f nandi 23h ago592 }
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago593
594 /// A panel over the screen rather than a screen of its own.
595 case 'dialog':
596 return Card(
597 color: t.cardComponent,
598 child: Padding(
599 padding: const EdgeInsets.all(t.spaceS),
600 child: Column(
601 mainAxisSize: MainAxisSize.min,
602 crossAxisAlignment: CrossAxisAlignment.start,
603 children: [
604 if (n.prop('title', '').isNotEmpty)
605 Padding(
606 padding: const EdgeInsets.only(bottom: t.spaceXxs),
607 child: Text(n.prop('title', ''),
608 style: _style(t.textTitle4, t.onCard)
609 .copyWith(fontWeight: FontWeight.w600)),
610 ),
611 ..._spaced(kids, spacing, vertical: true),
612 ],
613 ),
The data model, in Nim d333b6f nandi 23h ago614 ),
615 );
616
617 default:
618 // An unknown tag paints as itself rather than crashing or vanishing.
619 // Nim can add one and see it before this file has heard of it, which
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago620 // is what makes the boundary pleasant to work across.
The data model, in Nim d333b6f nandi 23h ago621 return Container(
622 padding: const EdgeInsets.all(4),
623 color: Colors.orange.withValues(alpha: 0.3),
624 child: Text('?${n.tag}'),
625 );
626 }
627 }
628
Lay every screen out in a test, and fix what that found 36bdfc5 nandi 20h ago629 /// One node of an inline paragraph, as a span.
630 ///
631 /// Only `text` and `link` appear here — they are the only things `runNodes`
632 /// emits — and anything else falls back to its plain text so an unexpected
633 /// tag degrades to something readable rather than vanishing.
634 InlineSpan _span(core.UiNode n) {
635 switch (n.tag) {
636 case 'link':
637 final url = n.prop('url', n.prop('label', ''));
638 final onClick = n.prop('onClick', '');
639 return TextSpan(
640 text: n.prop('label', ''),
641 style: _style(t.textBody, t.accent)
642 .copyWith(decoration: TextDecoration.underline,
643 decorationColor: t.accent),
644 recognizer: onClick.isEmpty
645 ? null
646 : (_linkTaps[url] ??= TapGestureRecognizer()
647 ..onTap = () => _send(onClick)),
648 );
649 case 'text':
650 return TextSpan(
651 text: n.prop('text', ''), style: _style(t.textBody, t.onBg));
652 default:
653 return TextSpan(
654 text: n.prop('label', n.prop('text', '')),
655 style: _style(t.textBody, t.onBg));
656 }
657 }
658
Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 20h ago659 /// `margin` and its four sides — the props the
The renderer, and a transport bug that took four tries 58e6c97 nandi 22h ago660 /// screens use to buy air without a wrapper each time.
661 Widget _margins(core.UiNode n, Widget child) {
662 final all = _d(n.props['margin'], 0);
663 final top = _d(n.props['marginTop'], all);
664 final bottom = _d(n.props['marginBottom'], all);
665 final right = _d(n.props['marginRight'], all);
666 final left = _d(n.props['marginLeft'], all);
667 if (top == 0 && bottom == 0 && right == 0 && left == 0) return child;
668 return Padding(
669 padding: EdgeInsets.only(
670 top: top, bottom: bottom, right: right, left: left),
671 child: child,
672 );
The data model, in Nim d333b6f nandi 23h ago673 }
674}