@nowline/layout 0.2.0

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 (183) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +103 -0
  3. package/dist/band-scale.d.ts +56 -0
  4. package/dist/band-scale.d.ts.map +1 -0
  5. package/dist/band-scale.js +86 -0
  6. package/dist/band-scale.js.map +1 -0
  7. package/dist/calendar.d.ts +79 -0
  8. package/dist/calendar.d.ts.map +1 -0
  9. package/dist/calendar.js +210 -0
  10. package/dist/calendar.js.map +1 -0
  11. package/dist/capacity.d.ts +72 -0
  12. package/dist/capacity.d.ts.map +1 -0
  13. package/dist/capacity.js +163 -0
  14. package/dist/capacity.js.map +1 -0
  15. package/dist/dsl-utils.d.ts +5 -0
  16. package/dist/dsl-utils.d.ts.map +1 -0
  17. package/dist/dsl-utils.js +28 -0
  18. package/dist/dsl-utils.js.map +1 -0
  19. package/dist/edge-routing.d.ts +89 -0
  20. package/dist/edge-routing.d.ts.map +1 -0
  21. package/dist/edge-routing.js +435 -0
  22. package/dist/edge-routing.js.map +1 -0
  23. package/dist/frame-tab-geometry.d.ts +78 -0
  24. package/dist/frame-tab-geometry.d.ts.map +1 -0
  25. package/dist/frame-tab-geometry.js +115 -0
  26. package/dist/frame-tab-geometry.js.map +1 -0
  27. package/dist/header-card-geometry.d.ts +29 -0
  28. package/dist/header-card-geometry.d.ts.map +1 -0
  29. package/dist/header-card-geometry.js +41 -0
  30. package/dist/header-card-geometry.js.map +1 -0
  31. package/dist/i18n.d.ts +48 -0
  32. package/dist/i18n.d.ts.map +1 -0
  33. package/dist/i18n.js +114 -0
  34. package/dist/i18n.js.map +1 -0
  35. package/dist/include-chrome-geometry.d.ts +86 -0
  36. package/dist/include-chrome-geometry.d.ts.map +1 -0
  37. package/dist/include-chrome-geometry.js +104 -0
  38. package/dist/include-chrome-geometry.js.map +1 -0
  39. package/dist/index.d.ts +11 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +10 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/item-bar-geometry.d.ts +127 -0
  44. package/dist/item-bar-geometry.d.ts.map +1 -0
  45. package/dist/item-bar-geometry.js +173 -0
  46. package/dist/item-bar-geometry.js.map +1 -0
  47. package/dist/lane-utilization.d.ts +90 -0
  48. package/dist/lane-utilization.d.ts.map +1 -0
  49. package/dist/lane-utilization.js +206 -0
  50. package/dist/lane-utilization.js.map +1 -0
  51. package/dist/layout-context.d.ts +143 -0
  52. package/dist/layout-context.d.ts.map +1 -0
  53. package/dist/layout-context.js +8 -0
  54. package/dist/layout-context.js.map +1 -0
  55. package/dist/layout.d.ts +18 -0
  56. package/dist/layout.d.ts.map +1 -0
  57. package/dist/layout.js +1213 -0
  58. package/dist/layout.js.map +1 -0
  59. package/dist/nodes/anchor-node.d.ts +16 -0
  60. package/dist/nodes/anchor-node.d.ts.map +1 -0
  61. package/dist/nodes/anchor-node.js +68 -0
  62. package/dist/nodes/anchor-node.js.map +1 -0
  63. package/dist/nodes/footnote-node.d.ts +10 -0
  64. package/dist/nodes/footnote-node.d.ts.map +1 -0
  65. package/dist/nodes/footnote-node.js +41 -0
  66. package/dist/nodes/footnote-node.js.map +1 -0
  67. package/dist/nodes/group-node.d.ts +29 -0
  68. package/dist/nodes/group-node.d.ts.map +1 -0
  69. package/dist/nodes/group-node.js +187 -0
  70. package/dist/nodes/group-node.js.map +1 -0
  71. package/dist/nodes/include-node.d.ts +16 -0
  72. package/dist/nodes/include-node.d.ts.map +1 -0
  73. package/dist/nodes/include-node.js +117 -0
  74. package/dist/nodes/include-node.js.map +1 -0
  75. package/dist/nodes/item-node.d.ts +51 -0
  76. package/dist/nodes/item-node.d.ts.map +1 -0
  77. package/dist/nodes/item-node.js +108 -0
  78. package/dist/nodes/item-node.js.map +1 -0
  79. package/dist/nodes/marker-geometry.d.ts +22 -0
  80. package/dist/nodes/marker-geometry.d.ts.map +1 -0
  81. package/dist/nodes/marker-geometry.js +38 -0
  82. package/dist/nodes/marker-geometry.js.map +1 -0
  83. package/dist/nodes/milestone-node.d.ts +48 -0
  84. package/dist/nodes/milestone-node.d.ts.map +1 -0
  85. package/dist/nodes/milestone-node.js +210 -0
  86. package/dist/nodes/milestone-node.js.map +1 -0
  87. package/dist/nodes/parallel-node.d.ts +21 -0
  88. package/dist/nodes/parallel-node.d.ts.map +1 -0
  89. package/dist/nodes/parallel-node.js +80 -0
  90. package/dist/nodes/parallel-node.js.map +1 -0
  91. package/dist/nodes/roadmap-node.d.ts +76 -0
  92. package/dist/nodes/roadmap-node.d.ts.map +1 -0
  93. package/dist/nodes/roadmap-node.js +788 -0
  94. package/dist/nodes/roadmap-node.js.map +1 -0
  95. package/dist/nodes/swimlane-node.d.ts +38 -0
  96. package/dist/nodes/swimlane-node.d.ts.map +1 -0
  97. package/dist/nodes/swimlane-node.js +308 -0
  98. package/dist/nodes/swimlane-node.js.map +1 -0
  99. package/dist/renderable.d.ts +44 -0
  100. package/dist/renderable.d.ts.map +1 -0
  101. package/dist/renderable.js +21 -0
  102. package/dist/renderable.js.map +1 -0
  103. package/dist/row-packer.d.ts +125 -0
  104. package/dist/row-packer.d.ts.map +1 -0
  105. package/dist/row-packer.js +169 -0
  106. package/dist/row-packer.js.map +1 -0
  107. package/dist/style-resolution.d.ts +14 -0
  108. package/dist/style-resolution.d.ts.map +1 -0
  109. package/dist/style-resolution.js +191 -0
  110. package/dist/style-resolution.js.map +1 -0
  111. package/dist/themes/dark.d.ts +4 -0
  112. package/dist/themes/dark.d.ts.map +1 -0
  113. package/dist/themes/dark.js +241 -0
  114. package/dist/themes/dark.js.map +1 -0
  115. package/dist/themes/index.d.ts +15 -0
  116. package/dist/themes/index.d.ts.map +1 -0
  117. package/dist/themes/index.js +30 -0
  118. package/dist/themes/index.js.map +1 -0
  119. package/dist/themes/light.d.ts +4 -0
  120. package/dist/themes/light.d.ts.map +1 -0
  121. package/dist/themes/light.js +248 -0
  122. package/dist/themes/light.js.map +1 -0
  123. package/dist/themes/shape.d.ts +194 -0
  124. package/dist/themes/shape.d.ts.map +1 -0
  125. package/dist/themes/shape.js +6 -0
  126. package/dist/themes/shape.js.map +1 -0
  127. package/dist/themes/shared.d.ts +145 -0
  128. package/dist/themes/shared.d.ts.map +1 -0
  129. package/dist/themes/shared.js +310 -0
  130. package/dist/themes/shared.js.map +1 -0
  131. package/dist/time-scale.d.ts +39 -0
  132. package/dist/time-scale.d.ts.map +1 -0
  133. package/dist/time-scale.js +62 -0
  134. package/dist/time-scale.js.map +1 -0
  135. package/dist/types.d.ts +483 -0
  136. package/dist/types.d.ts.map +1 -0
  137. package/dist/types.js +6 -0
  138. package/dist/types.js.map +1 -0
  139. package/dist/view-preset.d.ts +23 -0
  140. package/dist/view-preset.d.ts.map +1 -0
  141. package/dist/view-preset.js +146 -0
  142. package/dist/view-preset.js.map +1 -0
  143. package/dist/working-calendar.d.ts +14 -0
  144. package/dist/working-calendar.d.ts.map +1 -0
  145. package/dist/working-calendar.js +74 -0
  146. package/dist/working-calendar.js.map +1 -0
  147. package/package.json +37 -0
  148. package/src/band-scale.ts +115 -0
  149. package/src/calendar.ts +244 -0
  150. package/src/capacity.ts +191 -0
  151. package/src/dsl-utils.ts +30 -0
  152. package/src/edge-routing.ts +550 -0
  153. package/src/frame-tab-geometry.ts +165 -0
  154. package/src/header-card-geometry.ts +48 -0
  155. package/src/i18n.ts +124 -0
  156. package/src/include-chrome-geometry.ts +156 -0
  157. package/src/index.ts +116 -0
  158. package/src/item-bar-geometry.ts +222 -0
  159. package/src/lane-utilization.ts +259 -0
  160. package/src/layout-context.ts +182 -0
  161. package/src/layout.ts +1446 -0
  162. package/src/nodes/anchor-node.ts +77 -0
  163. package/src/nodes/footnote-node.ts +60 -0
  164. package/src/nodes/group-node.ts +252 -0
  165. package/src/nodes/include-node.ts +168 -0
  166. package/src/nodes/item-node.ts +171 -0
  167. package/src/nodes/marker-geometry.ts +43 -0
  168. package/src/nodes/milestone-node.ts +263 -0
  169. package/src/nodes/parallel-node.ts +101 -0
  170. package/src/nodes/roadmap-node.ts +957 -0
  171. package/src/nodes/swimlane-node.ts +423 -0
  172. package/src/renderable.ts +68 -0
  173. package/src/row-packer.ts +271 -0
  174. package/src/style-resolution.ts +243 -0
  175. package/src/themes/dark.ts +244 -0
  176. package/src/themes/index.ts +36 -0
  177. package/src/themes/light.ts +251 -0
  178. package/src/themes/shape.ts +230 -0
  179. package/src/themes/shared.ts +369 -0
  180. package/src/time-scale.ts +78 -0
  181. package/src/types.ts +607 -0
  182. package/src/view-preset.ts +180 -0
  183. package/src/working-calendar.ts +91 -0
