@typecad/ui 1.0.0-alpha.7 → 1.0.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +6 -0
  2. package/dist/engine-index.js +0 -2
  3. package/dist/preview/host-ui-runtime.d.ts +1 -0
  4. package/dist/preview/host-ui-runtime.js +56 -11
  5. package/dist/ui-engine/font-assets.d.ts +8 -0
  6. package/dist/ui-engine/font-assets.js +13 -0
  7. package/dist/ui-engine/layout-engine.js +19 -6
  8. package/dist/ui-engine/model.d.ts +1 -1
  9. package/dist/ui-engine/model.js +10 -4
  10. package/dist/ui-engine/runtime-header/antialiasing.js +52 -32
  11. package/dist/ui-engine/runtime-header/blend-bodies.js +4 -4
  12. package/dist/ui-engine/runtime-header/canvas-helpers.js +32 -65
  13. package/dist/ui-engine/runtime-header/canvas-scrollbar.js +22 -10
  14. package/dist/ui-engine/runtime-header/color-mono-refresh.js +10 -10
  15. package/dist/ui-engine/runtime-header/cuttlefish-gfx.js +59 -29
  16. package/dist/ui-engine/runtime-header/dirty-scroll-mutators.js +26 -10
  17. package/dist/ui-engine/runtime-header/display-shim.js +8 -8
  18. package/dist/ui-engine/runtime-header/forward-decls.js +48 -6
  19. package/dist/ui-engine/runtime-header/image-drawing.js +154 -84
  20. package/dist/ui-engine/runtime-header/init-press-input.js +20 -5
  21. package/dist/ui-engine/runtime-header/keyboard.js +19 -16
  22. package/dist/ui-engine/runtime-header/node-decoration.js +8 -8
  23. package/dist/ui-engine/runtime-header/node-draw-body.d.ts +1 -0
  24. package/dist/ui-engine/runtime-header/node-draw-body.js +1402 -0
  25. package/dist/ui-engine/runtime-header/paint-order-coords.js +48 -18
  26. package/dist/ui-engine/runtime-header/paint-rects-repair.js +38 -17
  27. package/dist/ui-engine/runtime-header/scroll-physics.js +15 -14
  28. package/dist/ui-engine/runtime-header/state-bindings-nav.js +23 -13
  29. package/dist/ui-engine/runtime-header/structs.js +12 -10
  30. package/dist/ui-engine/runtime-header/text-rendering.js +132 -71
  31. package/dist/ui-engine/runtime-header/tick/bindings-phase.js +29 -10
  32. package/dist/ui-engine/runtime-header/tick/dirty-draw-phase.js +299 -747
  33. package/dist/ui-engine/runtime-header/tick/flush-phase.js +7 -3
  34. package/dist/ui-engine/runtime-header/tick/scroll-canvas-phase.js +19 -3
  35. package/dist/ui-engine/runtime-header/tick/transitions-phase.js +31 -24
  36. package/dist/ui-engine/runtime-header/touch-keyboard-fwd.js +42 -40
  37. package/dist/ui-engine/runtime-header/types-defines.js +23 -3
  38. package/dist/ui-engine/runtime-header.js +2 -0
  39. package/dist/ui-engine/transpile-ui.js +2 -2
  40. package/dist/ui-engine/ua-stylesheet.js +46 -3
  41. package/dist/ui-engine/ui-lowering.d.ts +1 -3
  42. package/dist/ui-engine/ui-lowering.js +4 -23
  43. package/dist/ui-engine/ui-registry.js +1 -7
  44. package/package.json +2 -2
  45. package/src/engine-index.ts +0 -2
  46. package/src/preview/host-ui-runtime.ts +56 -12
  47. package/src/ui-engine/font-assets.ts +13 -0
  48. package/src/ui-engine/layout-engine.ts +19 -6
  49. package/src/ui-engine/model.ts +10 -5
  50. package/src/ui-engine/runtime-header/antialiasing.ts +52 -32
  51. package/src/ui-engine/runtime-header/blend-bodies.ts +4 -4
  52. package/src/ui-engine/runtime-header/canvas-helpers.ts +32 -65
  53. package/src/ui-engine/runtime-header/canvas-scrollbar.ts +22 -10
  54. package/src/ui-engine/runtime-header/color-mono-refresh.ts +10 -10
  55. package/src/ui-engine/runtime-header/cuttlefish-gfx.ts +59 -29
  56. package/src/ui-engine/runtime-header/dirty-scroll-mutators.ts +26 -10
  57. package/src/ui-engine/runtime-header/display-shim.ts +8 -8
  58. package/src/ui-engine/runtime-header/forward-decls.ts +48 -6
  59. package/src/ui-engine/runtime-header/image-drawing.ts +154 -84
  60. package/src/ui-engine/runtime-header/init-press-input.ts +20 -5
  61. package/src/ui-engine/runtime-header/keyboard.ts +19 -16
  62. package/src/ui-engine/runtime-header/node-decoration.ts +8 -8
  63. package/src/ui-engine/runtime-header/node-draw-body.ts +1402 -0
  64. package/src/ui-engine/runtime-header/paint-order-coords.ts +48 -18
  65. package/src/ui-engine/runtime-header/paint-rects-repair.ts +38 -17
  66. package/src/ui-engine/runtime-header/scroll-physics.ts +15 -14
  67. package/src/ui-engine/runtime-header/state-bindings-nav.ts +23 -13
  68. package/src/ui-engine/runtime-header/structs.ts +12 -10
  69. package/src/ui-engine/runtime-header/text-rendering.ts +132 -71
  70. package/src/ui-engine/runtime-header/tick/bindings-phase.ts +29 -10
  71. package/src/ui-engine/runtime-header/tick/dirty-draw-phase.ts +299 -747
  72. package/src/ui-engine/runtime-header/tick/flush-phase.ts +7 -3
  73. package/src/ui-engine/runtime-header/tick/scroll-canvas-phase.ts +19 -3
  74. package/src/ui-engine/runtime-header/tick/transitions-phase.ts +31 -24
  75. package/src/ui-engine/runtime-header/touch-keyboard-fwd.ts +42 -40
  76. package/src/ui-engine/runtime-header/types-defines.ts +23 -3
  77. package/src/ui-engine/runtime-header.ts +2 -0
  78. package/src/ui-engine/transpile-ui.ts +2 -2
  79. package/src/ui-engine/ua-stylesheet.ts +46 -3
  80. package/src/ui-engine/ui-lowering.ts +3 -25
  81. package/src/ui-engine/ui-registry.ts +1 -8
  82. package/dist/ui-engine/scroll-memory-diagnostics.d.ts +0 -16
  83. package/dist/ui-engine/scroll-memory-diagnostics.js +0 -58
  84. package/src/ui-engine/scroll-memory-diagnostics.ts +0 -79
@@ -46,7 +46,7 @@ export function emitTickDirtyDrawPhase() {
46
46
  ui_kb_draw_text_row();
47
47
  uint8_t has_key_repaint = (__ui_kb_repaint_key >= 0 && __ui_kb_keys[__ui_kb_repaint_key].special != 255) ? 1 : 0;
48
48
  if (has_key_repaint) {
49
- ui_kb_draw_key((uint8_t)__ui_kb_repaint_key);
49
+ ui_kb_draw_key(static_cast<uint8_t>(__ui_kb_repaint_key));
50
50
  }
51
51
  __ui_kb_box.x = saveBoxX;
52
52
  __ui_kb_box.y = saveBoxY;
@@ -57,17 +57,17 @@ export function emitTickDirtyDrawPhase() {
57
57
  display_startWrite();
58
58
  if (has_key_repaint) {
59
59
  display_setAddrWindow(saveBoxX, saveBoxY, kw, __ui_kb_box.h);
60
- display_writePixels(display_canvasBuffer(__ui_kb_canvas), (uint32_t)kw * __ui_kb_box.h);
60
+ display_writePixels(display_canvasBuffer(__ui_kb_canvas), static_cast<uint32_t>(kw) * __ui_kb_box.h);
61
61
  } else {
62
62
  display_setAddrWindow(saveBoxX, saveBoxY, kw, UI_KB_TEXT_H);
63
- display_writePixels(display_canvasBuffer(__ui_kb_canvas), (uint32_t)kw * UI_KB_TEXT_H);
63
+ display_writePixels(display_canvasBuffer(__ui_kb_canvas), static_cast<uint32_t>(kw) * UI_KB_TEXT_H);
64
64
  }
65
65
  display_endWrite();
66
66
  } else {
67
67
  // No canvas — fall back to direct draw (slow but correct).
68
68
  ui_kb_draw_text_row();
69
69
  if (__ui_kb_repaint_key >= 0 && __ui_kb_keys[__ui_kb_repaint_key].special != 255) {
70
- ui_kb_draw_key((uint8_t)__ui_kb_repaint_key);
70
+ ui_kb_draw_key(static_cast<uint8_t>(__ui_kb_repaint_key));
71
71
  }
72
72
  }
73
73
  }
@@ -76,6 +76,46 @@ export function emitTickDirtyDrawPhase() {
76
76
  return;
77
77
  }
78
78
  #endif
79
+ // Select and seed the framebuffer before scroll compositors run. Band/list
80
+ // renderers can then composite into this same off-screen target instead of
81
+ // pushing their bands to the live panel before the main draw pass starts.
82
+ // This prevents intermediate software-rendering seams; the final SPI transfer
83
+ // remains sequential on panels without TE/vblank synchronization.
84
+ CuttlefishCanvas16* __ui_fb = ui_get_framebuffer();
85
+ CuttlefishDisplayTarget* __ui_draw_target = __ui_fb ? (CuttlefishDisplayTarget*)__ui_fb : display_defaultTarget();
86
+ ui_fb_begin_frame();
87
+ // A retained framebuffer only needs an SPI transfer when composition changed.
88
+ // Avoid pushing a full frame on idle ticks; this is the main steady-state cost
89
+ // of the PSRAM path on otherwise static screens.
90
+ uint8_t __ui_fb_frame_dirty = 0;
91
+ if (__ui_fb && __ui_fb_needs_compose) {
92
+ UI_COLOR_T fbBg = (UI_COLOR_T)0;
93
+ if (__ui_active_screen_bg_node < __ui_node_count) {
94
+ fbBg = __ui_nodes[__ui_active_screen_bg_node].hasBg
95
+ ? __ui_nodes[__ui_active_screen_bg_node].bg
96
+ : __ui_nodes[__ui_active_screen_bg_node].clearColor;
97
+ }
98
+ display_canvasFillScreen(__ui_fb, fbBg);
99
+ ui_fb_add_rect(0, 0, display_width(), display_height());
100
+ // A retained framebuffer cannot reuse the previous screen's pixels. Mark
101
+ // every node on the active screen dirty for one coherent composition.
102
+ for (uint16_t f = 0; f < __ui_node_count; f++) {
103
+ if (__ui_nodes[f].screenId == __ui_active_screen) {
104
+ __ui_nodes[f].dirty = 1;
105
+ __ui_nodes[f].lastTextHeight = 0;
106
+ __ui_nodes[f].layoutCacheKey = 0;
107
+ if (__ui_nodes[f].kind == NODE_PROGRESS || __ui_nodes[f].kind == NODE_RANGE) __ui_nodes[f].lastTextWidth = -1;
108
+ else __ui_nodes[f].lastTextWidth = 0;
109
+ }
110
+ }
111
+ __ui_fb_needs_compose = 0;
112
+ __ui_fb_frame_dirty = 1;
113
+ }
114
+
115
+ if (__ui_scroll_render_locked) {
116
+ for (uint16_t lock = 0; lock < __ui_node_count; lock++) __ui_scroll_render_locked[lock] = 0;
117
+ }
118
+
79
119
  // Process each dirty scroll container (Mode B shift-and-repair). A scroll
