Make it possible to drag starting on a grid control button

This commit is contained in:
Max Goodman
2020-11-06 23:08:50 -08:00
committed by Max Goodhart
parent 978c986862
commit d36343a1d7

View File

@@ -306,20 +306,38 @@ function App({ wsEndpoint, role }) {
[stateDoc, stateIdxMap, swapStartIdx], [stateDoc, stateIdxMap, swapStartIdx],
) )
const [hoveringIdx, setHoveringIdx] = useState()
const updateHoveringIdx = useCallback(
(ev) => {
const {
width,
height,
left,
top,
} = ev.currentTarget.getBoundingClientRect()
const x = Math.floor(ev.clientX - left)
const y = Math.floor(ev.clientY - top)
const spaceWidth = width / gridCount
const spaceHeight = height / gridCount
const idx =
Math.floor(y / spaceHeight) * gridCount + Math.floor(x / spaceWidth)
setHoveringIdx(idx)
},
[setHoveringIdx, gridCount],
)
const [dragStart, setDragStart] = useState() const [dragStart, setDragStart] = useState()
const handleDragStart = useCallback( const handleDragStart = useCallback(
(idx, ev) => { (ev) => {
ev.preventDefault() ev.preventDefault()
if (swapStartIdx !== undefined) { if (swapStartIdx !== undefined) {
handleSwap(idx) handleSwap(hoveringIdx)
} else { } else {
setDragStart(idx) setDragStart(hoveringIdx)
ev.target.select() ev.target.select()
} }
}, },
[handleSwap], [handleSwap, swapStartIdx, hoveringIdx],
) )
const [dragEnd, setDragEnd] = useState()
useLayoutEffect(() => { useLayoutEffect(() => {
function endDrag() { function endDrag() {
if (dragStart === undefined) { if (dragStart === undefined) {
@@ -329,7 +347,7 @@ function App({ wsEndpoint, role }) {
const viewsState = stateDoc.getMap('views') const viewsState = stateDoc.getMap('views')
const streamId = viewsState.get(String(dragStart)).get('streamId') const streamId = viewsState.get(String(dragStart)).get('streamId')
for (let idx = 0; idx < gridCount ** 2; idx++) { for (let idx = 0; idx < gridCount ** 2; idx++) {
if (idxInBox(gridCount, dragStart, dragEnd, idx)) { if (idxInBox(gridCount, dragStart, hoveringIdx, idx)) {
viewsState.get(String(idx)).set('streamId', streamId) viewsState.get(String(idx)).set('streamId', streamId)
} }
} }
@@ -338,7 +356,7 @@ function App({ wsEndpoint, role }) {
} }
window.addEventListener('mouseup', endDrag) window.addEventListener('mouseup', endDrag)
return () => window.removeEventListener('mouseup', endDrag) return () => window.removeEventListener('mouseup', endDrag)
}, [stateDoc, dragStart, dragEnd]) }, [stateDoc, dragStart, hoveringIdx])
const [focusedInputIdx, setFocusedInputIdx] = useState() const [focusedInputIdx, setFocusedInputIdx] = useState()
const handleFocusInput = useCallback(setFocusedInputIdx, []) const handleFocusInput = useCallback(setFocusedInputIdx, [])
@@ -558,6 +576,7 @@ function App({ wsEndpoint, role }) {
<StyledDataContainer isConnected={isConnected}> <StyledDataContainer isConnected={isConnected}>
{gridCount && ( {gridCount && (
<StyledGridContainer <StyledGridContainer
onMouseMove={updateHoveringIdx}
windowWidth={windowWidth} windowWidth={windowWidth}
windowHeight={windowHeight} windowHeight={windowHeight}
> >
@@ -569,7 +588,7 @@ function App({ wsEndpoint, role }) {
const { streamId } = sharedState.views?.[idx] ?? {} const { streamId } = sharedState.views?.[idx] ?? {}
const isDragHighlighted = const isDragHighlighted =
dragStart !== undefined && dragStart !== undefined &&
idxInBox(gridCount, dragStart, dragEnd, idx) idxInBox(gridCount, dragStart, hoveringIdx, idx)
return ( return (
<GridInput <GridInput
style={{ style={{
@@ -584,7 +603,6 @@ function App({ wsEndpoint, role }) {
isHighlighted={isDragHighlighted} isHighlighted={isDragHighlighted}
role={role} role={role}
onMouseDown={handleDragStart} onMouseDown={handleDragStart}
onMouseEnter={setDragEnd}
onFocus={handleFocusInput} onFocus={handleFocusInput}
onBlur={handleBlurInput} onBlur={handleBlurInput}
/> />
@@ -648,6 +666,7 @@ function App({ wsEndpoint, role }) {
onSwapView={handleSwapView} onSwapView={handleSwapView}
onBrowse={handleBrowse} onBrowse={handleBrowse}
onDevTools={handleDevTools} onDevTools={handleDevTools}
onMouseDown={handleDragStart}
/> />
) )
}, },
@@ -856,7 +875,6 @@ function GridInput({
isHighlighted, isHighlighted,
role, role,
onMouseDown, onMouseDown,
onMouseEnter,
onFocus, onFocus,
onBlur, onBlur,
}) { }) {
@@ -883,13 +901,6 @@ function GridInput({
}, },
[idx, onChangeSpace], [idx, onChangeSpace],
) )
const handleMouseDown = useCallback(
(ev) => {
onMouseDown(idx, ev)
},
[onMouseDown],
)
const handleMouseEnter = useCallback(() => onMouseEnter(idx), [onMouseEnter])
return ( return (
<StyledGridInputContainer style={style}> <StyledGridInputContainer style={style}>
<StyledGridInput <StyledGridInput
@@ -899,8 +910,7 @@ function GridInput({
disabled={!roleCan(role, 'mutate-state-doc')} disabled={!roleCan(role, 'mutate-state-doc')}
onFocus={handleFocus} onFocus={handleFocus}
onBlur={handleBlur} onBlur={handleBlur}
onMouseDown={handleMouseDown} onMouseDown={onMouseDown}
onMouseEnter={handleMouseEnter}
onChange={handleChange} onChange={handleChange}
/> />
</StyledGridInputContainer> </StyledGridInputContainer>
@@ -925,6 +935,7 @@ function GridControls({
onSwapView, onSwapView,
onBrowse, onBrowse,
onDevTools, onDevTools,
onMouseDown,
}) { }) {
// TODO: Refactor callbacks to use streamID instead of idx. // TODO: Refactor callbacks to use streamID instead of idx.
// We should probably also switch the view-state-changing RPCs to use a view id instead of idx like they do currently. // We should probably also switch the view-state-changing RPCs to use a view id instead of idx like they do currently.
@@ -960,7 +971,7 @@ function GridControls({
onDevTools, onDevTools,
]) ])
return ( return (
<StyledGridControlsContainer style={style}> <StyledGridControlsContainer style={style} onMouseDown={onMouseDown}>
{isDisplaying && ( {isDisplaying && (
<StyledGridButtons side="left"> <StyledGridButtons side="left">
{showDebug ? ( {showDebug ? (