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

All ways of creating node (+ button, dragging out edges) #8341

108 changes: 80 additions & 28 deletions app/gui2/src/components/GraphEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import CodeEditor from '@/components/CodeEditor.vue'
import ComponentBrowser from '@/components/ComponentBrowser.vue'
import {
mouseDictatedPlacement,
nonDictatedPlacement,
previousNodeDictatedPlacement,
type Environment,
} from '@/components/ComponentBrowser/placement.ts'
import { Uploader, uploadedExpression } from '@/components/GraphEditor/upload'
import PlusButton from '@/components/PlusButton.vue'
import TopBar from '@/components/TopBar.vue'
import { provideGraphNavigator } from '@/providers/graphNavigator'
import { provideGraphSelection } from '@/providers/graphSelection'
Expand All @@ -31,6 +33,9 @@ const EXECUTION_MODES = ['design', 'live']
// Difference in position between the component browser and a node for the input of the component browser to
// be placed at the same position as the node.
const COMPONENT_BROWSER_TO_NODE_OFFSET = new Vec2(20, 35)
// Assumed size of a newly created node. This is used to place the component browser.
const DEFAULT_NODE_SIZE = new Vec2(0, 24)
const gapBetweenNodes = 48.0

const viewportNode = ref<HTMLElement>()
const graphNavigator = provideGraphNavigator(viewportNode)
Expand All @@ -51,37 +56,65 @@ const nodeSelection = provideGraphSelection(graphNavigator, graphStore.nodeRects

const interactionBindingsHandler = interactionBindings.handler({
cancel: () => interaction.handleCancel(),
click: (e) => (e instanceof MouseEvent ? interaction.handleClick(e) : false),
click: (e) => (e instanceof MouseEvent ? interaction.handleClick(e, graphNavigator) : false),
})

// Return the environment for the placement of a new node. The passed nodes should be the nodes that are
// used as the source of the placement. This means, for example, the selected nodes when creating from a selection
// or the node that is being edited when creating from a port double click.
function environmentForNodes(nodeIds: IterableIterator<ExprId>): Environment {
const nodeRects = [...graphStore.nodeRects.values()]
const selectedNodeRects: Iterable<Rect> = [...nodeIds]
kazcw marked this conversation as resolved.
Show resolved Hide resolved
.map((id) => graphStore.nodeRects.get(id))
.filter((item): item is Rect => item !== undefined)
const screenBounds = graphNavigator.viewport
const mousePosition = graphNavigator.sceneMousePos
return { nodeRects, selectedNodeRects, screenBounds, mousePosition } as Environment
}

const placementEnvironment = computed(() => {
return environmentForNodes(nodeSelection.selected.values())
})

// Return the position for a new node, assuming there are currently nodes selected. If there are no nodes
// selected, return undefined.
function placementPositionForSelection() {
const hasNodeSelected = nodeSelection.selected.size > 0
if (!hasNodeSelected) return undefined
const gapBetweenNodes = 48.0
return previousNodeDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment.value, {
horizontalGap: gapBetweenNodes,
verticalGap: gapBetweenNodes,
}).position
}

// This is where the component browser should be placed when it is opened.
function targetComponentBrowserPosition() {
const editedInfo = graphStore.editedNodeInfo
const isEditingNode = editedInfo != null
const hasNodeSelected = nodeSelection.selected.size > 0
const nodeSize = new Vec2(0, 24)
if (isEditingNode) {
const targetNode = graphStore.db.nodes.get(editedInfo.id)
const targetPos = targetNode?.position ?? Vec2.Zero
return targetPos.add(COMPONENT_BROWSER_TO_NODE_OFFSET)
} else if (hasNodeSelected) {
const gapBetweenNodes = 48.0
return previousNodeDictatedPlacement(nodeSize, placementEnvironment.value, {
horizontalGap: gapBetweenNodes,
verticalGap: gapBetweenNodes,
}).position
} else {
return mouseDictatedPlacement(nodeSize, placementEnvironment.value).position
const targetPos = placementPositionForSelection()
if (targetPos != undefined) {
return targetPos
} else {
return mouseDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment.value).position
}
}
}

