1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
//! Free constructors for common [`El`] tree shapes.
//!
//! Kept separate from the core `El` type so the central node definition
//! stays focused on fields and chainable modifiers.
// Lock in full per-item documentation for this module (issue #73).
use Location;
use Arc;
use crateImage;
use crateVirtualItems;
use crate;
use ;
use El;
use Kind;
/// A vertical container — the layout fallback.
///
/// Reach for a named widget first: [`card`] / [`titled_card`] for boxed
/// surfaces; [`sidebar`] for nav rails; [`toolbar`] for page headers;
/// [`item`] for object rows; [`form_item`] / [`field_row`] for stacked
/// fields. `column` is the right answer when no widget shape fits.
///
/// Defaults match CSS flex's `display: flex; flex-direction: column`:
/// `axis = Column`, `align = Stretch`, `width = Hug`, `height = Hug`,
/// `gap = 0`. Children shrink to content on the main axis (height)
/// and stretch to the column's width on the cross axis.
///
/// To claim the parent's extent (the analog of `width: 100%` /
/// `flex: 1`), set `.width(Size::Fill(1.0))` /
/// `.height(Size::Fill(1.0))`. To space children apart, set
/// `.gap(tokens::SPACE_*)` — CSS-style opt-in spacing.
///
/// Switch `align` to `Center` / `Start` / `End` and children shrink
/// to their content width so the alignment can position them — the
/// same as CSS `align-items` non-stretch semantics.
///
/// **Smell:** `column([...]).fill(CARD).stroke(BORDER).radius(...)`
/// reinvents [`card`]; `column([...]).fill(CARD).stroke(BORDER).width(SIDEBAR_WIDTH)`
/// reinvents [`sidebar`]. Use the named widget — same recipe, the right
/// surface role, less to forget.
///
/// [`card`]: crate::widgets::card::card
/// [`titled_card`]: crate::widgets::card::titled_card
/// [`sidebar`]: crate::widgets::sidebar::sidebar
/// [`toolbar`]: crate::widgets::toolbar::toolbar
/// [`item`]: crate::widgets::item::item
/// [`form_item`]: crate::widgets::form::form_item
/// [`field_row`]: crate::widgets::form::field_row
/// A horizontal container — the layout fallback.
///
/// Reach for a named widget first: [`item`] for clickable object rows
/// (recent file, repo, project, person, asset entry — anywhere you'd
/// otherwise build a focusable row with stacked text and trailing
/// buttons); [`toolbar`] for page chrome; [`field_row`] for label +
/// control; [`tabs_list`] for segmented controls; [`breadcrumb_list`] /
/// [`pagination_content`] for navigation rows. `row` is the right
/// answer when no widget shape fits.
///
/// Defaults match CSS flex's `display: flex; flex-direction: row`:
/// `axis = Row`, `align = Stretch`, `width = Hug`, `height = Hug`,
/// `gap = 0`. Children shrink to content on the main axis (width)
/// and stretch to the row's height on the cross axis.
///
/// `Stretch` is the cross-axis default the same way `align-items:
/// stretch` is in CSS. For typical content rows (`[icon, text,
/// button]`) you almost always want `.align(Center)` to vertically
/// center the children — the CSS-Tailwind muscle memory of
/// `flex items-center`. Without it, smaller fixed-size children
/// (badges, icons) sit at the top of the row, just like CSS does.
///
/// To space children apart, set `.gap(tokens::SPACE_*)` — opt-in
/// like CSS.
///
/// **Smell:** a focusable, keyed `row([column([t1, t2]), button, button])`
/// used as a clickable resource entry — that's [`item`], not a hand-rolled
/// row. The named widget gives you hover, press, focus, the rail, and
/// the slots (`item_media`, `item_content`, `item_actions`) for free.
///
/// [`item`]: crate::widgets::item::item
/// [`toolbar`]: crate::widgets::toolbar::toolbar
/// [`field_row`]: crate::widgets::form::field_row
/// [`tabs_list`]: crate::widgets::tabs::tabs_list
/// [`breadcrumb_list`]: crate::widgets::breadcrumb::breadcrumb_list
/// [`pagination_content`]: crate::widgets::pagination::pagination_content
/// An overlay stack; children share the parent's rect.
///
/// For modals, sheets, popovers, and tooltips reach for the named
/// widget instead — [`dialog`], [`sheet`], [`popover`], `.tooltip(...)`.
/// `stack` is the layered-visuals primitive (focus rings, custom
/// badges painted over content) that those widgets compose against.
///
/// [`dialog`]: crate::widgets::dialog::dialog
/// [`sheet`]: crate::widgets::sheet::sheet
/// [`popover`]: crate::widgets::popover::popover
/// A vertical scroll viewport. Children stack as in [`column()`]; the
/// container clips overflow and translates content by the current scroll
/// offset. Wheel events over the viewport update the offset.
///
/// Give it a `.key("...")` so the offset persists by name across
/// rebuilds — without a key, the offset is keyed by sibling index and
/// resets if structure shifts.
/// A pan/zoom viewport — a clipped window onto a content layer the user
/// can drag to pan and wheel to zoom (anchored under the cursor). The
/// CSS `overflow: hidden` wrapper around a `transform: translate() scale()`
/// content box, with the gestures built in.
///
/// `children` are laid out once in un-transformed **content space** (as
/// if they filled the viewport); the layout pass then bakes the current
/// pan/zoom into their rects, and paint scales their `font_size` /
/// `padding` / `radius` / `stroke` / `shadow` to match — so nothing inside
/// needs to multiply by the zoom by hand. Because the transform lands in
/// the computed rects, hit-testing, links, and selection work through the
/// zoom automatically.
///
/// Give it a `.key("...")` so the pan/zoom persists by name across
/// rebuilds (like [`scroll()`]). Tune the zoom range with
/// [`min_zoom`](El::min_zoom) / [`max_zoom`](El::max_zoom) and the pan
/// gesture with [`pan_button`](El::pan_button) / [`pan_modifier`](El::pan_modifier).
/// Drive it programmatically (fit-to-content, reset, center) with
/// [`crate::viewport::ViewportRequest`], or let the widget keep the
/// content framed itself with [`fit_policy`](El::fit_policy)
/// ([`FitPolicy::Contain`](crate::viewport::FitPolicy::Contain) for
/// viewers that open fit and release on the first gesture,
/// [`Lock`](crate::viewport::FitPolicy::Lock) for always-fit thumbnails).
/// Chrome reads the view back with
/// [`BuildCx::viewport_view`](crate::event::BuildCx::viewport_view) /
/// [`viewport_at_home`](crate::event::BuildCx::viewport_at_home) /
/// [`viewport_content_bounds`](crate::event::BuildCx::viewport_content_bounds).
/// Scale `child` to the largest size that *fits inside* this container
/// while preserving `aspect` (width ÷ height), centered — the CSS
/// `object-fit: contain` shape for arbitrary subtrees (the [`image()`]
/// and [`surface()`] builders have it built in via
/// [`crate::image::ImageFit`]; this brings the same math to anything
/// else: an SVG preview pane, a fixed-ratio chart, a video frame
/// wrapper).
///
/// The container fills its parent by default (override with the usual
/// size modifiers — any non-`Hug` size works); the child is laid out
/// at the fitted rect, letterboxed on the slack axis. Aspect must be
/// positive.
///
/// When the ratio should come from the child's own intrinsic size
/// instead, use [`fit_contain_intrinsic`].
/// [`fit_contain`] with the aspect ratio taken from the child's
/// intrinsic `(width, height)` measure each layout. Falls back to
/// filling the container when the child has no measurable size (e.g.
/// an empty group).
/// Scale `child` to the smallest size that *covers* this container
/// while preserving `aspect` (width ÷ height), centered and clipped —
/// the CSS `object-fit: cover` shape for arbitrary subtrees. The
/// overflow on the slack axis is clipped to the container. Aspect must
/// be positive.
/// The centered rect of ratio `aspect` that fits inside (`cover =
/// false`) or covers (`cover = true`) `container`.
/// Block whose direct children flow inline (text leaves + embeds +
/// hard breaks). Models HTML's `<p>` shape: heterogeneous children,
/// attributed runs, optional inline embeds. Children are styled via
/// the existing modifier chain (`.bold()`, `.italic()`, `.color(c)`,
/// `.code()`, `.link(url)`, etc.) — there is no parallel
/// `RichText`/`TextRun` type.
///
/// ```ignore
/// text_runs([
/// text("Damascene — "),
/// text("rich text").bold(),
/// text(" composition."),
/// hard_break(),
/// text("Custom shaders, custom layouts, "),
/// text("virtual_list").code(),
/// text(" — and inline runs."),
/// ])
/// ```
/// Forced line break inside a [`text_runs`] block. Mirrors HTML's
/// `<br>`. Outside an `Inlines` parent, lays out as a zero-size leaf.
/// Native mathematical notation. Alias for [`math_inline`].
/// Math notation in compact in-line style (TeX text style); the El
/// hugs the rendered expression.
/// Math notation in display style for standalone equations; fills the
/// available width.
/// Virtualized vertical list of `count` rows of fixed height
/// `row_height`. Two contracts worth knowing:
///
/// - The row builder is `Fn + Send + Sync + 'static` (it's retained in
/// the tree), so display data it captures must be shared, not
/// borrowed — `Arc<Mutex<Vec<Row>>>` (or `Arc<RwLock<…>>`) on the
/// app struct, with a clone moved into the closure, is the intended
/// shape for data that mutates between frames.
/// - The builder runs **every frame** for each visible row. Side
/// effects inside it (queueing a thumbnail decode, a stat call) must
/// be deduplicated by the app, and `BuildCx::visible_range` gives
/// the eviction signal for results whose rows scrolled away.
///
/// The library calls `build_row(i)` only for indices
/// whose rect intersects the visible viewport, then lays them out at
/// the scroll-shifted Y. `.gap(...)` contributes spacing between rows,
/// matching column-style layout. Authors typically key rows with a
/// stable identifier (`button("foo").key("msg-abc")`) so hover/press/
/// focus state survives scrolling.
///
/// The returned El defaults to `Size::Fill(1.0)` on both axes (it's a
/// viewport — its size is decided by the parent). `Size::Hug` would
/// defeat virtualization and panics at layout time.
/// Variable-height variant of [`virtual_list`]. `row_key(i)` must
/// return a stable identity for the logical row at `i`; the dynamic
/// list uses those identities to preserve an in-viewport anchor while
/// rows are inserted, removed, measured, or remeasured at a new width.
/// Each row sizes itself from its own content (`Size::Hug` or
/// `Size::Fixed` on the main axis); `estimated_row_height` is used for
/// unmeasured rows when the library positions the visible window and
/// computes the scrollbar thumb. `.gap(...)` contributes spacing
/// between rows, matching column-style layout.
///
/// Use this when row heights are content-driven (diff hunks, expanded
/// rows, comment threads) and a single `row_height` would either waste
/// space or truncate. For genuinely uniform lists prefer
/// [`virtual_list`] — its O(1) range math is cheaper and free of any
/// estimate/measure jitter.
/// A `Fill(1)` filler. Inside a `row` it pushes siblings to the right;
/// inside a `column` it pushes siblings to the bottom.
/// A fixed-column grid — the CSS `display: grid;
/// grid-template-columns: repeat(cols, 1fr); gap: G` shape. Cells pack
/// left-to-right, top-to-bottom into rows of `cols` equal-width
/// columns separated by `gap` on both axes; the final partial row is
/// padded with invisible fillers so its cells align with the columns
/// above. Focusable cells get 2D arrow-key navigation
/// ([`crate::tree::ArrowNav::Grid`]) for free.
///
/// Each cell is stretched to its column width (`Size::Fill`); give
/// cells an explicit `.height(...)` (or let content hug). For large /
/// unbounded item sets, use [`virtual_grid`].
///
/// Derive `cols` from the viewport for responsive galleries:
/// `(cx.viewport_width().unwrap_or(1280.0) / MIN_CELL_W).max(1.0) as
/// usize` — the same value is available in `on_event` via
/// `EventCx::viewport_width`, so navigation math agrees with layout.
/// Virtualized fixed-column grid over `count` items of uniform
/// `cell_height` — the photo-wall / thumbnail-browser shape. Wraps
/// [`virtual_list`] with the row/column packing every consumer was
/// hand-rolling: rows of `cols` equal-width cells, `gap` on both axes,
/// partial tail row padded so columns align.
///
/// `build_cell(i)` is called only for items whose row intersects the
/// viewport, every frame they're visible — the same contract (and the
/// same `Send + Sync + 'static` capture rules and side-effect dedup
/// responsibility) as [`virtual_list`] row builders. Query the visible
/// item range as `visible_range(key)` row range × `cols`.
///
/// Programmatic scrolling: `ScrollRequest` rows are item `index /
/// cols`. Arrow-key navigation covers the *realized* rows (the
/// virtualization window); stepping focus past the realized edge is a
/// known gap shared with all virtualized content.
/// One packed grid row: `filled` real cells plus invisible fillers out
/// to `cols`, each at equal `Fill` width, separated by `gap`.
/// A raster image element. The El hugs the image's natural pixel
/// size by default; set [`El::width`] / [`El::height`] for an
/// explicit box, and [`El::image_fit`] to control projection.
///
/// ```
/// use damascene_core::prelude::*;
/// let pixels = vec![0u8; 4 * 4 * 4];
/// let img = Image::from_rgba8(4, 4, pixels);
/// let _ = image(img).image_fit(ImageFit::Cover).radius(8.0);
/// ```
/// An app-supplied vector asset. By default Damascene preserves authored
/// fills, strokes, and gradients through the painted vector path; call
/// [`El::vector_mask`] when the asset should be treated as a one-colour
/// coverage mask. Companion to [`crate::icon`] for content that
/// doesn't fit icon conventions: arbitrary-aspect bounding boxes,
/// programmatic construction each frame. Pairs with
/// [`crate::vector::PathBuilder`] for ergonomic path construction.
///
/// # Sizing
///
/// The default size matches the asset's view-box dimensions in logical
/// pixels. Set [`El::width`] / [`El::height`] / [`El::fill_size`] to
/// override. Painted vectors are tessellated into the resolved rect;
/// mask vectors sample the backend MSDF atlas across that rect.
///
/// # Caching
///
/// The asset's [`VectorAsset::content_hash`](crate::vector::VectorAsset::content_hash)
/// is the backend cache key. Apps that build the same shape twice (two
/// commits sharing a merge connector geometry, two flowchart edges with
/// the same arc) can share backend work; per-frame-unique geometry gets
/// one cache entry per unique shape.
///
/// ```ignore
/// use damascene_core::prelude::*;
/// use damascene_core::tree::Color;
///
/// let curve = PathBuilder::new()
/// .move_to(0.0, 0.0)
/// .cubic_to(20.0, 0.0, 0.0, 60.0, 20.0, 60.0)
/// .stroke_solid(Color::srgb_u8(80, 200, 240), 2.0)
/// .stroke_line_cap(VectorLineCap::Round)
/// .build();
/// let asset = VectorAsset::from_paths([0.0, 0.0, 20.0, 60.0], vec![curve]);
/// let _ = vector(asset);
/// ```
/// An app-owned-texture surface. Damascene composites the texture into
/// the paint stream at the El's resolved rect — no upload, no per-frame
/// copy. The default size matches the texture's pixel dimensions; set
/// [`El::width`] / [`El::height`] (or `.fill_size()`) for an explicit
/// box.
///
/// # Sizing, projection, and transforms
///
/// The texture's pixel dimensions are **independent of the rendered
/// size**. By default the widget stretches the texture across the
/// resolved rect ([`crate::image::ImageFit::Fill`]); reach for
/// [`El::surface_fit`] to letterbox-preserve aspect ratio
/// ([`crate::image::ImageFit::Contain`]), crop-cover
/// ([`crate::image::ImageFit::Cover`]), or paint at natural size
/// ([`crate::image::ImageFit::None`]). [`El::surface_transform`]
/// composes an affine on top — rotate, mirror, zoom/pan — applied
/// around the centre of the post-fit rect.
///
/// Picking a sizing strategy:
/// - For pixel-accurate display, size the widget to the texture's
/// pixel dimensions (the default constructor does this for you).
/// - For a 3D viewport or video frame whose source resolution should
/// track the rendered size, the app should re-allocate its texture
/// to match the resolved rect (read it via `UiState::rect_of_key`
/// after `prepare()`).
/// - For an animated image whose natural dimensions are fixed
/// (decoded GIF / WebP / APNG, decoded video frame),
/// `surface_fit(Contain)` letterboxes into any layout rect with
/// no per-resize allocation.
///
/// # z-order, scissor, hit-test
///
/// The widget participates in layout, scissor, scrolling, hit-test,
/// and z-order like any other El: siblings declared before this one
/// paint underneath, siblings after paint on top. The auto-clip
/// scissor clamps painted content to the El's content rect — affines
/// or `Cover` projections that overflow are cropped.
///
/// ```ignore
/// // Pseudocode — the AppTexture comes from a backend constructor.
/// use damascene_core::prelude::*;
/// let tex: AppTexture = /* damascene_wgpu::app_texture(...) */ todo!();
/// let _ = surface(tex)
/// .fill_size()
/// .surface_fit(ImageFit::Contain)
/// .surface_alpha(SurfaceAlpha::Opaque)
/// .surface_transform(Affine2::rotate(0.1));
/// ```
/// A small, polished, hardware-accelerated 3D scene — point scatter, lit
/// meshes, and lines — as a first-class element. Unlike [`surface`], the
/// app does not own a renderer or a device: it describes the scene with a
/// [`crate::scene::SceneSpec`] and the backend renders it. Fills its area
/// by default (it has no intrinsic pixel size); size it like any El.
///
/// Two GPU-asynchrony notes worth knowing up front:
/// - **Hover picks are a frame late** — see
/// [`BuildCx::hovered_scene_point`](crate::BuildCx::hovered_scene_point).
/// - **Label depth-occlusion lags similarly**: labels are culled against
/// a depth map read back asynchronously from the previous frame, and
/// are hidden until the first map arrives. During fast camera motion a
/// label can appear/disappear a frame later than the geometry that
/// occludes it; at interactive orbit speeds this is imperceptible.
///
/// Geometry handles ([`crate::scene::PointsHandle`] et al.) must be
/// created once and cached in app state — see the handle-pattern notes
/// on [`crate::scene::GeometryHandle`].
///
/// ```ignore
/// use damascene_core::prelude::*;
/// use damascene_core::scene::{GridPlanes, SceneSpec};
///
/// let scene = SceneSpec::new().points(scatter).mesh(model).grid(GridPlanes::XZ);
/// let _ = chart3d(scene).key("scene");
/// ```
/// A 2D plot: line/scatter data over auto-scaled, pannable/zoomable axes.
///
/// Backed by [`crate::plot::PlotSpec`] — a fluent builder of marks + axis
/// scales — and resolved (in `draw_ops`) to an orthographic
/// `DrawOp::Scene3D` data layer plus themed axis/grid/crosshair chrome (see
/// `docs/PLOT2D_PLAN.md`). Fills its area like `chart3d`; give it a
/// `.key(...)` so its pan/zoom [`crate::plot::PlotView`] persists across
/// rebuilds and can be read back for the virtual-data pull.
///
/// ```ignore
/// use damascene_core::prelude::*;
/// use damascene_core::plot::{PlotSpec, Scale};
///
/// let spec = PlotSpec::new().x(Scale::time()).line(&cpu).line(&mem);
/// let _ = plot(spec).key("metrics");
/// ```
/// A 1-pixel separator line.
// ---------- &str → El convenience ----------
//
// Lets `titled_card("Title", ["a body line"])` work without `text(...)`.