@vitavision/stage2d 0.7.2 → 0.8.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.
@@ -11,11 +11,11 @@
11
11
  * until aimed at.
12
12
  */
13
13
 
14
- import { Maximize2, Minus, Plus, Scan } from "lucide-react";
14
+ import { Layers, Maximize2, Minus, Plus, Scan } from "lucide-react";
15
15
  import { useEffect, useRef, useState } from "react";
16
16
  import type { ComponentPropsWithRef, ReactNode } from "react";
17
17
 
18
- import { cn, focusRing } from "@vitavision/ui";
18
+ import { DropdownMenu, Kbd, MenuCheckboxItem, MenuLabel, Tooltip, cn, focusRing } from "@vitavision/ui";
19
19
  import { useStage } from "./ImageStage";
20
20
  import { MAX_SCALE, formatScale, scaleRange, steppedScale } from "./view";
21
21
 
@@ -37,6 +37,9 @@ export interface StageToolbarProps {
37
37
  *
38
38
  * The bar carries `data-fit` while the view is fit; the percentage button carries
39
39
  * `data-state="open"` or `"closed"` for its menu.
40
+ *
41
+ * On a canvas narrower than 30rem, zoom out, zoom in and 100% collapse: the percentage menu
42
+ * and the `+` / `-` / `1` keys still reach them, so the app's own groups keep their room.
40
43
  */
41
44
  export function StageToolbar({ children, className }: StageToolbarProps) {
42
45
  const stage = useStage();
@@ -66,7 +69,15 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
66
69
  onPointerDown={(event) => event.stopPropagation()}
67
70
  onDoubleClick={(event) => event.stopPropagation()}
68
71
  >
69
- <StageButton label="Zoom out" onClick={() => step(-1)} disabled={stage.view.scale <= min + 1e-9}>
72
+ {/* Priority collapse on a narrow canvas: zoom out/in and 100% go first (the percentage
73
+ menu and the + / - / 1 keys still reach them), so the app's own groups keep their
74
+ room. */}
75
+ <StageButton
76
+ label="Zoom out"
77
+ onClick={() => step(-1)}
78
+ disabled={stage.view.scale <= min + 1e-9}
79
+ className="@max-[30rem]/stage:hidden"
80
+ >
70
81
  <Minus className="size-4" aria-hidden />
71
82
  </StageButton>
72
83
 
@@ -118,6 +129,7 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
118
129
  label="Zoom in"
119
130
  onClick={() => step(1)}
120
131
  disabled={stage.view.scale >= Math.min(max, MAX_SCALE) - 1e-9}
132
+ className="@max-[30rem]/stage:hidden"
121
133
  >
122
134
  <Plus className="size-4" aria-hidden />
123
135
  </StageButton>
@@ -129,6 +141,7 @@ export function StageToolbar({ children, className }: StageToolbarProps) {
129
141
  </StageButton>
130
142
  <StageButton
131
143
  label="Actual size (100%)"
144
+ className="@max-[30rem]/stage:hidden"
132
145
  onClick={() => stage.zoomTo(1)}
133
146
  pressed={!stage.isFit && Math.abs(stage.view.scale - 1) < 1e-6}
134
147
  >
@@ -152,6 +165,14 @@ export interface StageButtonProps
152
165
  label: string;
153
166
  /** A toggle's state; omit for a plain action button. */
154
167
  pressed?: boolean;
168
+ /**
169
+ * A longer explanation for the tooltip, after the label. Giving `hint` or `shortcut`
170
+ * switches the native `title` for the ui `Tooltip`, which needs a `TooltipProvider` above
171
+ * the stage.
172
+ */
173
+ hint?: ReactNode;
174
+ /** The key that does the same, shown in the tooltip as a `Kbd`. Display only. */
175
+ shortcut?: string | undefined;
155
176
  /** The icon (mark it `aria-hidden`). */
156
177
  children: ReactNode;
157
178
  }
@@ -160,14 +181,19 @@ export interface StageButtonProps
160
181
  * The bar's own button shape, exported so app groups match it exactly. An icon button
161
182
  * named by `label`; a toggle when `pressed` is given, exposed as `aria-pressed` and
162
183
  * `data-state="on"` or `"off"`. Other button props (and `ref`) are passed through.
184
+ *
185
+ * Its tooltip is the native `title` (the label), or with `hint` / `shortcut` the ui
186
+ * `Tooltip`: the label, the hint, and the shortcut as a key cap.
163
187
  */
164
- export function StageButton({ label, pressed, className, children, ...props }: StageButtonProps) {
165
- return (
188
+ export function StageButton({ label, pressed, hint, shortcut, className, children, ...props }: StageButtonProps) {
189
+ const rich = hint !== undefined || shortcut !== undefined;
190
+ const button = (
166
191
  <button
167
192
  {...props}
168
193
  type="button"
169
- title={label}
194
+ title={rich ? undefined : label}
170
195
  aria-label={label}
196
+ aria-keyshortcuts={shortcut}
171
197
  aria-pressed={pressed}
172
198
  data-state={pressed === undefined ? undefined : pressed ? "on" : "off"}
173
199
  className={cn(
@@ -181,6 +207,79 @@ export function StageButton({ label, pressed, className, children, ...props }: S
181
207
  {children}
182
208
  </button>
183
209
  );
210
+ if (!rich) return button;
211
+ return (
212
+ <Tooltip
213
+ content={
214
+ <span className="flex items-center gap-2">
215
+ <span>
216
+ <span className="font-medium">{label}</span>
217
+ {hint !== undefined && <span className="block text-fg-muted">{hint}</span>}
218
+ </span>
219
+ {shortcut && <Kbd>{shortcut}</Kbd>}
220
+ </span>
221
+ }
222
+ >
223
+ {button}
224
+ </Tooltip>
225
+ );
226
+ }
227
+
228
+ /** One layer in a `StageLayersMenu`. */
229
+ export interface StageLayer {
230
+ /** Its identity, passed back to `onVisibleChange`. */
231
+ id: string;
232
+ /** Its name in the menu. */
233
+ label: string;
234
+ /** Whether it is shown. */
235
+ visible: boolean;
236
+ /** Shown but not switchable, e.g. a layer that has nothing to draw yet. */
237
+ disabled?: boolean | undefined;
238
+ /** A key that toggles it, shown as a `Kbd`. Display only: binding it is the app's. */
239
+ shortcut?: string | undefined;
240
+ }
241
+
242
+ /** Props of `StageLayersMenu`. */
243
+ export interface StageLayersMenuProps {
244
+ /** The layers, in menu order. */
245
+ layers: readonly StageLayer[];
246
+ /** Called with a layer's id and its new visibility. */
247
+ onVisibleChange: (id: string, visible: boolean) => void;
248
+ /** The trigger's accessible name and the menu's heading. Defaults to "Layers". */
249
+ label?: string | undefined;
250
+ }
251
+
252
+ /**
253
+ * A `StageButton`-shaped trigger opening a menu of layer toggles, for `StageToolbar`'s
254
+ * children. Each layer is a `menuitemcheckbox`; toggling one keeps the menu open for the
255
+ * next. The trigger shows as pressed while any layer is hidden, so a canvas missing a layer
256
+ * says so before the menu is opened.
257
+ */
258
+ export function StageLayersMenu({ layers, onVisibleChange, label = "Layers" }: StageLayersMenuProps) {
259
+ const hidden = layers.some((layer) => !layer.visible && !layer.disabled);
260
+ return (
261
+ <DropdownMenu
262
+ side="top"
263
+ trigger={
264
+ <StageButton label={label} pressed={hidden}>
265
+ <Layers className="size-4" aria-hidden />
266
+ </StageButton>
267
+ }
268
+ >
269
+ <MenuLabel>{label}</MenuLabel>
270
+ {layers.map((layer) => (
271
+ <MenuCheckboxItem
272
+ key={layer.id}
273
+ checked={layer.visible}
274
+ onCheckedChange={(visible) => onVisibleChange(layer.id, visible)}
275
+ disabled={layer.disabled}
276
+ shortcut={layer.shortcut}
277
+ >
278
+ {layer.label}
279
+ </MenuCheckboxItem>
280
+ ))}
281
+ </DropdownMenu>
282
+ );
184
283
  }
185
284
 
186
285
  /** Props of `StageToolbarDivider`. */
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Screen-constant geometry for a layer inside an `ImageStage`.
3
+ */
4
+
5
+ import { useStage } from "./ImageStage";
6
+
7
+ /**
8
+ * A function from screen pixels to image pixels at the current zoom. A stroke, handle or
9
+ * label drawn `px(1.5)` wide stays 1.5 screen pixels at every zoom (the overlay grammar's
10
+ * rule).
11
+ *
12
+ * @returns `px(css)`.
13
+ */
14
+ export function useScreenPx(): (css: number) => number {
15
+ return useStage().imageLength;
16
+ }
@@ -300,10 +300,15 @@ export function steppedScale(scale: number, direction: 1 | -1, min: number, max:
300
300
  const candidates = ladder.length > 0 ? ladder : [clamp(scale, min, max)];
301
301
  if (direction > 0) {
302
302
  const next = candidates.find((s) => s > scale * (1 + SCALE_EPSILON));
303
- return next ?? Math.min(max, Math.max(scale, candidates[candidates.length - 1]!));
303
+ // Past the last step but short of the ceiling: the ceiling is the step, or "zoom in"
304
+ // would stay enabled and do nothing.
305
+ if (next === undefined) return scale < max * (1 - SCALE_EPSILON) ? max : Math.min(max, scale);
306
+ return next;
304
307
  }
305
308
  const previous = [...candidates].reverse().find((s) => s < scale * (1 - SCALE_EPSILON));
306
- return previous ?? Math.max(min, Math.min(scale, candidates[0]!));
309
+ // Below the first step but above the floor: likewise, the floor is the step.
310
+ if (previous === undefined) return scale > min * (1 + SCALE_EPSILON) ? min : Math.max(min, scale);
311
+ return previous;
307
312
  }
308
313
 
309
314
  /** `0.5` → `"50%"`, `1` → `"100%"`, `0.0833` → `"8%"`. */
package/src/index.ts CHANGED
@@ -8,14 +8,28 @@ export {
8
8
  useStage,
9
9
  type ImageStageProps,
10
10
  type StageContext,
11
+ type StageHandle,
11
12
  } from "./components/stage/ImageStage";
12
13
 
14
+ export { ImageLayer, type ImageLayerProps, type ImageTier } from "./components/stage/ImageLayer";
15
+
16
+ export {
17
+ StageSurface,
18
+ useStageDrag,
19
+ type StageDrag,
20
+ type StagePress,
21
+ type StageSurfaceProps,
22
+ } from "./components/stage/StageSurface";
23
+
13
24
  export {
14
25
  StageButton,
26
+ StageLayersMenu,
15
27
  StageReadout,
16
28
  StageToolbar,
17
29
  StageToolbarDivider,
18
30
  type StageButtonProps,
31
+ type StageLayer,
32
+ type StageLayersMenuProps,
19
33
  type StageReadoutProps,
20
34
  type StageToolbarDividerProps,
21
35
  type StageToolbarProps,
@@ -79,10 +93,51 @@ export {
79
93
  type MeasureOverlayProps,
80
94
  type MeasurePrimitive,
81
95
  type PointPrimitive,
96
+ type PolylinePrimitive,
97
+ type PrimitiveCommon,
82
98
  type SegmentPrimitive,
83
99
  } from "./components/MeasureOverlay";
84
100
 
101
+ export {
102
+ OVERLAY_ROLES,
103
+ OVERLAY_STATE_OPACITY,
104
+ OVERLAY_STATE_WIDTH,
105
+ overlayRole,
106
+ type OverlayRole,
107
+ type OverlayState,
108
+ } from "./components/overlayRole";
109
+ export { useScreenPx } from "./components/stage/useScreenPx";
110
+
85
111
  export { ContourEditor, nearestContourSegment, type ContourEditorProps } from "./components/ContourEditor";
112
+ export { RectRoiEditor, type RectRoiEditorProps } from "./components/RectRoiEditor";
113
+ export {
114
+ PolylineSet,
115
+ type PolylineSelectMode,
116
+ type PolylineSetItem,
117
+ type PolylineSetProps,
118
+ } from "./components/PolylineSet";
119
+ export {
120
+ buildPolylineIndex,
121
+ nearestPolyline,
122
+ polylineBounds,
123
+ polylinePath,
124
+ polylinesInRect,
125
+ type Polyline,
126
+ type PolylineHit,
127
+ type PolylineId,
128
+ type PolylineIndex,
129
+ } from "./components/polylineIndex";
130
+ export {
131
+ ROI_HANDLES,
132
+ ROI_HANDLE_CURSOR,
133
+ clampRect,
134
+ moveRect,
135
+ rectFromCorners,
136
+ resizeRect,
137
+ roiHandlePoint,
138
+ sameRect,
139
+ type RoiHandle,
140
+ } from "./components/roiEdit";
86
141
  export { MaskEditor, paintMask, type MaskEditorProps } from "./components/MaskEditor";
87
142
 
88
143
  export {
package/src/styles.css CHANGED
@@ -1,7 +1,31 @@
1
1
  /*
2
- * Nothing to style: this file exists so Tailwind v4 finds this package's class names.
3
- * `@source` resolves against the stylesheet that declares it, and this file is exported from
4
- * `src/` (which ships), so it scans the package's sources. Import it after
2
+ * Tailwind v4 finds this package's class names through this file: `@source` resolves against
3
+ * the stylesheet that declares it, and this file ships from `src/`. Import it after
5
4
  * `@vitavision/ui/styles.css`.
6
5
  */
7
6
  @source "./";
7
+
8
+ /*
9
+ * Overlay role colours (docs/visual-language.md §5). Overlays sit on the image, not on the
10
+ * chrome, so there is one set for both themes. The values come from the OKLCH search in
11
+ * tools/visual-language/colours.py: feature and model stay ≥ 7.3 OKLab ΔE×100 apart from each
12
+ * other, from selection, from the label white and from every verdict colour, under typical
13
+ * vision and each simulated dichromacy.
14
+ */
15
+ :root {
16
+ --stage-feature: #ed9d43;
17
+ --stage-model: #9dbdff;
18
+ --stage-structure: rgb(255 255 255 / 0.55);
19
+ --stage-selection: #2db2d4;
20
+ --stage-label: #e8ebed;
21
+ --stage-halo: rgb(8 10 11 / 0.72);
22
+ }
23
+
24
+ @theme inline {
25
+ --color-stage-feature: var(--stage-feature);
26
+ --color-stage-model: var(--stage-model);
27
+ --color-stage-structure: var(--stage-structure);
28
+ --color-stage-selection: var(--stage-selection);
29
+ --color-stage-label: var(--stage-label);
30
+ --color-stage-halo: var(--stage-halo);
31
+ }