Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It barely scrubs #6195

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion editor/src/uuiui/inputs/number-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ export const NumberInput = React.memo<NumberInputProps>(
setScrubThresholdPassedState(b)
}

const simulatedPointerRef = React.useRef(null)
const pointerOriginRef = React.useRef(null)

const simulatedMouseDeltaX = React.useRef(0)

const [valueChangedSinceFocus, setValueChangedSinceFocus] = React.useState<boolean>(false)

const scaleFactor = valueUnit === '%' ? 100 : 1
Expand Down Expand Up @@ -398,8 +403,17 @@ export const NumberInput = React.memo<NumberInputProps>(
(e: MouseEvent) => {
onThresholdPassed(e, () => {
if (!scrubThresholdPassed.current) {
console.log('scrub threshold passed')
setScrubThresholdPassed(true)
if (pointerOriginRef.current) {
console.log('requesting pointer lock')
// @ts-ignore
pointerOriginRef.current.requestPointerLock()
}
}
console.log('scrubbing')
simulatedMouseDeltaX.current = e.screenX - dragOriginX.current

setScrubValue(
valueUnit,
e.screenX,
Expand All @@ -409,15 +423,34 @@ export const NumberInput = React.memo<NumberInputProps>(
true,
)
})

setScrubValue(
valueUnit,
e.screenX,
e.screenY,
dragOriginX.current,
dragOriginY.current,
true,
)
},
[setScrubValue, valueUnit],
)

const scrubOnMouseUp = React.useCallback(
(e: MouseEvent) => {
console.log('removing event listeners')
window.removeEventListener('mouseup', scrubOnMouseUp)
window.removeEventListener('mousemove', scrubOnMouseMove)

// removing pointer lock
if (document.pointerLockElement === pointerOriginRef.current) {
console.log('exiting pointer lock')
document.exitPointerLock()
console.log('removing pointer lock event listeners')
// TODO we need a pointer lock event listener
// since users can exit pointer lock by pressing escape
}

setIsFauxcused(false)
ref.current?.focus()

Expand Down Expand Up @@ -635,13 +668,15 @@ export const NumberInput = React.memo<NumberInputProps>(
if (e.button === 0) {
e.stopPropagation()
setIsFauxcused(true)
console.log('adding event listeners oMouseMove and onMouseUp')
window.addEventListener('mousemove', scrubOnMouseMove)
window.addEventListener('mouseup', scrubOnMouseUp)
setLabelDragDirection('horizontal')
setValueAtDragOrigin(value?.value ?? 0)
setDragOriginX(e.screenX)
setDragOriginY(e.screenY)
setGlobalCursor?.(CSSCursor.ResizeEW)
simulatedMouseDeltaX.current = 0
}
},
[scrubOnMouseMove, scrubOnMouseUp, setGlobalCursor, value, disabled],
Expand All @@ -667,7 +702,29 @@ export const NumberInput = React.memo<NumberInputProps>(
: undefined

return (
<div onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} style={style}>
<div
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
ref={pointerOriginRef}
style={style}
>
{/* this will contain the fake mouse pointer, but for debugging I always show it */}
<div
ref={simulatedPointerRef}
className='simulatedPosition'
style={{
transform: `translateX(${simulatedMouseDeltaX.current}px)`,
width: 5,
height: 5,
background: 'red',
position: 'fixed',
visibility: scrubThresholdPassed.current ? 'visible' : 'hidden',
// i know, but it works
zIndex: 999999,
}}
>
{scrubThresholdPassed.current ? simulatedMouseDeltaX.current : '-'}
</div>
<div
className='number-input-container'
css={{
Expand Down
Loading