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

Close dropdowns #9206

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 0 additions & 1 deletion app/gui2/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const componentBrowserBindings = defineKeybinds('component-browser', {
applySuggestion: ['Tab'],
acceptSuggestion: ['Enter'],
acceptInput: ['Mod+Enter'],
cancelEditing: ['Escape'],
moveUp: ['ArrowUp'],
moveDown: ['ArrowDown'],
})
Expand Down
41 changes: 21 additions & 20 deletions app/gui2/src/components/ComponentBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import ToggleIcon from '@/components/ToggleIcon.vue'
import { useApproach } from '@/composables/animation'
import { useEvent, useResizeObserver } from '@/composables/events'
import type { useNavigator } from '@/composables/navigator'
import { injectInteractionHandler, type Interaction } from '@/providers/interactionHandler'
import { useGraphStore } from '@/stores/graph'
import type { RequiredImport } from '@/stores/graph/imports'
import { useProjectStore } from '@/stores/project'
import { groupColorStyle, useSuggestionDbStore } from '@/stores/suggestionDatabase'
import { SuggestionKind, type SuggestionEntry } from '@/stores/suggestionDatabase/entry'
import type { VisualizationDataSource } from '@/stores/visualization'
import { targetIsOutside } from '@/util/autoBlur'
import { tryGetIndex } from '@/util/data/array'
import type { Opt } from '@/util/data/opt'
import { allRanges } from '@/util/data/range'
Expand All @@ -35,6 +37,7 @@ const COMPONENT_BROWSER_TO_NODE_OFFSET = new Vec2(-4, -4)
const projectStore = useProjectStore()
const suggestionDbStore = useSuggestionDbStore()
const graphStore = useGraphStore()
const interaction = injectInteractionHandler()

