-
Notifications
You must be signed in to change notification settings - Fork 576
fix: prevent escape from exiting subgraph while a context menu is open #12308
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
Draft
christian-byrne
wants to merge
3
commits into
main
Choose a base branch
from
glary/escape-key-raised-surfaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+322
−11
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest' | ||
|
|
||
| import { ContextMenu } from '@/lib/litegraph/src/ContextMenu' | ||
|
|
||
| describe('ContextMenu lifecycle events', () => { | ||
| let listener: (event: Event) => void | ||
| let calls: Event[] | ||
| let menus: ContextMenu[] | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = '' | ||
| calls = [] | ||
| menus = [] | ||
| listener = (event) => calls.push(event) | ||
| document.addEventListener('litegraph:contextmenu', listener) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| for (const menu of menus) menu.close() | ||
| document.removeEventListener('litegraph:contextmenu', listener) | ||
| }) | ||
|
|
||
| it('dispatches an "open" event when a top-level menu is constructed', () => { | ||
| menus.push(new ContextMenu(['Item A', 'Item B'], {})) | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| const detail = (calls[0] as CustomEvent).detail | ||
| expect(detail.type).toBe('open') | ||
| }) | ||
|
|
||
| it('dispatches a "close" event when a top-level menu closes', () => { | ||
| const menu = new ContextMenu(['Item A'], {}) | ||
| calls.length = 0 | ||
|
|
||
| menu.close() | ||
|
|
||
| expect(calls).toHaveLength(1) | ||
| const detail = (calls[0] as CustomEvent).detail | ||
| expect(detail.type).toBe('close') | ||
| expect(detail.menu).toBe(menu) | ||
| }) | ||
|
|
||
| it('does not dispatch lifecycle events for submenus', () => { | ||
| const parent = new ContextMenu(['Item A'], {}) | ||
| menus.push(parent) | ||
| calls.length = 0 | ||
|
|
||
| new ContextMenu(['Sub Item'], { parentMenu: parent }) | ||
|
|
||
| expect(calls).toHaveLength(0) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { onScopeDispose } from 'vue' | ||
|
|
||
| import { useRaisedSurfaceStore } from './raisedSurfaceStore' | ||
|
|
||
| interface LiteGraphContextMenuEventDetail { | ||
| type: 'open' | 'close' | ||
| menu: object | ||
| } | ||
|
|
||
| export function useLiteGraphContextMenuTracking(): void { | ||
| const store = useRaisedSurfaceStore() | ||
| const idsByMenu = new Map<object, symbol>() | ||
|
|
||
| const handler = (event: Event) => { | ||
| const detail = (event as CustomEvent<LiteGraphContextMenuEventDetail>) | ||
| .detail | ||
| if (detail.type === 'open') { | ||
| idsByMenu.set(detail.menu, store.open('context-menu')) | ||
| return | ||
| } | ||
| const id = idsByMenu.get(detail.menu) | ||
| if (id !== undefined) { | ||
| store.close(id) | ||
| idsByMenu.delete(detail.menu) | ||
| } | ||
| } | ||
|
|
||
| document.addEventListener('litegraph:contextmenu', handler) | ||
| onScopeDispose(() => { | ||
| document.removeEventListener('litegraph:contextmenu', handler) | ||
| for (const id of idsByMenu.values()) store.close(id) | ||
| idsByMenu.clear() | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { createPinia, setActivePinia } from 'pinia' | ||
| import { effectScope, ref } from 'vue' | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
|
|
||
| import { useRaisedSurface, useRaisedSurfaceStore } from './raisedSurfaceStore' | ||
|
|
||
| describe('raisedSurfaceStore', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
| }) | ||
|
|
||
| it('is empty by default', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| expect(store.isAnyOpen).toBe(false) | ||
| expect(store.stack).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('tracks open/close and reports isAnyOpen', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| const id = store.open('context-menu') | ||
| expect(store.isAnyOpen).toBe(true) | ||
| expect(store.stack).toHaveLength(1) | ||
| store.close(id) | ||
| expect(store.isAnyOpen).toBe(false) | ||
| expect(store.stack).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('supports concurrent surfaces and closes by id (LIFO not required)', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| const a = store.open('context-menu') | ||
| const b = store.open('popover') | ||
| expect(store.stack).toHaveLength(2) | ||
| store.close(a) | ||
| expect(store.stack).toHaveLength(1) | ||
| expect(store.stack[0].kind).toBe('popover') | ||
| store.close(b) | ||
| expect(store.isAnyOpen).toBe(false) | ||
| }) | ||
|
|
||
| it('close is a no-op for unknown ids', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| store.open('modal') | ||
| store.close(Symbol('stale')) | ||
| expect(store.stack).toHaveLength(1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('useRaisedSurface', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
| }) | ||
|
|
||
| it('opens when reactive flag turns true and closes when it turns false', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| const isOpen = ref(false) | ||
| const scope = effectScope() | ||
| scope.run(() => useRaisedSurface('context-menu', isOpen)) | ||
|
|
||
| expect(store.isAnyOpen).toBe(false) | ||
| isOpen.value = true | ||
| expect(store.isAnyOpen).toBe(true) | ||
| isOpen.value = false | ||
| expect(store.isAnyOpen).toBe(false) | ||
| scope.stop() | ||
| }) | ||
|
|
||
| it('does not double-register when flag is toggled true twice', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| const isOpen = ref(false) | ||
| const scope = effectScope() | ||
| scope.run(() => useRaisedSurface('context-menu', isOpen)) | ||
|
|
||
| isOpen.value = true | ||
| isOpen.value = true | ||
| expect(store.stack).toHaveLength(1) | ||
| scope.stop() | ||
| }) | ||
|
|
||
| it('releases the surface when the owning scope is disposed', () => { | ||
| const store = useRaisedSurfaceStore() | ||
| const isOpen = ref(true) | ||
| const scope = effectScope() | ||
| scope.run(() => useRaisedSurface('context-menu', isOpen)) | ||
|
|
||
| expect(store.isAnyOpen).toBe(true) | ||
| scope.stop() | ||
| expect(store.isAnyOpen).toBe(false) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.