mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 08:14:45 +00:00
Fix node drops on fast drags and tidy node menus
- Work out a drop from the release itself, so a quick drag still regrows or moves the node - Draw the chosen node above the others - Write curves spaced like the rest of the file - Keep focus off a node's controls after its menu closes - Size dropdown rows like the context menu's
This commit is contained in:
1 parent
8fe8942f5d
commit
1efb4c14db
7 files changed
+51
-22
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { canBecomeCurve, curveAt, curveFrom, isCurve, keyframesFrom, plotRange, roundedNumber, samplesOf, shapeFrom, withAddedKeyframe, withKeyframe, withoutKeyframe, withSetting, withShape } from './curves'
|
||||
import { canBecomeCurve, curveAt, curveFrom, curveLiteral, isCurve, keyframesFrom, plotRange, roundedNumber, samplesOf, shapeFrom, withAddedKeyframe, withKeyframe, withoutKeyframe, withSetting, withShape } from './curves'
|
||||
|
||||
describe('isCurve', () => {
|
||||
it('holds only for keyframes and named shapes', () => {
|
||||
@@ -93,6 +93,13 @@ describe('plotRange', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('curveLiteral', () => {
|
||||
it('writes keyframes and shapes spaced like hand-written JSON', () => {
|
||||
expect(curveLiteral([[0, 1.4], [1, 0.4]])).toBe('[[0, 1.4], [1, 0.4]]')
|
||||
expect(curveLiteral({ shape: 'power', from: 0, to: 2 })).toBe('{"shape": "power", "from": 0, "to": 2}')
|
||||
})
|
||||
})
|
||||
|
||||
describe('roundedNumber', () => {
|
||||
it('reads a number with two decimals at most', () => {
|
||||
expect(roundedNumber(1.3333)).toBe('1.33')
|
||||
|
||||
@@ -170,6 +170,11 @@ export function plotRange(samples: number[]): [number, number] {
|
||||
return [low < 0 ? low - margin : 0, high + margin]
|
||||
}
|
||||
|
||||
/** The curve as the JSON written to the file, spaced like hand-written JSON: {@code [[0, 1.4], [1, 0.4]]}. */
|
||||
export function curveLiteral(curve: unknown): string {
|
||||
return JSON.stringify(curve).replaceAll(',', ', ').replaceAll('":', '": ')
|
||||
}
|
||||
|
||||
/** A number only to be read, never typed back: two decimals at most, like 1.3 or 0.25. */
|
||||
export function roundedNumber(value: number): string {
|
||||
return String(Math.round(value * READ_PRECISION) / READ_PRECISION)
|
||||
|
||||
@@ -37,6 +37,7 @@ export const NodeBox = memo(function NodeBox({ placed, rows, selected, dropTarge
|
||||
className={cn(
|
||||
'absolute cursor-move rounded-md bg-popover text-[11.5px] outline -outline-offset-1 dark:bg-secondary',
|
||||
dropTarget ? 'outline-2 outline-success' : selected ? 'outline-2 outline-primary' : 'outline-1 outline-overlay-border',
|
||||
selected && 'z-[5]',
|
||||
offset !== null && 'z-10 shadow-lg'
|
||||
)}
|
||||
style={{ left: placed.x + (offset?.x ?? 0), top: placed.y + (offset?.y ?? 0), width: NODE_WIDTH, height: placed.height }}
|
||||
|
||||
@@ -105,35 +105,46 @@ export function NodeCanvas({ roots, types, values, file, selectedId, locked, sug
|
||||
}
|
||||
}
|
||||
|
||||
// The gesture as it stands at {@code event}, worked out from the event itself: a release can come before the last
|
||||
// move was drawn.
|
||||
const gestureAt = (event: React.PointerEvent): NodeGesture | null => {
|
||||
if (gesture?.kind === 'moving') {
|
||||
const [dx, dy] = [event.clientX - gesture.start.x, event.clientY - gesture.start.y]
|
||||
const moved = gesture.moved || Math.hypot(dx, dy) > DRAG_START_PIXELS
|
||||
return { ...gesture, moved, offset: moved ? { x: dx / view.camera.scale, y: dy / view.camera.scale } : gesture.offset }
|
||||
}
|
||||
if (gesture?.kind === 'regrowing') {
|
||||
const pointer = view.graphPoint(event.clientX, event.clientY)
|
||||
const over = nodeAt(layout.nodes, pointer)
|
||||
const takesIt = over !== null && over !== gesture.placed && canRegrow(gesture.placed.node, over.node)
|
||||
return { ...gesture, pointer, target: takesIt ? over : null }
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const pointerMove = (event: React.PointerEvent): void => {
|
||||
if (panFrom.current !== null) {
|
||||
view.panBy(event.clientX - panFrom.current.x, event.clientY - panFrom.current.y)
|
||||
panFrom.current = { x: event.clientX, y: event.clientY }
|
||||
return
|
||||
}
|
||||
if (gesture?.kind === 'moving') {
|
||||
const [dx, dy] = [event.clientX - gesture.start.x, event.clientY - gesture.start.y]
|
||||
const moved = gesture.moved || Math.hypot(dx, dy) > DRAG_START_PIXELS
|
||||
setGesture({ ...gesture, moved, offset: moved ? { x: dx / view.camera.scale, y: dy / view.camera.scale } : gesture.offset })
|
||||
} else if (gesture?.kind === 'regrowing') {
|
||||
const pointer = view.graphPoint(event.clientX, event.clientY)
|
||||
const over = nodeAt(layout.nodes, pointer)
|
||||
const takesIt = over !== null && over !== gesture.placed && canRegrow(gesture.placed.node, over.node)
|
||||
setGesture({ ...gesture, pointer, target: takesIt ? over : null })
|
||||
if (gesture !== null) {
|
||||
setGesture(gestureAt(event))
|
||||
}
|
||||
}
|
||||
|
||||
const pointerUp = (): void => {
|
||||
const pointerUp = (event: React.PointerEvent): void => {
|
||||
panFrom.current = null
|
||||
const finished = gestureAt(event)
|
||||
setGesture(null)
|
||||
if (gesture?.kind === 'moving' && gesture.moved) {
|
||||
const { placed, offset } = gesture
|
||||
if (finished?.kind === 'moving' && finished.moved) {
|
||||
const { placed, offset } = finished
|
||||
const dropped = { x: snappedToGrid(placed.x + offset.x), y: snappedToGrid(placed.y + offset.y) }
|
||||
if (dropped.x !== placed.x || dropped.y !== placed.y) {
|
||||
setPositions((current) => ({ ...current, [placed.id]: dropped }))
|
||||
}
|
||||
} else if (gesture?.kind === 'regrowing' && gesture.target !== null) {
|
||||
edit({ op: 'move', path: gesture.placed.node.path, toList: gesture.target.node.childList! })
|
||||
} else if (finished?.kind === 'regrowing' && finished.target !== null) {
|
||||
edit({ op: 'move', path: finished.placed.node.path, toList: finished.target.node.childList! })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuShortcut, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger } from '@/components/ui/context-menu'
|
||||
import type { GraphEdit, GraphNode } from '../bridge/types'
|
||||
import { canBecomeCurve, curveFrom, isCurve, roundedNumber, samplesOf } from '../editor/curves'
|
||||
import { canBecomeCurve, curveFrom, curveLiteral, isCurve, roundedNumber, samplesOf } from '../editor/curves'
|
||||
import { idOf } from '../editor/nodeLayout'
|
||||
import type { SettingRow } from './nodeEditing'
|
||||
import { useKeyText } from './useBindings'
|
||||
@@ -58,7 +58,7 @@ function CurveItems({ setting, locked, onEdit, onOpenCurve }: { setting: Setting
|
||||
}
|
||||
const set = (literal: string): void => onEdit({ op: 'set', object: fold.path, key: option.name, literal })
|
||||
const makeCurve = (): void => {
|
||||
set(JSON.stringify(curveFrom(current)))
|
||||
set(curveLiteral(curveFrom(current)))
|
||||
onOpenCurve(setting.id)
|
||||
}
|
||||
return (
|
||||
|
||||
@@ -6,7 +6,7 @@ import { NODE_HEADING, NODE_ROW, type NodeRow } from '../editor/nodeRows'
|
||||
import { idOfFold, type SettingsFold } from '../editor/settingFolds'
|
||||
import { defaultLiteral, defaultText } from '../editor/settingText'
|
||||
import { useNodeEditing } from './nodeEditing'
|
||||
import { NodeSettingRow, ROW_INDENT, RowButton } from './NodeSettingRow'
|
||||
import { keepFocusOnCanvas, NodeSettingRow, ROW_INDENT, RowButton } from './NodeSettingRow'
|
||||
|
||||
const ADD_SETTING_TEXT = '+ Setting'
|
||||
|
||||
@@ -74,7 +74,7 @@ function AddSettingRow({ addable }: { addable: SettingsFold[] }): React.JSX.Elem
|
||||
{ADD_SETTING_TEXT}
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="min-w-[180px]">
|
||||
<DropdownMenuContent align="start" className="min-w-[180px]" onCloseAutoFocus={keepFocusOnCanvas}>
|
||||
{addable.length === 1 ? (
|
||||
<UnsetItems fold={addable[0]} />
|
||||
) : (
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useRef, useState } from 'react'
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
|
||||
import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/popover'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { isCurve } from '../editor/curves'
|
||||
import { curveLiteral, isCurve } from '../editor/curves'
|
||||
import { NODE_ROW } from '../editor/nodeRows'
|
||||
import { defaultText, isNumber, isStringList, literalOf, NOT_SET, problemWith, textOf } from '../editor/settingText'
|
||||
import { suggestionSourceOf } from '../editor/suggestions'
|
||||
@@ -55,6 +55,11 @@ interface RowButtonProps {
|
||||
onClick(): void
|
||||
}
|
||||
|
||||
/** A menu in a node gives no focus back to what opened it, which would leave a focus ring on the node. */
|
||||
export function keepFocusOnCanvas(event: Event): void {
|
||||
event.preventDefault()
|
||||
}
|
||||
|
||||
/** The small round control at a row's right end: × removes, + adds. */
|
||||
export function RowButton({ glyph, tone, hidden = false, title, onClick }: RowButtonProps): React.JSX.Element {
|
||||
return (
|
||||
@@ -130,7 +135,7 @@ function ChoiceValue({ row, editable }: SettingRowProps): React.JSX.Element {
|
||||
<IdeIcon name="chevronRight" className="size-3 shrink-0 rotate-90 text-secondary-text" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start">
|
||||
<DropdownMenuContent align="start" onCloseAutoFocus={keepFocusOnCanvas}>
|
||||
{constants.map((constant) => (
|
||||
<DropdownMenuItem
|
||||
key={constant.name}
|
||||
@@ -163,7 +168,7 @@ function CurveValue({ row, editable }: SettingRowProps): React.JSX.Element {
|
||||
</button>
|
||||
</PopoverAnchor>
|
||||
<PopoverContent onOpenAutoFocus={(event) => event.preventDefault()}>
|
||||
<CurveEditor value={row.value} locked={!editable} onWrite={(next) => onEdit({ op: 'set', object: row.fold.path, key: row.option.name, literal: JSON.stringify(next) })} />
|
||||
<CurveEditor value={row.value} locked={!editable} onWrite={(next) => onEdit({ op: 'set', object: row.fold.path, key: row.option.name, literal: curveLiteral(next) })} />
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user