@opencode/ui 0.0.0-dev-724 → 0.0.0-dev-750

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.
@@ -0,0 +1,135 @@
1
+ import type { FlightFrame } from "./image-flight";
2
+ /**
3
+ * How long the preview waits for its image to decode before it opens anyway. Decoding first keeps the flight's first
4
+ * frames smooth; a decode that hangs, such as the stand-in's on a slow second fetch, must not keep the preview hidden.
5
+ */
6
+ export declare const DECODE_TIMEOUT_MS = 1000;
7
+ /** What `data-stage` tells the stylesheet: hidden, a stand-in flying, the preview at rest, or fading out after a close. */
8
+ export type PreviewFlightStage = "pending" | "flying" | "rest" | "closing";
9
+ /**
10
+ * The preview's life from load to close. `attempt` counts image loads, so a decode that settles after the image
11
+ * changed or loaded again is ignored. `borrowed` says the preview holds the page's video, which it hands back once the
12
+ * close lands. `originHidden` says the origin on the page is hidden because the image flew out of it.
13
+ *
14
+ * A close flies home when the origin still shows, then fades the stand-in over the page's image (`veil`). It only
15
+ * fades when the origin is gone, out of sight, or motion is reduced (`fade`).
16
+ */
17
+ export type PreviewFlightState = {
18
+ readonly status: "pending";
19
+ readonly attempt: number;
20
+ } | {
21
+ readonly status: "decoding";
22
+ readonly attempt: number;
23
+ } | {
24
+ readonly status: "opening";
25
+ readonly mode: "flight" | "fade";
26
+ readonly borrowed: boolean;
27
+ } | {
28
+ readonly status: "open";
29
+ readonly originHidden: boolean;
30
+ readonly borrowed: boolean;
31
+ } | {
32
+ readonly status: "closing";
33
+ readonly mode: "flight" | "veil" | "fade";
34
+ readonly borrowed: boolean;
35
+ } | {
36
+ readonly status: "closed";
37
+ } | {
38
+ readonly status: "disposed";
39
+ };
40
+ /** `origin` is the origin's frame to fly from or to, or nothing when it is gone, out of sight, or motion is reduced. */
41
+ export type PreviewFlightEvent = {
42
+ readonly type: "src.changed";
43
+ } | {
44
+ readonly type: "image.loaded";
45
+ } | {
46
+ readonly type: "image.failed";
47
+ } | {
48
+ readonly type: "decode.ready";
49
+ readonly attempt: number;
50
+ readonly origin: FlightFrame | undefined;
51
+ } | {
52
+ readonly type: "video.ready";
53
+ readonly origin: FlightFrame | undefined;
54
+ } | {
55
+ readonly type: "close.started";
56
+ readonly origin: FlightFrame | undefined;
57
+ }
58
+ /** The flight landed with the backdrop at rest, or, without a flight, the backdrop and the veil came to rest. */
59
+ | {
60
+ readonly type: "motion.settled";
61
+ } | {
62
+ readonly type: "disposed";
63
+ };
64
+ export type PreviewFlightEffect = {
65
+ readonly type: "decode.start";
66
+ readonly attempt: number;
67
+ } | {
68
+ readonly type: "decode.cancel";
69
+ } | {
70
+ readonly type: "video.borrow";
71
+ } | {
72
+ readonly type: "video.return";
73
+ } | {
74
+ readonly type: "origin.hide";
75
+ } | {
76
+ readonly type: "origin.show";
77
+ } | {
78
+ readonly type: "open.flight";
79
+ readonly from: FlightFrame;
80
+ } | {
81
+ readonly type: "open.fade";
82
+ }
83
+ /** `turn` turns the opening flight around where it is, keeping its velocity, instead of starting from rest. */
84
+ | {
85
+ readonly type: "close.flight";
86
+ readonly to: FlightFrame;
87
+ readonly turn: boolean;
88
+ } | {
89
+ readonly type: "close.veil";
90
+ } | {
91
+ readonly type: "close.fade";
92
+ }
93
+ /** A video eases back to its whole frame on the way home; an image stops where it is and flies as it is shown. */
94
+ | {
95
+ readonly type: "zoom.fit";
96
+ } | {
97
+ readonly type: "zoom.settle";
98
+ } | {
99
+ readonly type: "body.closing.acquire";
100
+ } | {
101
+ readonly type: "body.closing.release";
102
+ } | {
103
+ readonly type: "loop.stop";
104
+ };
105
+ export type PreviewFlightTransition = {
106
+ readonly state: PreviewFlightState;
107
+ readonly effects: readonly PreviewFlightEffect[];
108
+ };
109
+ export declare const INITIAL_PREVIEW_FLIGHT_STATE: PreviewFlightState;
110
+ export declare function transitionPreviewFlight(state: PreviewFlightState, event: PreviewFlightEvent): PreviewFlightTransition;
111
+ export declare function selectFlightStage(state: PreviewFlightState): PreviewFlightStage;
112
+ /** Whether the preview takes zoom, pan and keyboard input: from the moment it reveals until a close starts. */
113
+ export declare function selectFlightInteractive(state: PreviewFlightState): state is {
114
+ readonly status: "opening";
115
+ readonly mode: "flight" | "fade";
116
+ readonly borrowed: boolean;
117
+ } | {
118
+ readonly status: "open";
119
+ readonly originHidden: boolean;
120
+ readonly borrowed: boolean;
121
+ };
122
+ /** Whether the preview, its controls and a borrowed video fade with the backdrop rather than flying. */
123
+ export declare function selectFlightFading(state: PreviewFlightState): boolean;
124
+ /** Whether a borrowed video sits fitted in the preview, not flying, so a resize moves it with the preview. */
125
+ export declare function selectVideoAtRest(state: PreviewFlightState): boolean;
126
+ /** Whether the preview still waits to reveal, so it is worth measuring where it would fly from. */
127
+ export declare function selectAwaitingReveal(state: PreviewFlightState): state is {
128
+ readonly status: "pending";
129
+ readonly attempt: number;
130
+ } | {
131
+ readonly status: "decoding";
132
+ readonly attempt: number;
133
+ };
134
+ /** Whether a decode of image load `attempt` is still the one the preview waits for. */
135
+ export declare function selectDecodeCurrent(state: PreviewFlightState, attempt: number): boolean;
@@ -0,0 +1,95 @@
1
+ /** The element a pan captures the pointer on: the preview's body for an image, the borrowed video for a video. */
2
+ export type PanTarget = "viewport" | "video";
3
+ /**
4
+ * The press under way. A `backdrop-press` began on the backdrop around the image and stayed put, so the click that ends
5
+ * it closes the preview. A pan captures the pointer, so its click lands on the backdrop wherever it began: `onBackdrop`
6
+ * says whether it began there, and `moved` whether it has moved, so the click that ends a moved pan neither closes the
7
+ * preview nor plays the video.
8
+ */
9
+ export type PreviewZoomGesture = {
10
+ readonly type: "idle";
11
+ } | {
12
+ readonly type: "backdrop-press";
13
+ } | {
14
+ readonly type: "panning";
15
+ readonly pointerId: number;
16
+ readonly target: PanTarget;
17
+ readonly onBackdrop: boolean;
18
+ readonly moved: boolean;
19
+ };
20
+ /**
21
+ * `gliding` is an eased zoom under way on the frame loop. Scale and offsets live on springs in the driver, not here: a
22
+ * frame of a glide or a pan writes no state. A press does not stop a glide, so a third click of a double-click zoom
23
+ * leaves it running; the hand moving does.
24
+ */
25
+ export type PreviewZoomState = {
26
+ readonly status: "still" | "gliding";
27
+ readonly gesture: PreviewZoomGesture;
28
+ } | {
29
+ readonly status: "disposed";
30
+ };
31
+ /** `zoomed` is whether the image shows above fit once the event's change applies. */
32
+ export type PreviewZoomEvent = {
33
+ readonly type: "pointer.down";
34
+ readonly pointerId: number;
35
+ readonly button: number;
36
+ /** `content` is anywhere in the preview but its body, such as the band around it or the controls. */
37
+ readonly target: PanTarget | "content";
38
+ readonly onBackdrop: boolean;
39
+ readonly zoomed: boolean;
40
+ } | {
41
+ readonly type: "pointer.moved";
42
+ readonly pointerId: number;
43
+ } | {
44
+ readonly type: "pointer.up";
45
+ readonly pointerId: number;
46
+ }
47
+ /** `pointercancel`, `lostpointercapture`, or a move with the primary button already up: the pan ends unreleased. */
48
+ | {
49
+ readonly type: "pointer.cancel";
50
+ readonly pointerId: number;
51
+ } | {
52
+ readonly type: "backdrop.click";
53
+ readonly onBackdrop: boolean;
54
+ }
55
+ /** Pinch, wheel, drag or a resize set the view directly. */
56
+ | {
57
+ readonly type: "follow";
58
+ readonly zoomed: boolean;
59
+ } | {
60
+ readonly type: "glide.start";
61
+ } | {
62
+ readonly type: "glide.settled";
63
+ readonly zoomed: boolean;
64
+ } | {
65
+ readonly type: "disposed";
66
+ };
67
+ export type PreviewZoomEffect = {
68
+ readonly type: "pointer.capture";
69
+ readonly target: PanTarget;
70
+ readonly pointerId: number;
71
+ } | {
72
+ readonly type: "pointer.release";
73
+ readonly target: PanTarget;
74
+ readonly pointerId: number;
75
+ } | {
76
+ readonly type: "glide.loop.start";
77
+ } | {
78
+ readonly type: "glide.loop.stop";
79
+ }
80
+ /** Swallows the click that ends a moved pan over the video, so it does not play or pause it. */
81
+ | {
82
+ readonly type: "video.suppress-click";
83
+ } | {
84
+ readonly type: "dialog.close";
85
+ };
86
+ export type PreviewZoomTransition = {
87
+ readonly state: PreviewZoomState;
88
+ readonly effects: readonly PreviewZoomEffect[];
89
+ };
90
+ export declare const INITIAL_PREVIEW_ZOOM_STATE: PreviewZoomState;
91
+ export declare function transitionPreviewZoom(state: PreviewZoomState, event: PreviewZoomEvent): PreviewZoomTransition;
92
+ /** Whether a pan holds the pointer, which shows the grabbing cursor. */
93
+ export declare function selectDragging(state: PreviewZoomState): boolean;
94
+ /** Whether `pointerId` is the pointer the pan under way follows. */
95
+ export declare function selectPanPointer(state: PreviewZoomState, pointerId: number): boolean;
@@ -32,10 +32,11 @@ export declare function clampView(view: ZoomView, bounds: ZoomBounds): ZoomView;
32
32
  /** The double-click target: 100% for a downscaled image, or twice the fitted size when 100% is barely larger. */
