Skip to content

Commit

Permalink
Call showAll on opening projects (#9974)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitvakatu committed May 17, 2024
1 parent 5c06535 commit 6e9b4ec
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/gui2/e2e/componentBrowser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test('Graph Editor pans to Component Browser', async ({ page }) => {
await locate.graphNodeByBinding(page, 'final').click()
await page.mouse.move(100, 80)
await page.mouse.down({ button: 'middle' })
await page.mouse.move(100, 700)
await page.mouse.move(100, 1200)
await page.mouse.up({ button: 'middle' })
await expect(locate.graphNodeByBinding(page, 'final')).not.toBeInViewport()
await locate.graphEditor(page).press('Enter')
Expand Down
2 changes: 1 addition & 1 deletion app/gui2/e2e/edgeRendering.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, Page, test } from '@playwright/test'
import { expect, test, type Page } from '@playwright/test'
import * as actions from './actions'
import { edgesFromNodeWithBinding, edgesToNodeWithBinding } from './locate'

Expand Down
45 changes: 33 additions & 12 deletions app/gui2/src/components/GraphEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { Rect } from '@/util/data/rect'
import { Vec2 } from '@/util/data/vec2'
import { encoding, set } from 'lib0'
import { encodeMethodPointer } from 'shared/languageServerTypes'
import { computed, onMounted, ref, shallowRef, toRef, watch } from 'vue'
import { computed, onMounted, ref, shallowRef, toRef, watch, watchEffect } from 'vue'
const keyboard = provideKeyboard()
const graphStore = useGraphStore()
Expand All @@ -69,14 +69,32 @@ const suggestionDb = useSuggestionDbStore()
const viewportNode = ref<HTMLElement>()
onMounted(() => viewportNode.value?.focus())
const graphNavigator = provideGraphNavigator(viewportNode, keyboard)
useNavigatorStorage(graphNavigator, (enc) => {
// Navigator viewport needs to be stored separately for:
// - each project
// - each function within the project
encoding.writeVarString(enc, projectStore.name)
const methodPtr = graphStore.currentMethodPointer()
if (methodPtr != null) encodeMethodPointer(enc, methodPtr)
})
useNavigatorStorage(
graphNavigator,
(enc) => {
// Navigator viewport needs to be stored separately for:
// - each project
// - each function within the project
encoding.writeVarString(enc, projectStore.name)
const methodPtr = graphStore.currentMethodPointer()
if (methodPtr != null) encodeMethodPointer(enc, methodPtr)
},
waitInitializationAndPanToAll,
)
let stopInitialization: (() => void) | undefined
function waitInitializationAndPanToAll() {
stopInitialization?.()
stopInitialization = watchEffect(() => {
const nodesCount = graphStore.db.nodeIdToNode.size
const visibleNodeAreas = graphStore.visibleNodeAreas
if (nodesCount > 0 && visibleNodeAreas.length == nodesCount) {
zoomToSelected(true)
stopInitialization?.()
stopInitialization = undefined
}
})
}
function selectionBounds() {
if (!viewportNode.value) return
Expand All @@ -90,9 +108,10 @@ function selectionBounds() {
if (bounds.isFinite()) return bounds
}
function zoomToSelected() {
function zoomToSelected(skipAnimation: boolean = false) {
const bounds = selectionBounds()
if (bounds) graphNavigator.panAndZoomTo(bounds, 0.1, Math.max(1, graphNavigator.targetScale))
if (bounds)
graphNavigator.panAndZoomTo(bounds, 0.1, Math.max(1, graphNavigator.targetScale), skipAnimation)
}
function panToSelected() {
Expand Down Expand Up @@ -210,7 +229,9 @@ const graphBindingsHandler = graphBindings.handler({
}
},
deleteSelected,
zoomToSelected,
zoomToSelected() {
zoomToSelected()
},
selectAll() {
nodeSelection.selectAll()
},
Expand Down
5 changes: 5 additions & 0 deletions app/gui2/src/composables/navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export function useNavigator(viewportNode: Ref<Element | undefined>, keyboard: K
rect: Rect,
minScale = PAN_AND_ZOOM_DEFAULT_SCALE_RANGE[0],
maxScale = PAN_AND_ZOOM_DEFAULT_SCALE_RANGE[1],
skipAnimation = false,
) {
if (!viewportNode.value) return
targetScale.value = Math.max(
Expand All @@ -87,6 +88,10 @@ export function useNavigator(viewportNode: Ref<Element | undefined>, keyboard: K
),
)
targetCenter.value = rect.center().finiteOrZero()
if (skipAnimation) {
scale.skip()
center.skip()
}
}

/** Pan to include the given prioritized list of coordinates.
Expand Down
6 changes: 5 additions & 1 deletion app/gui2/src/composables/navigatorStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Vec2 } from '@/util/data/vec2'
import { debouncedWatch, useLocalStorage } from '@vueuse/core'
import { encoding } from 'lib0'
import { xxHash128 } from 'shared/ast/ffi'
import { computed, watch } from 'vue'
import { computed, nextTick, watch } from 'vue'
import type { NavigatorComposable } from './navigator'

/**
Expand All @@ -12,10 +12,13 @@ import type { NavigatorComposable } from './navigator'
* @param reactiveStorageKeyEncoder A **reactive** encoder from which a storage key is derived. Data
* that is encoded in this function dictates the effective identity of stored viewport. Whenever the
* encoded data changes, the stored viewport value is restored to navigator.
* @param initializeViewport A function that will be called when no stored viewport is found for the
* current storage key.
*/
export function useNavigatorStorage(
navigator: NavigatorComposable,
reactiveStorageKeyEncoder: (enc: encoding.Encoder) => void,
initializeViewport: () => void,
) {
const graphViewportStorageKey = computed(() =>
xxHash128(encoding.encode(reactiveStorageKeyEncoder)),
Expand Down Expand Up @@ -64,6 +67,7 @@ export function useNavigatorStorage(

function restoreViewport(storageKey: string) {
const restored = storedViewport.value.get(storageKey)
if (restored == null) nextTick(initializeViewport)
const pos = restored ? Vec2.FromXY(restored).finiteOrZero() : Vec2.Zero
const scale = restored?.s ?? 1
navigator.setCenterAndScale(pos, scale)
Expand Down

0 comments on commit 6e9b4ec

Please sign in to comment.