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

Offset distance indicators for grid absolute move #6339

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Changes from 2 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
335 changes: 328 additions & 7 deletions editor/src/components/canvas/controls/grid-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { AnimationControls } from 'framer-motion'
import { motion, useAnimationControls } from 'framer-motion'
import type { CSSProperties } from 'react'
import React from 'react'
import type { Sides } from 'utopia-api/core'
import type { ElementPath } from 'utopia-shared/src/types'
import type { GridDimension } from '../../../components/inspector/common/css-utils'
import {
Expand All @@ -23,6 +24,7 @@ import {
import type { CanvasPoint, CanvasRectangle } from '../../../core/shared/math-utils'
import {
canvasPoint,
canvasRectangle,
isFiniteRectangle,
isInfinityRectangle,
pointsEqual,
Expand All @@ -39,9 +41,11 @@ import {
} from '../../../core/shared/optics/optic-creators'
import { toFirst } from '../../../core/shared/optics/optic-utilities'
import type { Optic } from '../../../core/shared/optics/optics'
import { optionalMap } from '../../../core/shared/optional-utils'
import { assertNever } from '../../../core/shared/utils'
import { Modifier } from '../../../utils/modifiers'
import { when } from '../../../utils/react-conditionals'
import { useColorTheme, UtopiaStyles } from '../../../uuiui'
import { useDispatch } from '../../editor/store/dispatch-context'
import { Substores, useEditorState, useRefEditorState } from '../../editor/store/store-hook'
import { useRollYourOwnFeatures } from '../../navigator/left-pane/roll-your-own-pane'
Expand All @@ -52,16 +56,13 @@ import type {
GridResizeEdgeProperties,
} from '../canvas-strategies/interaction-state'
import {
GridResizeEdges,
createInteractionViaMouse,
gridAxisHandle,
gridCellHandle,
gridResizeEdgeProperties,
GridResizeEdges,
gridResizeHandle,
} from '../canvas-strategies/interaction-state'
import { windowToCanvasCoordinates } from '../dom-lookup'
import { CanvasOffsetWrapper } from './canvas-offset-wrapper'
import { useColorTheme, UtopiaStyles } from '../../../uuiui'
import { gridCellTargetId } from '../canvas-strategies/strategies/grid-helpers'
import { resizeBoundingBoxFromSide } from '../canvas-strategies/strategies/resize-helpers'
import type { EdgePosition } from '../canvas-types'
Expand All @@ -72,11 +73,11 @@ import {
EdgePositionRight,
EdgePositionTop,
} from '../canvas-types'
import { windowToCanvasCoordinates } from '../dom-lookup'
import type { Axis } from '../gap-utils'
import { useCanvasAnimation } from '../ui-jsx-canvas-renderer/animation-context'
import { CanvasOffsetWrapper } from './canvas-offset-wrapper'
import { CanvasLabel } from './select-mode/controls-common'
import { optionalMap } from '../../../core/shared/optional-utils'
import type { Sides } from 'utopia-api/core'
import type { Axis } from '../gap-utils'
import { useMaybeHighlightElement } from './select-mode/select-mode-hooks'

const CELL_ANIMATION_DURATION = 0.15 // seconds
Expand Down Expand Up @@ -962,11 +963,331 @@ export const GridControls = controlForStrategyMemoized<GridControlsProps>(({ tar
}}
/>
) : null}
<AbsoluteDistanceIndicators targetRootCell={targetRootCell} />
</CanvasOffsetWrapper>
</React.Fragment>
)
})