80
120
  // delta shifts existing canvas pixels by the delta and repaints only the
81
121
  // newly-exposed strip; a full invalidation (or no canvas) redraws the subtree.
@@ -87,7 +127,20 @@ export function emitTickDirtyDrawPhase() {
87
127
  CuttlefishCanvas16* bufferedScrollRepaintCanvas = nullptr;
88
128
  int16_t bufferedScrollRepaintY = 0;
89
129
  int16_t bufferedScrollRepaintH = 0;
90
- uint8_t bufferedScrollDirectStrip = 0;
130
+ // Kept as a compatibility flag for the old compositor state machine. It is
131
+ // never enabled: direct per-node SPI drawing is a visible tearing path, so a
132
+ // missing canvas freezes the affected viewport instead.
133
+ uint8_t bufferedScrollDirectFull = 0;
134
+ // Local retained-canvas repair: dirty descendants draw directly into the
135
+ // full-frame framebuffer while the existing scroll canvas is invalidated for
136
+ // the next actual scroll. This avoids copying an unchanged viewport.
137
+ uint8_t bufferedScrollLocalRepair = 0;
138
+ // Band renderer pending: like bufferedScrollDirectFull but the actual paint
139
+ // is deferred to the scroll owner's z-order slot in the main draw loop. The
140
+ // band composites the whole subtree at once, so it must paint AFTER lower-z
141
+ // nodes (e.g. the screen background fill) but BEFORE higher-z ones (header).
142
+ // Painting it here (before the main loop) lets the bg fill overwrite it.
143
+ uint8_t bufferedScrollBands = 0;
91
144
  for (uint16_t oi = 0; ; oi++) {
92
145
  uint16_t s;
93
146
  if (__ui_scroll_owners) {
@@ -101,7 +154,17 @@ export function emitTickDirtyDrawPhase() {
101
154
  if (!ui_is_effectively_visible(s)) continue;
102
155
  if (__ui_nodes[s].screenId != __ui_active_screen) continue;
103
156
  if (__ui_nodes[s].contentHeight <= __ui_nodes[s].box.h) continue;
104
- if (!__ui_nodes[s].dirty) continue;
157
+ if (!__ui_nodes[s].dirty) continue;
158
+ // Virtualized lists manage their own rendering (NODE_LIST case in
159
+ // ui_draw_node_body). Skip them in the scroll dispatch — the band
160
+ // renderer walks their subtree (which is empty for virtualized lists),
161
+ // leaving the list node itself unpainted.
162
+ if (__ui_nodes[s].virtualized) continue;
163
+ // The retained framebuffer only needs the full viewport for actual scroll
164
+ // movement or the first composition. A local child update is repaired in
165
+ // the existing scroll canvas and contributes its own paint rect later;
166
+ // pushing the whole viewport here is the Forms-screen flash.
167
+ if (__ui_fb) __ui_fb_frame_dirty = 1;
105
168
 
106
169
  int16_t vw = __ui_nodes[s].box.w;
107
170
  int16_t vh = __ui_nodes[s].box.h;
@@ -115,7 +178,7 @@ export function emitTickDirtyDrawPhase() {
115
178
  __ui_scroll_canvas_ok[s] = bufferedScrollCanvas ? 1 : 0;
116
179
  }
117
180
  if (bufferedScrollCanvas) {
118
- bufferedScrollNode = (int16_t)s;
181
+ bufferedScrollNode = static_cast<int16_t>(s);
119
182
  bufferedScrollVX = vox;
120
183
  bufferedScrollVY = voy;
121
184
 
@@ -134,7 +197,7 @@ export function emitTickDirtyDrawPhase() {
134
197
  bufferedScrollRepaintY = exposedY;
135
198
  bufferedScrollRepaintH = exposedH;
136
199
  display_canvasFillScreen(bufferedScrollRepaintCanvas, scrollBg);
137
- UIRect exposed = { vox, (int16_t)(voy + exposedY), vw, exposedH };
200
+ UIRect exposed = { vox, static_cast<int16_t>(voy + exposedY), vw, exposedH };
138
201
  for (uint16_t c = s + 1; c < __ui_nodes[s].subtreeEnd; c++) {
139
202
  if (!ui_is_effectively_visible(c) || __ui_nodes[c].screenId != __ui_active_screen) {
140
203
  __ui_nodes[c].dirty = 0;
@@ -159,66 +222,82 @@ export function emitTickDirtyDrawPhase() {
159
222
  }
160
223
  }
161
224
  if (!canShift) {
162
- for (uint16_t c = s; c < __ui_nodes[s].subtreeEnd; c++) {
163
- __ui_nodes[c].dirty = 1;
164
- if (__ui_nodes[c].kind == NODE_PROGRESS) __ui_nodes[c].lastTextWidth = -1;
165
- else if (__ui_nodes[c].kind == NODE_RANGE) __ui_nodes[c].lastTextWidth = -1;
166
- __ui_nodes[c].lastTextHeight = 0;
167
- }
168
- display_canvasFillScreen(bufferedScrollCanvas, scrollBg);
225
+ // A zero-delta dirty scroll owner is usually a local child update (a
226
+ // form binding, button transition, or animation). Keep the retained
227
+ // canvas and its existing pixels in that case; the dirty descendants
228
+ // will redraw in place and the final framebuffer union stays local.
229
+ uint8_t localRepair = __ui_fb && deltaY == 0 &&
230
+ __ui_nodes[s].lastPaintedScrollY == __ui_nodes[s].scrollY &&
231
+ ui_scroll_subtree_has_dirty(s);
232
+ if (localRepair) {
233
+ // The framebuffer is already the authoritative composition surface for
234
+ // this zero-delta update. Let dirty descendants draw there directly;
235
+ // invalidate the retained scroll canvas after each child draw so it is
236
+ // rebuilt before the next scroll gesture.
237
+ bufferedScrollLocalRepair = 1;
238
+ bufferedScrollCanvas = nullptr;
239
+ bufferedScrollRepaintCanvas = nullptr;
240
+ } else {
241
+ // Initial composition, a large jump, or an owner-only invalidation:
242
+ // seed the whole viewport and repaint its complete stacking subtree.
243
+ for (uint16_t c = s; c < __ui_nodes[s].subtreeEnd; c++) {
244
+ __ui_nodes[c].dirty = 1;
245
+ if (__ui_nodes[c].kind == NODE_PROGRESS) __ui_nodes[c].lastTextWidth = -1;
246
+ else if (__ui_nodes[c].kind == NODE_RANGE) __ui_nodes[c].lastTextWidth = -1;
247
+ __ui_nodes[c].lastTextHeight = 0;
248
+ }
249
+ display_canvasFillScreen(bufferedScrollCanvas, scrollBg);
250
+ if (__ui_fb) ui_fb_add_rect(__ui_nodes[s].box.x, __ui_nodes[s].box.y,
251
+ __ui_nodes[s].box.w, __ui_nodes[s].box.h);
252
+ }
253
+ } else if (__ui_fb) {
254
+ // Shift-and-repair changes the entire viewport's visible mapping.
255
+ ui_fb_add_rect(__ui_nodes[s].box.x, __ui_nodes[s].box.y,
256
+ __ui_nodes[s].box.w, __ui_nodes[s].box.h);
169
257
  }
170
258
  // The scroll owner is represented by bufferedScrollCanvas this frame.
171
259
  // Drawing it directly first clears the live display and makes scrolling
172
260
  // visibly flash before the canvas is pushed.
173
261
  __ui_nodes[s].dirty = 0;
174
262
  } else {
175
- // Canvas won't fit. Mode C strip-only for small in-viewport deltas; otherwise
176
- // graceful skip (keep last frame, retry next tick). Never direct-draw a full
177
- // scroll subtree to the display that clears the live viewport and flashes.
178
- int16_t deltaY = __ui_nodes[s].scrollY - __ui_nodes[s].lastPaintedScrollY;
179
- int16_t absDelta = deltaY < 0 ? -deltaY : deltaY;
180
- uint32_t scrollNeed = (uint32_t)(vw > 0 ? vw : 0) * (uint32_t)(vh > 0 ? vh : 0) * 2u;
181
- if (absDelta > 0 && absDelta < vh) {
182
- bufferedScrollNode = (int16_t)s;
183
- bufferedScrollDirectStrip = 1;
184
- ui_warn_scroll_memory((uint16_t)s, 2);
185
- ui_scroll_direct_prepare(s, &bufferedScrollVX, &bufferedScrollVY);
186
- break;
187
- }
188
- ui_warn_scroll_memory((uint16_t)s, scrollNeed > (uint32_t)UI_SCROLL_CANVAS_BUDGET_BYTES ? 1 : 0);
263
+ // Canvas won't fit (no PSRAM / over budget). The band renderer composes the
264
+ // whole visible subtree into a short horizontal band canvas (vw ×
265
+ // UI_STRIP_BAND_HEIGHT) and pushes one band at a time, so each band
266
+ // completes before it touches the panel — tear-free, with ~10KB of SRAM
267
+ // regardless of program size. If the band canvas also cannot allocate,
268
+ // the viewport is retained rather than using direct per-node SPI draws.
189
269
  if (__ui_scroll_canvas_ok) __ui_scroll_canvas_ok[s] = 0;
190
- continue;
191
- }
192
- // Only one scroll container per frame (the canvas is reused for subsequent
193
- // ones in the next dirty frame). This matches the original design.
194
- break;
195
- }
196
- // ── Framebuffer target selection ──────────────────────────────────────────
197
- // When a full-screen framebuffer is available (ESP32 + PSRAM), redirect the
198
- // entire dirty-node draw pass into it and push once at the end. Otherwise
199
- // __ui_draw_target == display_defaultTarget() and draws go straight to the display
200
- // (byte-identical to the pre-framebuffer path).
201
- CuttlefishCanvas16* __ui_fb = ui_get_framebuffer();
202
- CuttlefishDisplayTarget* __ui_draw_target = __ui_fb ? (CuttlefishDisplayTarget*)__ui_fb : display_defaultTarget();
203
- if (__ui_fb) {
204
- // Seed the framebuffer with the active screen's background so cleared/
205
- // transparent regions resolve correctly, then draw dirty nodes on top.
206
- uint16_t fbBg = 0x0000;
207
- if (__ui_active_screen_bg_node < __ui_node_count) {
208
- fbBg = __ui_nodes[__ui_active_screen_bg_node].hasBg
209
- ? __ui_nodes[__ui_active_screen_bg_node].bg
210
- : __ui_nodes[__ui_active_screen_bg_node].clearColor;
211
- }
212
- display_canvasFillScreen(__ui_fb, fbBg);
213
- }
270
+ // Defer the band render to the scroll owner's z-order slot in the main
271
+ // draw loop (see bufferedScrollBands handling below). Painting here would
272
+ // let lower-z nodes (screen bg fill) draw over the bands afterward. The
273
+ // owner stays dirty so the main loop reaches its z-slot and triggers the
274
+ // render; the band code clears it once painted. The subtree children are
275
+ // cleared now so the main loop never draws them directly — the band path
276
+ // is the sole renderer for the subtree this frame.
277
+ bufferedScrollNode = static_cast<int16_t>(s);
278
+ bufferedScrollVX = vox;
279
+ bufferedScrollVY = voy;
280
+ bufferedScrollBands = 1;
281
+ for (uint16_t c = s + 1; c < __ui_nodes[s].subtreeEnd; c++) {
282
+ __ui_nodes[c].dirty = 0;
283
+ }
284
+ break;
285
+ } // Only one scroll container per frame (the canvas is reused for subsequent
286
+ // ones in the next dirty frame). This matches the original design.
287
+ break;
288
+ }
289
+ // The framebuffer target was selected and seeded before scroll compositors;
290
+ // the stacking draw pass below uses the same __ui_draw_target. The retained
291
+ // single buffer is a composition surface, not a synchronized panel swap.
214
292
 
215
293
  // Draw dirty nodes in stacking order: lower z-index first, then source order.
216
294
  // __ui_draw_order is built once in ui_init so each frame is O(N).
217
295
  for (uint16_t __ui_draw_pass = 0; __ui_draw_pass < __ui_node_count; ) {
218
296
  int16_t i;
219
297
  if (__ui_draw_order) {
220
- i = (int16_t)__ui_draw_order[__ui_draw_pass++];
298
+ i = static_cast<int16_t>(__ui_draw_order[__ui_draw_pass++]);
221
299
  if (!__ui_nodes[i].dirty) continue;
300
+ if (__ui_fb) __ui_fb_frame_dirty = 1;
222
301
  if (!ui_is_effectively_visible(i)) { __ui_nodes[i].dirty = 0; continue; }
223
302
  if (__ui_nodes[i].screenId != __ui_active_screen) { __ui_nodes[i].dirty = 0; continue; }
224
303
  } else {
@@ -226,41 +305,85 @@ export function emitTickDirtyDrawPhase() {
226
305
  i = -1;
227
306
  for (uint16_t candidate = 0; candidate < __ui_node_count; candidate++) {
228
307
  if (!__ui_nodes[candidate].dirty) continue;
308
+ if (__ui_fb) __ui_fb_frame_dirty = 1;
229
309
  if (!ui_is_effectively_visible(candidate)) { __ui_nodes[candidate].dirty = 0; continue; }
230
310
  if (__ui_nodes[candidate].screenId != __ui_active_screen) { __ui_nodes[candidate].dirty = 0; continue; }
231
- if (i < 0 || ui_node_draws_before(candidate, (uint16_t)i)) i = (int16_t)candidate;
311
+ if (i < 0 || ui_node_draws_before(candidate, static_cast<uint16_t>(i))) i = static_cast<int16_t>(candidate);
232
312
  }
233
313
  if (i < 0) break;
234
314
  __ui_draw_pass++;
235
315
  }
236
316
 
237
317
  // Defer direct display draws for overflow scroll subtrees not composited this
238
- // frame (Mode B canvas or Mode C strip). Drawing them directly clears the live
239
- // viewport and produces sequential flashes (AGENTS.md).
318
+ // frame (Mode B canvas, Mode C strip, direct-full fallback, or band render).
319
+ // Drawing them directly clears the live viewport and produces sequential
320
+ // flashes (AGENTS.md) — except in direct-full mode, where there is no canvas
321
+ // and direct draw is the only way to show content.
240
322
  {
241
- int16_t scrollComp = ui_overflow_scroll_compositor((uint16_t)i);
323
+ int16_t scrollComp = ui_overflow_scroll_compositor(static_cast<uint16_t>(i));
242
324
  if (scrollComp >= 0) {
243
325
  uint8_t compositing = scrollComp == bufferedScrollNode &&
244
- (bufferedScrollCanvas || bufferedScrollDirectStrip);
326
+ (bufferedScrollCanvas || bufferedScrollDirectFull || bufferedScrollBands || bufferedScrollLocalRepair);
245
327
  if (!compositing) {
246
- if ((uint16_t)i == (uint16_t)scrollComp) break;
328
+ if (static_cast<uint16_t>(i) == static_cast<uint16_t>(scrollComp)) break;
247
329
  __ui_nodes[i].dirty = 0;
248
330
  continue;
249
331
  }
250
332
  }
251
333
  }
252
334
 
253
- // Mode C strip: scroll owner is not drawn directly (would fill the viewport).
254
- if (bufferedScrollDirectStrip && bufferedScrollNode >= 0 &&
255
- (uint16_t)i == (uint16_t)bufferedScrollNode) {
335
+ // Band renderer: paint the whole subtree at the scroll owner's z-order slot.
336
+ // This must run AFTER lower-z nodes (e.g. the screen background fill, which
337
+ // has lower source order) have been drawn, so they don't overwrite the bands.
338
+ // When the main loop reaches the scroll owner itself, render the bands now
339
+ // (its z-position), mark the owner handled, and continue.
340
+ if (bufferedScrollBands && bufferedScrollNode >= 0 &&
341
+ static_cast<uint16_t>(i) == static_cast<uint16_t>(bufferedScrollNode)) {
342
+ if (ui_render_scroll_bands(static_cast<uint16_t>(bufferedScrollNode))) {
343
+ bufferedScrollBands = 0;
344
+ bufferedScrollNode = -1;
345
+ __ui_nodes[i].dirty = 0;
346
+ continue;
347
+ }
348
+ // Band canvas allocation failed. Retain the existing viewport pixels rather
349
+ // than falling back to direct-full SPI primitives; direct-full is the
350
+ // tearing path this compositor is designed to prevent.
351
+ bufferedScrollBands = 0;
352
+ if (__ui_scroll_render_locked) __ui_scroll_render_locked[static_cast<uint16_t>(bufferedScrollNode)] = 1;
353
+ __ui_nodes[static_cast<uint16_t>(bufferedScrollNode)].dirty = 0;
354
+ bufferedScrollNode = -1;
355
+ } // Band renderer early-push: if we're about to draw a node that comes AFTER
356
+ // the scroll owner in draw order (e.g. a header with higher source order)
357
+ // and the bands haven't rendered yet, render them first so they land below
358
+ // that higher-z node. Mirrors the Mode B canvas early-push below.
359
+ if (bufferedScrollBands && bufferedScrollNode >= 0 &&
360
+ !(i > bufferedScrollNode && i < __ui_nodes[bufferedScrollNode].subtreeEnd) &&
361
+ ui_node_draws_before(static_cast<uint16_t>(bufferedScrollNode), static_cast<uint16_t>(i))) {
362
+ if (ui_render_scroll_bands(static_cast<uint16_t>(bufferedScrollNode))) {
363
+ bufferedScrollBands = 0;
364
+ bufferedScrollNode = -1;
365
+ } else {
366
+ // Band alloc failed — retain the previous viewport rather than exposing
367
+ // a direct-full per-primitive SPI repaint.
368
+ bufferedScrollBands = 0;
369
+ if (__ui_scroll_render_locked) __ui_scroll_render_locked[static_cast<uint16_t>(bufferedScrollNode)] = 1;
370
+ __ui_nodes[static_cast<uint16_t>(bufferedScrollNode)].dirty = 0;
371
+ bufferedScrollNode = -1;
372
+ }
373
+ }
374
+
375
+ // Mode C strip / direct-full: scroll owner is not drawn directly (would fill
376
+ // the viewport); its children are drawn directly below.
377
+ if ((bufferedScrollDirectFull || bufferedScrollLocalRepair) && bufferedScrollNode >= 0 &&
378
+ static_cast<uint16_t>(i) == static_cast<uint16_t>(bufferedScrollNode)) {
256
379
  __ui_nodes[i].dirty = 0;
257
380
  continue;
258
381
  }
259
382
 
260
383
  if (bufferedScrollNode >= 0 && bufferedScrollCanvas &&
261
384
  !(i > bufferedScrollNode && i < __ui_nodes[bufferedScrollNode].subtreeEnd) &&
262
- ui_node_draws_before((uint16_t)bufferedScrollNode, (uint16_t)i) &&
263
- !ui_scroll_subtree_has_dirty((uint16_t)bufferedScrollNode)) {
385
+ ui_node_draws_before(static_cast<uint16_t>(bufferedScrollNode), static_cast<uint16_t>(i)) &&
386
+ !ui_scroll_subtree_has_dirty(static_cast<uint16_t>(bufferedScrollNode))) {
264
387
  ui_push_buffered_scroll_canvas(bufferedScrollCanvas, bufferedScrollRepaintCanvas,
265
388
  bufferedScrollNode, bufferedScrollVX, bufferedScrollVY,
266
389
  bufferedScrollRepaintY, bufferedScrollRepaintH, __ui_draw_target);
@@ -269,8 +392,16 @@ export function emitTickDirtyDrawPhase() {
269
392
  bufferedScrollRepaintCanvas = nullptr;
270
393
  }
271
394
 
272
- // Redirect to the scroll canvas if this node is inside the buffered container.
273
- uint8_t drawingBufferedScroll = bufferedScrollNode >= 0 && i > bufferedScrollNode && i < __ui_nodes[bufferedScrollNode].subtreeEnd;
395
+ // Redirect to the scroll canvas if this node is inside the buffered container
396
+ // AND a real canvas is active this frame (Mode B only). Direct-strip and
397
+ // direct-full have no canvas — their children must draw to the display target
398
+ // (and thus qualify for the per-node paint-canvas optimization), not redirect
399
+ // to a null scroll canvas. Without this, a strip-drag frame sets
400
+ // drawingBufferedScroll=1, which skips the paint canvas and forces the
401
+ // repainting children to direct-draw per-pixel to SPI (~100ms/text node).
402
+ uint8_t drawingBufferedScroll = !bufferedScrollDirectFull &&
403
+ (bufferedScrollCanvas != nullptr) && bufferedScrollNode >= 0 &&
404
+ i > bufferedScrollNode && i < __ui_nodes[bufferedScrollNode].subtreeEnd;
274
405
  int16_t origBoxX = __ui_nodes[i].box.x;
275
406
  int16_t origBoxY = __ui_nodes[i].box.y;
276
407
  if (drawingBufferedScroll) {
@@ -298,11 +429,11 @@ export function emitTickDirtyDrawPhase() {
298
429
  ui_node_text_layout_metrics(i, textMaxW, &tw, &th);
299
430
  uint16_t paintTextW = tw;
300
431
  uint16_t paintTextH = th;
301
- if (__ui_nodes[i].kind == NODE_TEXT) {
302
- uint16_t hInset = (uint16_t)__ui_nodes[i].paddingLeft + (uint16_t)__ui_nodes[i].paddingRight + (uint16_t)__ui_nodes[i].borderWidth * 2;
303
- uint16_t vInset = (uint16_t)__ui_nodes[i].paddingTop + (uint16_t)__ui_nodes[i].paddingBottom + (uint16_t)__ui_nodes[i].borderWidth * 2;
304
- paintTextW = (uint16_t)(tw + hInset);
305
- paintTextH = (uint16_t)(th + vInset);
432
+ if (__ui_nodes[i].kind == NODE_TEXT || __ui_nodes[i].kind == NODE_SELECT) {
433
+ uint16_t hInset = static_cast<uint16_t>(__ui_nodes[i].paddingLeft) + static_cast<uint16_t>(__ui_nodes[i].paddingRight) + static_cast<uint16_t>(__ui_nodes[i].borderWidth) * 2;
434
+ uint16_t vInset = static_cast<uint16_t>(__ui_nodes[i].paddingTop) + static_cast<uint16_t>(__ui_nodes[i].paddingBottom) + static_cast<uint16_t>(__ui_nodes[i].borderWidth) * 2;
435
+ paintTextW = static_cast<uint16_t>(tw + hInset);
436
+ paintTextH = static_cast<uint16_t>(th + vInset);
306
437
  }
307
438
  if (__ui_nodes[i].kind == NODE_CHECK || __ui_nodes[i].kind == NODE_RADIO) {
308
439
  paintTextW = tw + 22;
@@ -324,18 +455,24 @@ export function emitTickDirtyDrawPhase() {
324
455
  int16_t faceW = __ui_nodes[i].box.w;
325
456
  int16_t faceH = __ui_nodes[i].box.h;
326
457
  CuttlefishCanvas16* scrollDrawCanvas = bufferedScrollRepaintCanvas ? bufferedScrollRepaintCanvas : bufferedScrollCanvas;
327
- int16_t scrollDrawW = display_canvasWidth(scrollDrawCanvas);
328
- int16_t scrollDrawH = display_canvasHeight(scrollDrawCanvas);
329
- if (bufferedScrollRepaintCanvas) {
330
- scrollDrawW = __ui_nodes[bufferedScrollNode].box.w;
331
- scrollDrawH = bufferedScrollRepaintH;
332
- }
333
- if (faceY + faceH <= 0 || faceY >= scrollDrawH ||
334
- faceX + faceW <= 0 || faceX >= scrollDrawW) {
335
- __ui_nodes[i].box.x = origBoxX;
336
- __ui_nodes[i].box.y = origBoxY;
337
- __ui_nodes[i].dirty = 0;
338
- continue;
458
+ // Guard against a null canvas (the subtree was flagged drawingBufferedScroll
459
+ // but no canvas is available this frame — e.g. a second scroll owner whose
460
+ // canvas couldn't allocate). Skip the canvas-local clip and draw the node
461
+ // to the display target; the scroll-viewport clip below still bounds it.
462
+ if (scrollDrawCanvas) {
463
+ int16_t scrollDrawW = display_canvasWidth(scrollDrawCanvas);
464
+ int16_t scrollDrawH = display_canvasHeight(scrollDrawCanvas);
465
+ if (bufferedScrollRepaintCanvas) {
466
+ scrollDrawW = __ui_nodes[bufferedScrollNode].box.w;
467
+ scrollDrawH = bufferedScrollRepaintH;
468
+ }
469
+ if (faceY + faceH <= 0 || faceY >= scrollDrawH ||
470
+ faceX + faceW <= 0 || faceX >= scrollDrawW) {
471
+ __ui_nodes[i].box.x = origBoxX;
472
+ __ui_nodes[i].box.y = origBoxY;
473
+ __ui_nodes[i].dirty = 0;
474
+ continue;
475
+ }
339
476
  }
340
477
  } else if (ui_is_rect_clipped_by_scroll(i, drawX, drawY, __ui_nodes[i].box.w, __ui_nodes[i].box.h)) {
341
478
  __ui_nodes[i].box.x = origBoxX;
@@ -352,12 +489,20 @@ export function emitTickDirtyDrawPhase() {
352
489
  int16_t paintCanvasH = paintRect.h;
353
490
  // RAM-composite pixel-heavy nodes before SPI push. Skip when already drawing
354
491
  // into a scroll canvas or a full-screen framebuffer (both are RAM targets).
355
- if (!drawingBufferedScroll && !__ui_fb && ui_should_buffer_paint(i, paintCanvasW, paintCanvasH)) {
492
+ uint8_t wantedBuffer = !drawingBufferedScroll && !__ui_fb && ui_should_buffer_paint(i, paintCanvasW, paintCanvasH);
493
+ // Large paint rects prefer the band renderer (small ~10KB SRAM canvas,
494
+ // strip-at-a-time) over the repair canvas even when the repair canvas could
495
+ // allocate (e.g. in PSRAM). A large repair canvas has higher alloc/seed/push
496
+ // latency than banding, which shows as a press/scroll flash; the band canvas
497
+ // lives in fast internal SRAM and pushes smaller per-band transactions.
498
+ uint8_t preferBand = wantedBuffer && (UI_BAND_PREFER_PIXELS > 0) &&
499
+ (static_cast<uint32_t>(paintCanvasW) * static_cast<uint32_t>(paintCanvasH) > static_cast<uint32_t>(UI_BAND_PREFER_PIXELS));
500
+ if (wantedBuffer && !preferBand) {
356
501
  paintCanvas = ui_get_repair_canvas(paintCanvasW, paintCanvasH);
357
502
  if (paintCanvas) {
358
503
  drawingPaintCanvas = 1;
359
504
  ui_display_set_target(paintCanvas);
360
- ui_seed_paint_canvas_for_node(i, paintCanvas, paintCanvasX, paintCanvasY);
505
+ ui_seed_paint_canvas_for_node(i, paintCanvas, paintCanvasX, paintCanvasY, 0);
361
506
  baseDrawX -= paintCanvasX;
362
507
  baseDrawY -= paintCanvasY;
363
508
  drawX -= paintCanvasX;
@@ -368,7 +513,49 @@ export function emitTickDirtyDrawPhase() {
368
513
  }
369
514
  }
370
515
  if (!drawingPaintCanvas) {
371
- ui_clear_press_offset_area(i, baseDrawX, baseDrawY, drawX, drawY, paintTextW, paintTextH);
516
+ // No repair canvas this frame — either the paint rect is large enough to
517
+ // prefer the band renderer (preferBand: lower-latency than a big PSRAM
518
+ // repair canvas), or the repair canvas wouldn't allocate (over budget /
519
+ // heap fragmentation). Composite the node into the ~10KB band canvas one
520
+ // horizontal strip at a time, pushing each band tear-free. Avoids the
521
+ // direct clear→redraw-to-SPI that visibly flashes on press/scroll.
522
+ if (wantedBuffer && ui_render_node_bands(static_cast<uint16_t>(i), paintCanvasX, paintCanvasY, paintCanvasW, paintCanvasH)) {
523
+ if (!drawingBufferedScroll) {
524
+ ui_invalidate_scroll_canvas_for_node(i);
525
+ }
526
+ __ui_nodes[i].box.x = origBoxX;
527
+ __ui_nodes[i].box.y = origBoxY;
528
+ __ui_nodes[i].dirty = 0;
529
+ ui_refresh_add_rect(__ui_nodes[i].box.x, __ui_nodes[i].box.y, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
530
+ continue;
531
+ }
532
+ // Band canvas also failed — last resort: retry the repair canvas (it
533
+ // might fit when the band canvas is contended). If both allocations fail,
534
+ // lock this scroll owner for the frame and retain its previous pixels;
535
+ // never fall through to direct primitive SPI draws, which visibly tear.
536
+ if (wantedBuffer && preferBand) {
537
+ paintCanvas = ui_get_repair_canvas(paintCanvasW, paintCanvasH);
538
+ if (paintCanvas) {
539
+ drawingPaintCanvas = 1;
540
+ ui_display_set_target(paintCanvas);
541
+ ui_seed_paint_canvas_for_node(i, paintCanvas, paintCanvasX, paintCanvasY, 0);
542
+ baseDrawX -= paintCanvasX;
543
+ baseDrawY -= paintCanvasY;
544
+ drawX -= paintCanvasX;
545
+ drawY -= paintCanvasY;
546
+ }
547
+ }
548
+ if (!drawingPaintCanvas) {
549
+ if (wantedBuffer && preferBand && __ui_scroll_render_locked &&
550
+ __ui_nodes[i].scrollable) {
551
+ __ui_scroll_render_locked[i] = 1;
552
+ __ui_nodes[i].dirty = 0;
553
+ __ui_nodes[i].box.x = origBoxX;
554
+ __ui_nodes[i].box.y = origBoxY;
555
+ continue;
556
+ }
557
+ ui_clear_press_offset_area(i, baseDrawX, baseDrawY, drawX, drawY, paintTextW, paintTextH);
558
+ }
372
559
  }
373
560
  uint8_t skipListOutsetShadow =
374
561
  (__ui_nodes[i].kind == NODE_LIST && !drawingBufferedScroll && !__ui_fb);
@@ -396,666 +583,30 @@ export function emitTickDirtyDrawPhase() {
396
583
  bColor = ui_blend(bColor, backdrop, __ui_nodes[i].opacity);
397
584
  fillBg = ui_blend(__ui_nodes[i].bg, backdrop, __ui_nodes[i].opacity);
398
585
  }
399
- switch (__ui_nodes[i].kind) {
400
- case NODE_FILL:
401
- if (__ui_nodes[i].gradientEnabled > 0) {
402
- ui_draw_gradient_fill(i, drawY);
403
- } else if (__ui_nodes[i].borderRadius > 0 && __ui_nodes[i].hasBg) {
404
- int16_t fillW = ui_rotated_face_w(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
405
- int16_t fillH = ui_rotated_face_h(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
406
- ui_display_fill_round_rect(__ui_nodes[i].box.x, drawY, fillW, fillH, __ui_nodes[i].borderRadius, fillBg);
407
- } else if (__ui_nodes[i].hasBg) {
408
- int16_t fillW = ui_rotated_face_w(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
409
- int16_t fillH = ui_rotated_face_h(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
410
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, fillW, fillH, fillBg);
411
- }
412
- ui_draw_shadow(i, drawY, 1);
413
- if (__ui_nodes[i].borderStyle != 0) {
414
- UI_COLOR_T bColor = __ui_nodes[i].borderColor ? __ui_nodes[i].borderColor : __ui_nodes[i].fg;
415
- int16_t borderW = ui_rotated_face_w(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
416
- int16_t borderH = ui_rotated_face_h(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
417
- ui_draw_rect_outline(__ui_nodes[i].box.x, drawY, borderW, borderH,
418
- __ui_nodes[i].borderRadius, __ui_nodes[i].borderStyle, __ui_nodes[i].borderWidth, bColor);
419
- }
420
- break;
421
- case NODE_TEXT:
422
- {
423
- int16_t insetL = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingLeft;
424
- int16_t insetR = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingRight;
425
- int16_t insetT = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingTop;
426
- int16_t textX = __ui_nodes[i].box.x + insetL;
427
- int16_t textY = drawY + insetT;
428
- int16_t textW = (int16_t)__ui_nodes[i].box.w - insetL - insetR;
429
- if (textW < 1) textW = 1;
430
- uint8_t textBoxPainted = 0;
431
- if (__ui_nodes[i].gradientEnabled > 0) {
432
- ui_draw_gradient_fill(i, drawY);
433
- textBoxPainted = 1;
434
- } else if (__ui_nodes[i].borderRadius > 0 && __ui_nodes[i].hasBg) {
435
- ui_display_fill_round_rect(__ui_nodes[i].box.x, drawY,
436
- __ui_nodes[i].box.w, __ui_nodes[i].box.h, __ui_nodes[i].borderRadius, fillBg);
437
- textBoxPainted = 1;
438
- } else if (__ui_nodes[i].hasBg) {
439
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY,
440
- __ui_nodes[i].box.w, __ui_nodes[i].box.h, fillBg);
441
- textBoxPainted = 1;
442
- }
443
- {
444
- uint16_t clearW = __ui_nodes[i].box.w;
445
- int16_t paintedTextW = (int16_t)__ui_nodes[i].lastTextWidth + insetL + insetR;
446
- if (__ui_nodes[i].lastTextWidth > 0 && paintedTextW > (int16_t)clearW) {
447
- clearW = (uint16_t)paintedTextW;
448
- }
449
- uint16_t paddedTw = (uint16_t)((int16_t)tw + insetL + insetR);
450
- if (paddedTw > clearW) clearW = paddedTw;
451
- // overflow:hidden/scroll: never clear past the node's own box. A nowrap
452
- // line wider than its box would otherwise erase the parent's border.
453
- if (__ui_nodes[i].scrollable) clearW = __ui_nodes[i].box.w;
454
- uint16_t clearH = __ui_nodes[i].box.h;
455
- int16_t paintedTextH = (int16_t)__ui_nodes[i].lastTextHeight + insetT + (int16_t)__ui_nodes[i].paddingBottom + (int16_t)__ui_nodes[i].borderWidth;
456
- if (__ui_nodes[i].lastTextHeight > 0 && paintedTextH > (int16_t)clearH) {
457
- clearH = (uint16_t)paintedTextH;
458
- }
459
- uint16_t paddedTh = (uint16_t)((int16_t)th + insetT + (int16_t)__ui_nodes[i].paddingBottom + (int16_t)__ui_nodes[i].borderWidth);
460
- if (paddedTh > clearH) clearH = paddedTh;
461
- // Dynamic transparent text still needs a clear, otherwise old glyph
462
- // pixels accumulate when only this text node is dirty.
463
- UI_COLOR_T clearCol = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
464
- // Blend the clear toward the backdrop by opacity so a translucent text
465
- // node (inherited from an opacity:<1 parent) doesn't repaint a solid
466
- // block of its parent's fill around the glyphs.
467
- if (__ui_nodes[i].opacity < 100) {
468
- clearCol = ui_blend(clearCol, ui_parent_clear_color(i), __ui_nodes[i].opacity);
469
- }
470
- if (!textBoxPainted) {
471
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, clearW, clearH, clearCol);
472
- }
473
- __ui_nodes[i].lastTextWidth = tw;
474
- __ui_nodes[i].lastTextHeight = th;
475
- }
476
- ui_draw_shadow(i, drawY, 1);
477
- if (__ui_nodes[i].borderStyle != 0) {
478
- ui_draw_node_border(i, __ui_nodes[i].box.x, drawY, bColor);
479
- }
480
- // Rich-text (inline runs): draw from precomputed geometry instead of the
481
- // single-string wrapped path. Geometry is baked at transpile time; the
482
- // runtime does not re-wrap.
483
- if (__ui_nodes[i].runCount > 0) {
484
- UI_COLOR_T richTextBg;
485
- if (__ui_nodes[i].opacity < 100) {
486
- uint16_t p = __ui_nodes[i].parent;
487
- UI_COLOR_T source = (p != UI_NO_PARENT && __ui_nodes[p].hasBg) ? __ui_nodes[p].bg
488
- : (__ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor);
489
- richTextBg = ui_blend(source, __ui_nodes[i].clearColor, __ui_nodes[i].opacity);
490
- } else {
491
- richTextBg = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : ui_parent_clear_color(i);
492
- }
493
- if (__ui_nodes[i].textShadowCount > 0) {
494
- UI_COLOR_T tsClear = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
495
- uint32_t tsCol = ui_blend(__ui_nodes[i].textShadowColor, tsClear, __ui_nodes[i].textShadowAlpha);
496
- // Shadow pass: draw the rich block in the shadow color at the offset.
497
- // (Per-segment shadow color is approximated by drawing the whole
498
- // block once in tsCol.)
499
- ui_draw_rich_text(i,
500
- textX + __ui_nodes[i].textShadowOffsetX,
501
- textY + __ui_nodes[i].textShadowOffsetY,
502
- tsClear, __ui_nodes[i].fontAntialias, 1, tsCol, (uint16_t)textW);
503
- }
504
- ui_draw_rich_text(i, textX, textY, richTextBg, __ui_nodes[i].fontAntialias, 0, 0, (uint16_t)textW);
505
- break;
506
- }
507
- {
508
- // Text shadow: draw the text in the shadow color at the offset first.
509
- UI_COLOR_T tsClear = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
510
- if (__ui_nodes[i].textShadowCount > 0) {
511
- uint32_t tsCol = ui_blend(__ui_nodes[i].textShadowColor, tsClear, __ui_nodes[i].textShadowAlpha);
512
- ui_draw_wrapped_text(displayText,
513
- textX + __ui_nodes[i].textShadowOffsetX,
514
- textY + __ui_nodes[i].textShadowOffsetY,
515
- (uint16_t)textW, tsCol, tsCol, ts, __ui_nodes[i].fontAntialias,
516
- __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing, __ui_nodes[i].lineHeight,
517
- __ui_nodes[i].whiteSpaceMode, __ui_nodes[i].textAlign, 0, __ui_nodes[i].textOverflow);
518
- }
519
- // Use the parent's clear color as the text background when the node
520
- // has no own background. This makes Adafruit_GFX's opaque glyph-cell
521
- // fill blend with the parent (instead of drawing solid fg blocks that
522
- // overlap adjacent lines/elements). For AA text, fg != bg so the
523
- // edge-detection path still runs correctly.
524
- // Glyph-cell background. For a translucent text node sitting on a
525
- // filled translucent parent, the glyph cells must match the parent's
526
- // blended fill: blend565(parent.bg, backdrop, opacity). The node's own
527
- // clearColor carries the backdrop (set by flatten), and opacity has
528
- // already inherited from the parent. Use the parent's raw bg as the
529
- // source so the glyph cells reproduce the parent's translucent fill.
530
- UI_COLOR_T textBg;
531
- if (__ui_nodes[i].opacity < 100) {
532
- uint16_t p = __ui_nodes[i].parent;
533
- UI_COLOR_T source = (p != UI_NO_PARENT && __ui_nodes[p].hasBg) ? __ui_nodes[p].bg
534
- : (__ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor);
535
- textBg = ui_blend(source, __ui_nodes[i].clearColor, __ui_nodes[i].opacity);
536
- } else {
537
- textBg = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : ui_parent_clear_color(i);
538
- }
539
- ui_draw_wrapped_text(displayText, textX, textY, (uint16_t)textW,
540
- __ui_nodes[i].fg, textBg, ts, __ui_nodes[i].fontAntialias,
541
- __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing, __ui_nodes[i].lineHeight,
542
- __ui_nodes[i].whiteSpaceMode, __ui_nodes[i].textAlign, __ui_nodes[i].underline, __ui_nodes[i].textOverflow);
543
- }
544
- }
545
- break;
546
- case NODE_BUTTON:
547
- if (__ui_nodes[i].borderRadius > 0 && __ui_nodes[i].hasBg)
548
- ui_display_fill_round_rect(__ui_nodes[i].box.x, drawY, __ui_nodes[i].box.w, __ui_nodes[i].box.h, __ui_nodes[i].borderRadius, __ui_nodes[i].bg);
549
- else if (__ui_nodes[i].hasBg)
550
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, __ui_nodes[i].box.w, __ui_nodes[i].box.h, __ui_nodes[i].bg);
551
- ui_draw_shadow(i, drawY, 1);
552
- if (__ui_nodes[i].borderStyle != 0) {
553
- ui_draw_node_border(i, __ui_nodes[i].box.x, drawY, bColor);
554
- }
555
- {
556
- int16_t insetL = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingLeft;
557
- int16_t insetR = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingRight;
558
- int16_t insetT = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingTop;
559
- int16_t insetB = (int16_t)__ui_nodes[i].borderWidth + (int16_t)__ui_nodes[i].paddingBottom;
560
- int16_t textX = __ui_nodes[i].box.x + insetL;
561
- int16_t textY = drawY + insetT;
562
- int16_t textW = (int16_t)__ui_nodes[i].box.w - insetL - insetR;
563
- int16_t textH = (int16_t)__ui_nodes[i].box.h - insetT - insetB;
564
- if (textW < 1) textW = 1;
565
- if (textH < 1) textH = (int16_t)th;
566
- ui_draw_wrapped_text(displayText,
567
- textX,
568
- textY + (textH - (int16_t)th) / 2,
569
- (uint16_t)textW,
570
- __ui_nodes[i].fg,
571
- __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor,
572
- ts, __ui_nodes[i].fontAntialias, __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing,
573
- __ui_nodes[i].lineHeight, __ui_nodes[i].whiteSpaceMode, __ui_nodes[i].textAlign, __ui_nodes[i].underline, __ui_nodes[i].textOverflow);
574
- }
575
- break;
576
- case NODE_CHECK:
577
- {
578
- uint16_t clearW = __ui_nodes[i].box.w;
579
- if (__ui_nodes[i].lastTextWidth > 0 && __ui_nodes[i].lastTextWidth > (int16_t)clearW) {
580
- clearW = (uint16_t)__ui_nodes[i].lastTextWidth;
581
- }
582
- if (paintTextW > clearW) clearW = paintTextW;
583
- uint16_t clearH = __ui_nodes[i].box.h;
584
- if (__ui_nodes[i].lastTextHeight > 0 && __ui_nodes[i].lastTextHeight > (int16_t)clearH) {
585
- clearH = (uint16_t)__ui_nodes[i].lastTextHeight;
586
- }
587
- if (paintTextH > clearH) clearH = paintTextH;
588
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, clearW, clearH,
589
- __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor);
590
- __ui_nodes[i].lastTextWidth = paintTextW;
591
- __ui_nodes[i].lastTextHeight = paintTextH;
592
- }
593
- {
594
- int16_t cbX = __ui_nodes[i].box.x;
595
- int16_t cbY = drawY;
596
- if (__ui_nodes[i].value) {
597
- ui_display_fill_rect(cbX, cbY, 16, 16, __ui_nodes[i].fg);
598
- UI_COLOR_T inv = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
599
- #ifdef UI_AA
600
- {
601
- // Draw the checkmark to a 16×16 AA canvas for smooth diagonals.
602
- CuttlefishCanvas16* c = ui_aa_begin(16, 16, __ui_nodes[i].fg);
603
- // First stroke: down-left (3,8 → 7,12)
604
- ui_aa_line(c, 4.0f, 8.0f, 7.0f, 12.0f, inv);
605
- ui_aa_line(c, 5.0f, 8.0f, 8.0f, 12.0f, inv);
606
- // Second stroke: up-right (7,11 → 13,4)
607
- ui_aa_line(c, 7.0f, 11.0f, 13.0f, 4.0f, inv);
608
- ui_aa_line(c, 8.0f, 11.0f, 14.0f, 4.0f, inv);
609
- ui_aa_push(c, cbX, cbY);
610
- }
611
- #else
612
- ui_display_draw_line(cbX + 3, cbY + 8, cbX + 7, cbY + 12, inv);
613
- ui_display_draw_line(cbX + 4, cbY + 8, cbX + 8, cbY + 12, inv);
614
- ui_display_draw_line(cbX + 3, cbY + 9, cbX + 7, cbY + 13, inv);
615
- ui_display_draw_line(cbX + 7, cbY + 12, cbX + 13, cbY + 4, inv);
616
- ui_display_draw_line(cbX + 8, cbY + 12, cbX + 14, cbY + 4, inv);
617
- ui_display_draw_line(cbX + 7, cbY + 13, cbX + 13, cbY + 5, inv);
618
- #endif
619
- } else {
620
- ui_display_draw_rect(cbX, cbY, 16, 16, __ui_nodes[i].fg);
621
- }
622
- }
623
- ui_draw_wrapped_text(displayText, __ui_nodes[i].box.x + 22, drawY, textMaxW,
624
- __ui_nodes[i].fg, __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor,
625
- ts, __ui_nodes[i].fontAntialias, __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing,
626
- __ui_nodes[i].lineHeight, __ui_nodes[i].whiteSpaceMode, 0, __ui_nodes[i].underline, __ui_nodes[i].textOverflow);
627
- break;
628
- case NODE_RADIO:
629
- {
630
- uint16_t clearW = __ui_nodes[i].box.w;
631
- if (__ui_nodes[i].lastTextWidth > 0 && __ui_nodes[i].lastTextWidth > (int16_t)clearW) {
632
- clearW = (uint16_t)__ui_nodes[i].lastTextWidth;
633
- }
634
- if (paintTextW > clearW) clearW = paintTextW;
635
- uint16_t clearH = __ui_nodes[i].box.h;
636
- if (__ui_nodes[i].lastTextHeight > 0 && __ui_nodes[i].lastTextHeight > (int16_t)clearH) {
637
- clearH = (uint16_t)__ui_nodes[i].lastTextHeight;
638
- }
639
- if (paintTextH > clearH) clearH = paintTextH;
640
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, clearW, clearH,
641
- __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor);
642
- __ui_nodes[i].lastTextWidth = paintTextW;
643
- __ui_nodes[i].lastTextHeight = paintTextH;
644
- int16_t cbX = __ui_nodes[i].box.x;
645
- int16_t cbY = drawY;
646
- #ifdef UI_AA
647
- {
648
- // Render the radio circle to a 16×16 AA canvas, then push.
649
- UI_COLOR_T radioBg = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
650
- CuttlefishCanvas16* c = ui_aa_begin(16, 16, radioBg);
651
- if (__ui_nodes[i].value) {
652
- ui_aa_fill_circle(c, 8, 8, 7.0f, __ui_nodes[i].fg);
653
- ui_aa_fill_circle(c, 8, 8, 3.0f, radioBg);
654
- } else {
655
- ui_aa_circle(c, 8, 8, 7.0f, __ui_nodes[i].fg);
656
- }
657
- ui_aa_push(c, cbX, cbY);
658
- }
659
- #else
660
- if (__ui_nodes[i].value) {
661
- ui_display_fill_circle(cbX + 8, cbY + 8, 7, __ui_nodes[i].fg);
662
- ui_display_fill_circle(cbX + 8, cbY + 8, 3, __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor);
663
- } else {
664
- ui_display_draw_circle(cbX + 8, cbY + 8, 7, __ui_nodes[i].fg);
665
- }
666
- #endif
667
- }
668
- ui_draw_wrapped_text(displayText, __ui_nodes[i].box.x + 22, drawY, textMaxW,
669
- __ui_nodes[i].fg, __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor,
670
- ts, __ui_nodes[i].fontAntialias, __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing,
671
- __ui_nodes[i].lineHeight, __ui_nodes[i].whiteSpaceMode, 0, __ui_nodes[i].underline, __ui_nodes[i].textOverflow);
672
- break;
673
- case NODE_PROGRESS:
674
- // Progress bar: outline track + filled portion based on .value (0-100).
675
- // Incremental redraw — only draws/clears the delta to avoid flashing.
676
- {
677
- int16_t bx = __ui_nodes[i].box.x;
678
- int16_t by = drawY;
679
- int16_t bw = __ui_nodes[i].box.w;
680
- int16_t bh = __ui_nodes[i].box.h;
681
- UI_COLOR_T bgCol = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
682
- UI_COLOR_T fgCol = __ui_nodes[i].fg;
683
-
684
- // On first draw (lastTextWidth < 0), draw everything.
685
- // Otherwise incremental: only update the changed portion.
686
- uint8_t pct = constrain(__ui_nodes[i].value, 0, 100);
687
- int16_t fillW = ((int32_t)(bw - 2) * pct) / 100;
688
- int16_t prevW = __ui_nodes[i].lastTextWidth; // reused as previous fill width
689
-
690
- if (prevW < 0) {
691
- // Full redraw: outline + background + fill
692
- ui_display_draw_rect(bx, by, bw, bh, fgCol);
693
- ui_display_fill_rect(bx + 1, by + 1, bw - 2, bh - 2, bgCol);
694
- if (fillW > 0) {
695
- ui_display_fill_rect(bx + 1, by + 1, fillW, bh - 2, fgCol);
696
- }
697
- } else if (fillW > prevW) {
698
- // Value increased: draw new fill segment on top (no clear needed)
699
- ui_display_fill_rect(bx + 1 + prevW, by + 1, fillW - prevW, bh - 2, fgCol);
700
- } else if (fillW < prevW) {
701
- // Value decreased: clear the removed portion
702
- ui_display_fill_rect(bx + 1 + fillW, by + 1, prevW - fillW, bh - 2, bgCol);
703
- }
704
- // Remember current fill width for next incremental update
705
- __ui_nodes[i].lastTextWidth = fillW;
706
- }
707
- break;
708
- case NODE_RANGE:
709
- // Range slider: horizontal track + draggable thumb.
710
- // Incremental redraw (like NODE_PROGRESS): lastTextWidth holds the
711
- // previous fill width. We erase the delta region between old and new
712
- // thumb positions with the background, then redraw the track portion
713
- // and the new thumb — so dragging backward doesn't leave ghost thumbs.
714
- {
715
- int16_t bx = __ui_nodes[i].box.x;
716
- int16_t by = drawY;
717
- int16_t bw = __ui_nodes[i].box.w;
718
- int16_t bh = __ui_nodes[i].box.h;
719
- UI_COLOR_T fgCol = __ui_nodes[i].fg;
720
- UI_COLOR_T bgCol = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
721
- UI_COLOR_T dimFg = (UI_COLOR_T)((fgCol >> 1) & UI_DIM_MASK);
722
-
723
- int16_t trackY = by + bh / 2;
724
- int16_t rMin = __ui_nodes[i].rangeMin;
725
- int16_t rMax = __ui_nodes[i].rangeMax;
726
- int16_t range = rMax - rMin;
727
- if (range <= 0) range = 100;
728
- int16_t pct = constrain(__ui_nodes[i].value, rMin, rMax) - rMin;
729
- int16_t fillW = ((int32_t)(bw - 8) * pct) / range;
730
- // lastTextWidth carries the previous fill width, or -1 if this node
731
- // has never been drawn (fillW=0 at value=min is a valid thumb pos).
732
- int16_t prevFillW = __ui_nodes[i].lastTextWidth;
733
- int16_t newThumbX = bx + 4 + fillW - 3;
734
-
735
- if (prevFillW < 0) {
736
- // First draw: redraw the whole track + fill from scratch.
737
- ui_display_draw_fast_hline(bx, trackY, bw, dimFg);
738
- ui_display_draw_fast_hline(bx + 4, trackY, fillW, fgCol);
739
- } else {
740
- // Incremental: wipe the strip between the old and new thumb
741
- // positions (whichever extends further on each side), then restore
742
- // the track line. This is symmetric — old thumbs disappear whether
743
- // the drag moves forward or backward.
744
- int16_t prevThumbX = bx + 4 + prevFillW - 3;
745
- int16_t left = prevThumbX < newThumbX ? prevThumbX : newThumbX;
746
- int16_t right = prevThumbX + 6 > newThumbX + 6 ? prevThumbX + 6 : newThumbX + 6;
747
- if (left < bx) left = bx;
748
- if (right > bx + bw) right = bx + bw;
749
- // Erase the thumb band (10px tall) to background.
750
- ui_display_fill_rect(left, trackY - 5, right - left, 10, bgCol);
751
- // Restore the track line over the wiped strip: bright up to the
752
- // current fill end, dim beyond it.
753
- int16_t fillEnd = bx + 4 + fillW;
754
- if (right <= fillEnd) {
755
- ui_display_draw_fast_hline(left, trackY, right - left, fgCol);
756
- } else if (left >= fillEnd) {
757
- ui_display_draw_fast_hline(left, trackY, right - left, dimFg);
758
- } else {
759
- ui_display_draw_fast_hline(left, trackY, fillEnd - left, fgCol);
760
- ui_display_draw_fast_hline(fillEnd, trackY, right - fillEnd, dimFg);
761
- }
762
- }
763
-
764
- // Thumb: small filled rectangle at the current position.
765
- if (newThumbX < bx + 1) newThumbX = bx + 1;
766
- if (newThumbX > bx + bw - 7) newThumbX = bx + bw - 7;
767
- ui_display_fill_rect(newThumbX, trackY - 5, 6, 10, fgCol);
768
-
769
- // Remember current fill width for the next incremental update.
770
- __ui_nodes[i].lastTextWidth = fillW;
771
- }
772
- break;
773
- case NODE_INPUT:
774
- // Input field: bordered rect + current text (or placeholder), clipped to box width.
775
- {
776
- int16_t bx = __ui_nodes[i].box.x;
777
- int16_t by = drawY;
778
- int16_t bw = __ui_nodes[i].box.w;
779
- int16_t bh = __ui_nodes[i].box.h;
780
- UI_COLOR_T fgCol = __ui_nodes[i].fg;
781
- UI_COLOR_T bgCol = __ui_nodes[i].hasBg ? __ui_nodes[i].bg : __ui_nodes[i].clearColor;
782
- if (__ui_nodes[i].borderRadius > 0) {
783
- ui_display_fill_round_rect(bx, by, bw, bh, __ui_nodes[i].borderRadius, bgCol);
784
- } else {
785
- ui_display_fill_rect(bx, by, bw, bh, bgCol);
786
- }
787
- UI_COLOR_T inputBorder = __ui_nodes[i].borderColor ? __ui_nodes[i].borderColor : fgCol;
788
- uint8_t inputBorderStyle = __ui_nodes[i].borderStyle ? __ui_nodes[i].borderStyle : 1;
789
- uint8_t inputBorderWidth = __ui_nodes[i].borderWidth ? __ui_nodes[i].borderWidth : 1;
790
- ui_draw_rect_outline(bx, by, bw, bh, __ui_nodes[i].borderRadius, inputBorderStyle, inputBorderWidth, inputBorder);
791
- // Show typed text (textBuffer) in fg color, or placeholder (.text)
792
- // dimmed gray when the buffer is empty.
793
- UI_COLOR_T textCol = fgCol;
794
- const char* disp = (__ui_nodes[i].textBuffer[0] != 0)
795
- ? __ui_nodes[i].textBuffer
796
- : (__ui_nodes[i].text ? __ui_nodes[i].text : "");
797
- #if defined(UI_HIDE_OSK)
798
- // Desktop target: when this input is the active edit target, suppress
799
- // the placeholder. The editing buffer is loaded from the (likely empty)
800
- // textBuffer on focus, so without this the placeholder ("enter name")
801
- // would render with the caret at its end. Hide it so the caret shows
802
- // on a clean field at position 0 until the user types.
803
- if (__ui_kb_visible && (int16_t)__ui_kb_target == (int16_t)i && __ui_nodes[i].textBuffer[0] == 0) {
804
- disp = "";
805
- }
806
- #endif
807
- if (__ui_nodes[i].textBuffer[0] == 0) {
808
- #if UI_COLOR_DEPTH == 888
809
- textCol = 0x848484;
810
- #else
811
- textCol = 0x8410;
812
- #endif
813
- }
814
- // Note: the actual text draw is via ui_draw_text (which uses __ui_gfx).
815
- // The setCursor/setTextColor/setTextSize below are legacy — ui_draw_text
816
- // handles its own cursor/colors. Keep them on __ui_gfx for consistency
817
- // (in case __ui_gfx is the scroll canvas, not __tc_display).
818
- ui_display_set_cursor(bx + 4, by + (bh - ts * 8) / 2);
819
- ui_display_set_text_color(textCol, bgCol);
820
- ui_display_set_text_size(ts);
821
- // Clip: at textSize ts, each char is ts*6px advance. Only print chars
822
- // that fit within the box (bw - 8px margin), so text never overflows
823
- // the border or wraps to the next line.
824
- int16_t maxChars = (bw - 8) / (ts * 6);
825
- if (maxChars < 0) maxChars = 0;
826
- int16_t len = (int16_t)strlen(disp);
827
- if (len > maxChars) len = maxChars;
828
- if (len > UI_TEXT_BUF) len = UI_TEXT_BUF;
829
- char clipped[UI_TEXT_BUF + 1];
830
- for (int16_t c = 0; c < len; c++) {
831
- clipped[c] = disp[c];
832
- }
833
- clipped[len] = 0;
834
- ui_draw_text(clipped, bx + 4, by + (bh - ui_text_height(ts, __ui_nodes[i].fontFace)) / 2,
835
- textCol, bgCol, ts, __ui_nodes[i].fontAntialias, __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing);
836
- #if defined(UI_HIDE_OSK)
837
- // Desktop target: with no OSK grid there's no focus indicator. Draw a
838
- // blinking caret at the end of the typed text on the active edit target
839
- // so the user sees which field they're editing. Blink ~3×/sec via the
840
- // top bits of __ui_kb_blink (mask 0x20 toggles every 32 ticks ≈ 530ms).
841
- if (__ui_kb_visible && (int16_t)__ui_kb_target == (int16_t)i && (__ui_kb_blink & 0x20)) {
842
- uint16_t caretW = ui_text_width(clipped, ts, __ui_nodes[i].fontFace, __ui_nodes[i].letterSpacing);
843
- int16_t caretX = bx + 4 + (int16_t)caretW;
844
- int16_t caretYTop = by + (bh - ts * 8) / 2;
845
- // fgCol (not textCol): the placeholder-dimming path sets textCol to
846
- // gray, which would make the caret nearly invisible on a focused
847
- // empty field. The caret should always be the input's foreground.
848
- ui_display_fill_rect(caretX, caretYTop, (int16_t)(ts > 1 ? 2 : 1), (int16_t)(ts * 8), fgCol);
849
- }
850
- #endif
851
- }
852
- break;
853
- case NODE_IMG:
854
- {
855
- UI_COLOR_T imgBg = __ui_nodes[i].hasBg ? fillBg : __ui_nodes[i].clearColor;
856
- int16_t fillW = ui_rotated_face_w(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
857
- int16_t fillH = ui_rotated_face_h(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
858
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, fillW, fillH, imgBg);
859
- }
860
- if (__ui_nodes[i].imgDataId < __ui_image_count) {
861
- const UIImage* img = &__ui_images[__ui_nodes[i].imgDataId];
862
- int16_t targetW = __ui_nodes[i].box.w;
863
- int16_t targetH = __ui_nodes[i].box.h;
864
- ui_draw_image_with_fit(img, __ui_nodes[i].box.x, drawY, __ui_nodes[i].rotateDeg, __ui_nodes[i].objectFit, targetW, targetH);
865
- }
866
- if (__ui_nodes[i].borderStyle != 0) {
867
- int16_t borderW = ui_rotated_face_w(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
868
- int16_t borderH = ui_rotated_face_h(i, __ui_nodes[i].box.w, __ui_nodes[i].box.h);
869
- ui_draw_rect_outline(__ui_nodes[i].box.x, drawY, borderW, borderH,
870
- __ui_nodes[i].borderRadius, __ui_nodes[i].borderStyle, __ui_nodes[i].borderWidth, bColor);
871
- }
872
- break;
873
- case NODE_CANVAS: {
874
- // Find this node's draw callback.
875
- void (*__ui_canvas_fn)(CuttlefishCanvas16*) = nullptr;
876
- for (uint16_t b = 0; b < __ui_canvas_binding_count; b++) {
877
- if (__ui_canvas_bindings[b].node == i) { __ui_canvas_fn = __ui_canvas_bindings[b].fn; break; }
878
- }
879
- if (__ui_canvas_fn) {
880
- int16_t __ui_cw = __ui_nodes[i].canvasW;
881
- int16_t __ui_ch = __ui_nodes[i].canvasH;
882
- if (__ui_cw > 0 && __ui_ch > 0) {
883
- uint8_t __ui_canvas_drawn = 0;
884
- if (ui_display_is_default_target()) {
885
- // Cache a canvas sized to the buffer for direct hardware draws so
886
- // the callback's many primitives push as one bitmap. When the
887
- // active target is already a memory canvas (scroll/repair/full
888
- // framebuffer), skip the nested allocation and draw directly below.
889
- // A canvas can construct but fail its internal pixel-buffer malloc
890
- // (non-null canvas, null buffer). Treat that as "no canvas" so a
891
- // transient malloc failure self-heals instead of blanking the node.
892
- if (!__ui_node_canvas || !display_canvasBuffer(__ui_node_canvas) ||
893
- display_canvasWidth(__ui_node_canvas) != __ui_cw || display_canvasHeight(__ui_node_canvas) != __ui_ch) {
894
- display_deleteCanvas(__ui_node_canvas);
895
- __ui_node_canvas = display_createCanvas(__ui_cw, __ui_ch);
896
- }
897
- CuttlefishCanvas16* __ui_lc = __ui_node_canvas;
898
- if (__ui_lc && display_canvasBuffer(__ui_lc)) {
899
- display_canvasFillScreen(__ui_lc, __ui_nodes[i].clearColor);
900
- CuttlefishDisplayTarget* __ui_prev_target = ui_display_get_target();
901
- ui_display_set_target((CuttlefishDisplayTarget*)__ui_lc);
902
- __ui_canvas_fn(__ui_lc);
903
- ui_display_set_target(__ui_prev_target);
904
- ui_draw_canvas_rect(__ui_lc, __ui_nodes[i].box.x, drawY, __ui_cw, __ui_ch);
905
- __ui_canvas_drawn = 1;
906
- }
907
- }
908
- if (!__ui_canvas_drawn) {
909
- UI_COLOR_T __ui_canvas_bg = __ui_nodes[i].hasBg ? fillBg : __ui_nodes[i].clearColor;
910
- ui_display_fill_rect(__ui_nodes[i].box.x, drawY, __ui_nodes[i].box.w, __ui_nodes[i].box.h, __ui_canvas_bg);
911
- int16_t __ui_prev_off_x = __ui_draw_off_x;
912
- int16_t __ui_prev_off_y = __ui_draw_off_y;
913
- int16_t __ui_prev_canvas_w = __ui_canvas_fallback_w;
914
- int16_t __ui_prev_canvas_h = __ui_canvas_fallback_h;
915
- __ui_canvas_fallback_w = __ui_cw;
916
- __ui_canvas_fallback_h = __ui_ch;
917
- __ui_draw_off_x = (int16_t)(__ui_prev_off_x + __ui_nodes[i].box.x);
918
- __ui_draw_off_y = (int16_t)(__ui_prev_off_y + drawY);
919
- __ui_canvas_fn(nullptr);
920
- __ui_draw_off_x = __ui_prev_off_x;
921
- __ui_draw_off_y = __ui_prev_off_y;
922
- __ui_canvas_fallback_w = __ui_prev_canvas_w;
923
- __ui_canvas_fallback_h = __ui_prev_canvas_h;
924
- }
925
- }
926
- }
927
- break;
928
- }
929
- case NODE_LIST: {
930
- // Virtualized list: state lives on the node now (listCountFn/listItemFn/
931
- // listCount/scrollY/contentHeight/listItemHeight), not in a side table.
932
- if (!__ui_nodes[i].listItemFn) break;
933
- int16_t bx = __ui_nodes[i].box.x;
934
- int16_t by = drawY;
935
- int16_t bw = __ui_nodes[i].box.w;
936
- int16_t bh = __ui_nodes[i].box.h;
937
- uint16_t ih = __ui_nodes[i].listItemHeight > 0 ? __ui_nodes[i].listItemHeight : 24;
938
- uint16_t itemCount = __ui_nodes[i].listCount;
939
- int16_t listScrollY = __ui_nodes[i].scrollY;
940
- int16_t listContentH = __ui_nodes[i].contentHeight;
941
- UI_COLOR_T clearCol = __ui_nodes[i].clearColor;
942
- uint8_t listFullRepaint = 0;
943
- // Render to a viewport-sized canvas so edge glyphs are naturally clipped.
944
- // Treat a null buffer (failed internal malloc) as "no canvas" and retry —
945
- // see __ui_node_canvas for the zombie-caching rationale.
946
- // __ui_list_canvas is file-scoped so ui_release_canvas_state() can free it.
947
- if (!__ui_list_canvas || !display_canvasBuffer(__ui_list_canvas) ||
948
- display_canvasWidth(__ui_list_canvas) != bw || display_canvasHeight(__ui_list_canvas) != bh) {
949
- display_deleteCanvas(__ui_list_canvas);
950
- __ui_list_canvas_node = -1;
951
- __ui_list_canvas = display_createCanvas(bw, bh);
952
- listFullRepaint = 1;
953
- }
954
- CuttlefishCanvas16* lc = __ui_list_canvas;
955
- if (!lc || !display_canvasBuffer(lc)) {
956
- break;
957
- }
958
- if (__ui_list_canvas_node != (int16_t)i) listFullRepaint = 1;
959
- int16_t repaintY = 0;
960
- int16_t repaintH = bh;
961
- int16_t deltaY = listScrollY - __ui_nodes[i].lastPaintedScrollY;
962
- int16_t absDelta = deltaY < 0 ? -deltaY : deltaY;
963
- uint8_t canShiftList = (!listFullRepaint && deltaY != 0 && absDelta < bh);
964
- if (canShiftList) {
965
- ui_shift_container_canvas(lc, deltaY, clearCol, &repaintY, &repaintH);
966
- } else {
967
- display_canvasFillScreen(lc, clearCol);
968
- repaintY = 0;
969
- repaintH = bh;
970
- }
971
- // GFXcanvas text has no clipping. For shift-and-repair frames, draw row
972
- // text into a strip-sized repair canvas first, then blit only that strip
973
- // into the shifted list canvas. This prevents a 1-5px repair from
974
- // repainting full glyphs across pixels that were already shifted.
975
- CuttlefishCanvas16* listTextCanvas = lc;
976
- int16_t listTextOffsetY = 0;
977
- uint8_t drawingListRepair = 0;
978
- if (canShiftList && repaintH > 0 && repaintH < bh) {
979
- CuttlefishCanvas16* rc = ui_get_repair_canvas(bw, repaintH);
980
- if (rc) {
981
- display_canvasFillScreen(rc, clearCol);
982
- listTextCanvas = rc;
983
- listTextOffsetY = repaintY;
984
- drawingListRepair = 1;
985
- } else {
986
- display_canvasFillScreen(lc, clearCol);
987
- repaintY = 0;
988
- repaintH = bh;
989
- canShiftList = 0;
990
- }
991
- }
992
- // Compute visible range for the repainted strip. Previously-rendered
993
- // pixels are shifted in-place; only the exposed band needs new rows.
994
- uint16_t first = (listScrollY + repaintY) / ih;
995
- uint16_t last = (listScrollY + repaintY + repaintH - 1) / ih + 1;
996
- if (itemCount > 0 && last >= itemCount) last = itemCount - 1;
997
- // Draw each visible item (canvas-local coords: 0,0 = viewport top).
998
- char listBuf[UI_TEXT_BUF + 1];
999
- display_targetSetTextWrap((CuttlefishDisplayTarget*)listTextCanvas, false);
1000
- if (itemCount > 0 && repaintH > 0) {
1001
- for (uint16_t idx = first; idx <= last; idx++) {
1002
- int16_t itemY = (int16_t)(idx * ih) - listScrollY - listTextOffsetY;
1003
- __ui_nodes[i].listItemFn(idx, listBuf, UI_TEXT_BUF + 1);
1004
- listBuf[UI_TEXT_BUF] = 0;
1005
- display_targetSetCursor((CuttlefishDisplayTarget*)listTextCanvas, 4, itemY + (ih - 16) / 2);
1006
- display_targetSetTextColor((CuttlefishDisplayTarget*)listTextCanvas, __ui_nodes[i].fg);
1007
- display_targetSetTextSize((CuttlefishDisplayTarget*)listTextCanvas, 2);
1008
- display_targetPrint((CuttlefishDisplayTarget*)listTextCanvas, listBuf);
1009
- }
1010
- }
1011
- if (drawingListRepair) {
1012
- CuttlefishDisplayTarget* prevTarget = ui_display_get_target();
1013
- ui_display_set_target((CuttlefishDisplayTarget*)lc);
1014
- ui_draw_canvas_rect(listTextCanvas, 0, repaintY, bw, repaintH);
1015
- ui_display_set_target(prevTarget);
1016
- }
1017
- // Scrollbar (canvas-local coords).
1018
- if (listContentH > bh) {
1019
- int16_t tx = bw - 4;
1020
- uint16_t thumbH = (uint32_t)bh * bh / listContentH;
1021
- if (thumbH < 8) thumbH = 8;
1022
- int16_t maxScroll = listContentH - bh;
1023
- uint16_t thumbY = maxScroll > 0 ? (uint32_t)(bh - thumbH) * listScrollY / maxScroll : 0;
1024
- UI_COLOR_T dimFg = (UI_COLOR_T)((__ui_nodes[i].fg >> 1) & UI_DIM_MASK);
1025
- display_canvasFillRect(lc, tx, 0, 3, bh, dimFg);
1026
- display_canvasFillRect(lc, tx, thumbY, 3, thumbH, __ui_nodes[i].fg);
1027
- }
1028
- // Outset shadows are static decoration. Redrawing the hard shadow
1029
- // directly to the panel before every small scroll-frame creates a
1030
- // visible shadow-then-content intermediate state on SPI TFTs. Keep it
1031
- // for full list repaints, but skip it for shift-and-repair scrolls.
1032
- if (!drawingBufferedScroll && !__ui_fb && !canShiftList) {
1033
- ui_draw_shadow(i, by, 0);
1034
- }
1035
- // Standalone lists push directly. Lists inside a buffered scroll
1036
- // container must composite into that scroll canvas; their box has
1037
- // already been translated to canvas-local coordinates.
1038
- if (drawingBufferedScroll) {
1039
- ui_draw_canvas_rect(lc, bx, by, bw, bh);
1040
- } else {
1041
- ui_push_canvas_rect(lc, bx, by, bw, bh);
1042
- }
1043
- // Draw static decoration after the scrollable pixels are composited.
1044
- // Keeping border rows out of __ui_list_canvas prevents the cached
1045
- // shift step from dragging top/bottom border pixels through the list.
1046
- ui_draw_shadow(i, by, 1);
1047
- if (__ui_nodes[i].borderStyle != 0) {
1048
- ui_draw_node_border(i, bx, by, bColor);
1049
- }
1050
- ui_draw_node_outline(i, bx, by);
1051
- __ui_list_canvas_node = (int16_t)i;
1052
- __ui_nodes[i].lastPaintedScrollY = listScrollY;
1053
- __ui_nodes[i].dirty = 0;
1054
- ui_display_set_target(__ui_draw_target);
1055
- __ui_nodes[i].box.x = origBoxX;
1056
- __ui_nodes[i].box.y = origBoxY;
1057
- continue; // list handled canvas push, decoration, and coordinate restore
1058
- }
586
+ // ── Per-kind draw dispatch ─────────────────────────────────────────────
587
+ // The kind switch lives in ui_draw_node_body (node-draw-body slice) so the
588
+ // strip/band renderer can reuse it. The ctx carries the draw state both
589
+ // paths share; the list case may return 1 (it handled canvas push,
590
+ // decoration, coord restore, and dirty-clear itself) in which case this
591
+ // node is done skip the post-switch epilogue exactly as the old inline
592
+ // 'continue;' did.
593
+ UINodeDrawCtx __ui_ctx;
594
+ __ui_ctx.drawY = drawY;
595
+ __ui_ctx.bColor = bColor;
596
+ __ui_ctx.fillBg = fillBg;
597
+ __ui_ctx.ts = ts;
598
+ __ui_ctx.textMaxW = textMaxW;
599
+ __ui_ctx.tw = tw;
600
+ __ui_ctx.th = th;
601
+ __ui_ctx.paintTextW = paintTextW;
602
+ __ui_ctx.paintTextH = paintTextH;
603
+ __ui_ctx.displayText = displayText;
604
+ __ui_ctx.drawingBufferedScroll = drawingBufferedScroll;
605
+ __ui_ctx.drawTarget = __ui_draw_target;
606
+ __ui_ctx.origBoxX = origBoxX;
607
+ __ui_ctx.origBoxY = origBoxY;
608
+ if (ui_draw_node_body(i, &__ui_ctx)) {
609
+ continue;
1059
610
  }
1060
611
  if (__ui_nodes[i].kind == NODE_FILL && ui_rotation_quadrant(__ui_nodes[i].rotateDeg) != 0 &&
1061
612
  __ui_nodes[i].outlineStyle != 0 && __ui_nodes[i].outlineWidth > 0) {
@@ -1082,5 +633,6 @@ export function emitTickDirtyDrawPhase() {
1082
633
  // Report this node's bounding box to the deferred-refresh accumulator. Phase
1083
634
  // 4 uses the box as the dirty rect (a safe over-estimate); Phase 5 tightens
1084
635
  // to the actual paint rect. No-op on TFT (compiles to nothing).
636
+ if (__ui_fb) ui_fb_add_rect(paintRect.x, paintRect.y, paintRect.w, paintRect.h);
1085
637
  ui_refresh_add_rect(__ui_nodes[i].box.x, __ui_nodes[i].box.y, __ui_nodes[i].box.w, __ui_nodes[i].box.h);`;
1086
638
  }