@opencode/ui 0.0.0-dev-735 → 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,191 @@
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
+ /**
5
+ * The press under way. A `backdrop-press` began on the backdrop around the image and stayed put, so the click that ends
6
+ * it closes the preview. A pan captures the pointer, so its click lands on the backdrop wherever it began: `onBackdrop`
7
+ * says whether it began there, and `moved` whether it has moved, so the click that ends a moved pan neither closes the
8
+ * preview nor plays the video.
9
+ */
10
+ export type PreviewZoomGesture =
11
+ | { readonly type: "idle" }
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
+ /**
22
+ * `gliding` is an eased zoom under way on the frame loop. Scale and offsets live on springs in the driver, not here: a
23
+ * 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
24
+ * leaves it running; the hand moving does.
25
+ */
26
+ export type PreviewZoomState =
27
+ | { readonly status: "still" | "gliding"; readonly gesture: PreviewZoomGesture }
28
+ | { readonly status: "disposed" }
29
+
30
+ /** `zoomed` is whether the image shows above fit once the event's change applies. */
31
+ export type PreviewZoomEvent =
32
+ | {
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"; readonly pointerId: number }
42
+ | { readonly type: "pointer.up"; readonly pointerId: number }
43
+ /** `pointercancel`, `lostpointercapture`, or a move with the primary button already up: the pan ends unreleased. */
44
+ | { readonly type: "pointer.cancel"; readonly pointerId: number }
45
+ | { readonly type: "backdrop.click"; readonly onBackdrop: boolean }
46
+ /** Pinch, wheel, drag or a resize set the view directly. */
47
+ | { readonly type: "follow"; readonly zoomed: boolean }
48
+ | { readonly type: "glide.start" }
49
+ | { readonly type: "glide.settled"; readonly zoomed: boolean }
50
+ | { readonly type: "disposed" }
51
+
52
+ export type PreviewZoomEffect =
53
+ | { readonly type: "pointer.capture"; readonly target: PanTarget; readonly pointerId: number }
54
+ | { readonly type: "pointer.release"; readonly target: PanTarget; readonly pointerId: number }
55
+ | { readonly type: "glide.loop.start" }
56
+ | { readonly type: "glide.loop.stop" }
57
+ /** Swallows the click that ends a moved pan over the video, so it does not play or pause it. */
58
+ | { readonly type: "video.suppress-click" }
59
+ | { readonly type: "dialog.close" }
60
+
61
+ export type PreviewZoomTransition = {
62
+ readonly state: PreviewZoomState
63
+ readonly effects: readonly PreviewZoomEffect[]
64
+ }
65
+
66
+ const IDLE: PreviewZoomGesture = Object.freeze({ type: "idle" })
67
+
68
+ const BACKDROP_PRESS: PreviewZoomGesture = Object.freeze({ type: "backdrop-press" })
69
+
70
+ const NONE: readonly PreviewZoomEffect[] = Object.freeze([])
71
+
72
+ export const INITIAL_PREVIEW_ZOOM_STATE: PreviewZoomState = Object.freeze({ status: "still", gesture: IDLE })
73
+
74
+ const DISPOSED: PreviewZoomState = Object.freeze({ status: "disposed" })
75
+
76
+ export function transitionPreviewZoom(state: PreviewZoomState, event: PreviewZoomEvent): PreviewZoomTransition {
77
+ if (state.status === "disposed") return unchanged(state)
78
+ const gliding = state.status === "gliding"
79
+ const pan = state.gesture.type === "panning" ? state.gesture : undefined
80
+
81
+ if (event.type === "disposed") {
82
+ return { state: DISPOSED, effects: [...release(pan), ...(gliding ? [{ type: "glide.loop.stop" } as const] : [])] }
83
+ }
84
+
85
+ if (event.type === "pointer.down") {
86
+ // A second pointer leaves the pan alone; the same pointer pressing again means its release never arrived.
87
+ if (pan && pan.pointerId !== event.pointerId) return unchanged(state)
88
+
89
+ if (event.button === 0 && event.zoomed && event.target !== "content") {
90
+ return {
91
+ state: {
92
+ status: state.status,
93
+ gesture: {
94
+ type: "panning",
95
+ pointerId: event.pointerId,
96
+ target: event.target,
97
+ onBackdrop: event.onBackdrop,
98
+ moved: false,
99
+ },
100
+ },
101
+ effects: [{ type: "pointer.capture", target: event.target, pointerId: event.pointerId }],
102
+ }
103
+ }
104
+
105
+ return gesture(state, event.button === 0 && event.onBackdrop ? BACKDROP_PRESS : IDLE)
106
+ }
107
+
108
+ if (event.type === "pointer.moved") {
109
+ if (pan?.pointerId !== event.pointerId || pan.moved) return unchanged(state)
110
+
111
+ return gesture(state, { ...pan, onBackdrop: false, moved: true })
112
+ }
113
+
114
+ if (event.type === "pointer.up") {
115
+ if (pan?.pointerId !== event.pointerId) return unchanged(state)
116
+
117
+ if (pan.moved && pan.target === "video") {
118
+ return { state: { status: state.status, gesture: IDLE }, effects: [{ type: "video.suppress-click" }] }
119
+ }
120
+
121
+ return gesture(state, !pan.moved && pan.onBackdrop ? BACKDROP_PRESS : IDLE)
122
+ }
123
+
124
+ if (event.type === "pointer.cancel") {
125
+ if (pan?.pointerId !== event.pointerId) return unchanged(state)
126
+
127
+ return { state: { status: state.status, gesture: IDLE }, effects: release(pan) }
128
+ }
129
+
130
+ if (event.type === "backdrop.click") {
131
+ if (state.gesture.type !== "backdrop-press") return unchanged(state)
132
+
133
+ return {
134
+ state: { status: state.status, gesture: IDLE },
135
+ effects: event.onBackdrop ? [{ type: "dialog.close" }] : NONE,
136
+ }
137
+ }
138
+
139
+ // An eased zoom takes over from a pan, so a pan never outlives a zoom back to fit.
140
+ if (event.type === "glide.start") {
141
+ if (gliding && !pan) return unchanged(state)
142
+
143
+ return {
144
+ state: { status: "gliding", gesture: pan ? IDLE : state.gesture },
145
+ effects: [...release(pan), ...(gliding ? [] : [{ type: "glide.loop.start" } as const])],
146
+ }
147
+ }
148
+
149
+ if (event.type === "glide.settled" && !gliding) return unchanged(state)
150
+ // The hand takes over from an eased zoom; a pan ends once the image is back at fit.
151
+ const ends = pan && !event.zoomed ? pan : undefined
152
+
153
+ if (!gliding && !ends) return unchanged(state)
154
+
155
+ return {
156
+ state: { status: "still", gesture: ends ? IDLE : state.gesture },
157
+ effects: [...(gliding ? [{ type: "glide.loop.stop" } as const] : []), ...release(ends)],
158
+ }
159
+ }
160
+
161
+ /** Whether a pan holds the pointer, which shows the grabbing cursor. */
162
+ export function selectDragging(state: PreviewZoomState) {
163
+ return state.status !== "disposed" && state.gesture.type === "panning"
164
+ }
165
+
166
+ /** Whether `pointerId` is the pointer the pan under way follows. */
167
+ export function selectPanPointer(state: PreviewZoomState, pointerId: number) {
168
+ return state.status !== "disposed" && state.gesture.type === "panning" && state.gesture.pointerId === pointerId
169
+ }
170
+
171
+ function gesture(state: Exclude<PreviewZoomState, { status: "disposed" }>, next: PreviewZoomGesture) {
172
+ if (state.gesture === next) return unchanged(state)
173
+
174
+ return { state: { status: state.status, gesture: next }, effects: NONE }
175
+ }
176
+
177
+ function release(pan: Extract<PreviewZoomGesture, { type: "panning" }> | undefined): readonly PreviewZoomEffect[] {
178
+ if (!pan) return NONE
179
+
180
+ return [{ type: "pointer.release", target: pan.target, pointerId: pan.pointerId }]
181
+ }
182
+
183
+ // Every pointer move of a pan dispatches events that change nothing; answering them with the transition already built
184
+ // for that state keeps the hot path allocation-free.
185
+ let last: PreviewZoomTransition = Object.freeze({ state: DISPOSED, effects: NONE })
186
+
187
+ function unchanged(state: PreviewZoomState) {
188
+ if (last.state !== state) last = Object.freeze({ state, effects: NONE })
189
+
190
+ return last
191
+ }
@@ -59,26 +59,25 @@ export function detailScale(fitRatio: number) {
59
59
  }
60
60
 
61
61
  /**
62
- * The `object-view-box` that shows `view` of media fitted at `size`, for media that must zoom inside its own box, such
63
- * as a video under its controls. The inset is in the media's natural pixels.
62
+ * The `object-view-box` that shows the view at `scale`, `x` and `y` of media fitted at `size`, for media that must zoom
63
+ * inside its own box, such as a video under its controls. The inset is in the media's natural pixels. It takes the view
64
+ * as numbers, so painting a frame of a glide allocates nothing but the string.
64
65
  */
65
66
  export function viewBox(
66
- view: ZoomView,
67
+ scale: number,
68
+ x: number,
69
+ y: number,
67
70
  size: { width: number; height: number },
68
71
  natural: { width: number; height: number },
69
72
  ) {
70
- if (view.scale <= 1) return "none"
73
+ if (scale <= 1) return "none"
71
74
 
72
- const edge = (offset: number, fitted: number) => 0.5 - 0.5 / view.scale - offset / (view.scale * fitted)
75
+ const top = px(edge(y, size.height, scale) * natural.height)
76
+ const right = px(edge(-x, size.width, scale) * natural.width)
77
+ const bottom = px(edge(-y, size.height, scale) * natural.height)
78
+ const left = px(edge(x, size.width, scale) * natural.width)
73
79
 
74
- const inset = [
75
- edge(view.y, size.height) * natural.height,
76
- edge(-view.x, size.width) * natural.width,
77
- edge(-view.y, size.height) * natural.height,
78
- edge(view.x, size.width) * natural.width,
79
- ]
80
-
81
- return `inset(${inset.map((value) => `${Math.max(0, Math.round(value * 100) / 100)}px`).join(" ")})`
80
+ return `inset(${top} ${right} ${bottom} ${left})`
82
81
  }
83
82
 
84
83
  /** The zoom factor for one wheel event, bounded so a coarse mouse-wheel notch is a step rather than a jump. */
@@ -86,13 +85,22 @@ export function wheelZoomFactor(deltaY: number) {
86
85
  return Math.exp(-clamp(deltaY, -30, 30) / 100)
87
86
  }
88
87
 
89
- function clampAxis(offset: number, size: number, viewport: number) {
88
+ /** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
89
+ export function clampAxis(offset: number, size: number, viewport: number) {
90
90
  const limit = Math.max(0, (size - viewport) / 2)
91
91
 
92
92
  // Adding zero turns -0 into 0.
93
93
  return clamp(offset, -limit, limit) + 0
94
94
  }
95
95
 
96
+ function edge(offset: number, fitted: number, scale: number) {
97
+ return 0.5 - 0.5 / scale - offset / (scale * fitted)
98
+ }
99
+
100
+ function px(value: number) {
101
+ return `${Math.max(0, Math.round(value * 100) / 100)}px`
102
+ }
103
+
96
104
  function clamp(value: number, min: number, max: number) {
97
105
  return Math.min(Math.max(value, min), max)
98
106
  }