// This is the current position of the component browser.
const componentBrowserPosition = ref<Vec2>(Vec2.Zero)

const graphEditorSourceNode = computed(() => {
function sourceNodeForSelection() {
if (graphStore.editedNodeInfo != null) return undefined
return nodeSelection.selected.values().next().value
})
}

const componentBrowserSourceNode = ref<ExprId | undefined>(sourceNodeForSelection())

useEvent(window, 'keydown', (event) => {
interactionBindingsHandler(event) || graphBindingsHandler(event) || codeEditorHandler(event)
Expand Down Expand Up @@ -194,26 +227,31 @@ const editingNode: Interaction = {
const nodeIsBeingEdited = computed(() => graphStore.editedNodeInfo != null)
interaction.setWhen(nodeIsBeingEdited, editingNode)

const placementEnvironment = computed(() => {
const mousePosition = graphNavigator.sceneMousePos ?? Vec2.Zero
const nodeRects = [...graphStore.nodeRects.values()]
const selectedNodesIter = nodeSelection.selected.values()
const selectedNodeRects: Iterable<Rect> = [...selectedNodesIter]
.map((id) => graphStore.nodeRects.get(id))
.filter((item): item is Rect => item !== undefined)
const screenBounds = graphNavigator.viewport
const environment: Environment = { mousePosition, nodeRects, selectedNodeRects, screenBounds }
return environment
})

const creatingNode: Interaction = {
init: () => {
componentBrowserInputContent.value = ''
componentBrowserSourceNode.value = sourceNodeForSelection()
componentBrowserPosition.value = targetComponentBrowserPosition()
componentBrowserVisible.value = true
},
cancel: () => {
// Nothing to do here. We just don't create a node and the component browser will close itself.
}

const creatingNodeFromButton: Interaction = {
init: () => {
componentBrowserInputContent.value = ''
let targetPos = placementPositionForSelection()
if (targetPos == undefined) {
targetPos = nonDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment.value).position
}
componentBrowserPosition.value = targetPos
componentBrowserVisible.value = true
},
}

const creatingNodeFromPortDoubleClick: Interaction = {
init: () => {
componentBrowserInputContent.value = ''
componentBrowserVisible.value = true
},
}

Expand Down Expand Up @@ -376,6 +414,19 @@ async function readNodeFromClipboard() {
console.warn('No valid expression in clipboard.')
}
}

function handleNodeOutputPortDoubleClick(id: ExprId) {
componentBrowserSourceNode.value = id
const placementEnvironment = environmentForNodes([id].values())
componentBrowserPosition.value = previousNodeDictatedPlacement(
DEFAULT_NODE_SIZE,
placementEnvironment,
{
gap: gapBetweenNodes,
},
).position
interaction.setCurrent(creatingNodeFromPortDoubleClick)
}
</script>

<template>
Expand All @@ -395,7 +446,7 @@ async function readNodeFromClipboard() {
<GraphEdges />
</svg>
<div :style="{ transform: graphNavigator.transform }" class="htmlLayer">
<GraphNodes />
<GraphNodes @nodeOutputPortDoubleClick="handleNodeOutputPortDoubleClick" />
</div>
<ComponentBrowser
v-if="componentBrowserVisible"
Expand All @@ -407,7 +458,7 @@ async function readNodeFromClipboard() {
@canceled="onComponentBrowserCancel"
:initialContent="componentBrowserInputContent"
:initialCaretPosition="graphStore.editedNodeInfo?.range ?? [0, 0]"
:sourceNode="graphEditorSourceNode"
:sourceNode="componentBrowserSourceNode"
/>
<TopBar
v-model:mode="projectStore.executionMode"
Expand All @@ -425,6 +476,7 @@ async function readNodeFromClipboard() {
</Suspense>
</Transition>
<GraphMouse />
<PlusButton @pointerdown="interaction.setCurrent(creatingNodeFromButton)" />
</div>
</template>

Expand Down
15 changes: 11 additions & 4 deletions app/gui2/src/components/GraphEditor/GraphEdges.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<script setup lang="ts">
import GraphEdge from '@/components/GraphEditor/GraphEdge.vue'
import { GraphNavigator } from '@/providers/graphNavigator.ts'
import { injectGraphSelection } from '@/providers/graphSelection.ts'
import { injectInteractionHandler, type Interaction } from '@/providers/interactionHandler'
import { useGraphStore } from '@/stores/graph'
import { Vec2 } from '@/util/vec2.ts'
import type { ExprId } from 'shared/yjsModel.ts'

const graph = useGraphStore()
Expand All @@ -17,7 +19,7 @@ const editingEdge: Interaction = {
graph.clearUnconnected()
})
},
click(_e: MouseEvent): boolean {
click(_e: MouseEvent, graphNavigator: GraphNavigator): boolean {
if (graph.unconnectedEdge == null) return false
const source = graph.unconnectedEdge.source ?? selection?.hoveredNode
const target = graph.unconnectedEdge.target ?? selection?.hoveredPort
Expand All @@ -27,7 +29,7 @@ const editingEdge: Interaction = {
if (target == null) {
if (graph.unconnectedEdge?.disconnectedEdgeTarget != null)
disconnectEdge(graph.unconnectedEdge.disconnectedEdgeTarget)
createNodeFromEdgeDrop(source)
createNodeFromEdgeDrop(source, graphNavigator)
} else {
createEdge(source, target)
}
Expand All @@ -42,8 +44,13 @@ interaction.setWhen(() => graph.unconnectedEdge != null, editingEdge)
function disconnectEdge(target: ExprId) {
graph.setExpressionContent(target, '_')
}
function createNodeFromEdgeDrop(source: ExprId) {
console.log(`TODO: createNodeFromEdgeDrop(${JSON.stringify(source)})`)
function createNodeFromEdgeDrop(source: ExprId, graphNavigator: GraphNavigator) {
const node = graph.createNodeFromSource(graphNavigator.sceneMousePos ?? Vec2.Zero, source)
if (node != null) {
graph.setEditedNode(node, 0)
} else {
console.error('Failed to create node from edge drop.')
}
}
function createEdge(source: ExprId, target: ExprId) {
const sourceNode = graph.db.getNode(source)
Expand Down
13 changes: 11 additions & 2 deletions app/gui2/src/components/GraphEditor/GraphNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CircularMenu from '@/components/CircularMenu.vue'
import GraphVisualization from '@/components/GraphEditor/GraphVisualization.vue'
import NodeWidgetTree from '@/components/GraphEditor/NodeWidgetTree.vue'
import SvgIcon from '@/components/SvgIcon.vue'
import { useDoubleClick } from '@/composables/doubleClick'
import { injectGraphSelection } from '@/providers/graphSelection'
import { useGraphStore, type Node } from '@/stores/graph'
import { useApproach } from '@/util/animation'
Expand Down Expand Up @@ -33,7 +34,8 @@ const emit = defineEmits<{
delete: []
replaceSelection: []
'update:selected': [selected: boolean]
outputPortAction: []
outputPortClick: []
outputPortDoubleClick: []
'update:edited': [cursorPosition: number]
}>()

Expand Down Expand Up @@ -191,6 +193,13 @@ function getRelatedSpanOffset(domNode: globalThis.Node, domOffset: number): numb
}
return 0
}

const handlePortClick = useDoubleClick(
() => emit('outputPortClick'),
() => {
emit('outputPortDoubleClick')
},
).handleClick
</script>

<template>
Expand Down Expand Up @@ -245,7 +254,7 @@ function getRelatedSpanOffset(domNode: globalThis.Node, domOffset: number): numb
class="outputPortHoverArea"
@pointerenter="outputHovered = true"
@pointerleave="outputHovered = false"
@pointerdown.stop.prevent="emit('outputPortAction')"
@pointerdown="handlePortClick()"
/>
<rect class="outputPort" />
<text class="outputTypeName">{{ outputTypeName }}</text>
Expand Down
7 changes: 6 additions & 1 deletion app/gui2/src/components/GraphEditor/GraphNodes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const dragging = useDragging()
const selection = injectGraphSelection(true)
const navigator = injectGraphNavigator(true)
const emit = defineEmits<{
nodeOutputPortDoubleClick: [nodeId: ExprId]
}>()
function updateNodeContent(id: ExprId, updates: [ContentRange, string][]) {
graphStore.transact(() => {
for (const [range, content] of updates) {
Expand Down Expand Up @@ -59,7 +63,8 @@ const uploadingFiles = computed<[FileName, File][]>(() => {
@setVisualizationVisible="graphStore.setNodeVisualizationVisible(id, $event)"
@dragging="nodeIsDragged(id, $event)"
@draggingCommited="dragging.finishDrag()"
@outputPortAction="graphStore.createEdgeFromOutput(id)"
@outputPortClick="graphStore.createEdgeFromOutput(id)"
@outputPortDoubleClick="emit('nodeOutputPortDoubleClick', id)"
/>
<UploadingFile
v-for="(nameAndFile, index) in uploadingFiles"
Expand Down
55 changes: 55 additions & 0 deletions app/gui2/src/components/PlusButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<template>
<div class="parent">
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like this is an unused class, and doesn't match component name. Use PlusButton class here, as it makes tests and debugging easier if we keep that convention everywhere.

<div class="circle-plus">
<div class="vertical"></div>
<div class="horizontal"></div>
</div>
</div>
</template>

<script>
export default {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an atypical component definition. Use <script setup lang="ts">.

name: 'PlusButton',
}
</script>

<style scoped>
.circle-plus {
position: absolute;
bottom: 10px;
left: 10px;
width: 60px;
height: 60px;
border-radius: 50%;
background-color: white;
}

.circle-plus:hover {
background: rgb(230, 230, 255);
}

.circle-plus:active {
background: rgb(158, 158, 255);
}

.vertical,
.horizontal {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgb(0, 115, 219);
margin: auto;
}

.vertical {
width: 4px;
height: 70%;
}

.horizontal {
width: 70%;
height: 4px;
}
</style>
25 changes: 25 additions & 0 deletions app/gui2/src/composables/doubleClick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export function useDoubleClick(onClick: Function, onDoubleClick: Function) {
const timeBetweenClicks = 200
let lastClickTime = 0
let clickCount = 0
let singleClickTimer: ReturnType<typeof setTimeout>

const handleClick = () => {
clickCount++
if (clickCount === 1) {
singleClickTimer = setTimeout(() => {
clickCount = 0
// If within proper time range, consider it as fast click
if (Date.now() - lastClickTime >= timeBetweenClicks) {
onClick()
}
lastClickTime = Date.now()
}, timeBetweenClicks)
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic seems wrong to me, as lastClickTime is set within the timer callback only. Isn't it true that if the timer callback was allowed to execute in the first place, then the event wasn't a double-click?

Also, delaying a click handler just to allow for doubleclick action is a really bad UX. The doubleclick can only really be used in cases where the first click handler can be executed immediately anyway, e.g. allowing the node to be selected, only then to start editing it after second click. In your case, I believe it would mean first click starts edge drag, but then subsequent doubleclick simply "overrides" that action with edge creation and predefined position.

Also for handling that, you can use builtin events instead of relying on your own timer. For example, a click provides a detail field, which tells you about which click in a sequence it is (1 for first, 2 for doubleclick etc.). There is also a dedicated dblclick event, though I'd rather use the detail field instead of two handlers (because click will fire again together with dblclick).

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've removed the delay for now as for my use case it is not relevant either way.

It seems detail does not work for pointerdown events, as it is always zero. So I left this to support usage in those events.

} else if (clickCount === 2) {
clearTimeout(singleClickTimer)
clickCount = 0
onDoubleClick()
}
}
return { handleClick }
}
Loading