-
Notifications
You must be signed in to change notification settings - Fork 449
Fix: Search Box Implementation for keyboard shortcut #5140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ed6ecf5
refactor: Move searchbox preference to the searchboxstore
DrJKL d4fd18f
fix: Ensure that the search box uses the preferred implementation.
DrJKL 9333fb1
polish: Open at current mouse location.
DrJKL 0ccae41
[test] add basic unit tests for searchBoxStore
arjansingh fd8747b
types/testing: Tweak the types and setup for the searchBoxStore tests
DrJKL 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
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 |
|---|---|---|
| @@ -1,14 +1,50 @@ | ||
| import { useMouse } from '@vueuse/core' | ||
| import { defineStore } from 'pinia' | ||
| import { ref } from 'vue' | ||
| import { computed, ref, shallowRef } from 'vue' | ||
|
|
||
| import type NodeSearchBoxPopover from '@/components/searchbox/NodeSearchBoxPopover.vue' | ||
| import type { CanvasPointerEvent } from '@/lib/litegraph/src/litegraph' | ||
| import { useSettingStore } from '@/stores/settingStore' | ||
|
|
||
| export const useSearchBoxStore = defineStore('searchBox', () => { | ||
| const settingStore = useSettingStore() | ||
| const { x, y } = useMouse() | ||
|
|
||
| const newSearchBoxEnabled = computed( | ||
| () => settingStore.get('Comfy.NodeSearchBoxImpl') === 'default' | ||
| ) | ||
|
|
||
| const popoverRef = shallowRef<InstanceType< | ||
| typeof NodeSearchBoxPopover | ||
| > | null>(null) | ||
|
|
||
| function setPopoverRef( | ||
| popover: InstanceType<typeof NodeSearchBoxPopover> | null | ||
| ) { | ||
| popoverRef.value = popover | ||
| } | ||
|
|
||
| const visible = ref(false) | ||
| function toggleVisible() { | ||
| visible.value = !visible.value | ||
| if (newSearchBoxEnabled.value) { | ||
| visible.value = !visible.value | ||
| return | ||
| } | ||
| if (!popoverRef.value) return | ||
| popoverRef.value.showSearchBox( | ||
| new MouseEvent('click', { | ||
| clientX: x.value, | ||
| clientY: y.value, | ||
| // @ts-expect-error layerY is a nonstandard property | ||
| layerY: y.value | ||
| }) as unknown as CanvasPointerEvent | ||
| ) | ||
| } | ||
|
|
||
| return { | ||
| visible, | ||
| toggleVisible | ||
| newSearchBoxEnabled, | ||
| setPopoverRef, | ||
| toggleVisible, | ||
| visible | ||
| } | ||
| }) |
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,137 @@ | ||
| import { createPinia, setActivePinia } from 'pinia' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type NodeSearchBoxPopover from '@/components/searchbox/NodeSearchBoxPopover.vue' | ||
| import type { useSettingStore } from '@/stores/settingStore' | ||
| import { useSearchBoxStore } from '@/stores/workspace/searchBoxStore' | ||
|
|
||
| // Mock dependencies | ||
| vi.mock('@vueuse/core', () => ({ | ||
| useMouse: vi.fn(() => ({ | ||
| x: { value: 100 }, | ||
| y: { value: 200 } | ||
| })) | ||
| })) | ||
|
|
||
| const mockSettingStore = createMockSettingStore() | ||
| vi.mock('@/stores/settingStore', () => ({ | ||
| useSettingStore: vi.fn(() => mockSettingStore) | ||
| })) | ||
|
|
||
| function createMockPopover(): InstanceType<typeof NodeSearchBoxPopover> { | ||
| return { showSearchBox: vi.fn() } satisfies Partial< | ||
| InstanceType<typeof NodeSearchBoxPopover> | ||
| > as unknown as InstanceType<typeof NodeSearchBoxPopover> | ||
| } | ||
|
|
||
| function createMockSettingStore(): ReturnType<typeof useSettingStore> { | ||
| return { | ||
| get: vi.fn() | ||
| } satisfies Partial< | ||
| ReturnType<typeof useSettingStore> | ||
| > as unknown as ReturnType<typeof useSettingStore> | ||
| } | ||
|
|
||
| describe('useSearchBoxStore', () => { | ||
| beforeEach(() => { | ||
| setActivePinia(createPinia()) | ||
|
|
||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| describe('when user has new search box enabled', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(mockSettingStore.get).mockReturnValue('default') | ||
| }) | ||
|
|
||
| it('should show new search box is enabled', () => { | ||
| const store = useSearchBoxStore() | ||
| expect(store.newSearchBoxEnabled).toBe(true) | ||
| }) | ||
|
|
||
| it('should toggle search box visibility when user presses shortcut', () => { | ||
| const store = useSearchBoxStore() | ||
|
|
||
| expect(store.visible).toBe(false) | ||
|
|
||
| store.toggleVisible() | ||
| expect(store.visible).toBe(true) | ||
|
|
||
| store.toggleVisible() | ||
| expect(store.visible).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when user has legacy search box enabled', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(mockSettingStore.get).mockReturnValue('legacy') | ||
| }) | ||
|
|
||
| it('should show new search box is disabled', () => { | ||
| const store = useSearchBoxStore() | ||
| expect(store.newSearchBoxEnabled).toBe(false) | ||
| }) | ||
|
|
||
| it('should open legacy search box at mouse position when user presses shortcut', () => { | ||
| const store = useSearchBoxStore() | ||
| const mockPopover = createMockPopover() | ||
| store.setPopoverRef(mockPopover) | ||
|
|
||
| expect(vi.mocked(store.visible)).toBe(false) | ||
|
|
||
| store.toggleVisible() | ||
|
|
||
| expect(vi.mocked(store.visible)).toBe(false) // Doesn't become visible in legacy mode. | ||
|
|
||
| expect(vi.mocked(mockPopover.showSearchBox)).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| clientX: 100, | ||
| clientY: 200 | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('should do nothing when user presses shortcut but popover is not ready', () => { | ||
| const store = useSearchBoxStore() | ||
| store.setPopoverRef(null) | ||
|
|
||
| store.toggleVisible() | ||
|
|
||
| expect(store.visible).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when user configures popover reference', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(mockSettingStore.get).mockReturnValue('legacy') | ||
| }) | ||
|
|
||
| it('should enable legacy search when popover is set', () => { | ||
| const store = useSearchBoxStore() | ||
| const mockPopover = createMockPopover() | ||
| store.setPopoverRef(mockPopover) | ||
|
|
||
| store.toggleVisible() | ||
|
|
||
| expect(vi.mocked(mockPopover.showSearchBox)).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should disable legacy search when popover is cleared', () => { | ||
| const store = useSearchBoxStore() | ||
| const mockPopover = createMockPopover() | ||
| store.setPopoverRef(mockPopover) | ||
| store.setPopoverRef(null) | ||
|
|
||
| store.toggleVisible() | ||
|
|
||
| expect(vi.mocked(mockPopover.showSearchBox)).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe('when user first loads the application', () => { | ||
| it('should have search box hidden by default', () => { | ||
| const store = useSearchBoxStore() | ||
| expect(store.visible).toBe(false) | ||
| }) | ||
| }) | ||
| }) |
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.