const props = defineProps<{
nodePosition: Vec2
Expand All @@ -47,7 +50,24 @@ const emit = defineEmits<{
canceled: []
}>()

const cbOpen: Interaction = {
cancel: () => {
emit('canceled')
},
click: (e: PointerEvent) => {
if (targetIsOutside(e, cbRoot)) {
if (input.anyChange.value) {
acceptInput()
} else {
interaction.cancel(cbOpen)
}
}
return false
},
}

onMounted(() => {
interaction.setCurrent(cbOpen)
input.reset(props.usage)
if (inputField.value != null) {
inputField.value.focus({ preventScroll: true })
Expand Down Expand Up @@ -159,23 +179,6 @@ function preventNonInputDefault(e: Event) {
}
}

useEvent(
window,
'pointerdown',
(event) => {
if (event.button !== 0) return
if (!(event.target instanceof Element)) return
if (!cbRoot.value?.contains(event.target)) {
if (input.anyChange.value) {
emit('accepted', input.code.value, input.importsToAdd())
} else {
emit('canceled')
}
}
},
{ capture: true },
)

const inputElement = ref<HTMLElement>()
const inputSize = useResizeObserver(inputElement, false)

Expand Down Expand Up @@ -357,6 +360,7 @@ function acceptSuggestion(index: Opt<Component> = null) {

function acceptInput() {
emit('accepted', input.code.value.trim(), input.importsToAdd())
interaction.end(cbOpen)
}

// === Key Events Handler ===
Expand Down Expand Up @@ -386,9 +390,6 @@ const handler = componentBrowserBindings.handler({
}
scrolling.scrollWithTransition({ type: 'selected' })
},
cancelEditing() {
emit('canceled')
},
})
</script>

Expand Down
119 changes: 32 additions & 87 deletions app/gui2/src/components/GraphEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { keyboardBusy, keyboardBusyExceptIn, useEvent } from '@/composables/even
import { useStackNavigator } from '@/composables/stackNavigator'
import { provideGraphNavigator } from '@/providers/graphNavigator'
import { provideGraphSelection } from '@/providers/graphSelection'
import { provideInteractionHandler, type Interaction } from '@/providers/interactionHandler'
import { provideInteractionHandler } from '@/providers/interactionHandler'
import { provideWidgetRegistry } from '@/providers/widgetRegistry'
import { useGraphStore, type NodeId } from '@/stores/graph'
import type { RequiredImport } from '@/stores/graph/imports'
Expand Down Expand Up @@ -110,7 +110,7 @@ const nodeSelection = provideGraphSelection(graphNavigator, graphStore.nodeRects

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

// Return the environment for the placement of a new node. The passed nodes should be the nodes that are
Expand Down Expand Up @@ -162,7 +162,8 @@ function sourcePortForSelection() {
}

useEvent(window, 'keydown', (event) => {
;(!keyboardBusy() && (interactionBindingsHandler(event) || graphBindingsHandler(event))) ||
interactionBindingsHandler(event) ||
(!keyboardBusy() && graphBindingsHandler(event)) ||
(!keyboardBusyExceptIn(codeEditorArea.value) && codeEditorHandler(event))
})
useEvent(window, 'pointerdown', interactionBindingsHandler, { capture: true })
Expand Down Expand Up @@ -208,7 +209,7 @@ const graphBindingsHandler = graphBindings.handler({
openComponentBrowser() {
if (keyboardBusy()) return false
if (graphNavigator.sceneMousePos != null && !componentBrowserVisible.value) {
interaction.setCurrent(creatingNode)
showComponentBrowser()
}
},
newNode() {
Expand Down Expand Up @@ -317,11 +318,10 @@ const graphBindingsHandler = graphBindings.handler({
})

const { handleClick } = useDoubleClick(
(e: MouseEvent) => {
(e: PointerEvent) => {
graphBindingsHandler(e)
},
() => {
if (keyboardBusy()) return false
stackNavigator.exitNode()
},
)
Expand Down Expand Up @@ -360,62 +360,25 @@ const groupColors = computed(() => {
return styles
})

const editingNode: Interaction = {
init: () => {
// component browser usage is set in `graphStore.editedNodeInfo` watch
componentBrowserNodePosition.value = targetComponentBrowserNodePosition()
},
cancel: () => {
hideComponentBrowser()
graphStore.editedNodeInfo = undefined
},
}
const nodeIsBeingEdited = computed(() => graphStore.editedNodeInfo != null)
interaction.setWhen(nodeIsBeingEdited, editingNode)

const creatingNode: Interaction = {
init: () => {
componentBrowserUsage.value = { type: 'newNode', sourcePort: sourcePortForSelection() }
componentBrowserNodePosition.value = targetComponentBrowserNodePosition()
componentBrowserVisible.value = true
},
cancel: hideComponentBrowser,
}

const creatingNodeFromButton: Interaction = {
init: () => {
componentBrowserUsage.value = { type: 'newNode', sourcePort: sourcePortForSelection() }
let targetPos = placementPositionForSelection()
if (targetPos == undefined) {
targetPos = nonDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment.value).position
}
componentBrowserNodePosition.value = targetPos
componentBrowserVisible.value = true
},
cancel: hideComponentBrowser,
}

const creatingNodeFromPortDoubleClick: Interaction = {
init: () => {
// component browser usage is set in event handler
componentBrowserVisible.value = true
},
cancel: hideComponentBrowser,
function showComponentBrowser(nodePosition?: Vec2, usage?: Usage) {
componentBrowserUsage.value = usage ?? { type: 'newNode', sourcePort: sourcePortForSelection() }
componentBrowserNodePosition.value = nodePosition ?? targetComponentBrowserNodePosition()
componentBrowserVisible.value = true
}

const creatingNodeFromEdgeDrop: Interaction = {
init: () => {
// component browser usage is set in event handler
componentBrowserVisible.value = true
},
cancel: hideComponentBrowser,
function startCreatingNodeFromButton() {
const targetPos =
placementPositionForSelection() ??
nonDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment.value).position
showComponentBrowser(targetPos)
}

function hideComponentBrowser() {
graphStore.editedNodeInfo = undefined
componentBrowserVisible.value = false
}

function onComponentBrowserCommit(content: string, requiredImports: RequiredImport[]) {
function commitComponentBrowser(content: string, requiredImports: RequiredImport[]) {
if (content != null) {
if (graphStore.editedNodeInfo) {
// We finish editing a node.
Expand All @@ -432,29 +395,21 @@ function onComponentBrowserCommit(content: string, requiredImports: RequiredImpo
if (createdNode) nodeSelection.setSelection(new Set([createdNode]))
}
}
// Finish interaction. This should also hide component browser.
interaction.setCurrent(undefined)
}

function onComponentBrowserCancel() {
// Finish interaction. This should also hide component browser.
interaction.setCurrent(undefined)
hideComponentBrowser()
}

// Watch the `editedNode` in the graph store
watch(
() => graphStore.editedNodeInfo,
(editedInfo) => {
if (editedInfo) {
componentBrowserNodePosition.value = targetComponentBrowserNodePosition()
componentBrowserUsage.value = {
showComponentBrowser(undefined, {
type: 'editNode',
node: editedInfo.id,
cursorPos: editedInfo.initialCursorPos,
}
componentBrowserVisible.value = true
})
} else {
componentBrowserVisible.value = false
hideComponentBrowser()
}
},
)
Expand Down Expand Up @@ -599,30 +554,23 @@ async function readNodeFromExcelClipboard(
}

function handleNodeOutputPortDoubleClick(id: AstId) {
componentBrowserUsage.value = { type: 'newNode', sourcePort: id }
const srcNode = graphStore.db.getPatternExpressionNodeId(id)
if (srcNode == null) {
console.error('Impossible happened: Double click on port not belonging to any node: ', id)
return
}
const placementEnvironment = environmentForNodes([srcNode].values())
componentBrowserNodePosition.value = previousNodeDictatedPlacement(
DEFAULT_NODE_SIZE,
placementEnvironment,
{
horizontalGap: gapBetweenNodes,
verticalGap: gapBetweenNodes,
},
).position
interaction.setCurrent(creatingNodeFromPortDoubleClick)
const position = previousNodeDictatedPlacement(DEFAULT_NODE_SIZE, placementEnvironment, {
horizontalGap: gapBetweenNodes,
verticalGap: gapBetweenNodes,
}).position
showComponentBrowser(position, { type: 'newNode', sourcePort: id })
}

const stackNavigator = useStackNavigator()

function handleEdgeDrop(source: AstId, position: Vec2) {
componentBrowserUsage.value = { type: 'newNode', sourcePort: source }
componentBrowserNodePosition.value = position
interaction.setCurrent(creatingNodeFromEdgeDrop)
showComponentBrowser(position, { type: 'newNode', sourcePort: source })
}
</script>

Expand All @@ -634,7 +582,7 @@ function handleEdgeDrop(source: AstId, position: Vec2) {
:style="groupColors"
v-on.="graphNavigator.events"
v-on..="nodeSelection.events"
@click="handleClick"
@pointerdown="handleClick"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change? I deliberately changed it to click once.

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 was preventing some user actions triggering multiple handlers, but we can do that some other way. Fixing it isn't needed for this PR and it doesn't currently cause any observed problems.

@dragover.prevent
@drop.prevent="handleFileDrop($event)"
>
Expand All @@ -652,8 +600,8 @@ function handleEdgeDrop(source: AstId, position: Vec2) {
:navigator="graphNavigator"
:nodePosition="componentBrowserNodePosition"
:usage="componentBrowserUsage"
@accepted="onComponentBrowserCommit"
@canceled="onComponentBrowserCancel"
@accepted="commitComponentBrowser"
@canceled="hideComponentBrowser"
/>
<TopBar
v-model:recordMode="projectStore.recordMode"
Expand All @@ -669,11 +617,7 @@ function handleEdgeDrop(source: AstId, position: Vec2) {
@zoomIn="graphNavigator.scale *= 1.1"
@zoomOut="graphNavigator.scale *= 0.9"
/>
<PlusButton
@click.stop="interaction.setCurrent(creatingNodeFromButton)"
@pointerdown.stop
@pointerup.stop
/>
<PlusButton @pointerdown.stop="startCreatingNodeFromButton()" @click.stop @pointerup.stop />
<Transition>
<Suspense ref="codeEditorArea">
<CodeEditor v-if="showCodeEditor" />
Expand All @@ -688,6 +632,7 @@ function handleEdgeDrop(source: AstId, position: Vec2) {
position: relative;
contain: layout;
overflow: clip;
user-select: none;
--group-color-fallback: #006b8a;
--node-color-no-type: #596b81;
}
Expand Down
2 changes: 1 addition & 1 deletion app/gui2/src/components/GraphEditor/GraphEdge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ const connected = computed(() => isConnected(props.edge))
class="edge io"
:data-source-node-id="sourceNode"
:data-target-node-id="targetNode"
@pointerdown="click"
@pointerdown.stop="click"
@pointerenter="hovered = true"
@pointerleave="hovered = false"
/>
Expand Down
2 changes: 1 addition & 1 deletion app/gui2/src/components/GraphEditor/GraphEdges.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const editingEdge: Interaction = {
cancel() {
graph.clearUnconnected()
},
click(_e: MouseEvent, graphNavigator: GraphNavigator): boolean {
click(_e: PointerEvent, graphNavigator: GraphNavigator): boolean {
if (graph.unconnectedEdge == null) return false
let source: AstId | undefined
let sourceNode: NodeId | undefined
Expand Down
Loading
Loading