| The data model, in Nim d333b6f nandi 17h ago | 1 | /// The renderer: a Nim widget tree, walked into Flutter widgets. |
| 2 | /// |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 3 | /// 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 17h ago | 6 | /// |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 7 | /// 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. |
| 11 | library; |
| 12 | |
| The data model, in Nim d333b6f nandi 17h ago | 13 | import 'dart:async'; |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 14 | import 'dart:io'; |
| The data model, in Nim d333b6f nandi 17h ago | 15 | |
| Lay every screen out in a test, and fix what that found 36bdfc5 nandi 14h ago | 16 | import 'package:flutter/gestures.dart'; |
| 17 | |
| The data model, in Nim d333b6f nandi 17h ago | 18 | import 'package:flutter/material.dart'; |
| 19 | import 'package:frq_core/frq_core.dart' as core; |
| 20 | |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 21 | import 'nim_theme.dart' as t; |
| 22 | |
| The data model, in Nim d333b6f nandi 17h ago | 23 | class NimApp extends StatefulWidget { |
| 24 | const NimApp({super.key}); |
| 25 | @override |
| 26 | State<NimApp> createState() => _NimAppState(); |
| 27 | } |
| 28 | |
| 29 | class _NimAppState extends State<NimApp> { |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 30 | late core.UiFrame _frame = core.renderFrame(); |
| 31 | core.UiNode get _tree => _frame.tree; |
| The data model, in Nim d333b6f nandi 17h ago | 32 | Timer? _poll; |
| 33 | |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 34 | // 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 14h ago | 45 | // 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 17h ago | 50 | @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 15h ago | 54 | // callback into Dart. At ~70µs a render a 100ms timer costs nothing. |
| The data model, in Nim d333b6f nandi 17h ago | 55 | _poll = Timer.periodic(const Duration(milliseconds: 100), (_) { |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 56 | // 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 17h ago | 62 | }); |
| 63 | } |
| 64 | |
| 65 | void _send(String id, [String value = '']) { |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 66 | if (id.isEmpty) return; |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 67 | setState(() => _frame = core.dispatchFrame(id, value)); |
| The data model, in Nim d333b6f nandi 17h ago | 68 | 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 14h ago | 80 | for (final r in _linkTaps.values) { |
| 81 | r.dispose(); |
| 82 | } |
| The data model, in Nim d333b6f nandi 17h ago | 83 | 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 15h ago | 89 | 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 17h ago | 101 | ), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 102 | home: Scaffold(backgroundColor: t.bg, body: SafeArea(child: _build(_tree))), |
| The data model, in Nim d333b6f nandi 17h ago | 103 | ); |
| 104 | |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 105 | // ---------------------------------------------------------------- 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 14h ago | 150 | /// 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 13h ago | 159 | /// 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 14h ago | 168 | 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 15h ago | 173 | final spacing = _d(n.props['spacing'], 0); |
| Expanded only where a Flex can hold it f1e99b4 nandi 14h ago | 174 | 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 13h ago | 179 | // 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 14h ago | 183 | 'hbox' => n.prop('wrap', true) ? _noAxis : _row, |
| 184 | _ => _noAxis, |
| 185 | }; |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 186 | // 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 17h ago | 197 | |
| 198 | switch (n.tag) { |
| 199 | case 'page': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 200 | 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 17h ago | 211 | ), |
| 212 | ), |
| 213 | ); |
| 214 | |
| 215 | case 'vbox': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 216 | { |
| 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 13h ago | 225 | return expanded(col); |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 226 | } |
| The data model, in Nim d333b6f nandi 17h ago | 227 | |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 228 | // 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 14h ago | 229 | // 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 13h ago | 231 | // 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 14h ago | 233 | return Text.rich( |
| 234 | TextSpan(children: n.children.map(_span).toList()), |
| 235 | softWrap: true, |
| 236 | ); |
| 237 | |
| The data model, in Nim d333b6f nandi 17h ago | 238 | case 'hbox': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 239 | { |
| 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 13h ago | 247 | // 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 14h ago | 252 | final row = Row( |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 253 | crossAxisAlignment: fills |
| Lay every screen out in a test, and fix what that found 36bdfc5 nandi 14h ago | 254 | ? 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 15h ago | 259 | ); |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 260 | return expanded(_margins(n, row)); |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 261 | } |
| 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 17h ago | 271 | ), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 272 | ); |
| 273 | } |
| The data model, in Nim d333b6f nandi 17h ago | 274 | |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 275 | // Container::Card in the Clojure: padding 12, fills its width. |
| The data model, in Nim d333b6f nandi 17h ago | 276 | case 'card': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 277 | 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 17h ago | 284 | ), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 285 | 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 17h ago | 289 | ); |
| 290 | |
| 291 | case 'title': |
| 292 | return Text(n.prop('label', ''), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 293 | style: _style(t.textTitle3, t.onBg) |
| 294 | .copyWith(fontWeight: FontWeight.bold)); |
| The data model, in Nim d333b6f nandi 17h ago | 295 | |
| 296 | case 'title-2': |
| 297 | return Padding( |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 298 | padding: const EdgeInsets.only(top: t.spaceXxs, bottom: t.spaceXxxs), |
| The data model, in Nim d333b6f nandi 17h ago | 299 | child: Text(n.prop('label', ''), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 300 | style: _style(t.textTitle4, t.onBg) |
| 301 | .copyWith(fontWeight: FontWeight.w600)), |
| The data model, in Nim d333b6f nandi 17h ago | 302 | ); |
| 303 | |
| 304 | case 'label': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 305 | return Text(n.prop('label', ''), style: _style(t.textBody, t.onBg)); |
| The data model, in Nim d333b6f nandi 17h ago | 306 | |
| 307 | case 'dim-label': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 308 | 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 17h ago | 335 | |
| 336 | case 'spinner': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 337 | 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 17h ago | 369 | |
| 370 | case 'button': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 371 | { |
| 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 | } |
| 379 | if (kind == 'destructive') { |
| 380 | return FilledButton( |
| 381 | style: FilledButton.styleFrom( |
| 382 | backgroundColor: t.destructive, |
| 383 | foregroundColor: t.onDestructive), |
| 384 | onPressed: () => _send(onClick), |
| 385 | child: label, |
| 386 | ); |
| 387 | } |
| 388 | return OutlinedButton(onPressed: () => _send(onClick), child: label); |
| 389 | } |
| The data model, in Nim d333b6f nandi 17h ago | 390 | |
| 391 | case 'checkbutton': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 392 | { |
| 393 | // The label is part of the target. 20 logical pixels is a fine tick |
| 394 | // on a desktop pointer and a miss on a thumb, so the whole row taps. |
| 395 | final onToggled = n.prop('onToggled', ''); |
| 396 | return InkWell( |
| 397 | onTap: () => _send(onToggled), |
| 398 | child: Row( |
| 399 | mainAxisSize: MainAxisSize.min, |
| 400 | children: [ |
| 401 | Checkbox( |
| 402 | value: n.prop('active', false), |
| 403 | onChanged: (_) => _send(onToggled), |
| 404 | ), |
| Lay every screen out in a test, and fix what that found 36bdfc5 nandi 14h ago | 405 | // Flexible, because the label is prose and the row is as wide |
| 406 | // as the window: "Hide join/part messages" beside a checkbox |
| 407 | // overflows a phone otherwise. |
| 408 | Flexible( |
| 409 | child: Text(n.prop('label', ''), |
| 410 | style: _style(t.textBody, t.onBg)), |
| 411 | ), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 412 | ], |
| 413 | ), |
| 414 | ); |
| 415 | } |
| 416 | |
| 417 | case 'emoji': |
| 418 | return _wrapTap( |
| 419 | n.prop('onClick', ''), |
| 420 | Text(n.prop('glyph', n.prop('emoji', '')), |
| 421 | style: TextStyle(fontSize: _d(n.props['size'], 16))), |
| 422 | ); |
| 423 | |
| 424 | /// A reaction pill: the glyph, and the tally beside it where there is |
| 425 | /// one to show. The same shape whether it is a reaction under a message, |
| 426 | /// a swatch in the picker, or a chip on the sender's row — which is the |
| 427 | /// point: what you press to react and what appears once you have should |
| 428 | /// look like one family. |
| 429 | /// |
| 430 | /// A count of zero is no count. The picker passes 0 for every swatch, |
| 431 | /// and a grid of little grey zeroes is noise where a reader is scanning |
| 432 | /// for a face. `mine` is the accent, because the only thing a pill has |
| 433 | /// to say at a glance is whether pressing it again takes yours off. |
| 434 | case 'reaction': |
| 435 | { |
| 436 | final size = _d(n.props['size'], 14); |
| 437 | final count = n.prop('count', 0); |
| 438 | final mine = n.prop('mine', false); |
| 439 | final pad = (0.25 * size).clamp(2.0, 8.0); |
| 440 | return _wrapTap( |
| 441 | n.prop('onClick', ''), |
| 442 | Container( |
| 443 | padding: EdgeInsets.symmetric(horizontal: pad, vertical: pad / 2), |
| 444 | decoration: BoxDecoration( |
| 445 | color: mine ? t.accent : t.component, |
| 446 | borderRadius: BorderRadius.circular(t.radiusS), |
| 447 | ), |
| 448 | child: Row( |
| 449 | mainAxisSize: MainAxisSize.min, |
| 450 | children: [ |
| 451 | Text(n.prop('emoji', ''), style: TextStyle(fontSize: size)), |
| 452 | if (count > 0) ...[ |
| 453 | const SizedBox(width: 4), |
| 454 | Text('$count', |
| 455 | style: _style(t.textCaption, |
| 456 | mine ? t.onAccent : t.dim)), |
| 457 | ], |
| 458 | ], |
| 459 | ), |
| 460 | ), |
| 461 | ); |
| 462 | } |
| 463 | |
| 464 | /// A face is a way in to who someone is, so it takes the press that |
| 465 | /// opens their profile. A picture that will not load is a face that |
| 466 | /// stays its initial and nothing else. |
| 467 | case 'avatar': |
| 468 | { |
| 469 | final size = _d(n.props['size'], 32); |
| 470 | final provider = _imageProvider(n.prop('url', '')); |
| 471 | final fallback = n.prop('fallback', ''); |
| 472 | final face = CircleAvatar( |
| 473 | radius: size / 2, |
| 474 | backgroundColor: t.component, |
| 475 | backgroundImage: provider, |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 476 | onBackgroundImageError: provider == null ? null : (_, _) {}, |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 477 | child: provider == null |
| 478 | ? Text( |
| 479 | fallback.isNotEmpty |
| 480 | ? fallback.substring(0, 1).toUpperCase() |
| 481 | : '?', |
| 482 | style: _style(t.textBody, t.onBg)) |
| 483 | : null, |
| 484 | ); |
| 485 | final onClick = n.prop('onClick', ''); |
| 486 | if (onClick.isEmpty) return face; |
| 487 | return InkWell( |
| 488 | onTap: () => _send(onClick), |
| 489 | customBorder: const CircleBorder(), |
| 490 | child: face, |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | case 'image': |
| 495 | { |
| 496 | final provider = _imageProvider(n.prop('src', '')); |
| 497 | if (provider == null) return const SizedBox.shrink(); |
| 498 | final maxW = _d(n.props['maxWidth'], 0); |
| 499 | final maxH = _d(n.props['maxHeight'], 0); |
| 500 | Widget img = Image( |
| 501 | image: provider, |
| 502 | fit: BoxFit.contain, |
| 503 | // A half-written cache file, or one deleted under us: the decoder |
| 504 | // throws during the build, and an exception in a build is a red |
| 505 | // screen for the whole conversation rather than a gap where one |
| 506 | // picture was. |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 507 | errorBuilder: (_, _, _) => const SizedBox.shrink(), |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 508 | ); |
| 509 | if (maxW > 0 || maxH > 0) { |
| 510 | img = ConstrainedBox( |
| 511 | constraints: BoxConstraints( |
| 512 | maxWidth: maxW > 0 ? maxW : double.infinity, |
| 513 | maxHeight: maxH > 0 ? maxH : double.infinity, |
| 514 | ), |
| 515 | child: img, |
| 516 | ); |
| 517 | } |
| 518 | return _wrapTap(n.prop('onClick', ''), img); |
| 519 | } |
| The data model, in Nim d333b6f nandi 17h ago | 520 | |
| 521 | case 'entry': |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 522 | { |
| 523 | final key = n.prop('key', ''); |
| 524 | final value = n.prop('text', ''); |
| 525 | final c = _controllers.putIfAbsent( |
| 526 | key, () => TextEditingController(text: value)); |
| 527 | // Only when it actually differs: assigning unconditionally moves the |
| 528 | // caret to the end on every keystroke. |
| 529 | if (c.text != value) { |
| 530 | c.value = c.value.copyWith( |
| 531 | text: value, |
| 532 | selection: TextSelection.collapsed(offset: value.length), |
| 533 | ); |
| 534 | } |
| 535 | final field = TextField( |
| 536 | controller: c, |
| 537 | focusNode: _focus.putIfAbsent(key, FocusNode.new), |
| 538 | style: _style(t.textBody, t.onBg), |
| 539 | decoration: InputDecoration( |
| 540 | hintText: n.prop('placeholder', ''), |
| 541 | hintStyle: _style(t.textBody, t.dim), |
| 542 | isDense: true, |
| 543 | filled: true, |
| 544 | fillColor: t.component, |
| 545 | border: OutlineInputBorder( |
| 546 | borderRadius: BorderRadius.circular(t.radiusS), |
| 547 | borderSide: BorderSide.none, |
| 548 | ), |
| 549 | ), |
| 550 | onChanged: (v) => _send(n.prop('onChange', ''), v), |
| 551 | onSubmitted: (_) => _send(n.prop('onSubmit', '')), |
| 552 | ); |
| 553 | final w = _d(n.props['widthRequest'], 0); |
| Expanded only where a Flex can hold it f1e99b4 nandi 14h ago | 554 | if (w > 0) return SizedBox(width: w, child: field); |
| 555 | // No width asked for: take the rest of the row where there is a row |
| 556 | // to take it from, and otherwise a definite width. NOT Expanded |
| 557 | // unconditionally — a TextField has no intrinsic width, so in a Wrap |
| 558 | // it is both illegal and unmeasurable, and that combination is what |
| 559 | // took the whole screen down rather than one field. |
| 560 | return axis == _row |
| 561 | ? Expanded(child: field) |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 562 | : SizedBox(width: _unsizedEntry, child: field); |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 563 | } |
| 564 | |
| 565 | case 'scroll': |
| 566 | { |
| 567 | Widget body = SingleChildScrollView( |
| 568 | // The backlog reads from the bottom; a settings list from the top. |
| 569 | reverse: n.prop('stickToBottom', false), |
| 570 | child: Column( |
| 571 | crossAxisAlignment: CrossAxisAlignment.start, |
| 572 | children: _spaced(kids, spacing, vertical: true)), |
| The data model, in Nim d333b6f nandi 17h ago | 573 | ); |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 574 | body = Scrollbar(child: body); |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 575 | // A scroll takes what the column has left. Outside a Flex there is |
| 576 | // nothing to take, and the tree is malformed — `_strandedScroll` is |
| 577 | // a visible size rather than a correct one, so the layout tests see |
| 578 | // a screen instead of an exception. |
| 579 | return flex |
| 580 | ? Expanded(child: body) |
| 581 | : const SizedBox(height: _strandedScroll); |
| The data model, in Nim d333b6f nandi 17h ago | 582 | } |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 583 | |
| 584 | /// A panel over the screen rather than a screen of its own. |
| 585 | case 'dialog': |
| 586 | return Card( |
| 587 | color: t.cardComponent, |
| 588 | child: Padding( |
| 589 | padding: const EdgeInsets.all(t.spaceS), |
| 590 | child: Column( |
| 591 | mainAxisSize: MainAxisSize.min, |
| 592 | crossAxisAlignment: CrossAxisAlignment.start, |
| 593 | children: [ |
| 594 | if (n.prop('title', '').isNotEmpty) |
| 595 | Padding( |
| 596 | padding: const EdgeInsets.only(bottom: t.spaceXxs), |
| 597 | child: Text(n.prop('title', ''), |
| 598 | style: _style(t.textTitle4, t.onCard) |
| 599 | .copyWith(fontWeight: FontWeight.w600)), |
| 600 | ), |
| 601 | ..._spaced(kids, spacing, vertical: true), |
| 602 | ], |
| 603 | ), |
| The data model, in Nim d333b6f nandi 17h ago | 604 | ), |
| 605 | ); |
| 606 | |
| 607 | default: |
| 608 | // An unknown tag paints as itself rather than crashing or vanishing. |
| 609 | // 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 15h ago | 610 | // is what makes the boundary pleasant to work across. |
| The data model, in Nim d333b6f nandi 17h ago | 611 | return Container( |
| 612 | padding: const EdgeInsets.all(4), |
| 613 | color: Colors.orange.withValues(alpha: 0.3), |
| 614 | child: Text('?${n.tag}'), |
| 615 | ); |
| 616 | } |
| 617 | } |
| 618 | |
| Lay every screen out in a test, and fix what that found 36bdfc5 nandi 14h ago | 619 | /// One node of an inline paragraph, as a span. |
| 620 | /// |
| 621 | /// Only `text` and `link` appear here — they are the only things `runNodes` |
| 622 | /// emits — and anything else falls back to its plain text so an unexpected |
| 623 | /// tag degrades to something readable rather than vanishing. |
| 624 | InlineSpan _span(core.UiNode n) { |
| 625 | switch (n.tag) { |
| 626 | case 'link': |
| 627 | final url = n.prop('url', n.prop('label', '')); |
| 628 | final onClick = n.prop('onClick', ''); |
| 629 | return TextSpan( |
| 630 | text: n.prop('label', ''), |
| 631 | style: _style(t.textBody, t.accent) |
| 632 | .copyWith(decoration: TextDecoration.underline, |
| 633 | decorationColor: t.accent), |
| 634 | recognizer: onClick.isEmpty |
| 635 | ? null |
| 636 | : (_linkTaps[url] ??= TapGestureRecognizer() |
| 637 | ..onTap = () => _send(onClick)), |
| 638 | ); |
| 639 | case 'text': |
| 640 | return TextSpan( |
| 641 | text: n.prop('text', ''), style: _style(t.textBody, t.onBg)); |
| 642 | default: |
| 643 | return TextSpan( |
| 644 | text: n.prop('label', n.prop('text', '')), |
| 645 | style: _style(t.textBody, t.onBg)); |
| 646 | } |
| 647 | } |
| 648 | |
| Quality pass: reuse, dead weight, and two real costs 4dfc719 nandi 13h ago | 649 | /// `margin` and its four sides — the props the |
| The renderer, and a transport bug that took four tries 58e6c97 nandi 15h ago | 650 | /// screens use to buy air without a wrapper each time. |
| 651 | Widget _margins(core.UiNode n, Widget child) { |
| 652 | final all = _d(n.props['margin'], 0); |
| 653 | final top = _d(n.props['marginTop'], all); |
| 654 | final bottom = _d(n.props['marginBottom'], all); |
| 655 | final right = _d(n.props['marginRight'], all); |
| 656 | final left = _d(n.props['marginLeft'], all); |
| 657 | if (top == 0 && bottom == 0 && right == 0 && left == 0) return child; |
| 658 | return Padding( |
| 659 | padding: EdgeInsets.only( |
| 660 | top: top, bottom: bottom, right: right, left: left), |
| 661 | child: child, |
| 662 | ); |
| The data model, in Nim d333b6f nandi 17h ago | 663 | } |
| 664 | } |