@@ -0,0 +1,222 @@
1
+ // Item-bar internal geometry — the "rails" inside an item rectangle:
2
+ // caption text (title + meta), label chips along the bottom, status
3
+ // dot at the upper-right, footnote indicators just left of the dot,
4
+ // and the link icon tile at the bottom-right.
5
+ //
6
+ // These metrics are shared between layout (label-chip placement,
7
+ // future content-aware width estimation) and renderer (text baselines
8
+ // + glyph positions). Keeping them in one module means a single edit
9
+ // re-skins every item bar consistently — the alternative is hunting
10
+ // matching `+ 12`s across two packages.
11
+ //
12
+ // Coordinate convention: every offset is **relative to the item's
13
+ // `box`** (`box.x` for X, `box.y` for Y), with `box.height` /
14
+ // `box.width` used as the "right edge" / "bottom edge" anchor where
15
+ // noted.
16
+
17
+ // ---- Caption (title + meta text) ---------------------------------
18
+
19
+ /** Horizontal inset (px) for the caption's left edge inside the bar. */
20
+ export const ITEM_CAPTION_INSET_X_PX = 12;
21
+
22
+ /**
23
+ * When the bar's title would overflow to the right, layout sets
24
+ * `textSpills` and the caption renders to the RIGHT of the bar; this
25
+ * is the gap (px) between the bar's right edge and the spilled
26
+ * caption.
27
+ */
28
+ export const ITEM_CAPTION_SPILL_GAP_PX = 6;
29
+
30
+ /** Baseline Y of the title text relative to the bar's top. */
31
+ export const ITEM_CAPTION_TITLE_BASELINE_OFFSET_PX = 20;
32
+
33
+ /** Baseline Y of the meta text (second line) relative to the bar's top. */
34
+ export const ITEM_CAPTION_META_BASELINE_OFFSET_PX = 38;
35
+
36
+ /** Font size (px) of the title text. */
37
+ export const ITEM_CAPTION_TITLE_FONT_SIZE_PX = 13;
38
+
39
+ /** Font size (px) of the meta text. */
40
+ export const ITEM_CAPTION_META_FONT_SIZE_PX = 11;
41
+
42
+ // ---- Status dot (upper-right glyph) ------------------------------
43
+
44
+ /** Distance (px) from the bar's right edge to the dot's center. */
45
+ export const ITEM_STATUS_DOT_INSET_RIGHT_PX = 12;
46
+
47
+ /** Distance (px) from the bar's top edge to the dot's center. */
48
+ export const ITEM_STATUS_DOT_INSET_TOP_PX = 12;
49
+
50
+ /** Radius (px) of the status dot. */
51
+ export const ITEM_STATUS_DOT_RADIUS_PX = 5;
52
+
53
+ // ---- Footnote indicators (numbers next to status dot) ------------
54
+
55
+ /**
56
+ * X offset (px from bar's right edge) where the rightmost footnote
57
+ * digit's anchor sits. Tuned to leave room for the status dot.
58
+ */
59
+ export const ITEM_FOOTNOTE_INDICATOR_INSET_RIGHT_PX = 22;
60
+
61
+ /** Baseline Y (px from bar's top) for footnote indicator digits. */
62
+ export const ITEM_FOOTNOTE_INDICATOR_BASELINE_OFFSET_PX = 14;
63
+
64
+ /**
65
+ * Horizontal step (px) between consecutive footnote indicators when
66
+ * an item carries more than one footnote — they walk LEFT from the
67
+ * status dot.
68
+ */
69
+ export const ITEM_FOOTNOTE_INDICATOR_STEP_PX = 8;
70
+
71
+ // ---- Link icon tile (bottom-right) -------------------------------
72
+
73
+ /** Side length (px) of the square link-icon tile. */
74
+ export const ITEM_LINK_ICON_TILE_SIZE_PX = 14;
75
+
76
+ /** Distance (px) from the bar's right/bottom edges to the tile's edge. */
77
+ export const ITEM_LINK_ICON_INSET_PX = 6;
78
+
79
+ // ---- Narrow-bar decoration spill --------------------------------
80
+
81
+ /**
82
+ * Horizontal gap (px) between consecutive decorations (status dot,
83
+ * link icon, footnote, title) when they spill into the column to
84
+ * the right of a bar that's too narrow to host them inside.
85
+ */
86
+ export const ITEM_DECORATION_SPILL_GAP_PX = 4;
87
+
88
+ /**
89
+ * Minimum bar width (px) needed to host the status dot inside the
90
+ * bar with its full inset. Below this, the dot would have to
91
+ * extend past the bar's left edge, so the dot spills into the
92
+ * caption column to the right of the bar instead.
93
+ */
94
+ export const MIN_BAR_WIDTH_FOR_DOT_PX = ITEM_STATUS_DOT_INSET_RIGHT_PX + ITEM_STATUS_DOT_RADIUS_PX;
95
+
96
+ /**
97
+ * Minimum bar width (px) needed to host the link-icon tile AND the
98
+ * status dot inside the bar with at least
99
+ * `ITEM_DECORATION_SPILL_GAP_PX` of breathing room between them.
100
+ * Below this, the link icon would visually collide with (or push
101
+ * into) the dot's column, so the icon spills out and renders ahead
102
+ * of the (also-spilled) title.
103
+ */
104
+ export const MIN_BAR_WIDTH_FOR_LINK_AND_DOT_PX =
105
+ ITEM_LINK_ICON_INSET_PX +
106
+ ITEM_LINK_ICON_TILE_SIZE_PX +
107
+ ITEM_DECORATION_SPILL_GAP_PX +
108
+ ITEM_STATUS_DOT_INSET_RIGHT_PX +
109
+ ITEM_STATUS_DOT_RADIUS_PX;
110
+
111
+ /**
112
+ * Minimum bar width (px) needed to host the footnote indicator at
113
+ * its inset-right position without overshooting the bar's left
114
+ * edge or colliding with a leading link icon. Approximate width
115
+ * for one digit is 8px (font-size 10, bold).
116
+ */
117
+ export const MIN_BAR_WIDTH_FOR_FOOTNOTE_PX = ITEM_FOOTNOTE_INDICATOR_INSET_RIGHT_PX + 1;
118
+
119
+ // ---- Label chips (along the bar's bottom) ------------------------
120
+
121
+ /** Height (px) of a label chip rectangle. */
122
+ export const LABEL_CHIP_HEIGHT_PX = 13;
123
+
124
+ /**
125
+ * Vertical gap (px) between the top of the bottom progress strip and
126
+ * the BOTTOM of a label chip. Keeps chips from touching the strip and
127
+ * gives a clear visual rail.
128
+ */
129
+ export const LABEL_CHIP_GAP_ABOVE_PROGRESS_STRIP_PX = 3;
130
+
131
+ /** Horizontal gap (px) between consecutive chips in a row. */
132
+ export const LABEL_CHIP_GAP_BETWEEN_PX = 4;
133
+
134
+ /**
135
+ * Vertical gap (px) between two stacked chip rows when chips spill
136
+ * outside the bar and form a multi-row column to the right.
137
+ */
138
+ export const LABEL_CHIP_ROW_GAP_PX = 4;
139
+
140
+ /**
141
+ * Vertical row pitch (px) for a stacked chip column — chip height +
142
+ * inter-row gap.
143
+ */
144
+ export const LABEL_CHIP_ROW_STEP_PX = LABEL_CHIP_HEIGHT_PX + LABEL_CHIP_ROW_GAP_PX;
145
+
146
+ /**
147
+ * Slack budget (fraction) applied ONCE per item when chips spill
148
+ * outside the bar. If a chip would overflow its row by less than
149
+ * `SPILL_ROW_SLACK_FRACTION × chip.width`, the row is allowed to
150
+ * stretch by the overflow amount and keep the chip on it instead of
151
+ * wrapping. This rescues "lonely chip" cases where one chip just
152
+ * barely overshoots; once the slack is consumed, no further row
153
+ * expansions happen for that item.
154
+ */
155
+ export const SPILL_ROW_SLACK_FRACTION = 0.25;
156
+
157
+ export interface ChipRowSample<T> {
158
+ id: T;
159
+ width: number;
160
+ }
161
+
162
+ export interface SpillChipPack<T> {
163
+ /** Rows ordered top-to-bottom — `rows[0]` sits at the chip
164
+ * column's top y, `rows[1]` one `LABEL_CHIP_ROW_STEP_PX` below,
165
+ * and so on. */
166
+ rows: ChipRowSample<T>[][];
167
+ /** True when the slack rule was used to keep an extra chip on
168
+ * row 0 (or whichever row was being filled when the overflow
169
+ * happened). At most one expansion per item. */
170
+ expanded: boolean;
171
+ }
172
+
173
+ /**
174
+ * Pack `chips` into rows for the SPILL column to the right of an
175
+ * item bar. Each row is capped at `barVisualWidth` (with gaps
176
+ * between chips). When a chip would overflow:
177
+ *
178
+ * - If the row is empty, place the chip anyway (a chip wider than
179
+ * the cap occupies its own row).
180
+ * - Else if the slack rule applies (`overflow ≤ 0.25 × chip.width`)
181
+ * AND the slack hasn't already been used for this item, expand
182
+ * the current row by `overflow` and keep the chip on it.
183
+ * - Else wrap the chip to a fresh row.
184
+ */
185
+ export function packSpillChips<T>(
186
+ chips: ChipRowSample<T>[],
187
+ barVisualWidth: number,
188
+ ): SpillChipPack<T> {
189
+ if (chips.length === 0) return { rows: [[]], expanded: false };
190
+ const rows: ChipRowSample<T>[][] = [[]];
191
+ let used = 0;
192
+ let rowCap = barVisualWidth;
193
+ let expanded = false;
194
+ for (const chip of chips) {
195
+ const row = rows[rows.length - 1];
196
+ const needed = (row.length === 0 ? 0 : LABEL_CHIP_GAP_BETWEEN_PX) + chip.width;
197
+ const wouldBe = used + needed;
198
+ if (wouldBe <= rowCap) {
199
+ row.push(chip);
200
+ used = wouldBe;
201
+ continue;
202
+ }
203
+ if (row.length === 0) {
204
+ row.push(chip);
205
+ used = chip.width;
206
+ continue;
207
+ }
208
+ const overflow = wouldBe - rowCap;
209
+ const slack = chip.width * SPILL_ROW_SLACK_FRACTION;
210
+ if (!expanded && overflow <= slack) {
211
+ row.push(chip);
212
+ used = wouldBe;
213
+ rowCap = wouldBe;
214
+ expanded = true;
215
+ continue;
216
+ }
217
+ rows.push([chip]);
218
+ used = chip.width;
219
+ rowCap = barVisualWidth;
220
+ }
221
+ return { rows, expanded };
222
+ }
@@ -0,0 +1,259 @@
1
+ // Lane utilization computation per specs/rendering.md § Lane utilization
2
+ // underline.
3
+ //
4
+ // Walks a positioned lane's children (items, plus the items inside parallel
5
+ // and group blocks), collects each contributor's pixel span and capacity load,
6
+ // then sweeps the timeline to produce a list of half-open segments
7
+ // classified `green | yellow | red` against the lane's resolved capacity and
8
+ // thresholds. Adjacent same-classification segments coalesce so the renderer
9
+ // paints one rectangle per color band rather than per event boundary.
10
+ //
11
+ // Pure data in / pure data out — no AST, no theme, no side effects. Lives
12
+ // next to `capacity.ts` (the other capacity-helper module) and is tested in
13
+ // isolation.
14
+
15
+ import type { DefaultDeclaration, SwimlaneDeclaration } from '@nowline/core';
16
+ import { propValue } from './dsl-utils.js';
17
+ import type {
18
+ PositionedItem,
19
+ PositionedLaneUtilization,
20
+ PositionedTrackChild,
21
+ PositionedUtilizationSegment,
22
+ UtilizationClassification,
23
+ } from './types.js';
24
+
25
+ /**
26
+ * Built-in defaults used when neither the lane nor its `default swimlane`
27
+ * declares a threshold. Mirrors specs/dsl.md rule 17d.
28
+ */
29
+ export const DEFAULT_UTILIZATION_WARN_FRACTION = 0.8; // 80%
30
+ export const DEFAULT_UTILIZATION_OVER_FRACTION = 1.0; // 100%
31
+
32
+ /**
33
+ * One contributor to a lane's load function: a single positioned item with
34
+ * its visual horizontal span and the load it contributes during that span.
35
+ * Children inside `parallel` / `group` blocks contribute individually so
36
+ * concurrent load reads correctly (the parallel block itself spans the union
37
+ * of its children's bars but does not itself add load — only the items do).
38
+ */
39
+ interface LoadContributor {
40
+ startX: number;
41
+ endX: number;
42
+ load: number;
43
+ }
44
+
45
+ /**
46
+ * Walk the lane's positioned children and return one `LoadContributor` per
47
+ * item that contributes load. Items inside `parallel` and `group` blocks are
48
+ * descended into recursively; the block envelopes themselves contribute
49
+ * nothing (they are pure containers).
50
+ *
51
+ * `inferItemLoad` mirrors the spec's "Default capacity" table:
52
+ * - Explicit `capacity:N` → that value.
53
+ * - Sized item without explicit `capacity:` → 1 (the default for sized
54
+ * items, matching the duration-derivation default).
55
+ * - Duration-literal item without `capacity:` → 0 (the bar paints normally
56
+ * but does not contribute load — this is the legacy "uncounted" item
57
+ * family from before the size system existed).
58
+ */
59
+ export function collectLoadContributors(children: PositionedTrackChild[]): LoadContributor[] {
60
+ const out: LoadContributor[] = [];
61
+ walk(children, out);
62
+ return out;
63
+ }
64
+
65
+ function walk(children: PositionedTrackChild[], out: LoadContributor[]): void {
66
+ for (const child of children) {
67
+ if (child.kind === 'item') {
68
+ const load = inferItemLoad(child);
69
+ if (load > 0 && child.box.width > 0) {
70
+ out.push({
71
+ startX: child.box.x,
72
+ endX: child.box.x + child.box.width,
73
+ load,
74
+ });
75
+ }
76
+ } else if (child.kind === 'parallel' || child.kind === 'group') {
77
+ walk(child.children, out);
78
+ }
79
+ }
80
+ }
81
+
82
+ function inferItemLoad(item: PositionedItem): number {
83
+ if (item.capacity) return item.capacity.value;
84
+ if (item.size) return 1;
85
+ return 0;
86
+ }
87
+
88
+ /**
89
+ * Compute a lane's utilization model. Returns `null` when there is no
90
+ * meaningful underline to paint:
91
+ * - Lane has no `capacity:` (no denominator → undefined utilization).
92
+ * - Lane has no items contributing load (collectLoadContributors returns
93
+ * an empty list).
94
+ * - Both thresholds resolve to `none` (caller has opted out of every
95
+ * color band; nothing to paint).
96
+ *
97
+ * The returned `PositionedLaneUtilization` carries the resolved thresholds
98
+ * (in fraction form, or `null` for opt-out) alongside the segments so the
99
+ * renderer can render legends / tooltips without re-resolving anything.
100
+ */
101
+ export function computeLaneUtilization(opts: {
102
+ children: PositionedTrackChild[];
103
+ capacityValue: number;
104
+ warnFraction: number | null;
105
+ overFraction: number | null;
106
+ }): PositionedLaneUtilization | null {
107
+ if (opts.capacityValue <= 0) return null;
108
+ if (opts.warnFraction === null && opts.overFraction === null) return null;
109
+
110
+ const contributors = collectLoadContributors(opts.children);
111
+ if (contributors.length === 0) return null;
112
+
113
+ // Build sorted unique x coordinates from every event boundary.
114
+ const xSet = new Set<number>();
115
+ for (const c of contributors) {
116
+ xSet.add(c.startX);
117
+ xSet.add(c.endX);
118
+ }
119
+ const xs = Array.from(xSet).sort((a, b) => a - b);
120
+ if (xs.length < 2) return null;
121
+
122
+ // Per half-open interval [xs[j], xs[j+1]) compute the active load by
123
+ // summing every contributor whose span covers the interval midpoint.
124
+ // O(n²) but n is small (typically <20 items per lane); avoids the
125
+ // bookkeeping of an event-stream sweep without a measurable cost.
126
+ const raw: PositionedUtilizationSegment[] = [];
127
+ for (let j = 0; j < xs.length - 1; j++) {
128
+ const a = xs[j];
129
+ const b = xs[j + 1];
130
+ const mid = (a + b) / 2;
131
+ let load = 0;
132
+ for (const c of contributors) {
133
+ if (c.startX <= mid && mid < c.endX) load += c.load;
134
+ }
135
+ raw.push({
136
+ startX: a,
137
+ endX: b,
138
+ load,
139
+ classification: classifyLoad(
140
+ load,
141
+ opts.capacityValue,
142
+ opts.warnFraction,
143
+ opts.overFraction,
144
+ ),
145
+ });
146
+ }
147
+
148
+ // Coalesce adjacent same-classification segments so the renderer paints
149
+ // one rectangle per visible color band rather than per event boundary.
150
+ const segments: PositionedUtilizationSegment[] = [];
151
+ for (const s of raw) {
152
+ const last = segments[segments.length - 1];
153
+ if (last && last.classification === s.classification && last.endX === s.startX) {
154
+ last.endX = s.endX;
155
+ // Keep the first interval's `load` as a representative; the
156
+ // coalesced span may have varying load within a single color
157
+ // band but renderers paint by class, not by exact load.
158
+ } else {
159
+ segments.push({ ...s });
160
+ }
161
+ }
162
+
163
+ return {
164
+ segments,
165
+ capacityValue: opts.capacityValue,
166
+ warnFraction: opts.warnFraction,
167
+ overFraction: opts.overFraction,
168
+ };
169
+ }
170
+
171
+ /**
172
+ * Classify a single load value against the lane's resolved capacity and
173
+ * thresholds. Spec § "Load function and segmentation":
174
+ * - `u < warn-at` → green (healthy; includes `u = 0` so the underline
175
+ * stays continuous).
176
+ * - `warn-at ≤ u < over-at` → yellow.
177
+ * - `u ≥ over-at` → red.
178
+ *
179
+ * `null` thresholds disable that color band: a `null` warnFraction collapses
180
+ * to a binary green / red indicator; a `null` overFraction collapses to
181
+ * green / yellow; both `null` is unreachable here (caller short-circuits
182
+ * upstream).
183
+ */
184
+ export function classifyLoad(
185
+ load: number,
186
+ capacity: number,
187
+ warnFraction: number | null,
188
+ overFraction: number | null,
189
+ ): UtilizationClassification {
190
+ const u = load / capacity;
191
+ if (overFraction !== null && u >= overFraction) return 'red';
192
+ if (warnFraction !== null && u >= warnFraction) return 'yellow';
193
+ return 'green';
194
+ }
195
+
196
+ /**
197
+ * Resolve the lane's effective utilization thresholds, applying the spec's
198
+ * resolution order:
199
+ *
200
+ * 1. Lane's own `utilization-warn-at:` / `utilization-over-at:` properties.
201
+ * 2. The applicable `default swimlane` declaration's properties.
202
+ * 3. Built-in defaults (`warn-at:80%`, `over-at:100%`).
203
+ *
204
+ * Each side resolves independently — a lane can pin only one threshold and
205
+ * inherit the other from defaults / built-ins. The returned `null` means
206
+ * "opted out via `none`" and disables that color band downstream.
207
+ *
208
+ * Malformed values (already reported by the validator) are treated as
209
+ * "unset" — the resolver falls through to the next layer rather than
210
+ * double-reporting.
211
+ */
212
+ export function resolveLaneUtilizationThresholds(
213
+ lane: SwimlaneDeclaration,
214
+ defaults: Map<string, DefaultDeclaration>,
215
+ ): { warn: number | null; over: number | null } {
216
+ const dflt = defaults.get('swimlane');
217
+ const warn = resolveThreshold(
218
+ propValue(lane.properties, 'utilization-warn-at'),
219
+ dflt ? propValue(dflt.properties, 'utilization-warn-at') : undefined,
220
+ DEFAULT_UTILIZATION_WARN_FRACTION,
221
+ );
222
+ const over = resolveThreshold(
223
+ propValue(lane.properties, 'utilization-over-at'),
224
+ dflt ? propValue(dflt.properties, 'utilization-over-at') : undefined,
225
+ DEFAULT_UTILIZATION_OVER_FRACTION,
226
+ );
227
+ return { warn, over };
228
+ }
229
+
230
+ function resolveThreshold(
231
+ laneVal: string | undefined,
232
+ defaultVal: string | undefined,
233
+ builtinFraction: number,
234
+ ): number | null {
235
+ const raw = laneVal ?? defaultVal;
236
+ const parsed = parseThresholdRaw(raw);
237
+ if (parsed.kind === 'unset') return builtinFraction;
238
+ if (parsed.kind === 'none') return null;
239
+ return parsed.value;
240
+ }
241
+
242
+ type ParsedThreshold = { kind: 'unset' } | { kind: 'none' } | { kind: 'number'; value: number };
243
+
244
+ const PERCENT_RE = /^\d+(?:\.\d+)?%$/;
245
+ const DECIMAL_FRACTION_RE = /^\d+\.\d+$/;
246
+
247
+ function parseThresholdRaw(val: string | undefined): ParsedThreshold {
248
+ if (val === undefined) return { kind: 'unset' };
249
+ if (val === 'none') return { kind: 'none' };
250
+ if (PERCENT_RE.test(val)) {
251
+ const n = parseFloat(val) / 100;
252
+ return Number.isFinite(n) && n > 0 ? { kind: 'number', value: n } : { kind: 'unset' };
253
+ }
254
+ if (DECIMAL_FRACTION_RE.test(val)) {
255
+ const n = parseFloat(val);
256
+ return Number.isFinite(n) && n > 0 ? { kind: 'number', value: n } : { kind: 'unset' };
257
+ }
258
+ return { kind: 'unset' };
259
+ }
@@ -0,0 +1,182 @@
1
+ // Layout context — shared internal types used by `layout.ts` and the
2
+ // node files under `nodes/`. Extracted so per-entity nodes (e.g.
3
+ // `swimlane-node.ts`) can type their inputs without forcing a circular
4
+ // import on the production composition root in `layout.ts`.
5
+
6
+ import type {
7
+ EntityProperty,
8
+ GroupBlock,
9
+ ItemDeclaration,
10
+ LabelDeclaration,
11
+ ParallelBlock,
12
+ SymbolDeclaration,
13
+ } from '@nowline/core';
14
+ import type { BandScale } from './band-scale.js';
15
+ import type { resolveCalendar } from './calendar.js';
16
+ import type { StyleContext } from './style-resolution.js';
17
+ import type { TimeScale } from './time-scale.js';
18
+ import type {
19
+ MarkerRowPlacement,
20
+ Point,
21
+ PositionedItem,
22
+ PositionedTimelineScale,
23
+ PositionedTrackChild,
24
+ ResolvedSize,
25
+ SlackCorridor,
26
+ } from './types.js';
27
+ import type { WorkingCalendar } from './working-calendar.js';
28
+
29
+ /** Slim accumulator used while sequencing items into a track. */
30
+ export interface TrackCursor {
31
+ /** Left edge where the next item begins. */
32
+ x: number;
33
+ /** Top edge of the current row. */
34
+ y: number;
35
+ /** Accumulated height of the track. */
36
+ height: number;
37
+ /** Rightmost edge reached. */
38
+ maxX: number;
39
+ }
40
+
41
+ export function newCursor(x: number, y: number): TrackCursor {
42
+ return { x, y, height: 0, maxX: x };
43
+ }
44
+
45
+ /**
46
+ * Shared layout state passed through the per-entity sequencers and
47
+ * Renderable nodes. Owns the resolved calendar/timeline/style scope plus
48
+ * the running entity-edge maps that `after:` / `before:` references read.
49
+ */
50
+ export interface LayoutContext {
51
+ cal: ReturnType<typeof resolveCalendar>;
52
+ styleCtx: StyleContext;
53
+ sizes: Map<string, ResolvedSize>;
54
+ labels: Map<string, LabelDeclaration>;
55
+ teams: Map<string, import('@nowline/core').TeamDeclaration>;
56
+ persons: Map<string, import('@nowline/core').PersonDeclaration>;
57
+ /**
58
+ * Custom `symbol` declarations from `ResolvedConfig.symbols`. Used by
59
+ * `resolveCapacityIcon` (in `capacity.ts`) to dereference custom symbol
60
+ * ids like `capacity-icon:budget` to the symbol's `unicode:` payload.
61
+ * Empty when the file declares no symbols.
62
+ */
63
+ symbols: Map<string, SymbolDeclaration>;
64
+ footnoteIndex: Map<string, number>;
65
+ /** For each footnote id, the list of `on:` host ids it references. */
66
+ footnoteHosts: Map<string, string[]>;
67
+ timeline: PositionedTimelineScale;
68
+ scale: TimeScale;
69
+ calendar: WorkingCalendar;
70
+ bandScale: BandScale;
71
+ entityLeftEdges: Map<string, number>;
72
+ entityRightEdges: Map<string, number>;
73
+ entityMidpoints: Map<string, Point>;
74
+ /**
75
+ * Visual edges for items (entries with a painted bar). Differs
76
+ * from `entityLeftEdges`/`entityRightEdges` (logical column
77
+ * boundaries) by `ITEM_INSET_PX` on each side so dependency
78
+ * arrows attach to the painted bar edge instead of landing in
79
+ * the inter-column gutter. Anchors and milestones are absent —
80
+ * their attach geometry uses `(center.x, target.row.midY)` on
81
+ * the cut line, computed inline by `buildDependencies`.
82
+ */
83
+ entityVisualLeftX: Map<string, number>;
84
+ entityVisualRightX: Map<string, number>;
85
+ /**
86
+ * Per-item exit point for `after:` dependency arrows leaving
87
+ * this entity. Default = `(visualRight, midY)`. When the
88
+ * caption spills past the bar's right edge (`textSpills`), the
89
+ * exit drops to `(box.x + box.width / 2, box.y + box.height)`
90
+ * — the bottom-middle of the progress strip — so the arrow
91
+ * doesn't visually pierce the spilled title/meta text to the
92
+ * right of the bar.
93
+ */
94
+ itemArrowSource: Map<string, Point>;
95
+ /**
96
+ * Flow key for each item, used to dedupe milestone slack arrows.
97
+ * A "flow" is the deepest enclosing single-track container —
98
+ * swimlane root, sequential group, or one parallel sub-track.
99
+ * Two items share a flowKey iff they share that container path
100
+ * (file order already encodes their ordering, so only the
101
+ * latest predecessor in each flow contributes a slack arrow).
102
+ */
103
+ itemFlowKey: Map<string, string>;
104
+ /**
105
+ * The flow key currently being built by the swimlane walk.
106
+ * Container nodes (`SwimlaneNode`, `GroupNode`, `ParallelNode`)
107
+ * push their own segment onto this string before recursing into
108
+ * children and restore it afterward. `sequenceItem` reads this
109
+ * value to populate `itemFlowKey`.
110
+ */
111
+ currentFlowKey: string;
112
+ /**
113
+ * Y coordinate where milestone slack arrows attach for each item id.
114
+ * Defaults to the item's row midpoint; when an item's caption spills
115
+ * past the bar's right edge, drops to the progress-strip's vertical
116
+ * center (`box.y + box.height - PROGRESS_STRIP_HEIGHT_PX / 2`) so the
117
+ * arrow stays clear of the spilled title/meta line and visually
118
+ * aligns with the bottom-edge progress bar instead.
119
+ */
120
+ itemSlackAttachY: Map<string, number>;
121
+ /**
122
+ * Horizontal arrow corridors that the swimlane row-packer must avoid.
123
+ * Empty during the first layout pass; populated from the first
124
+ * pass's milestones and consulted on the second pass so the binding
125
+ * predecessor (and any unrelated overlapping item) drops to a row
126
+ * whose Y does not match the corridor.
127
+ */
128
+ slackCorridors: SlackCorridor[];
129
+ /**
130
+ * Pre-computed marker-row placement (row index + label box + side)
131
+ * for every anchor and date-pinned milestone. After-only milestones
132
+ * pack against this map at build time; date-pinned entries are
133
+ * snapshot upstream so their (Y, label) survives swimlane reflows.
134
+ */
135
+ markerRowPlacements: Map<string, MarkerRowPlacement>;
136
+ chartTopY: number;
137
+ chartBottomY: number;
138
+ /**
139
+ * Y coordinate at the bottom of the last swimlane / include region.
140
+ * Distinct from `chartBottomY`, which extends through any mirrored
141
+ * bottom timeline tick panel. Marker cut-lines (anchors, milestones)
142
+ * stop here so they never invade the bottom date strip; the now-line
143
+ * uses the wider `chartBottomY` (or the bottom panel's bottom edge)
144
+ * to thread the entire timeline strip.
145
+ */
146
+ swimlaneBottomY: number;
147
+ chartRightX: number;
148
+ }
149
+
150
+ /**
151
+ * Bundle of layout-internal helper callbacks injected from `layout.ts`
152
+ * into the per-entity Renderable nodes. Keeping the helpers in
153
+ * `layout.ts` (rather than splitting them across many tiny files) means
154
+ * the nodes stay small and importable without a runtime cycle: they
155
+ * receive the helpers at construction time via this struct.
156
+ */
157
+ export interface LayoutHelpers {
158
+ sequenceItem: (
159
+ child: ItemDeclaration,
160
+ cursor: TrackCursor,
161
+ ctx: LayoutContext,
162
+ ownerOverride?: string,
163
+ ) => PositionedItem;
164
+ sequenceOne: (
165
+ child: ItemDeclaration | GroupBlock | ParallelBlock,
166
+ cursor: TrackCursor,
167
+ ctx: LayoutContext,
168
+ ) => PositionedTrackChild;
169
+ resolveChildStart: (
170
+ props: EntityProperty[],
171
+ seqDefault: number,
172
+ laneLeftX: number,
173
+ ctx: LayoutContext,
174
+ ) => number;
175
+ newCursor: (x: number, y: number) => TrackCursor;
176
+ estimateTextWidth: (text: string, fontSize: number) => number;
177
+ /** Predict the extra height an item's wrapped label-chip rows will
178
+ * add, so callers can size the row pitch BEFORE handing off to
179
+ * `sequenceItem`. Returns 0 when the item's labels all fit on a
180
+ * single chip row. */
181
+ predictItemChipExtraHeight: (item: ItemDeclaration, ctx: LayoutContext) => number;
182
+ }