@orangecharts/wrun-chart-host 0.1.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 (198) hide show
  1. package/README.md +72 -0
  2. package/dist/constants/governed-exchanges.constants.d.ts +11 -0
  3. package/dist/constants/governed-exchanges.constants.js +11 -0
  4. package/dist/constants/indicator-sets.constants.d.ts +8 -0
  5. package/dist/constants/indicator-sets.constants.js +8 -0
  6. package/dist/constants/interval.constants.d.ts +34 -0
  7. package/dist/constants/interval.constants.js +121 -0
  8. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.d.ts +28 -0
  9. package/dist/constants/scriptEditor/technicalIndicatorScriptKeys.constants.js +65 -0
  10. package/dist/constants/tpo-color-schemes.constants.d.ts +30 -0
  11. package/dist/constants/tpo-color-schemes.constants.js +32 -0
  12. package/dist/host-env.d.ts +102 -0
  13. package/dist/host-env.js +26 -0
  14. package/dist/index.d.ts +93 -0
  15. package/dist/index.js +96 -0
  16. package/dist/indicators/controls/tpo-letter.util.d.ts +2 -0
  17. package/dist/indicators/controls/tpo-letter.util.js +13 -0
  18. package/dist/indicators/controls/tpo-session.util.d.ts +99 -0
  19. package/dist/indicators/controls/tpo-session.util.js +389 -0
  20. package/dist/interfaces/kscript-diagnostics.interface.d.ts +64 -0
  21. package/dist/interfaces/kscript-diagnostics.interface.js +1 -0
  22. package/dist/interfaces/om-panel-data.interface.d.ts +103 -0
  23. package/dist/interfaces/om-panel-data.interface.js +7 -0
  24. package/dist/interfaces/wrun-runtime.interface.d.ts +2659 -0
  25. package/dist/interfaces/wrun-runtime.interface.js +55 -0
  26. package/dist/utility/chart/calendar-interval.util.d.ts +24 -0
  27. package/dist/utility/chart/calendar-interval.util.js +81 -0
  28. package/dist/utility/chart/candle-bar-end.util.d.ts +15 -0
  29. package/dist/utility/chart/candle-bar-end.util.js +23 -0
  30. package/dist/utility/chart/cme-realtime.util.d.ts +24 -0
  31. package/dist/utility/chart/cme-realtime.util.js +53 -0
  32. package/dist/utility/chart/interval-ladder.util.d.ts +27 -0
  33. package/dist/utility/chart/interval-ladder.util.js +92 -0
  34. package/dist/utility/chart/kscript-drawing-prep.util.d.ts +40 -0
  35. package/dist/utility/chart/kscript-drawing-prep.util.js +184 -0
  36. package/dist/utility/chart/kscript-drawing-safe-area.util.d.ts +95 -0
  37. package/dist/utility/chart/kscript-drawing-safe-area.util.js +805 -0
  38. package/dist/utility/chart/kscript-legend-entries.util.d.ts +166 -0
  39. package/dist/utility/chart/kscript-legend-entries.util.js +538 -0
  40. package/dist/utility/chart/kscript-output-role.util.d.ts +32 -0
  41. package/dist/utility/chart/kscript-output-role.util.js +215 -0
  42. package/dist/utility/chart/kscript-provenance-keys.util.d.ts +22 -0
  43. package/dist/utility/chart/kscript-provenance-keys.util.js +56 -0
  44. package/dist/utility/chart/kscript-run-result-normalize.util.d.ts +53 -0
  45. package/dist/utility/chart/kscript-run-result-normalize.util.js +476 -0
  46. package/dist/utility/chart/kscript-run-result-shape.util.d.ts +14 -0
  47. package/dist/utility/chart/kscript-run-result-shape.util.js +66 -0
  48. package/dist/utility/chart/kscript-titancharts-container.util.d.ts +29 -0
  49. package/dist/utility/chart/kscript-titancharts-container.util.js +151 -0
  50. package/dist/utility/chart/kscript-viewport-overlays.util.d.ts +83 -0
  51. package/dist/utility/chart/kscript-viewport-overlays.util.js +443 -0
  52. package/dist/utility/chart/om-panel-push-style.util.d.ts +78 -0
  53. package/dist/utility/chart/om-panel-push-style.util.js +301 -0
  54. package/dist/utility/chart/om-panel.util.d.ts +166 -0
  55. package/dist/utility/chart/om-panel.util.js +815 -0
  56. package/dist/utility/chart/om-series.util.d.ts +42 -0
  57. package/dist/utility/chart/om-series.util.js +330 -0
  58. package/dist/utility/chart/om-target.util.d.ts +11 -0
  59. package/dist/utility/chart/om-target.util.js +24 -0
  60. package/dist/utility/chart/om-widget.util.d.ts +148 -0
  61. package/dist/utility/chart/om-widget.util.js +413 -0
  62. package/dist/utility/chart/overlay-output-budget.util.d.ts +16 -0
  63. package/dist/utility/chart/overlay-output-budget.util.js +109 -0
  64. package/dist/utility/chart/overlay-paint-flags.util.d.ts +24 -0
  65. package/dist/utility/chart/overlay-paint-flags.util.js +55 -0
  66. package/dist/utility/chart/profile-row-height.util.d.ts +21 -0
  67. package/dist/utility/chart/profile-row-height.util.js +110 -0
  68. package/dist/utility/chart/session-interval.util.d.ts +1 -0
  69. package/dist/utility/chart/session-interval.util.js +35 -0
  70. package/dist/utility/chart/technical-script-legend-value.util.d.ts +18 -0
  71. package/dist/utility/chart/technical-script-legend-value.util.js +53 -0
  72. package/dist/utility/chart/trading-day.util.d.ts +70 -0
  73. package/dist/utility/chart/trading-day.util.js +238 -0
  74. package/dist/utility/chart/wrun-legend-name.util.d.ts +25 -0
  75. package/dist/utility/chart/wrun-legend-name.util.js +87 -0
  76. package/dist/utility/chart/wrun-run-result.util.d.ts +977 -0
  77. package/dist/utility/chart/wrun-run-result.util.js +5179 -0
  78. package/dist/utility/chart/wrun-visibility-chart.util.d.ts +25 -0
  79. package/dist/utility/chart/wrun-visibility-chart.util.js +22 -0
  80. package/dist/utility/indicators/heatmap-sensitivity.util.d.ts +62 -0
  81. package/dist/utility/indicators/heatmap-sensitivity.util.js +82 -0
  82. package/dist/utility/number-format.util.d.ts +68 -0
  83. package/dist/utility/number-format.util.js +188 -0
  84. package/dist/utility/v2/opra-governed.util.d.ts +1 -0
  85. package/dist/utility/v2/opra-governed.util.js +13 -0
  86. package/dist/utility/wrun/wrun-auto-row.util.d.ts +54 -0
  87. package/dist/utility/wrun/wrun-auto-row.util.js +96 -0
  88. package/dist/utility/wrun/wrun-batched-marks.util.d.ts +341 -0
  89. package/dist/utility/wrun/wrun-batched-marks.util.js +938 -0
  90. package/dist/utility/wrun/wrun-batched-readout.util.d.ts +75 -0
  91. package/dist/utility/wrun/wrun-batched-readout.util.js +171 -0
  92. package/dist/utility/wrun/wrun-box-segment.util.d.ts +185 -0
  93. package/dist/utility/wrun/wrun-box-segment.util.js +570 -0
  94. package/dist/utility/wrun/wrun-build-memo.util.d.ts +133 -0
  95. package/dist/utility/wrun/wrun-build-memo.util.js +301 -0
  96. package/dist/utility/wrun/wrun-builtin-twin-ports.util.d.ts +25 -0
  97. package/dist/utility/wrun/wrun-builtin-twin-ports.util.js +75 -0
  98. package/dist/utility/wrun/wrun-builtin-twin.util.d.ts +9 -0
  99. package/dist/utility/wrun/wrun-builtin-twin.util.js +40 -0
  100. package/dist/utility/wrun/wrun-canvas-cache.util.d.ts +28 -0
  101. package/dist/utility/wrun/wrun-canvas-cache.util.js +56 -0
  102. package/dist/utility/wrun/wrun-canvas-readout.util.d.ts +69 -0
  103. package/dist/utility/wrun/wrun-canvas-readout.util.js +53 -0
  104. package/dist/utility/wrun/wrun-canvases.util.d.ts +295 -0
  105. package/dist/utility/wrun/wrun-canvases.util.js +1432 -0
  106. package/dist/utility/wrun/wrun-cap-notice.util.d.ts +77 -0
  107. package/dist/utility/wrun/wrun-cap-notice.util.js +296 -0
  108. package/dist/utility/wrun/wrun-card-style.util.d.ts +154 -0
  109. package/dist/utility/wrun/wrun-card-style.util.js +539 -0
  110. package/dist/utility/wrun/wrun-chart-context.util.d.ts +89 -0
  111. package/dist/utility/wrun/wrun-chart-context.util.js +213 -0
  112. package/dist/utility/wrun/wrun-chart-names.util.d.ts +23 -0
  113. package/dist/utility/wrun/wrun-chart-names.util.js +33 -0
  114. package/dist/utility/wrun/wrun-chart-time.util.d.ts +12 -0
  115. package/dist/utility/wrun/wrun-chart-time.util.js +23 -0
  116. package/dist/utility/wrun/wrun-child-overlay-id.util.d.ts +13 -0
  117. package/dist/utility/wrun/wrun-child-overlay-id.util.js +44 -0
  118. package/dist/utility/wrun/wrun-color-facts.util.d.ts +40 -0
  119. package/dist/utility/wrun/wrun-color-facts.util.js +88 -0
  120. package/dist/utility/wrun/wrun-color-settings.lazy.d.ts +26 -0
  121. package/dist/utility/wrun/wrun-color-settings.lazy.js +9 -0
  122. package/dist/utility/wrun/wrun-color-settings.util.d.ts +351 -0
  123. package/dist/utility/wrun/wrun-color-settings.util.js +1363 -0
  124. package/dist/utility/wrun/wrun-footprint-modes.util.d.ts +23 -0
  125. package/dist/utility/wrun/wrun-footprint-modes.util.js +24 -0
  126. package/dist/utility/wrun/wrun-frames.d.ts +217 -0
  127. package/dist/utility/wrun/wrun-frames.js +1021 -0
  128. package/dist/utility/wrun/wrun-handles.util.d.ts +792 -0
  129. package/dist/utility/wrun/wrun-handles.util.js +2050 -0
  130. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.d.ts +156 -0
  131. package/dist/utility/wrun/wrun-heatmap-cell-budget.util.js +342 -0
  132. package/dist/utility/wrun/wrun-hosted-wire-caps.d.ts +218 -0
  133. package/dist/utility/wrun/wrun-hosted-wire-caps.js +667 -0
  134. package/dist/utility/wrun/wrun-hosted-wire.util.d.ts +40 -0
  135. package/dist/utility/wrun/wrun-hosted-wire.util.js +27 -0
  136. package/dist/utility/wrun/wrun-hover.util.d.ts +182 -0
  137. package/dist/utility/wrun/wrun-hover.util.js +534 -0
  138. package/dist/utility/wrun/wrun-hud-looks.util.d.ts +246 -0
  139. package/dist/utility/wrun/wrun-hud-looks.util.js +587 -0
  140. package/dist/utility/wrun/wrun-integration-budget.util.d.ts +47 -0
  141. package/dist/utility/wrun/wrun-integration-budget.util.js +122 -0
  142. package/dist/utility/wrun/wrun-latest-snapshot.d.ts +19 -0
  143. package/dist/utility/wrun/wrun-latest-snapshot.js +7 -0
  144. package/dist/utility/wrun/wrun-levels-child.util.d.ts +131 -0
  145. package/dist/utility/wrun/wrun-levels-child.util.js +514 -0
  146. package/dist/utility/wrun/wrun-lifecycle.contract.d.ts +75 -0
  147. package/dist/utility/wrun/wrun-lifecycle.contract.js +9 -0
  148. package/dist/utility/wrun/wrun-market-facts.util.d.ts +116 -0
  149. package/dist/utility/wrun/wrun-market-facts.util.js +238 -0
  150. package/dist/utility/wrun/wrun-output-style.util.d.ts +344 -0
  151. package/dist/utility/wrun/wrun-output-style.util.js +1239 -0
  152. package/dist/utility/wrun/wrun-overlay-identity.util.d.ts +84 -0
  153. package/dist/utility/wrun/wrun-overlay-identity.util.js +142 -0
  154. package/dist/utility/wrun/wrun-overlay-result.util.d.ts +120 -0
  155. package/dist/utility/wrun/wrun-overlay-result.util.js +219 -0
  156. package/dist/utility/wrun/wrun-pack.util.d.ts +70 -0
  157. package/dist/utility/wrun/wrun-pack.util.js +301 -0
  158. package/dist/utility/wrun/wrun-pane-format.util.d.ts +180 -0
  159. package/dist/utility/wrun/wrun-pane-format.util.js +299 -0
  160. package/dist/utility/wrun/wrun-panel-style.lazy.d.ts +25 -0
  161. package/dist/utility/wrun/wrun-panel-style.lazy.js +15 -0
  162. package/dist/utility/wrun/wrun-panel-style.util.d.ts +174 -0
  163. package/dist/utility/wrun/wrun-panel-style.util.js +726 -0
  164. package/dist/utility/wrun/wrun-param-values.util.d.ts +69 -0
  165. package/dist/utility/wrun/wrun-param-values.util.js +230 -0
  166. package/dist/utility/wrun/wrun-refusal.util.d.ts +263 -0
  167. package/dist/utility/wrun/wrun-refusal.util.js +1259 -0
  168. package/dist/utility/wrun/wrun-registry-errors.util.d.ts +199 -0
  169. package/dist/utility/wrun/wrun-registry-errors.util.js +257 -0
  170. package/dist/utility/wrun/wrun-render-select.util.d.ts +336 -0
  171. package/dist/utility/wrun/wrun-render-select.util.js +1157 -0
  172. package/dist/utility/wrun/wrun-script-inputs.util.d.ts +434 -0
  173. package/dist/utility/wrun/wrun-script-inputs.util.js +1971 -0
  174. package/dist/utility/wrun/wrun-setting-words.lazy.d.ts +20 -0
  175. package/dist/utility/wrun/wrun-setting-words.lazy.js +9 -0
  176. package/dist/utility/wrun/wrun-setting-words.util.d.ts +79 -0
  177. package/dist/utility/wrun/wrun-setting-words.util.js +176 -0
  178. package/dist/utility/wrun/wrun-socket-row-store.d.ts +98 -0
  179. package/dist/utility/wrun/wrun-socket-row-store.js +417 -0
  180. package/dist/utility/wrun/wrun-style-flags.util.d.ts +18 -0
  181. package/dist/utility/wrun/wrun-style-flags.util.js +21 -0
  182. package/dist/utility/wrun/wrun-style-words.util.d.ts +148 -0
  183. package/dist/utility/wrun/wrun-style-words.util.js +439 -0
  184. package/dist/utility/wrun/wrun-table-look.util.d.ts +125 -0
  185. package/dist/utility/wrun/wrun-table-look.util.js +225 -0
  186. package/dist/utility/wrun/wrun-text-look.util.d.ts +51 -0
  187. package/dist/utility/wrun/wrun-text-look.util.js +227 -0
  188. package/dist/utility/wrun/wrun-text-params.util.d.ts +106 -0
  189. package/dist/utility/wrun/wrun-text-params.util.js +171 -0
  190. package/dist/utility/wrun/wrun-tpo-rows.util.d.ts +71 -0
  191. package/dist/utility/wrun/wrun-tpo-rows.util.js +438 -0
  192. package/dist/utility/wrun/wrun-twin-compute-inputs.util.d.ts +14 -0
  193. package/dist/utility/wrun/wrun-twin-compute-inputs.util.js +58 -0
  194. package/dist/utility/wrun/wrun-visibility-table.d.ts +437 -0
  195. package/dist/utility/wrun/wrun-visibility-table.js +237 -0
  196. package/dist/utility/wrun/wrun-volume-profile.util.d.ts +37 -0
  197. package/dist/utility/wrun/wrun-volume-profile.util.js +278 -0
  198. package/package.json +38 -0