33
33
  export declare function detailScale(fitRatio: number): number;
34
34
  /**
35
- * The `object-view-box` that shows `view` of media fitted at `size`, for media that must zoom inside its own box, such
36
- * as a video under its controls. The inset is in the media's natural pixels.
35
+ * The `object-view-box` that shows the view at `scale`, `x` and `y` of media fitted at `size`, for media that must zoom
36
+ * inside its own box, such as a video under its controls. The inset is in the media's natural pixels. It takes the view
37
+ * as numbers, so painting a frame of a glide allocates nothing but the string.
37
38
  */
38
- export declare function viewBox(view: ZoomView, size: {
39
+ export declare function viewBox(scale: number, x: number, y: number, size: {
39
40
  width: number;
40
41
  height: number;
41
42
  }, natural: {
@@ -44,3 +45,5 @@ export declare function viewBox(view: ZoomView, size: {
44
45
  }): string;
45
46
  /** The zoom factor for one wheel event, bounded so a coarse mouse-wheel notch is a step rather than a jump. */
46
47
  export declare function wheelZoomFactor(deltaY: number): number;
48
+ /** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
49
+ export declare function clampAxis(offset: number, size: number, viewport: number): number;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Every tooltip trigger runs this machine, so it never allocates: each reachable state, event and transition is a
3
+ * frozen module singleton, and an event that changes nothing returns the state it was given.
4
+ */
5
+ export type TooltipState =
6
+ /** Kobalte is not mounted yet. Most triggers are never hovered, so it mounts on the first hover or focus. */
7
+ {
8
+ readonly status: "unmounted";
9
+ readonly disabled: boolean;
10
+ } | {
11
+ readonly status: "idle";
12
+ readonly hovered: boolean;
13
+ readonly disabled: boolean;
14
+ }
15
+ /** `pressing` lasts from a press on the trigger until that press ends, so its click keeps the tooltip open. */
16
+ | {
17
+ readonly status: "open";
18
+ readonly hovered: boolean;
19
+ readonly pressing: boolean;
20
+ }
21
+ /**
22
+ * A popup the trigger opened (a menu, a popover) hides the tooltip. While it is open, a modal layer turns pointer
23
+ * events off for the page, so the trigger's own enter and leave events lie and `hovered` follows the pointer's
24
+ * position instead. Once it closes over a hovered trigger, the tooltip stays hidden until the pointer moves off the
25
+ * trigger or the trigger itself takes focus.
26
+ */
27
+ | {
28
+ readonly status: "suppressed";
29
+ readonly popup: "open";
30
+ readonly hovered: boolean;
31
+ readonly disabled: boolean;
32
+ } | {
33
+ readonly status: "suppressed";
34
+ readonly popup: "closed";
35
+ readonly disabled: boolean;
36
+ }
37
+ /** The caller sets `forceOpen`. `expanded` tracks the popup so releasing control can suppress the tooltip. */
38
+ | {
39
+ readonly status: "controlled";
40
+ readonly open: boolean;
41
+ readonly hovered: boolean;
42
+ readonly expanded: boolean;
43
+ readonly disabled: boolean;
44
+ } | {
45
+ readonly status: "disposed";
46
+ };
47
+ export type TooltipEvent = {
48
+ readonly type: "props.synced";
49
+ readonly forceOpen: boolean | undefined;
50
+ readonly disabled: boolean;
51
+ }
52
+ /** The pointer entered or left the trigger. Touch never hovers. */
53
+ | {
54
+ readonly type: "pointer.entered";
55
+ } | {
56
+ readonly type: "pointer.left";
57
+ }
58
+ /** Where a pointer move landed while a popup suppresses the tooltip, measured against the trigger's box. */
59
+ | {
60
+ readonly type: "pointer.moved";
61
+ readonly inside: boolean;
62
+ }
63
+ /** The trigger element itself took focus. */
64
+ | {
65
+ readonly type: "focused";
66
+ } | {
67
+ readonly type: "trigger.pressed";
68
+ }
69
+ /** The press ended, after the click it caused, if any. */
70
+ | {
71
+ readonly type: "press.ended";
72
+ }
73
+ /** The microtask after the pointer left, once Kobalte has seen the tooltip open and started its skip-delay window. */
74
+ | {
75
+ readonly type: "leave.settled";
76
+ } | {
77
+ readonly type: "popup.changed";
78
+ readonly expanded: boolean;
79
+ } | {
80
+ readonly type: "kobalte.changed";
81
+ readonly open: boolean;
82
+ } | {
83
+ readonly type: "disposed";
84
+ };
85
+ /** `replay` forwards the mounting hover or focus to Kobalte, which attached its listeners too late to receive it. */
86
+ export type TooltipEffect = "replay" | "schedule-leave";
87
+ export type TooltipTransition = {
88
+ readonly state: TooltipState;
89
+ readonly effects: readonly TooltipEffect[];
90
+ };
91
+ export declare const POINTER_ENTERED: TooltipEvent;
92
+ export declare const POINTER_LEFT: TooltipEvent;
93
+ export declare const POINTER_INSIDE: TooltipEvent;
94
+ export declare const POINTER_OUTSIDE: TooltipEvent;
95
+ export declare const FOCUSED: TooltipEvent;
96
+ export declare const TRIGGER_PRESSED: TooltipEvent;
97
+ export declare const PRESS_ENDED: TooltipEvent;
98
+ export declare const LEAVE_SETTLED: TooltipEvent;
99
+ export declare const POPUP_EXPANDED: TooltipEvent;
100
+ export declare const POPUP_COLLAPSED: TooltipEvent;
101
+ export declare const KOBALTE_OPENED: TooltipEvent;
102
+ export declare const KOBALTE_CLOSED: TooltipEvent;
103
+ export declare const DISPOSED_EVENT: TooltipEvent;
104
+ export declare function syncPropsEvent(forceOpen: boolean | undefined, disabled: boolean): TooltipEvent;
105
+ export declare function createTooltipState(forceOpen: boolean | undefined, disabled: boolean): TooltipState;
106
+ export declare function transitionTooltip(state: TooltipState, event: TooltipEvent): TooltipTransition;
107
+ /** Kobalte is mounted: the trigger owns its listeners and the popup observer runs. */
108
+ export declare function selectTooltipMounted(state: TooltipState): state is {
109
+ readonly status: "idle";
110
+ readonly hovered: boolean;
111
+ readonly disabled: boolean;
112
+ } | {
113
+ readonly status: "open";
114
+ readonly hovered: boolean;
115
+ readonly pressing: boolean;
116
+ } | {
117
+ readonly status: "suppressed";
118
+ readonly popup: "open";
119
+ readonly hovered: boolean;
120
+ readonly disabled: boolean;
121
+ } | {
122
+ readonly status: "suppressed";
123
+ readonly popup: "closed";
124
+ readonly disabled: boolean;
125
+ } | {
126
+ readonly status: "controlled";
127
+ readonly open: boolean;
128
+ readonly hovered: boolean;
129
+ readonly expanded: boolean;
130
+ readonly disabled: boolean;
131
+ };
132
+ export declare function selectTooltipOpen(state: TooltipState): boolean;
133
+ export declare function selectTooltipForceOpen(state: TooltipState): boolean | undefined;
134
+ /** A press is in progress, so the driver listens for its end. */
135
+ export declare function selectTooltipPressing(state: TooltipState): boolean;
136
+ /** A popup suppresses the tooltip, so the driver reports where the pointer moves. */
137
+ export declare function selectTooltipTracking(state: TooltipState): state is {
138
+ readonly status: "suppressed";
139
+ readonly popup: "open";
140
+ readonly hovered: boolean;
141
+ readonly disabled: boolean;
142
+ } | {
143
+ readonly status: "suppressed";
144
+ readonly popup: "closed";
145
+ readonly disabled: boolean;
146
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-724",
3
+ "version": "0.0.0-dev-750",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -273,7 +273,7 @@
273
273
  "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts"
274
274
  },
275
275
  "devDependencies": {
276
- "@hona/strobe": "0.7.0",
276
+ "@hona/strobe": "0.7.3",
277
277
  "@solidjs/meta": "0.29.4",
278
278
  "@tsconfig/node22": "22.0.2",
279
279
  "@types/bun": "1.4.3",
@@ -288,7 +288,7 @@
288
288
  "dependencies": {
289
289
  "@kitlangton/solid-motion": "0.2.2",
290
290
  "@kobalte/core": "0.13.13",
291
- "@opencode/util": "0.0.0-dev-724",
291
+ "@opencode/util": "0.0.0-dev-750",
292
292
  "@pierre/diffs": "1.6.1",
293
293
  "@pierre/highlights": "0.2.0",
294
294
  "@solid-primitives/event-listener": "2.4.6",
@@ -0,0 +1,247 @@
1
+ import type { FlightFrame } from "./image-flight"
2
+
3
+ /**
4
+ * How long the preview waits for its image to decode before it opens anyway. Decoding first keeps the flight's first
5
+ * frames smooth; a decode that hangs, such as the stand-in's on a slow second fetch, must not keep the preview hidden.
6
+ */
7
+ export const DECODE_TIMEOUT_MS = 1000
8
+
9
+ /** What `data-stage` tells the stylesheet: hidden, a stand-in flying, the preview at rest, or fading out after a close. */
10
+ export type PreviewFlightStage = "pending" | "flying" | "rest" | "closing"
11
+
12
+ /**
13
+ * The preview's life from load to close. `attempt` counts image loads, so a decode that settles after the image
14
+ * changed or loaded again is ignored. `borrowed` says the preview holds the page's video, which it hands back once the
15
+ * close lands. `originHidden` says the origin on the page is hidden because the image flew out of it.
16
+ *
17
+ * A close flies home when the origin still shows, then fades the stand-in over the page's image (`veil`). It only
18
+ * fades when the origin is gone, out of sight, or motion is reduced (`fade`).
19
+ */
20
+ export type PreviewFlightState =
21
+ | { readonly status: "pending"; readonly attempt: number }
22
+ | { readonly status: "decoding"; readonly attempt: number }
23
+ | { readonly status: "opening"; readonly mode: "flight" | "fade"; readonly borrowed: boolean }
24
+ | { readonly status: "open"; readonly originHidden: boolean; readonly borrowed: boolean }
25
+ | { readonly status: "closing"; readonly mode: "flight" | "veil" | "fade"; readonly borrowed: boolean }
26
+ | { readonly status: "closed" }
27
+ | { readonly status: "disposed" }
28
+
29
+ /** `origin` is the origin's frame to fly from or to, or nothing when it is gone, out of sight, or motion is reduced. */
30
+ export type PreviewFlightEvent =
31
+ | { readonly type: "src.changed" }
32
+ | { readonly type: "image.loaded" }
33
+ | { readonly type: "image.failed" }
34
+ | { readonly type: "decode.ready"; readonly attempt: number; readonly origin: FlightFrame | undefined }
35
+ | { readonly type: "video.ready"; readonly origin: FlightFrame | undefined }
36
+ | { readonly type: "close.started"; readonly origin: FlightFrame | undefined }
37
+ /** The flight landed with the backdrop at rest, or, without a flight, the backdrop and the veil came to rest. */
38
+ | { readonly type: "motion.settled" }
39
+ | { readonly type: "disposed" }
40
+
41
+ export type PreviewFlightEffect =
42
+ | { readonly type: "decode.start"; readonly attempt: number }
43
+ | { readonly type: "decode.cancel" }
44
+ | { readonly type: "video.borrow" }
45
+ | { readonly type: "video.return" }
46
+ | { readonly type: "origin.hide" }
47
+ | { readonly type: "origin.show" }
48
+ | { readonly type: "open.flight"; readonly from: FlightFrame }
49
+ | { readonly type: "open.fade" }
50
+ /** `turn` turns the opening flight around where it is, keeping its velocity, instead of starting from rest. */
51
+ | { readonly type: "close.flight"; readonly to: FlightFrame; readonly turn: boolean }
52
+ | { readonly type: "close.veil" }
53
+ | { readonly type: "close.fade" }
54
+ /** A video eases back to its whole frame on the way home; an image stops where it is and flies as it is shown. */
55
+ | { readonly type: "zoom.fit" }
56
+ | { readonly type: "zoom.settle" }
57
+ | { readonly type: "body.closing.acquire" }
58
+ | { readonly type: "body.closing.release" }
59
+ | { readonly type: "loop.stop" }
60
+
61
+ export type PreviewFlightTransition = {
62
+ readonly state: PreviewFlightState
63
+ readonly effects: readonly PreviewFlightEffect[]
64
+ }
65
+
66
+ export const INITIAL_PREVIEW_FLIGHT_STATE: PreviewFlightState = Object.freeze({ status: "pending", attempt: 0 })
67
+
68
+ const CLOSED: PreviewFlightState = Object.freeze({ status: "closed" })
69
+
70
+ const DISPOSED: PreviewFlightState = Object.freeze({ status: "disposed" })
71
+
72
+ const NONE: readonly PreviewFlightEffect[] = Object.freeze([])
73
+
74
+ export function transitionPreviewFlight(state: PreviewFlightState, event: PreviewFlightEvent): PreviewFlightTransition {
75
+ if (state.status === "disposed") return { state, effects: NONE }
76
+
77
+ if (event.type === "disposed") return { state: DISPOSED, effects: teardown(state) }
78
+
79
+ if (event.type === "close.started") return close(state, event.origin)
80
+
81
+ if (state.status === "pending" || state.status === "decoding") return reveal(state, event)
82
+
83
+ if (event.type !== "motion.settled") return { state, effects: NONE }
84
+
85
+ return settle(state)
86
+ }
87
+
88
+ export function selectFlightStage(state: PreviewFlightState): PreviewFlightStage {
89
+ if (state.status === "pending" || state.status === "decoding") return "pending"
90
+
91
+ if (state.status === "opening") return state.mode === "flight" ? "flying" : "rest"
92
+
93
+ if (state.status === "open") return "rest"
94
+
95
+ if (state.status === "closing" && state.mode !== "fade") return "flying"
96
+
97
+ return "closing"
98
+ }
99
+
100
+ /** Whether the preview takes zoom, pan and keyboard input: from the moment it reveals until a close starts. */
101
+ export function selectFlightInteractive(state: PreviewFlightState) {
102
+ return state.status === "opening" || state.status === "open"
103
+ }
104
+
105
+ /** Whether the preview, its controls and a borrowed video fade with the backdrop rather than flying. */
106
+ export function selectFlightFading(state: PreviewFlightState) {
107
+ return (state.status === "opening" || state.status === "closing") && state.mode === "fade"
108
+ }
109
+
110
+ /** Whether a borrowed video sits fitted in the preview, not flying, so a resize moves it with the preview. */
111
+ export function selectVideoAtRest(state: PreviewFlightState) {
112
+ if (state.status === "open") return state.borrowed
113
+
114
+ return (state.status === "opening" || state.status === "closing") && state.borrowed && state.mode === "fade"
115
+ }
116
+
117
+ /** Whether the preview still waits to reveal, so it is worth measuring where it would fly from. */
118
+ export function selectAwaitingReveal(state: PreviewFlightState) {
119
+ return state.status === "pending" || state.status === "decoding"
120
+ }
121
+
122
+ /** Whether a decode of image load `attempt` is still the one the preview waits for. */
123
+ export function selectDecodeCurrent(state: PreviewFlightState, attempt: number) {
124
+ return state.status === "decoding" && state.attempt === attempt
125
+ }
126
+
127
+ function reveal(
128
+ state: Extract<PreviewFlightState, { status: "pending" | "decoding" }>,
129
+ event: Exclude<PreviewFlightEvent, { type: "close.started" | "disposed" }>,
130
+ ): PreviewFlightTransition {
131
+ const cancel: readonly PreviewFlightEffect[] = state.status === "decoding" ? [{ type: "decode.cancel" }] : []
132
+ const attempt = state.attempt + 1
133
+
134
+ if (event.type === "src.changed") return { state: { status: "pending", attempt }, effects: cancel }
135
+
136
+ if (event.type === "image.loaded") {
137
+ return { state: { status: "decoding", attempt }, effects: [...cancel, { type: "decode.start", attempt }] }
138
+ }
139
+
140
+ if (event.type === "image.failed") return open(cancel, undefined, false)
141
+
142
+ if (event.type === "decode.ready" && state.status === "decoding" && event.attempt === state.attempt) {
143
+ return open(cancel, event.origin, false)
144
+ }
145
+
146
+ if (event.type === "video.ready" && state.status === "pending") {
147
+ return open([{ type: "video.borrow" }], event.origin, true)
148
+ }
149
+
150
+ return { state, effects: NONE }
151
+ }
152
+
153
+ function open(
154
+ before: readonly PreviewFlightEffect[],
155
+ origin: FlightFrame | undefined,
156
+ borrowed: boolean,
157
+ ): PreviewFlightTransition {
158
+ if (!origin)
159
+ return { state: { status: "opening", mode: "fade", borrowed }, effects: [...before, { type: "open.fade" }] }
160
+
161
+ return {
162
+ state: { status: "opening", mode: "flight", borrowed },
163
+ effects: [...before, { type: "origin.hide" }, { type: "open.flight", from: origin }],
164
+ }
165
+ }
166
+
167
+ function close(state: PreviewFlightState, origin: FlightFrame | undefined): PreviewFlightTransition {
168
+ if (state.status === "pending" || state.status === "decoding") {
169
+ return {
170
+ state: CLOSED,
171
+ effects: [
172
+ ...(state.status === "decoding" ? [{ type: "decode.cancel" } as const] : []),
173
+ { type: "body.closing.acquire" },
174
+ ],
175
+ }
176
+ }
177
+
178
+ if (state.status !== "opening" && state.status !== "open") return { state, effects: NONE }
179
+ const hidden = originHidden(state)
180
+
181
+ const start: PreviewFlightEffect[] = [
182
+ { type: "body.closing.acquire" },
183
+ { type: state.borrowed ? "zoom.fit" : "zoom.settle" },
184
+ ]
185
+
186
+ if (!origin) {
187
+ return {
188
+ state: { status: "closing", mode: "fade", borrowed: state.borrowed },
189
+ effects: [...start, ...(hidden ? [{ type: "origin.show" } as const] : []), { type: "close.fade" }],
190
+ }
191
+ }
192
+
193
+ return {
194
+ state: { status: "closing", mode: "flight", borrowed: state.borrowed },
195
+ effects: [
196
+ ...start,
197
+ ...(hidden ? [] : [{ type: "origin.hide" } as const]),
198
+ { type: "close.flight", to: origin, turn: state.status === "opening" && state.mode === "flight" },
199
+ ],
200
+ }
201
+ }
202
+
203
+ function settle(state: PreviewFlightState): PreviewFlightTransition {
204
+ if (state.status === "opening") {
205
+ return {
206
+ state: { status: "open", originHidden: state.mode === "flight", borrowed: state.borrowed },
207
+ effects: NONE,
208
+ }
209
+ }
210
+
211
+ if (state.status !== "closing") return { state, effects: NONE }
212
+
213
+ // A video is the surface and goes straight back; an image's stand-in fades over the page's image once it lands.
214
+ if (state.mode === "flight" && state.borrowed) {
215
+ return { state: CLOSED, effects: [{ type: "origin.show" }, { type: "video.return" }] }
216
+ }
217
+
218
+ if (state.mode === "flight") {
219
+ return {
220
+ state: { status: "closing", mode: "veil", borrowed: false },
221
+ effects: [{ type: "origin.show" }, { type: "close.veil" }],
222
+ }
223
+ }
224
+
225
+ return { state: CLOSED, effects: state.borrowed ? [{ type: "video.return" }] : NONE }
226
+ }
227
+
228
+ function teardown(state: PreviewFlightState): readonly PreviewFlightEffect[] {
229
+ if (state.status === "pending" || state.status === "disposed") return NONE
230
+
231
+ if (state.status === "decoding") return [{ type: "decode.cancel" }]
232
+
233
+ if (state.status === "closed") return [{ type: "body.closing.release" }]
234
+
235
+ return [
236
+ { type: "loop.stop" },
237
+ ...(originHidden(state) ? [{ type: "origin.show" } as const] : []),
238
+ ...(state.borrowed ? [{ type: "video.return" } as const] : []),
239
+ ...(state.status === "closing" ? [{ type: "body.closing.release" } as const] : []),
240
+ ]
241
+ }
242
+
243
+ function originHidden(state: Extract<PreviewFlightState, { status: "opening" | "open" | "closing" }>) {
244
+ if (state.status === "open") return state.originHidden
245
+
246
+ return state.mode === "flight"
247
+ }