Repository navigation
Expand file tree
/
Copy pathSlider.ts
More file actions
302 lines (258 loc) · 10 KB
/
Copy pathSlider.ts
File metadata and controls
302 lines (258 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { type RenderableOptions, Renderable, type NativeRenderableIntegration } from "../Renderable.js"
import { type RenderContext } from "../types.js"
import { type ColorInput, RGBA, parseColor } from "../lib/RGBA.js"
import { OptimizedBuffer } from "../buffer.js"
import type { NativeSceneSliderOptions } from "../zig.js"
const defaultThumbBackgroundColor = RGBA.fromHex("#9a9ea3")
const defaultTrackBackgroundColor = RGBA.fromHex("#252527")
const defaultMin = 0
const defaultMax = 100
export interface SliderOptions extends RenderableOptions<SliderRenderable> {
orientation: "vertical" | "horizontal"
value?: number
min?: number
max?: number
viewPortSize?: number
backgroundColor?: ColorInput
foregroundColor?: ColorInput
onChange?: (value: number) => void
}
export class SliderRenderable extends Renderable {
static override readonly nativeIntegration: NativeRenderableIntegration = {
kind: "slider",
body: { native: this.prototype.renderSelf },
}
private _orientation: "vertical" | "horizontal"
private _value: number
private _min: number
private _max: number
private _viewPortSize: number
private _backgroundColor: RGBA
private _foregroundColor: RGBA
private _onChange?: (value: number) => void
constructor(ctx: RenderContext, options: SliderOptions) {
super(ctx, { flexShrink: 0, ...options })
try {
this._orientation = options.orientation
this._min = options.min ?? defaultMin
this._max = options.max ?? defaultMax
this._value = options.value ?? this._min
this._viewPortSize = options.viewPortSize ?? this.defaultViewPortSize()
this._onChange = options.onChange
this._backgroundColor = RGBA.clone(parseColor(options.backgroundColor || defaultTrackBackgroundColor))
this._foregroundColor = RGBA.clone(parseColor(options.foregroundColor || defaultThumbBackgroundColor))
this.setNativeSceneSlider()
this.setNativeScenePaint()
this.setupMouseHandling()
} catch (error) {
this.abortConstruction(error)
}
}
get orientation(): "vertical" | "horizontal" {
return this._orientation
}
set orientation(value: "vertical" | "horizontal") {
if (value === this._orientation) return
this.setNativeSceneSlider({ orientation: value })
this._orientation = value
this.requestRender()
}
get value(): number {
return this._value
}
set value(newValue: number) {
newValue ??= this._min
const clamped = Math.max(this._min, Math.min(this._max, newValue))
if (clamped !== this._value) {
this.setNativeSceneSlider({ value: clamped })
this.publishValue(clamped)
}
}
private publishValue(value: number): void {
if (value === this._value) return
this._value = value
this.requestRender()
this._onChange?.(value)
this.emit("change", { value })
}
get min(): number {
return this._min
}
set min(newMin: number) {
newMin ??= defaultMin
if (newMin !== this._min) {
const value = this._value < newMin ? newMin : this._value
this.setNativeSceneSlider({ min: newMin, value })
this._min = newMin
if (this._value < newMin) {
this.publishValue(value)
}
this.requestRender()
}
}
get max(): number {
return this._max
}
set max(newMax: number) {
newMax ??= defaultMax
if (newMax !== this._max) {
const value = this._value > newMax ? Math.max(this._min, newMax) : this._value
this.setNativeSceneSlider({ max: newMax, value })
this._max = newMax
if (this._value > newMax) {
this.publishValue(value)
}
this.requestRender()
}
}
set viewPortSize(size: number) {
size ??= this.defaultViewPortSize()
const clampedSize = Math.max(0.01, Math.min(size, this._max - this._min))
if (clampedSize !== this._viewPortSize) {
this.setNativeSceneSlider({ viewPortSize: clampedSize })
this._viewPortSize = clampedSize
this.requestRender()
}
}
get viewPortSize(): number {
return this._viewPortSize
}
private defaultViewPortSize(): number {
return Math.max(1, (this._max - this._min) * 0.1)
}
get backgroundColor(): RGBA {
return RGBA.clone(this._backgroundColor)
}
set backgroundColor(value: ColorInput) {
const color = RGBA.clone(parseColor(value ?? defaultTrackBackgroundColor))
this.setNativeSceneSlider({ backgroundColor: color })
this._backgroundColor = color
this.requestRender()
}
get foregroundColor(): RGBA {
return RGBA.clone(this._foregroundColor)
}
set foregroundColor(value: ColorInput) {
const color = RGBA.clone(parseColor(value ?? defaultThumbBackgroundColor))
this.setNativeSceneSlider({ foregroundColor: color })
this._foregroundColor = color
this.requestRender()
}
private setNativeSceneSlider(options: Partial<NativeSceneSliderOptions> = {}): void {
if (this.isFreed()) return
this._ctx.nativeScene.setSlider(this, {
orientation: this.orientation,
min: this._min,
max: this._max,
value: this._value,
viewPortSize: this._viewPortSize,
foregroundColor: this._foregroundColor,
backgroundColor: this._backgroundColor,
...options,
})
}
// The pointer's distance from the thumb start that the current value implies, not from the painted start, which is
// rounded to a half cell: a drag then moves the value by the pointer's travel from the first move. The clamp keeps
// the track start reachable from a press on the thumb's first cell, whose edge can lie before the exact start.
private calculateDragOffsetVirtual(event: any): number {
const trackStart = this.orientation === "vertical" ? this.y : this.x
const trackSize = this.orientation === "vertical" ? this.height : this.width
const mousePos = (this.orientation === "vertical" ? event.y : event.x) - trackStart
const virtualMousePos = Math.max(0, Math.min(trackSize, mousePos)) * 2
const thumbSize = this._ctx.nativeScene.getSliderThumb(this).size
const maxThumbStart = Math.max(0, trackSize * 2 - thumbSize)
const range = this._max - this._min
const ratio = range === 0 ? 0 : (this._value - this._min) / range
return Math.max(0, Math.min(thumbSize, virtualMousePos - ratio * maxThumbStart))
}
private setupMouseHandling(): void {
let isDragging = false
let dragOffsetVirtual = 0
// The pointer position the value already reflects. An event there has nothing to apply, and recomputing the value
// could differ from it by a rounding error: a press and release at one position would change it.
let appliedMousePos = 0
const pointerAxis = (event: any): number => (this.orientation === "vertical" ? event.y : event.x)
this.onMouseDown = (event) => {
event.stopPropagation()
event.preventDefault()
const thumb = this.getThumbRect()
const inThumb =
event.x >= thumb.x && event.x < thumb.x + thumb.width && event.y >= thumb.y && event.y < thumb.y + thumb.height
if (!inThumb) {
this.updateValueFromMouseDirect(event)
if (this.isDestroyed) return
}
isDragging = true
dragOffsetVirtual = this.calculateDragOffsetVirtual(event)
appliedMousePos = pointerAxis(event)
}
const drag = (event: any): void => {
const mousePos = pointerAxis(event)
if (mousePos === appliedMousePos) return
appliedMousePos = mousePos
this.updateValueFromMouseWithOffset(event, dragOffsetVirtual)
}
this.onMouseDrag = (event) => {
if (!isDragging) return
event.stopPropagation()
drag(event)
}
this.onMouseUp = (event) => {
if (isDragging) drag(event)
isDragging = false
}
}
private updateValueFromMouseDirect(event: any): void {
const trackStart = this.orientation === "vertical" ? this.y : this.x
const trackSize = this.orientation === "vertical" ? this.height : this.width
const mousePos = this.orientation === "vertical" ? event.y : event.x
const relativeMousePos = mousePos - trackStart
const clampedMousePos = Math.max(0, Math.min(trackSize, relativeMousePos))
const ratio = trackSize === 0 ? 0 : clampedMousePos / trackSize
const range = this._max - this._min
const newValue = this._min + ratio * range
this.value = newValue
}
private updateValueFromMouseWithOffset(event: any, offsetVirtual: number): void {
const trackStart = this.orientation === "vertical" ? this.y : this.x
const trackSize = this.orientation === "vertical" ? this.height : this.width
const mousePos = this.orientation === "vertical" ? event.y : event.x
const virtualTrackSize = trackSize * 2
const relativeMousePos = mousePos - trackStart
const clampedMousePos = Math.max(0, Math.min(trackSize, relativeMousePos))
const virtualMousePos = clampedMousePos * 2
const virtualThumbSize = this._ctx.nativeScene.getSliderThumb(this).size
const maxThumbStart = Math.max(0, virtualTrackSize - virtualThumbSize)
let desiredThumbStart = virtualMousePos - offsetVirtual
desiredThumbStart = Math.max(0, Math.min(maxThumbStart, desiredThumbStart))
const ratio = maxThumbStart === 0 ? 0 : desiredThumbStart / maxThumbStart
const range = this._max - this._min
const newValue = this._min + ratio * range
this.value = newValue
}
private getThumbRect(): { x: number; y: number; width: number; height: number } {
const thumb = this._ctx.nativeScene.getSliderThumb(this)
const virtualThumbSize = thumb.size
const virtualThumbStart = thumb.start
const realThumbStart = Math.floor(virtualThumbStart / 2)
const realThumbSize = Math.ceil((virtualThumbStart + virtualThumbSize) / 2) - realThumbStart
if (this.orientation === "vertical") {
return {
x: this.x,
y: this.y + realThumbStart,
width: this.width,
height: Math.max(1, realThumbSize),
}
} else {
return {
x: this.x + realThumbStart,
y: this.y,
width: Math.max(1, realThumbSize),
height: this.height,
}
}
}
protected renderSelf(buffer: OptimizedBuffer): void {
this._drawNativeBody(buffer)
}
}