@@ -0,0 +1,805 @@
1
+ /**
2
+ * The chrome safe area for pane-pixel drawings (WRUN-STYLE DW06, DW35).
3
+ *
4
+ * A `top_left` anchored drawing (a wrun HUD's boxes and label rows) is placed
5
+ * in CSS pixels from the plot's top-left corner, exactly where the chart's own
6
+ * legend stack lives: the symbol row, the OHLC row, Vol, the "N indicators"
7
+ * summary and one row per indicator (about 145 px tall with one indicator,
8
+ * growing with each). TitanCharts resolves pane anchors with no inset
9
+ * (`paneAnchorOffset`), so the adapter shifts the overlay's `top_left` group
10
+ * below the legend when the two boxes intersect, at the door to
11
+ * `updateDrawings`. The remembered records keep the author's coordinates, so
12
+ * a re-apply never stacks a second shift, and the whole group moves by one
13
+ * amount, so the HUD's own layout is untouched. The legend is watched
14
+ * (rows resize, rows come and go) and the mounted set follows it.
15
+ *
16
+ * The same door measures the rest of the pane chrome (DW06): the pane action
17
+ * bar and the compare strip along the top right, the price-axis tags along
18
+ * the right edge, and each offchart pane's own legend row and button strip
19
+ * (titan's pane move buttons, top right). The measure goes to the engine as
20
+ * card safe-area insets (`setCardSafeArea`, read by the card layer for
21
+ * `safeArea` cards, feeds and meters) and moves `safeArea` handles here: right
22
+ * anchors step inward past the axis tags, top-right and top-centre anchors
23
+ * step under the pane's buttons. The HUD layer reads the same measure
24
+ * (`hudTopLeft`) for its top-left inset.
25
+ *
26
+ * A notice or label that did not opt in clears the buttons too (template QA
27
+ * A17, B10): on a pane with a measured button strip (the price pane's action
28
+ * bar, a lower pane's move buttons), a plain `top_right` or `top_center` group
29
+ * whose top sits above the line a `safeArea` group starts at (the pane's
30
+ * `topRight` inset) moves down onto that line, so "Needs a chart finer than
31
+ * 4h" reads whole instead of ending under the expand button. A pane with no
32
+ * measured strip moves nothing.
33
+ *
34
+ * An overlay whose sheet declared `stack_handles: true` (DW35, the paint flag
35
+ * `stackHandles`) keeps its corner groups clear of the groups earlier
36
+ * overlays (in `tc.metadata` order) hold at the same corner: its group moves
37
+ * by the sum of their heights plus one gap each, down from a top corner and up
38
+ * from a bottom corner. Overlays that did not opt in never move for another
39
+ * overlay, and middle and edge anchors never stack.
40
+ *
41
+ * Every shift is capped so the moved group's box stays inside its pane (the
42
+ * LABELS contract, item 28): the plot is measured with the legend (the legend
43
+ * section's height is the pane's, its width the plot's without the price
44
+ * axis) and a group moves at most the room past its far edge, never
45
+ * backwards. The records sharing an anchor are capped as one group, so a HUD
46
+ * holds together. Without a measured plot nothing is capped.
47
+ */
48
+ import { overlayPaintFlagsOf } from './overlay-paint-flags.util.js';
49
+ /** Air between the legend's last row (or an earlier stacked group) and the shifted group. */
50
+ export const LEGEND_SAFE_AREA_GAP_PX = 8;
51
+ /** The price-axis tags a right-anchored widget steps past (DW06). */
52
+ export const CHART_AXIS_TAG_CLEARANCE_PX = 56;
53
+ /** The pane action bar a top-right or top-centre widget steps under (DW06); the compare strip adds to it. */
54
+ export const CHART_ACTION_BAR_CLEARANCE_PX = 40;
55
+ /** The plain edge of an offchart pane: no action bar, no compare strip (its move buttons, when it has them, are measured). */
56
+ const PANE_EDGE_PX = 8;
57
+ const MAIN_PANE_ID = 'main';
58
+ const COMPARE_STRIP_PROPERTY = '--compare-strip-h';
59
+ /** The price pane's button strip: the chart cell's action bar (ChartPaneActionBar), a child of the tc-container. */
60
+ const ACTION_BAR_SELECTOR = '.chart-pane-action-bar';
61
+ /** An offchart pane's button strip: titan's move buttons, top right of the pane's legend section (only while the pane can move). */
62
+ const PANE_CONTROLS_SELECTOR = '.titan-charts-pane-controls';
63
+ const LEGEND_ANCHOR = 'top_left';
64
+ /** The corners a stacking overlay's groups stack at (DW35). */
65
+ const STACK_CORNERS = new Set([
66
+ 'top_left',
67
+ 'top_center',
68
+ 'top_right',
69
+ 'bottom_left',
70
+ 'bottom_center',
71
+ 'bottom_right',
72
+ ]);
73
+ /** Anchors whose x counts inward from the pane's right edge. */
74
+ const RIGHT_ANCHORS = new Set(['top_right', 'middle_right', 'bottom_right', 'right']);
75
+ /** Anchors that meet the pane action bar. */
76
+ const ACTION_BAR_ANCHORS = new Set(['top_right', 'top_center']);
77
+ const SAFE_AREA_KINDS = new Set(['line', 'box', 'label', 'polyline']);
78
+ // Label extents are estimates (titan measures text at paint time): about 0.6 em
79
+ // per glyph, 1.4 em per line, 4 px of pill padding unless the record carries its own.
80
+ const LABEL_GLYPH_EM = 0.6;
81
+ const LABEL_LINE_EM = 1.4;
82
+ const LABEL_PAD_PX = 4;
83
+ const DEFAULT_LABEL_SIZE = 12;
84
+ // The pane's top-left stack. Host rows are children of the tc-container (the
85
+ // symbol row, the prediction-venue rows); titan's rows live in the main
86
+ // section's legend section (`.titan-charts-legend-area` per plotted overlay,
87
+ // the "N indicators" summary's indicator). All are content-sized, and hover
88
+ // fades them by opacity, so the measured box holds still under the pointer.
89
+ const HOST_ROW_SELECTOR = '.symbol-interval-row, .polymarket-row-1, .polymarket-row-2, .polymarket-row-3';
90
+ const PLOT_SELECTOR = '.titan-charts-main-section .titan-charts-legend-section';
91
+ const TITAN_ROW_SELECTOR = '.titan-charts-legend-area, .titan-charts-legend-collapse-indicator';
92
+ const SECTION_PREFIX = '.titan-charts-';
93
+ const SECTION_SUFFIX = '-section';
94
+ const LEGEND_SECTION_SELECTOR = '.titan-charts-legend-section';
95
+ function isRecord(value) {
96
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
97
+ }
98
+ function finite(value) {
99
+ return typeof value === 'number' && Number.isFinite(value);
100
+ }
101
+ function union(a, b) {
102
+ if (!a)
103
+ return b;
104
+ return {
105
+ left: Math.min(a.left, b.left),
106
+ top: Math.min(a.top, b.top),
107
+ right: Math.max(a.right, b.right),
108
+ bottom: Math.max(a.bottom, b.bottom),
109
+ };
110
+ }
111
+ function rect(x1, y1, x2, y2) {
112
+ if (!finite(x1) || !finite(y1) || !finite(x2) || !finite(y2))
113
+ return null;
114
+ return { left: Math.min(x1, x2), top: Math.min(y1, y2), right: Math.max(x1, x2), bottom: Math.max(y1, y2) };
115
+ }
116
+ /** A pane-pixel handle (a line, box, label or polyline with a pane anchor), or null. */
117
+ function memberOf(drawing) {
118
+ if (!isRecord(drawing))
119
+ return null;
120
+ const { kind, props, pane } = drawing;
121
+ if (typeof kind !== 'string' || !SAFE_AREA_KINDS.has(kind) || !isRecord(props))
122
+ return null;
123
+ if (typeof props.anchor !== 'string')
124
+ return null;
125
+ return { kind, props, anchor: props.anchor, pane: typeof pane === 'string' ? pane : '' };
126
+ }
127
+ function safeAreaOf(drawing) {
128
+ return isRecord(drawing) && isRecord(drawing.props) && drawing.props.safeArea === true;
129
+ }
130
+ /**
131
+ * A label's box in its anchor's coordinates: lines split on newlines, the
132
+ * longest line sets the width, `valign` places the box on its point (titan
133
+ * centres the text on `y` when the record has none). At a bottom corner `y`
134
+ * counts up from the pane's bottom edge, so the screen-space extents flip.
135
+ */
136
+ function labelBounds(props, up) {
137
+ const { x, y } = props;
138
+ if (!finite(x) || !finite(y))
139
+ return null;
140
+ const size = finite(props.size) ? props.size : finite(props.fontSize) ? props.fontSize : DEFAULT_LABEL_SIZE;
141
+ const pad = finite(props.padding) ? props.padding : LABEL_PAD_PX;
142
+ const lines = (typeof props.text === 'string' ? props.text : '').split('\n');
143
+ const longest = lines.reduce((max, line) => Math.max(max, line.length), 0);
144
+ const width = longest * size * LABEL_GLYPH_EM + 2 * pad;
145
+ const height = lines.length * size * LABEL_LINE_EM + pad;
146
+ const left = props.align === 'left' ? x : props.align === 'right' ? x - width : x - width / 2;
147
+ // Screen-space extents around the point (down is positive): [nearest, farthest] by valign.
148
+ const [lo, hi] = props.valign === 'top' ? [0, height] : props.valign === 'bottom' ? [-height, 0] : [-height / 2, height / 2];
149
+ const top = up ? y - hi : y + lo;
150
+ return { left, top, right: left + width, bottom: top + height };
151
+ }
152
+ /** The member's box in its anchor's coordinates (`up`: y counts up from the bottom edge). */
153
+ function memberBounds(kind, props, up) {
154
+ switch (kind) {
155
+ case 'box':
156
+ return rect(props.x1 ?? props.left, props.y1 ?? props.top, props.x2 ?? props.right, props.y2 ?? props.bottom);
157
+ case 'line':
158
+ return rect(props.x1, props.y1, props.x2, props.y2);
159
+ case 'label':
160
+ return labelBounds(props, up);
161
+ case 'polyline': {
162
+ if (!Array.isArray(props.points))
163
+ return null;
164
+ let bounds = null;
165
+ for (const point of props.points) {
166
+ if (!isRecord(point) || !finite(point.x) || !finite(point.y))
167
+ continue;
168
+ bounds = union(bounds, { left: point.x, top: point.y, right: point.x, bottom: point.y });
169
+ }
170
+ return bounds;
171
+ }
172
+ default:
173
+ return null;
174
+ }
175
+ }
176
+ /** The price-pane groups of a set, one box per corner anchor that holds a member. */
177
+ function cornerGroups(drawings) {
178
+ const groups = new Map();
179
+ for (const drawing of drawings) {
180
+ const member = memberOf(drawing);
181
+ if (!member || member.pane !== 'onchart' || !STACK_CORNERS.has(member.anchor))
182
+ continue;
183
+ const bounds = memberBounds(member.kind, member.props, member.anchor.startsWith('bottom_'));
184
+ if (bounds)
185
+ groups.set(member.anchor, union(groups.get(member.anchor) ?? null, bounds));
186
+ }
187
+ return groups;
188
+ }
189
+ /** How far down a `top_left` group must move to sit one gap under the legend: 0 when the two boxes do not meet. */
190
+ function legendShiftOf(group, legend) {
191
+ if (!group || !legend)
192
+ return 0;
193
+ const apart = group.right <= legend.left ||
194
+ group.left >= legend.right ||
195
+ group.bottom <= legend.top ||
196
+ group.top >= legend.bottom;
197
+ if (apart)
198
+ return 0;
199
+ return Math.max(0, Math.round(legend.bottom + LEGEND_SAFE_AREA_GAP_PX - group.top));
200
+ }
201
+ /** The room (CSS px) a group has before its far edge leaves the pane, never negative. */
202
+ function roomPast(far, size) {
203
+ return Math.max(0, Math.floor(size - far));
204
+ }
205
+ /**
206
+ * How far down (CSS px) the set's `top_left` group must move to sit
207
+ * `LEGEND_SAFE_AREA_GAP_PX` under the legend: 0 when there is no legend, no
208
+ * group, or the two boxes do not meet. With a `pane` box the shift stops
209
+ * where the group's bottom meets the pane's (item 28).
210
+ */
211
+ export function legendSafeAreaShift(drawings, legend, pane) {
212
+ if (!legend)
213
+ return 0;
214
+ const group = cornerGroups(drawings).get(LEGEND_ANCHOR);
215
+ const shift = legendShiftOf(group, legend);
216
+ return pane && group ? Math.min(shift, roomPast(group.bottom, pane.height)) : shift;
217
+ }
218
+ const NO_STACK = new Map();
219
+ const NO_INSETS = { main: null, pane: null };
220
+ const NO_BOXES = { main: null, pane: null };
221
+ const NO_STRIPS = { main: null, pane: null };
222
+ /** The records that move together: one pane, one anchor, safeArea or not (a
223
+ * safeArea handle takes the inset steps its siblings do not). */
224
+ function shiftClassOf(member) {
225
+ return `${member.pane}|${member.anchor}|${member.props.safeArea === true ? 'safe' : 'plain'}`;
226
+ }
227
+ /**
228
+ * A class's shift before the cap: the legend and stacking shifts of a
229
+ * price-pane corner group, the inset steps of a safeArea handle, and for a
230
+ * plain top_right / top_center class the drop that puts its top on the pane's
231
+ * strip line (A17). The drop reads the class's own box before any stacking:
232
+ * every group that starts above the line starts on it, so a stacking group
233
+ * still lands one gap under the earlier group it stacks on.
234
+ */
235
+ function rawShiftOf(placement, cls) {
236
+ const { pane, anchor } = cls;
237
+ const offchart = pane === 'offchart';
238
+ let dx = 0;
239
+ let dy = 0;
240
+ if (pane === 'onchart') {
241
+ if (anchor === LEGEND_ANCHOR)
242
+ dy += placement.legendDy;
243
+ dy += placement.stack.get(anchor) ?? 0;
244
+ }
245
+ if (cls.safeArea) {
246
+ const insets = offchart ? placement.insets.pane : placement.insets.main;
247
+ if (insets) {
248
+ if (RIGHT_ANCHORS.has(anchor))
249
+ dx += insets.right;
250
+ if (ACTION_BAR_ANCHORS.has(anchor))
251
+ dy += insets.topRight;
252
+ }
253
+ }
254
+ else if (ACTION_BAR_ANCHORS.has(anchor) && cls.bounds) {
255
+ const line = offchart ? placement.strips.pane : placement.strips.main;
256
+ if (line !== null)
257
+ dy += Math.max(0, Math.ceil(line - cls.bounds.top));
258
+ }
259
+ return { dx, dy };
260
+ }
261
+ /**
262
+ * The moving classes of a set under `placement`, each capped so the whole
263
+ * group at its anchor (safeArea members and plain ones alike) stays inside
264
+ * its pane: a right anchor's x and a corner's y count inward from the edge
265
+ * the anchor names, so the cap is the room past the group's far edge. A
266
+ * class that does not move is left out.
267
+ */
268
+ function shiftClasses(drawings, placement) {
269
+ const members = new Map();
270
+ const groups = new Map();
271
+ for (const drawing of drawings) {
272
+ const member = memberOf(drawing);
273
+ if (!member)
274
+ continue;
275
+ const { kind, props, anchor, pane } = member;
276
+ const key = shiftClassOf(member);
277
+ const cls = members.get(key) ?? { pane, anchor, safeArea: props.safeArea === true, bounds: null };
278
+ members.set(key, cls);
279
+ const bounds = memberBounds(kind, props, anchor.startsWith('bottom_'));
280
+ if (!bounds)
281
+ continue;
282
+ cls.bounds = union(cls.bounds, bounds);
283
+ const groupKey = `${pane}|${anchor}`;
284
+ groups.set(groupKey, union(groups.get(groupKey) ?? null, bounds));
285
+ }
286
+ const classes = new Map();
287
+ for (const [key, cls] of members) {
288
+ const shift = { ...rawShiftOf(placement, cls), pane: cls.pane, anchor: cls.anchor };
289
+ if (shift.dx === 0 && shift.dy === 0)
290
+ continue;
291
+ const box = cls.pane === 'offchart' ? placement.boxes.pane : placement.boxes.main;
292
+ const bounds = groups.get(`${cls.pane}|${cls.anchor}`);
293
+ if (box && bounds) {
294
+ if (shift.dx > 0)
295
+ shift.dx = Math.min(shift.dx, roomPast(bounds.right, box.width));
296
+ if (shift.dy > 0)
297
+ shift.dy = Math.min(shift.dy, roomPast(bounds.bottom, box.height));
298
+ }
299
+ classes.set(key, shift);
300
+ }
301
+ return classes;
302
+ }
303
+ /** One string per distinct placement of a set: its moving classes and their capped shifts. */
304
+ function shiftsKey(classes) {
305
+ return [...classes]
306
+ .map(([key, cls]) => `${key}:${cls.dx}/${cls.dy}`)
307
+ .sort()
308
+ .join(',');
309
+ }
310
+ function shifted(drawing, kind, props, dx, dy) {
311
+ const next = { ...props };
312
+ const bump = (key, by) => {
313
+ const value = next[key];
314
+ if (finite(value))
315
+ next[key] = value + by;
316
+ };
317
+ switch (kind) {
318
+ case 'box':
319
+ ['x1', 'x2', 'left', 'right'].forEach((key) => bump(key, dx));
320
+ ['y1', 'y2', 'top', 'bottom'].forEach((key) => bump(key, dy));
321
+ break;
322
+ case 'line':
323
+ ['x1', 'x2'].forEach((key) => bump(key, dx));
324
+ ['y1', 'y2'].forEach((key) => bump(key, dy));
325
+ break;
326
+ case 'label':
327
+ bump('x', dx);
328
+ bump('y', dy);
329
+ break;
330
+ case 'polyline':
331
+ if (Array.isArray(props.points)) {
332
+ next.points = props.points.map((point) => isRecord(point)
333
+ ? {
334
+ ...point,
335
+ ...(finite(point.x) ? { x: point.x + dx } : {}),
336
+ ...(finite(point.y) ? { y: point.y + dy } : {}),
337
+ }
338
+ : point);
339
+ }
340
+ break;
341
+ }
342
+ return { ...drawing, props: next };
343
+ }
344
+ /** The set with each class's shift applied to its members; the same array when nothing moves. */
345
+ function applyShifts(drawings, classes) {
346
+ if (classes.size === 0)
347
+ return drawings;
348
+ let placed = null;
349
+ for (let index = 0; index < drawings.length; index += 1) {
350
+ const drawing = drawings[index];
351
+ const member = memberOf(drawing);
352
+ if (!member)
353
+ continue;
354
+ const cls = classes.get(shiftClassOf(member));
355
+ if (!cls || (cls.dx === 0 && cls.dy === 0))
356
+ continue;
357
+ if (!placed)
358
+ placed = [...drawings];
359
+ placed[index] = shifted(drawing, member.kind, member.props, cls.dx, cls.dy);
360
+ }
361
+ return placed ?? drawings;
362
+ }
363
+ /** The set with every shift of `placement` applied; the same array when nothing moves. */
364
+ function placeDrawings(drawings, placement) {
365
+ return applyShifts(drawings, shiftClasses(drawings, placement));
366
+ }
367
+ /** The set with its `top_left` group moved down by `dy`; the same array when `dy` is 0. */
368
+ export function shiftLegendSafeArea(drawings, dy) {
369
+ if (dy === 0)
370
+ return drawings;
371
+ return placeDrawings(drawings, {
372
+ legendDy: dy,
373
+ stack: NO_STACK,
374
+ insets: NO_INSETS,
375
+ boxes: NO_BOXES,
376
+ strips: NO_STRIPS,
377
+ });
378
+ }
379
+ /** `shiftLegendSafeArea` by `legendSafeAreaShift`. */
380
+ export function applyLegendSafeArea(drawings, legend) {
381
+ return shiftLegendSafeArea(drawings, legendSafeAreaShift(drawings, legend));
382
+ }
383
+ const trackers = new WeakMap();
384
+ function isElementLike(value) {
385
+ return (isRecord(value) &&
386
+ typeof value.querySelector === 'function' &&
387
+ typeof value.querySelectorAll === 'function' &&
388
+ typeof value.closest === 'function' &&
389
+ typeof value.getBoundingClientRect === 'function');
390
+ }
391
+ export function legendPaneOf(tc) {
392
+ const main = tc.mainContainerDiv;
393
+ if (!isElementLike(main))
394
+ return null;
395
+ const plot = main.querySelector(PLOT_SELECTOR);
396
+ if (!plot)
397
+ return null;
398
+ return { main, root: main.closest('.tc-container') ?? main, plot };
399
+ }
400
+ export function legendRowsOf(pane) {
401
+ return [...pane.root.querySelectorAll(HOST_ROW_SELECTOR), ...pane.plot.querySelectorAll(TITAN_ROW_SELECTOR)];
402
+ }
403
+ /** The rows' box relative to `origin`'s top-left corner, rounded; null when no row has a size. */
404
+ function measureRows(origin, rows) {
405
+ const base = origin.getBoundingClientRect();
406
+ let box = null;
407
+ for (const row of rows) {
408
+ const r = row.getBoundingClientRect();
409
+ if (!(r.width > 0 && r.height > 0))
410
+ continue;
411
+ box = union(box, {
412
+ left: r.left - base.left,
413
+ top: r.top - base.top,
414
+ right: r.right - base.left,
415
+ bottom: r.bottom - base.top,
416
+ });
417
+ }
418
+ return (box && {
419
+ left: Math.round(box.left),
420
+ top: Math.round(box.top),
421
+ right: Math.round(box.right),
422
+ bottom: Math.round(box.bottom),
423
+ });
424
+ }
425
+ export function measureLegendBox(pane) {
426
+ return measureRows(pane.plot, legendRowsOf(pane));
427
+ }
428
+ function sameBox(a, b) {
429
+ if (!a || !b)
430
+ return a === b;
431
+ return a.left === b.left && a.top === b.top && a.right === b.right && a.bottom === b.bottom;
432
+ }
433
+ function sameInsets(a, b) {
434
+ if (!a || !b)
435
+ return a === b;
436
+ return a.topLeft === b.topLeft && a.topRight === b.topRight && a.right === b.right;
437
+ }
438
+ function sameMeasure(a, b) {
439
+ if (!a || !b)
440
+ return a === b;
441
+ if (a.hudTopLeft !== b.hudTopLeft)
442
+ return false;
443
+ const ids = Object.keys(a.panes);
444
+ if (ids.length !== Object.keys(b.panes).length)
445
+ return false;
446
+ return ids.every((id) => sameInsets(a.panes[id], b.panes[id]));
447
+ }
448
+ function samePaneBoxes(a, b) {
449
+ const ids = Object.keys(a);
450
+ if (ids.length !== Object.keys(b).length)
451
+ return false;
452
+ return ids.every((id) => b[id] !== undefined && a[id].width === b[id].width && a[id].height === b[id].height);
453
+ }
454
+ function sameStrips(a, b) {
455
+ const ids = Object.keys(a);
456
+ return ids.length === Object.keys(b).length && ids.every((id) => a[id] === b[id]);
457
+ }
458
+ /** An element's box when it has one; null for an unsized (hidden, detached) element, which caps nothing. */
459
+ function paneBoxOf(element) {
460
+ const r = element.getBoundingClientRect();
461
+ return r.width > 0 && r.height > 0 ? { width: r.width, height: r.height } : null;
462
+ }
463
+ function metadataOf(tc) {
464
+ return Array.isArray(tc.metadata) ? tc.metadata.filter(isRecord) : [];
465
+ }
466
+ /** The offchart metas with a pane of their own (a hosted meta draws in its host's pane). */
467
+ function offchartPaneIds(tc) {
468
+ const ids = [];
469
+ for (const meta of metadataOf(tc)) {
470
+ if (meta.type !== 'offchart' || typeof meta.id !== 'string' || meta.id === MAIN_PANE_ID)
471
+ continue;
472
+ ids.push(meta.id);
473
+ }
474
+ return ids;
475
+ }
476
+ /** The pane an overlay's `offchart` records draw on: its host's when it is hosted, else its own. */
477
+ function hostPaneIdOf(tc, overlayId) {
478
+ const metas = metadataOf(tc);
479
+ const meta = metas.find((candidate) => candidate.id === overlayId);
480
+ const hostId = meta?.hostPaneId;
481
+ if (typeof hostId !== 'string')
482
+ return overlayId;
483
+ const host = metas.find((candidate) => candidate.id === hostId);
484
+ return host && host.type === 'offchart' && !host.hostPaneId ? hostId : overlayId;
485
+ }
486
+ function cssEscape(value) {
487
+ return typeof CSS !== 'undefined' && typeof CSS.escape === 'function' ? CSS.escape(value) : value;
488
+ }
489
+ function offchartLegendSection(pane, paneId) {
490
+ return pane.main.querySelector(`${SECTION_PREFIX}${cssEscape(paneId)}${SECTION_SUFFIX} ${LEGEND_SECTION_SELECTOR}`);
491
+ }
492
+ /** The chart cell's compare strip height (`--compare-strip-h`, set inline by ChartArea), 0 without one. */
493
+ function compareStripPx(root) {
494
+ const style = root.style;
495
+ let value = typeof style?.getPropertyValue === 'function' ? style.getPropertyValue(COMPARE_STRIP_PROPERTY) : '';
496
+ if (!value && typeof getComputedStyle === 'function')
497
+ value = getComputedStyle(root).getPropertyValue(COMPARE_STRIP_PROPERTY);
498
+ const px = Number.parseFloat(typeof value === 'string' ? value : '');
499
+ return Number.isFinite(px) && px > 0 ? px : 0;
500
+ }
501
+ /** The bottom (CSS px under `origin`'s top) of the strip `elements` draw, plus the gap; null when none has a size. */
502
+ function stripLineOf(origin, elements) {
503
+ const strip = measureRows(origin, elements);
504
+ return strip ? strip.bottom + LEGEND_SAFE_AREA_GAP_PX : null;
505
+ }
506
+ function measureChrome(tc, pane, legend) {
507
+ const strips = {};
508
+ // The price pane's buttons ride the cell's action bar (8 px down, 26 px tall, under the compare strip), which the
509
+ // clearance already spells out; a bar with a box (one button or more) gives the pane its strip line.
510
+ const mainTopRight = CHART_ACTION_BAR_CLEARANCE_PX + compareStripPx(pane.root);
511
+ const bar = pane.root.querySelector(ACTION_BAR_SELECTOR);
512
+ if (bar && stripLineOf(pane.plot, [bar]) !== null)
513
+ strips[MAIN_PANE_ID] = mainTopRight;
514
+ const panes = {
515
+ [MAIN_PANE_ID]: {
516
+ topLeft: legend ? legend.bottom + LEGEND_SAFE_AREA_GAP_PX : 0,
517
+ topRight: mainTopRight,
518
+ right: CHART_AXIS_TAG_CLEARANCE_PX,
519
+ },
520
+ };
521
+ for (const paneId of offchartPaneIds(tc)) {
522
+ const section = offchartLegendSection(pane, paneId);
523
+ if (!section)
524
+ continue;
525
+ const rows = measureRows(section, [...section.querySelectorAll(TITAN_ROW_SELECTOR)]);
526
+ // A lower pane measures its own buttons (titan's move buttons, top right of its legend section).
527
+ const controlsLine = stripLineOf(section, [...section.querySelectorAll(PANE_CONTROLS_SELECTOR)]);
528
+ const topRight = controlsLine === null ? PANE_EDGE_PX : Math.max(PANE_EDGE_PX, controlsLine);
529
+ if (controlsLine !== null)
530
+ strips[paneId] = topRight;
531
+ panes[paneId] = {
532
+ topLeft: rows ? rows.bottom + LEGEND_SAFE_AREA_GAP_PX : 0,
533
+ topRight,
534
+ right: CHART_AXIS_TAG_CLEARANCE_PX,
535
+ };
536
+ }
537
+ const hudTopLeft = legend
538
+ ? Math.round(pane.plot.getBoundingClientRect().top -
539
+ pane.root.getBoundingClientRect().top +
540
+ legend.bottom +
541
+ LEGEND_SAFE_AREA_GAP_PX)
542
+ : null;
543
+ return { measure: { panes, hudTopLeft }, strips };
544
+ }
545
+ /** Every measurable pane's plot box: the price pane's legend section and each offchart pane's own. */
546
+ function measurePaneBoxes(tc, pane) {
547
+ const boxes = {};
548
+ const main = paneBoxOf(pane.plot);
549
+ if (main)
550
+ boxes[MAIN_PANE_ID] = main;
551
+ for (const paneId of offchartPaneIds(tc)) {
552
+ const section = offchartLegendSection(pane, paneId);
553
+ const box = section && paneBoxOf(section);
554
+ if (box)
555
+ boxes[paneId] = box;
556
+ }
557
+ return boxes;
558
+ }
559
+ /**
560
+ * One fresh measure of the chart's pane chrome (DW06): the price pane's
561
+ * insets (`main`: legend bottom plus the gap, the action bar plus the compare
562
+ * strip, the axis tags), each offchart pane's insets (its legend row plus the
563
+ * gap, its move buttons plus the gap or else the plain edge, the axis tags)
564
+ * and the HUD layer's top-left inset. Without a pane DOM: no panes, no inset.
565
+ */
566
+ export function measureChartChromeSafeArea(tc) {
567
+ const pane = legendPaneOf(tc);
568
+ if (!pane)
569
+ return { panes: {}, hudTopLeft: null };
570
+ return measureChrome(tc, pane, measureLegendBox(pane)).measure;
571
+ }
572
+ /** Hands every measured pane's insets to the engine, and retracts the panes that are gone. */
573
+ function pushSafeAreas(tc, tracker) {
574
+ if (typeof tc.setCardSafeArea !== 'function')
575
+ return;
576
+ const panes = tracker.measure?.panes ?? {};
577
+ for (const paneId of tracker.pushed)
578
+ if (!(paneId in panes))
579
+ tc.setCardSafeArea(paneId, null);
580
+ tracker.pushed.clear();
581
+ for (const [paneId, insets] of Object.entries(panes)) {
582
+ tc.setCardSafeArea(paneId, { ...insets });
583
+ tracker.pushed.add(paneId);
584
+ }
585
+ }
586
+ function dropTracker(tc, tracker) {
587
+ tracker.stop();
588
+ if (trackers.get(tc) === tracker)
589
+ trackers.delete(tc);
590
+ if (typeof tc.setCardSafeArea === 'function')
591
+ for (const paneId of tracker.pushed)
592
+ tc.setCardSafeArea(paneId, null);
593
+ tracker.pushed.clear();
594
+ }
595
+ function idle(tracker) {
596
+ return tracker.overlays.size === 0 && tracker.listeners.size === 0;
597
+ }
598
+ /**
599
+ * Places every tracked set in `tc.metadata` order: the legend shift, the
600
+ * stacking shift over the earlier groups at the same corner, the safe-area
601
+ * insets and the strip drop of the plain top_right / top_center groups.
602
+ * Overlays the chart no longer holds are forgotten (`current`, the set
603
+ * being mounted, is kept whatever the metadata says). Every set other than
604
+ * `current` whose placement changed is re-mounted; `current`'s placed set is
605
+ * returned.
606
+ */
607
+ function placeAll(tc, tracker, current) {
608
+ const order = new Map();
609
+ metadataOf(tc).forEach((meta, index) => {
610
+ if (typeof meta.id === 'string' && !order.has(meta.id))
611
+ order.set(meta.id, index);
612
+ });
613
+ for (const overlayId of [...tracker.overlays.keys()])
614
+ if (overlayId !== current && !order.has(overlayId))
615
+ tracker.overlays.delete(overlayId);
616
+ const rank = (overlayId) => order.get(overlayId) ?? Number.MAX_SAFE_INTEGER;
617
+ const entries = [...tracker.overlays.entries()].sort(([a], [b]) => rank(a) - rank(b));
618
+ const occupied = new Map();
619
+ let result;
620
+ for (const [overlayId, entry] of entries) {
621
+ const stack = new Map();
622
+ if (overlayPaintFlagsOf(overlayId).stackHandles)
623
+ for (const corner of entry.groups.keys()) {
624
+ const below = occupied.get(corner);
625
+ if (below)
626
+ stack.set(corner, below);
627
+ }
628
+ const panes = tracker.measure?.panes;
629
+ const ownPaneId = hostPaneIdOf(tc, overlayId);
630
+ const placement = {
631
+ legendDy: legendShiftOf(entry.groups.get(LEGEND_ANCHOR), tracker.legend),
632
+ stack,
633
+ // Only a safeArea class reads the insets (rawShiftOf), so every set can carry them.
634
+ insets: panes ? { main: panes[MAIN_PANE_ID] ?? null, pane: panes[ownPaneId] ?? null } : NO_INSETS,
635
+ boxes: { main: tracker.paneBoxes[MAIN_PANE_ID] ?? null, pane: tracker.paneBoxes[ownPaneId] ?? null },
636
+ strips: { main: tracker.strips[MAIN_PANE_ID] ?? null, pane: tracker.strips[ownPaneId] ?? null },
637
+ };
638
+ for (const [corner, bounds] of entry.groups)
639
+ occupied.set(corner, (occupied.get(corner) ?? 0) + (bounds.bottom - bounds.top) + LEGEND_SAFE_AREA_GAP_PX);
640
+ const classes = shiftClasses(entry.drawings, placement);
641
+ const key = shiftsKey(classes);
642
+ if (overlayId === current) {
643
+ entry.key = key;
644
+ result = applyShifts(entry.drawings, classes);
645
+ continue;
646
+ }
647
+ if (key === entry.key)
648
+ continue;
649
+ entry.key = key;
650
+ tc.updateDrawings?.(overlayId, applyShifts(entry.drawings, classes));
651
+ }
652
+ return result;
653
+ }
654
+ function startTracker(tc) {
655
+ const tracker = {
656
+ legend: null,
657
+ measure: null,
658
+ paneBoxes: {},
659
+ strips: {},
660
+ overlays: new Map(),
661
+ listeners: new Set(),
662
+ pushed: new Set(),
663
+ stop: () => { },
664
+ };
665
+ const pane = legendPaneOf(tc);
666
+ // No DOM (a node spec, a chart with no container): nothing to clear, nothing moves.
667
+ if (!pane)
668
+ return tracker;
669
+ tracker.legend = measureLegendBox(pane);
670
+ const read = measureChrome(tc, pane, tracker.legend);
671
+ tracker.measure = read.measure;
672
+ tracker.strips = read.strips;
673
+ tracker.paneBoxes = measurePaneBoxes(tc, pane);
674
+ pushSafeAreas(tc, tracker);
675
+ if (typeof ResizeObserver !== 'function')
676
+ return tracker;
677
+ const refresh = () => {
678
+ if (pane.main.isConnected === false) {
679
+ dropTracker(tc, tracker);
680
+ return;
681
+ }
682
+ const legend = measureLegendBox(pane);
683
+ const { measure, strips } = measureChrome(tc, pane, legend);
684
+ const paneBoxes = measurePaneBoxes(tc, pane);
685
+ const legendMoved = !sameBox(legend, tracker.legend);
686
+ const chromeMoved = !sameMeasure(measure, tracker.measure);
687
+ const panesMoved = !samePaneBoxes(paneBoxes, tracker.paneBoxes);
688
+ const stripsMoved = !sameStrips(strips, tracker.strips);
689
+ if (!legendMoved && !chromeMoved && !panesMoved && !stripsMoved)
690
+ return;
691
+ tracker.legend = legend;
692
+ tracker.measure = measure;
693
+ tracker.paneBoxes = paneBoxes;
694
+ tracker.strips = strips;
695
+ if (chromeMoved)
696
+ pushSafeAreas(tc, tracker);
697
+ placeAll(tc, tracker);
698
+ if (idle(tracker)) {
699
+ dropTracker(tc, tracker);
700
+ return;
701
+ }
702
+ if (chromeMoved)
703
+ for (const listener of tracker.listeners)
704
+ listener(measure);
705
+ };
706
+ const sizes = new ResizeObserver(refresh);
707
+ let rows = null;
708
+ const watch = () => {
709
+ sizes.disconnect();
710
+ rows?.disconnect();
711
+ for (const row of legendRowsOf(pane))
712
+ sizes.observe(row);
713
+ // The plot's own size caps the shifts (item 28): watched with the rows.
714
+ sizes.observe(pane.plot);
715
+ // The action bar sizes with its buttons (none, one, several): the price pane has a strip line while it has a box.
716
+ const bar = pane.root.querySelector(ACTION_BAR_SELECTOR);
717
+ if (bar)
718
+ sizes.observe(bar);
719
+ // Rows come and go with the indicators (direct children of the legend section): re-watch, re-measure.
720
+ rows?.observe(pane.plot, { childList: true });
721
+ for (const paneId of offchartPaneIds(tc)) {
722
+ const section = offchartLegendSection(pane, paneId);
723
+ if (!section)
724
+ continue;
725
+ sizes.observe(section);
726
+ for (const row of section.querySelectorAll(TITAN_ROW_SELECTOR))
727
+ sizes.observe(row);
728
+ // The move buttons come and go with the pane order (direct children of the section, caught below).
729
+ rows?.observe(section, { childList: true });
730
+ }
731
+ // Panes come and go (sections are direct children of the sections pane); the compare strip rides the cell's style.
732
+ const sectionsPane = pane.plot.parentElement?.parentElement;
733
+ if (sectionsPane)
734
+ rows?.observe(sectionsPane, { childList: true });
735
+ if (pane.root !== pane.main)
736
+ rows?.observe(pane.root, { attributes: true, attributeFilter: ['style'] });
737
+ };
738
+ if (typeof MutationObserver === 'function')
739
+ rows = new MutationObserver(() => {
740
+ watch();
741
+ refresh();
742
+ });
743
+ watch();
744
+ tracker.stop = () => {
745
+ sizes.disconnect();
746
+ rows?.disconnect();
747
+ };
748
+ return tracker;
749
+ }
750
+ function trackerFor(tc) {
751
+ let tracker = trackers.get(tc);
752
+ if (!tracker) {
753
+ tracker = startTracker(tc);
754
+ trackers.set(tc, tracker);
755
+ }
756
+ return tracker;
757
+ }
758
+ /**
759
+ * Follows the chart's pane chrome: `listener` gets every new measure (the
760
+ * legend grew or shrank, a pane joined or left, the compare strip came or
761
+ * went) until the returned function is called. The tracker stays alive for a
762
+ * listener even with no drawings mounted.
763
+ */
764
+ export function watchChartChromeSafeArea(tc, listener) {
765
+ const tracker = trackerFor(tc);
766
+ tracker.listeners.add(listener);
767
+ return () => {
768
+ tracker.listeners.delete(listener);
769
+ if (trackers.get(tc) === tracker && idle(tracker))
770
+ dropTracker(tc, tracker);
771
+ };
772
+ }
773
+ /** A pane-pixel handle at an anchor the pane's buttons sit over (top_right, top_center), in any pane. */
774
+ function meetsButtonStrip(drawing) {
775
+ const member = memberOf(drawing);
776
+ return member !== null && ACTION_BAR_ANCHORS.has(member.anchor);
777
+ }
778
+ /**
779
+ * The set to hand `updateDrawings`: the `top_left` group moved under the
780
+ * chart's legend when they meet, a stacking overlay's corner groups moved
781
+ * clear of the earlier groups at the same corner, `safeArea` handles stepped
782
+ * past the axis tags and the action bar, and plain `top_right` / `top_center`
783
+ * groups dropped under their pane's button strip. Every push of an overlay's
784
+ * drawings goes through here (a full apply, a replay-lane reveal, a clear), so
785
+ * the tracker always holds the mounted set of each overlay that owns a corner
786
+ * group, a handle under a button strip or a `safeArea` record (a card's
787
+ * insets ride the engine door) and can re-mount it when the chrome or an
788
+ * earlier group changes; a set without one (the clear included) drops the
789
+ * overlay from the tracker, and the overlays stacked on it follow.
790
+ */
791
+ export function mountWithLegendSafeArea(tc, overlayId, drawings) {
792
+ const groups = cornerGroups(drawings);
793
+ if (groups.size === 0 && !drawings.some(safeAreaOf) && !drawings.some(meetsButtonStrip)) {
794
+ const tracker = trackers.get(tc);
795
+ if (tracker && tracker.overlays.delete(overlayId)) {
796
+ placeAll(tc, tracker);
797
+ if (idle(tracker))
798
+ dropTracker(tc, tracker);
799
+ }
800
+ return drawings;
801
+ }
802
+ const tracker = trackerFor(tc);
803
+ tracker.overlays.set(overlayId, { drawings, groups, key: '' });
804
+ return placeAll(tc, tracker, overlayId) ?? drawings;
805
+ }