const AbsoluteDistanceIndicators = React.memo(
(props: { targetRootCell: GridCellCoordinates | null }) => {
const colorTheme = useColorTheme()

const cellFrame = useEditorState(
Substores.metadata,
(store) => {
if (store.editor.selectedViews.length !== 1) {
return null
}
const meta = MetadataUtils.findElementByElementPath(
store.editor.jsxMetadata,
store.editor.selectedViews[0],
)
if (
!MetadataUtils.isPositionAbsolute(meta) ||
meta?.globalFrame == null ||
!isFiniteRectangle(meta.globalFrame)
) {
return null
}
return meta.globalFrame
},
'AbsoluteDistanceIndicators cellFrame',
)
const canvasScale = useEditorState(
Substores.canvasOffset,
(store) => store.editor.canvas.scale,
'AbsoluteDistanceIndicators canvasScale',
)

const canvasOffset = useEditorState(
Substores.canvasOffset,
(store) => store.editor.canvas.roundedCanvasOffset,
'AbsoluteDistanceIndicators canvasOffset',
)

const targetCellBoundingBox = React.useMemo(() => {
if (props.targetRootCell == null) {
return null
}
const element = document.querySelector(
`[data-grid-row="${props.targetRootCell.row}"]` +
`[data-grid-column="${props.targetRootCell.column}"]`,
)
const boundingBox = element?.getBoundingClientRect()
if (boundingBox == null) {
return null
}

const canvasOrigin = windowToCanvasCoordinates(
canvasScale,
canvasOffset,
windowPoint({ x: boundingBox.left, y: boundingBox.top }),
).canvasPositionRounded
const canvasRect = canvasRectangle({
x: canvasOrigin.x,
y: canvasOrigin.y,
width: boundingBox.width * canvasScale,
height: boundingBox.height * canvasScale,
})

return canvasRect
}, [props.targetRootCell, canvasScale, canvasOffset])

const topDist = React.useMemo(() => {
if (targetCellBoundingBox == null || cellFrame == null) {
return 0
}
return cellFrame.y - targetCellBoundingBox.y
}, [cellFrame, targetCellBoundingBox])
const leftDist = React.useMemo(() => {
if (targetCellBoundingBox == null || cellFrame == null) {
return 0
}
return cellFrame.x - targetCellBoundingBox.x
}, [cellFrame, targetCellBoundingBox])

const positioning = React.useMemo(() => {
if (cellFrame == null || targetCellBoundingBox == null) {
return null
}

function position(
Copy link
Contributor

@liady liady Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest maybe positioning them in the middle of the axes (like the blue indicators):
image
vs
Monosnap flawless-santanaraptor - Utopia 2024-09-09 17-01-54

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't agree on this one: the positioning at the origin on the element is to better clarify this is an offset (relative to the 0;0 coord of the bounding rectangle)

Copy link
Contributor

@liady liady Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though it's not changing this meaning, since the "left" indicator will still be from the left side and the "top" will still be from the top side (they will just be positioned in the middle of the side itself).
but it's very minor and already clear, so np 👍

wantedCoord: 'x' | 'y',
cell: CanvasRectangle,
root: CanvasRectangle,
dominantDistance: number,
otherDistance: number,
): { left: number; top: number } {
const otherCoord = wantedCoord === 'x' ? 'y' : 'x'
const dimension = wantedCoord === 'x' ? 'width' : 'height'
const dominant =
cell[wantedCoord] < root[wantedCoord] || dominantDistance < 32 || otherDistance < 0
ruggi marked this conversation as resolved.
Show resolved Hide resolved
? root[wantedCoord] + root[dimension] / 2
: Math.max(root[wantedCoord], cell[wantedCoord])
const other = otherDistance < 0 ? cell[otherCoord] : root[otherCoord]
if (wantedCoord === 'x') {
return {
left: dominant,
top: other,
}
} else {
return {
left: other,
top: dominant,
}
}
}

function compensationNegative(
wantedCoord: 'x' | 'y',
cell: CanvasRectangle,
root: CanvasRectangle,
dist: number,
): { width: number; height: number; left: number; top: number } | null {
const otherCoord = wantedCoord === 'x' ? 'y' : 'x'
const dimension = wantedCoord === 'x' ? 'width' : 'height'

const shouldCompensate =
dist < 0 && cell[wantedCoord] > root[wantedCoord] + root[dimension] / 2
if (!shouldCompensate) {
return null
}

const size = Math.abs(root[wantedCoord] + root[dimension] / 2 - cell[wantedCoord])
const dominant = root[wantedCoord] + root[dimension] / 2
const other = cell[otherCoord]

return wantedCoord === 'x'
? {
width: size,
height: 1,
top: other,
left: dominant,
}
: {
width: 1,
height: size,
top: dominant,
left: other,
}
}

function compensationPositive(
wantedCoord: 'x' | 'y',
cell: CanvasRectangle,
root: CanvasRectangle,
dist: number,
): { width: number; height: number; left: number; top: number } | null {
const otherCoord = wantedCoord === 'x' ? 'y' : 'x'
const dimension = wantedCoord === 'x' ? 'width' : 'height'

const shouldCompensate = dist > 0 && cell[wantedCoord] > root[wantedCoord] + root[dimension]
if (!shouldCompensate) {
return null
}

const size = Math.abs(root[wantedCoord] + root[dimension] / 2 - cell[wantedCoord])
const other = root[otherCoord]
const dominant = root[wantedCoord] + root[dimension] / 2

return wantedCoord === 'x'
? {
width: size,
height: 1,
top: other,
left: dominant,
}
: {
height: size,
width: 1,
left: other,
top: dominant,
}
}

const topIndicator = {
...position('x', cellFrame, targetCellBoundingBox, leftDist, topDist),
compensateNegative: compensationNegative('x', cellFrame, targetCellBoundingBox, topDist),
compensatePositive: compensationPositive('x', cellFrame, targetCellBoundingBox, topDist),
}

const leftIndicator = {
...position('y', cellFrame, targetCellBoundingBox, leftDist, leftDist),
compensateNegative: compensationNegative('y', cellFrame, targetCellBoundingBox, leftDist),
compensatePositive: compensationPositive('y', cellFrame, targetCellBoundingBox, leftDist),
}

return { topIndicator, leftIndicator }
}, [cellFrame, targetCellBoundingBox, leftDist, topDist])

if (targetCellBoundingBox == null || cellFrame == null || positioning == null) {
return null
}

const backgroundColor = colorTheme.brandNeonPink.value
const dashedBorder = `1px dashed ${backgroundColor}`

return (
<React.Fragment>
{/* top distance */}
<React.Fragment>
<div
style={{
position: 'absolute',
borderLeft: dashedBorder,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',

left: positioning.topIndicator.left,
top: positioning.topIndicator.top,
width: 1,
height: Math.abs(topDist),
}}
>
<span
style={{
backgroundColor: backgroundColor,
padding: '0px 2px',
borderRadius: 2,
fontSize: 9,
color: '#fff',
}}
>
{topDist}
</span>
</div>
{/* compensate */}
{positioning.topIndicator.compensateNegative != null ? (
<div
style={{
position: 'absolute',
borderTop: dashedBorder,

left: positioning.topIndicator.compensateNegative.left,
top: positioning.topIndicator.compensateNegative.top,
width: positioning.topIndicator.compensateNegative.width,
height: positioning.topIndicator.compensateNegative.height,
}}
/>
) : null}
{positioning.topIndicator.compensatePositive != null ? (
<div
style={{
position: 'absolute',
borderTop: dashedBorder,

left: positioning.topIndicator.compensatePositive.left,
top: positioning.topIndicator.compensatePositive.top,
width: positioning.topIndicator.compensatePositive.width,
height: positioning.topIndicator.compensatePositive.height,
}}
/>
) : null}
</React.Fragment>

{/* left distance */}
<React.Fragment>
<div
style={{
position: 'absolute',
borderTop: dashedBorder,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',

left: positioning.leftIndicator.left,
top: positioning.leftIndicator.top,
width: Math.abs(leftDist),
height: 1,
}}
>
<span
style={{
backgroundColor: backgroundColor,
padding: '0px 2px',
borderRadius: 2,
fontSize: 9,
color: '#fff',
}}
>
{leftDist}
</span>
</div>
{/* compensate */}
{positioning.leftIndicator.compensateNegative != null ? (
<div
style={{
position: 'absolute',
borderLeft: dashedBorder,

left: positioning.leftIndicator.compensateNegative.left,
top: positioning.leftIndicator.compensateNegative.top,
width: positioning.leftIndicator.compensateNegative.width,
height: positioning.leftIndicator.compensateNegative.height,
}}
/>
) : null}
{positioning.leftIndicator.compensatePositive != null ? (
<div
style={{
position: 'absolute',
borderLeft: dashedBorder,

left: positioning.leftIndicator.compensatePositive.left,
top: positioning.leftIndicator.compensatePositive.top,
width: positioning.leftIndicator.compensatePositive.width,
height: positioning.leftIndicator.compensatePositive.height,
}}
/>
) : null}
</React.Fragment>
</React.Fragment>
)
},
)

function useCellAnimation(params: {
disabled: boolean
gridPath: ElementPath | null
Expand Down
Loading