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

Hide safe gaps after drag threshold #4254

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export interface DragInteractionData {
_accumulatedMovement: CanvasVector
spacePressed: boolean
zeroDragPermitted: ZeroDragPermitted // Will still complete the interaction with no drag distance applied.
hasBeenPastThreshold: boolean
}

export interface HoverInteractionData {
Expand Down Expand Up @@ -201,6 +202,7 @@ export function createInteractionViaMouse(
_accumulatedMovement: zeroCanvasPoint,
spacePressed: false,
zeroDragPermitted: zeroDragPermitted,
hasBeenPastThreshold: false,
},
activeControl: activeControl,
lastInteractionTime: Date.now(),
Expand Down Expand Up @@ -233,8 +235,8 @@ export function createHoverInteractionViaMouse(
}
}

function dragExceededThreshold(drag: CanvasVector): boolean {
return magnitude(drag) > MoveIntoDragThreshold
export function dragExceededThreshold(drag: CanvasVector | null): boolean {
return drag != null && magnitude(drag) > MoveIntoDragThreshold
}

export function updateInteractionViaDragDelta(
Expand All @@ -261,6 +263,8 @@ export function updateInteractionViaDragDelta(
_accumulatedMovement: accumulatedMovement,
spacePressed: currentState.interactionData.spacePressed,
zeroDragPermitted: currentState.interactionData.zeroDragPermitted,
hasBeenPastThreshold:
currentState.interactionData.hasBeenPastThreshold || dragThresholdPassed,
},
activeControl: sourceOfUpdate ?? currentState.activeControl,
lastInteractionTime: Date.now(),
Expand Down Expand Up @@ -334,6 +338,7 @@ function updateInteractionDataViaMouse(
_accumulatedMovement: currentData._accumulatedMovement,
spacePressed: currentData.spacePressed,
zeroDragPermitted: currentData.zeroDragPermitted,
hasBeenPastThreshold: currentData.hasBeenPastThreshold || dragThresholdPassed,
}
case 'HOVER':
return {
Expand All @@ -347,6 +352,7 @@ function updateInteractionDataViaMouse(
_accumulatedMovement: zeroCanvasPoint,
spacePressed: false,
zeroDragPermitted: currentData.zeroDragPermitted,
hasBeenPastThreshold: false,
ruggi marked this conversation as resolved.
Show resolved Hide resolved
}
default:
assertNever(currentData)
Expand Down Expand Up @@ -438,6 +444,9 @@ export function updateInteractionViaKeyboard(
_accumulatedMovement: currentState.interactionData._accumulatedMovement,
spacePressed: isSpacePressed,
zeroDragPermitted: currentState.interactionData.zeroDragPermitted,
hasBeenPastThreshold:
currentState.interactionData.hasBeenPastThreshold ||
dragExceededThreshold(currentState.interactionData.drag),
},
activeControl: currentState.activeControl,
lastInteractionTime: Date.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
mouseDoubleClickAtPoint,
mouseDownAtPoint,
mouseDragFromPointWithDelta,
mouseMoveToPoint,
} from '../../event-helpers.test-utils'
import { CanvasControlsContainerID } from '../../controls/new-canvas-controls'
import type { CSSProperties } from 'react'
Expand Down Expand Up @@ -2978,7 +2979,7 @@ describe('Absolute Resize Control', () => {
expect(resizeControlLeft.style.width).toEqual('10px')
expect(resizeControlLeft.style.height).toEqual(`${height + RESIZE_CONTROL_SAFE_GAP * 2}px`)
})
it("Doesn't show the safe gap during resize and mouse down", async () => {
it("Doesn't show the safe gap during resize after threshold is passed", async () => {
const width = SmallElementSize
const height = SmallElementSize
const renderResult = await renderTestEditorWithCode(
Expand All @@ -3003,6 +3004,9 @@ describe('Absolute Resize Control', () => {
const bottomLeftHandle = await renderResult.renderedDOM.findByTestId(`resize-control-0-1`)

await mouseDownAtPoint(bottomRightHandle, { x: 2, y: 2 })
await mouseMoveToPoint(bottomRightHandle, { x: 10, y: 0 })

const threshold = 2 // px

expect(bottomRightHandle.parentElement?.style.visibility).toEqual('visible')
expect(topRightHandle.parentElement?.style.visibility).toEqual('hidden')
Expand All @@ -3015,25 +3019,25 @@ describe('Absolute Resize Control', () => {
expect(resizeControlTop.style.transform).toEqual('translate(0px, -5px)')
expect(resizeControlTop.style.top).toEqual('')
expect(resizeControlTop.style.left).toEqual('')
expect(resizeControlTop.style.width).toEqual(`${width}px`)
expect(resizeControlTop.style.width).toEqual(`${width + 10 - threshold}px`)
expect(resizeControlTop.style.height).toEqual('5px')

const resizeControlRight = renderResult.renderedDOM.getByTestId(
`resize-control-${EdgePositionRight.x}-${EdgePositionRight.y}`,
)
expect(resizeControlRight.style.transform).toEqual('translate(0px, 0px)')
expect(resizeControlRight.style.transform).toEqual('translate(-5px, 0px)')
expect(resizeControlRight.style.top).toEqual('')
expect(resizeControlRight.style.left).toEqual(`${width}px`)
expect(resizeControlRight.style.width).toEqual('5px')
expect(resizeControlRight.style.height).toEqual(`${height}px`)
expect(resizeControlRight.style.left).toEqual(`${width + 10 - threshold}px`)
expect(resizeControlRight.style.width).toEqual('10px')
expect(resizeControlRight.style.height).toEqual(`${height - threshold}px`)

const resizeControlBottom = renderResult.renderedDOM.getByTestId(
`resize-control-${EdgePositionBottom.x}-${EdgePositionBottom.y}`,
)
expect(resizeControlBottom.style.transform).toEqual('translate(0px, 0px)')
expect(resizeControlBottom.style.top).toEqual(`${height}px`)
expect(resizeControlBottom.style.top).toEqual(`${height - 2}px`)
expect(resizeControlBottom.style.left).toEqual('')
expect(resizeControlBottom.style.width).toEqual(`${width}px`)
expect(resizeControlBottom.style.width).toEqual(`${width + 10 - threshold}px`)
expect(resizeControlBottom.style.height).toEqual('5px')

const resizeControlLeft = renderResult.renderedDOM.getByTestId(
Expand All @@ -3042,8 +3046,8 @@ describe('Absolute Resize Control', () => {
expect(resizeControlLeft.style.transform).toEqual('translate(-5px, 0px)')
expect(resizeControlLeft.style.top).toEqual('')
expect(resizeControlLeft.style.left).toEqual('')
expect(resizeControlLeft.style.width).toEqual('5px')
expect(resizeControlLeft.style.height).toEqual(`${height}px`)
expect(resizeControlLeft.style.width).toEqual('10px')
expect(resizeControlLeft.style.height).toEqual(`${height - threshold}px`)
})
})
it('Resize control on non-small elements extend into the draggable frame area', async () => {
Expand Down
30 changes: 25 additions & 5 deletions editor/src/components/canvas/controls/bounding-box-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
} from '../../editor/store/store-hook'
import { MetadataUtils } from '../../../core/model/element-metadata-utils'
import { getMetadata } from '../../editor/store/editor-state'
import { elementHasOnlyTextChildren } from '../canvas-utils'
import { isFixedHugFillModeApplied } from '../../inspector/inspector-common'

interface NotNullRefObject<T> {
readonly current: T
Expand Down Expand Up @@ -50,19 +52,37 @@ function useBoundingBoxFromMetadataRef(
const metadataRef = useRefEditorState((store) => getMetadata(store.editor))
const scaleRef = useRefEditorState((store) => store.editor.canvas.scale)

const isNotDuringInteraction = useEditorState(
const isInteractionDraggingPastThreshold = useEditorState(
Substores.canvas,
(store) => {
return store.editor.canvas.interactionSession?.interactionData == null
return (
store.editor.canvas.interactionSession?.interactionData.type === 'DRAG' &&
store.editor.canvas.interactionSession?.interactionData.hasBeenPastThreshold
)
},
'useBoundingBoxFromMetadataRef isNotDuringInteraction',
'useBoundingBoxFromMetadataRef isInteractionDraggingPastThreshold',
)

const shouldApplySafeGap = React.useCallback(
(dimension: number, scale: number): boolean => {
return isNotDuringInteraction && dimension <= SmallElementSize / scale
if (selectedElements.length === 1) {
// For text elements only show the safe gaps if they have been resized past the initial max-content dimensions
const meta = MetadataUtils.findElementByElementPath(
metadataRef.current,
selectedElements[0],
)
if (
meta != null &&
elementHasOnlyTextChildren(meta) &&
isFixedHugFillModeApplied(metadataRef.current, selectedElements[0], 'hug')
) {
return false
}
}

return !isInteractionDraggingPastThreshold && dimension <= SmallElementSize / scale
},
[isNotDuringInteraction],
[isInteractionDraggingPastThreshold, metadataRef, selectedElements],
)

const boundingBoxCallbackRef = React.useRef(boundingBoxCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ describe('interactionStart', () => {
"x": 100,
"y": 200,
},
"hasBeenPastThreshold": false,
"hasMouseMoved": false,
"modifiers": Object {
"alt": false,
Expand Down Expand Up @@ -375,6 +376,7 @@ describe('interactionUpdate', () => {
"x": 100,
"y": 200,
},
"hasBeenPastThreshold": true,
"hasMouseMoved": true,
"modifiers": Object {
"alt": false,
Expand Down Expand Up @@ -531,6 +533,7 @@ describe('interactionHardReset', () => {
"x": 110,
"y": 210,
},
"hasBeenPastThreshold": false,
"hasMouseMoved": false,
"modifiers": Object {
"alt": false,
Expand Down Expand Up @@ -703,6 +706,7 @@ describe('interactionUpdate with user changed strategy', () => {
"x": 110,
"y": 210,
},
"hasBeenPastThreshold": false,
"hasMouseMoved": false,
"modifiers": Object {
"alt": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2056,7 +2056,7 @@ export const ModifiersKeepDeepEquality: KeepDeepEqualityCall<Modifiers> = combin
)

export const DragInteractionDataKeepDeepEquality: KeepDeepEqualityCall<DragInteractionData> =
combine9EqualityCalls(
combine10EqualityCalls(
(data) => data.dragStart,
CanvasPointKeepDeepEquality,
(data) => data.drag,
Expand All @@ -2075,6 +2075,8 @@ export const DragInteractionDataKeepDeepEquality: KeepDeepEqualityCall<DragInter
BooleanKeepDeepEquality,
(data) => data.zeroDragPermitted,
createCallWithTripleEquals<ZeroDragPermitted>(),
(data) => data.hasBeenPastThreshold,
BooleanKeepDeepEquality,
(
dragStart,
drag,
Expand All @@ -2085,6 +2087,7 @@ export const DragInteractionDataKeepDeepEquality: KeepDeepEqualityCall<DragInter
accumulatedMovement,
spacePressed,
zeroDragPermitted,
hasBeenPastThreshold,
) => {
return {
type: 'DRAG',
Expand All @@ -2097,6 +2100,7 @@ export const DragInteractionDataKeepDeepEquality: KeepDeepEqualityCall<DragInter
_accumulatedMovement: accumulatedMovement,
spacePressed: spacePressed,
zeroDragPermitted: zeroDragPermitted,
hasBeenPastThreshold: hasBeenPastThreshold,
}
},
)
Expand Down
Loading