-
Notifications
You must be signed in to change notification settings - Fork 451
[fix] Prevent browser zoom on UI components with canvas wheel event forwarding #4574
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 1 commit
Commits
Show all changes
2 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,56 @@ | ||
| import { app } from '@/scripts/app' | ||
| import { useSettingStore } from '@/stores/settingStore' | ||
|
|
||
| /** | ||
| * Composable for handling canvas interactions from Vue components. | ||
| * This provides a unified way to forward events to the LiteGraph canvas | ||
| * and will be the foundation for migrating canvas interactions to Vue. | ||
| */ | ||
| export function useCanvasInteractions() { | ||
| const settingStore = useSettingStore() | ||
|
|
||
| const isStandardNavMode = () => | ||
| settingStore.get('Comfy.Canvas.NavigationMode') === 'standard' | ||
|
|
||
| /** | ||
| * Handles wheel events from UI components that should be forwarded to canvas | ||
| * when appropriate (e.g., Ctrl+wheel for zoom in standard mode) | ||
| */ | ||
| const handleWheel = (event: WheelEvent) => { | ||
| // In standard mode, Ctrl+wheel should go to canvas for zoom | ||
| if (isStandardNavMode() && (event.ctrlKey || event.metaKey)) { | ||
| event.preventDefault() // Prevent browser zoom | ||
| forwardEventToCanvas(event) | ||
| return | ||
| } | ||
|
|
||
| // In legacy mode, all wheel events go to canvas for zoom | ||
| if (!isStandardNavMode()) { | ||
| event.preventDefault() | ||
| forwardEventToCanvas(event) | ||
| return | ||
| } | ||
|
|
||
| // Otherwise, let the component handle it normally | ||
| } | ||
|
|
||
| /** | ||
| * Forwards an event to the LiteGraph canvas | ||
| */ | ||
| const forwardEventToCanvas = ( | ||
| event: WheelEvent | PointerEvent | MouseEvent | ||
| ) => { | ||
| const canvasEl = app.canvas?.canvas | ||
| if (!canvasEl) return | ||
|
|
||
| // Create new event with same properties | ||
| const EventConstructor = event.constructor as typeof WheelEvent | ||
| const newEvent = new EventConstructor(event.type, event) | ||
| canvasEl.dispatchEvent(newEvent) | ||
| } | ||
|
|
||
| return { | ||
| handleWheel, | ||
| forwardEventToCanvas | ||
| } | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
tests-ui/tests/composables/graph/useCanvasInteractions.test.ts
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,126 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { useCanvasInteractions } from '@/composables/graph/useCanvasInteractions' | ||
| import { app } from '@/scripts/app' | ||
| import * as settingStore from '@/stores/settingStore' | ||
|
|
||
| // Mock the app and canvas | ||
| vi.mock('@/scripts/app', () => ({ | ||
| app: { | ||
| canvas: { | ||
| canvas: null as HTMLCanvasElement | null | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| // Mock the setting store | ||
| vi.mock('@/stores/settingStore', () => ({ | ||
| useSettingStore: vi.fn() | ||
| })) | ||
|
|
||
| describe('useCanvasInteractions', () => { | ||
| let mockCanvas: HTMLCanvasElement | ||
| let mockSettingStore: { get: ReturnType<typeof vi.fn> } | ||
| let canvasInteractions: ReturnType<typeof useCanvasInteractions> | ||
|
|
||
| beforeEach(() => { | ||
| // Clear mocks | ||
| vi.clearAllMocks() | ||
|
|
||
| // Create mock canvas element | ||
| mockCanvas = document.createElement('canvas') | ||
| mockCanvas.dispatchEvent = vi.fn() | ||
| app.canvas!.canvas = mockCanvas | ||
|
|
||
| // Mock setting store | ||
| mockSettingStore = { get: vi.fn() } | ||
| vi.mocked(settingStore.useSettingStore).mockReturnValue( | ||
| mockSettingStore as any | ||
| ) | ||
|
|
||
| canvasInteractions = useCanvasInteractions() | ||
| }) | ||
|
|
||
| describe('handleWheel', () => { | ||
| it('should check navigation mode from settings', () => { | ||
| mockSettingStore.get.mockReturnValue('standard') | ||
|
|
||
| const wheelEvent = new WheelEvent('wheel', { | ||
| ctrlKey: true, | ||
| deltaY: -100 | ||
| }) | ||
|
|
||
| canvasInteractions.handleWheel(wheelEvent) | ||
|
|
||
| expect(mockSettingStore.get).toHaveBeenCalledWith( | ||
| 'Comfy.Canvas.NavigationMode' | ||
| ) | ||
| }) | ||
|
|
||
| it('should not forward regular wheel events in standard mode', () => { | ||
| mockSettingStore.get.mockReturnValue('standard') | ||
|
|
||
| const wheelEvent = new WheelEvent('wheel', { | ||
| deltaY: -100 | ||
| }) | ||
|
|
||
| canvasInteractions.handleWheel(wheelEvent) | ||
|
|
||
| expect(mockCanvas.dispatchEvent).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should forward all wheel events to canvas in legacy mode', () => { | ||
| mockSettingStore.get.mockReturnValue('legacy') | ||
|
|
||
| const wheelEvent = new WheelEvent('wheel', { | ||
| deltaY: -100, | ||
| cancelable: true | ||
| }) | ||
|
|
||
| canvasInteractions.handleWheel(wheelEvent) | ||
|
|
||
| expect(mockCanvas.dispatchEvent).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('should handle missing canvas gracefully', () => { | ||
| ;(app.canvas as any).canvas = null | ||
| mockSettingStore.get.mockReturnValue('standard') | ||
|
|
||
| const wheelEvent = new WheelEvent('wheel', { | ||
| ctrlKey: true, | ||
| deltaY: -100 | ||
| }) | ||
|
|
||
| expect(() => { | ||
| canvasInteractions.handleWheel(wheelEvent) | ||
| }).not.toThrow() | ||
| }) | ||
| }) | ||
|
|
||
| describe('forwardEventToCanvas', () => { | ||
| it('should dispatch event to canvas element', () => { | ||
| const wheelEvent = new WheelEvent('wheel', { | ||
| deltaY: -100, | ||
| ctrlKey: true | ||
| }) | ||
|
|
||
| canvasInteractions.forwardEventToCanvas(wheelEvent) | ||
|
|
||
| expect(mockCanvas.dispatchEvent).toHaveBeenCalledWith( | ||
| expect.any(WheelEvent) | ||
| ) | ||
| }) | ||
|
|
||
| it('should handle missing canvas gracefully', () => { | ||
| ;(app.canvas as any).canvas = null | ||
|
|
||
| const wheelEvent = new WheelEvent('wheel', { | ||
| deltaY: -100 | ||
| }) | ||
|
|
||
| expect(() => { | ||
| canvasInteractions.forwardEventToCanvas(wheelEvent) | ||
| }).not.toThrow() | ||
| }) | ||
| }) | ||
| }) |
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.