-
Notifications
You must be signed in to change notification settings - Fork 278
fix(window): 修复 macOS 全屏应用下呼出窗口一闪而过并落到桌面的问题 #577
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
Open
Ethan0x0000
wants to merge
1
commit into
ZToolsCenter:main
Choose a base branch
from
Ethan0x0000:fix/macos-window-fullscreen-activation
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.
+260
−16
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| type MockHandler = (...args: any[]) => void | ||
|
|
||
| const mocks = vi.hoisted(() => { | ||
| const appFocus = vi.fn() | ||
| const dbGet = vi.fn() | ||
| const clipboardGetCurrentWindow = vi.fn() | ||
| const globalInputOn = vi.fn() | ||
| const globalInputAcquire = vi.fn() | ||
| const globalInputRelease = vi.fn() | ||
| const latestWindow = { current: null as any } | ||
|
|
||
| const createMockWindow = (): any => { | ||
| const handlers: Record<string, MockHandler[]> = {} | ||
| const webContentsHandlers: Record<string, MockHandler[]> = {} | ||
|
|
||
| const emit = (event: string, ...args: any[]): void => { | ||
| for (const handler of handlers[event] || []) { | ||
| handler(...args) | ||
| } | ||
| } | ||
|
|
||
| const win = { | ||
| webContents: { | ||
| setZoomFactor: vi.fn(), | ||
| setVisualZoomLevelLimits: vi.fn(), | ||
| on: vi.fn((event: string, handler: MockHandler) => { | ||
| ;(webContentsHandlers[event] ||= []).push(handler) | ||
| }), | ||
| focus: vi.fn(), | ||
| send: vi.fn(), | ||
| getURL: vi.fn(() => 'app://ztools') | ||
| }, | ||
| setVisibleOnAllWorkspaces: vi.fn(), | ||
| setAlwaysOnTop: vi.fn(), | ||
| setPosition: vi.fn(), | ||
| getPosition: vi.fn(() => [100, 100]), | ||
| getBounds: vi.fn(() => ({ x: 100, y: 100, width: 800, height: 600 })), | ||
| isFocused: vi.fn(() => false), | ||
| isVisible: vi.fn(() => false), | ||
| show: vi.fn(() => emit('show')), | ||
| hide: vi.fn(), | ||
| blur: vi.fn(), | ||
| focus: vi.fn(), | ||
| loadFile: vi.fn(), | ||
| loadURL: vi.fn(), | ||
| on: vi.fn((event: string, handler: MockHandler) => { | ||
| ;(handlers[event] ||= []).push(handler) | ||
| }), | ||
| once: vi.fn((event: string, handler: MockHandler) => { | ||
| ;(handlers[event] ||= []).push(handler) | ||
| }) | ||
| } | ||
|
|
||
| latestWindow.current = win | ||
| return win | ||
| } | ||
|
|
||
| return { | ||
| appFocus, | ||
| dbGet, | ||
| clipboardGetCurrentWindow, | ||
| globalInputOn, | ||
| globalInputAcquire, | ||
| globalInputRelease, | ||
| latestWindow, | ||
| createMockWindow | ||
| } | ||
| }) | ||
|
|
||
| vi.mock('@electron-toolkit/utils', () => ({ | ||
| is: { dev: false }, | ||
| platform: { isMacOS: true, isWindows: false, isLinux: false } | ||
| })) | ||
|
|
||
| vi.mock('electron', () => ({ | ||
| app: { | ||
| focus: mocks.appFocus, | ||
| dock: { | ||
| show: vi.fn(), | ||
| hide: vi.fn() | ||
| } | ||
| }, | ||
| BrowserWindow: vi.fn(function BrowserWindowMock() { | ||
| return mocks.createMockWindow() | ||
| }), | ||
| globalShortcut: { | ||
| register: vi.fn(() => true), | ||
| unregister: vi.fn(), | ||
| unregisterAll: vi.fn(), | ||
| isRegistered: vi.fn(() => false) | ||
| }, | ||
| Menu: { | ||
| buildFromTemplate: vi.fn(() => ({})) | ||
| }, | ||
| nativeImage: { | ||
| createFromPath: vi.fn(() => ({ | ||
| setTemplateImage: vi.fn() | ||
| })) | ||
| }, | ||
| screen: { | ||
| getCursorScreenPoint: vi.fn(() => ({ x: 300, y: 300 })), | ||
| getDisplayNearestPoint: vi.fn(() => ({ | ||
| id: 1, | ||
| workArea: { x: 0, y: 0, width: 1440, height: 900 } | ||
| })) | ||
| }, | ||
| Tray: vi.fn(() => ({ | ||
| setToolTip: vi.fn(), | ||
| setContextMenu: vi.fn(), | ||
| on: vi.fn(), | ||
| popUpContextMenu: vi.fn() | ||
| })) | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/api', () => ({ | ||
| default: { | ||
| dbGet: vi.fn(() => null), | ||
| launchPlugin: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/api/shared/database', () => ({ | ||
| default: { | ||
| dbGet: mocks.dbGet, | ||
| dbPut: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/core/doubleTapManager.js', () => ({ | ||
| default: { | ||
| register: vi.fn(), | ||
| unregister: vi.fn(), | ||
| unregisterAll: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/core/globalInputManager.js', () => ({ | ||
| default: { | ||
| on: mocks.globalInputOn, | ||
| acquire: mocks.globalInputAcquire, | ||
| release: mocks.globalInputRelease | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/core/native/index.js', () => ({ | ||
| WindowManager: { | ||
| activateWindow: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/managers/clipboardManager', () => ({ | ||
| default: { | ||
| getCurrentWindow: mocks.clipboardGetCurrentWindow | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/core/detachedWindowManager', () => ({ | ||
| default: { | ||
| hasDetachedWindows: vi.fn(() => false) | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/core/superPanelManager', () => ({ | ||
| default: { | ||
| broadcastToSuperPanel: vi.fn() | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/utils/windowUtils', () => ({ | ||
| applyWindowMaterial: vi.fn(), | ||
| getDefaultWindowMaterial: vi.fn(() => 'none') | ||
| })) | ||
|
|
||
| vi.mock('../../src/main/managers/pluginManager', () => ({ | ||
| default: { | ||
| getCurrentPluginPath: vi.fn(() => null), | ||
| restoreCurrentPluginViewHeightOnWindowShow: vi.fn(), | ||
| isPluginViewFocused: vi.fn(() => false), | ||
| focusPluginView: vi.fn(), | ||
| forceRepaintCurrentView: vi.fn(), | ||
| hidePluginView: vi.fn(), | ||
| handlePluginEsc: vi.fn(), | ||
| shouldSuppressMainHide: vi.fn(() => false) | ||
| } | ||
| })) | ||
|
|
||
| describe('windowManager macOS activation', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| mocks.dbGet.mockReturnValue(null) | ||
| mocks.clipboardGetCurrentWindow.mockReturnValue(null) | ||
| mocks.latestWindow.current = null | ||
| }) | ||
|
|
||
| it('activates and focuses the app when showing the main window on macOS', async () => { | ||
| const { default: windowManager } = await import('../../src/main/managers/windowManager') | ||
|
|
||
| windowManager.createWindow() | ||
| windowManager.showWindow() | ||
|
|
||
| expect(mocks.latestWindow.current.show).toHaveBeenCalled() | ||
| expect(mocks.latestWindow.current.setVisibleOnAllWorkspaces).toHaveBeenCalledWith(true, { | ||
| visibleOnFullScreen: true | ||
| }) | ||
| expect(mocks.latestWindow.current.setAlwaysOnTop).toHaveBeenLastCalledWith( | ||
| true, | ||
| 'modal-panel', | ||
| 1 | ||
| ) | ||
| expect(mocks.appFocus).toHaveBeenCalledWith({ steal: true }) | ||
| expect(mocks.latestWindow.current.focus).toHaveBeenCalled() | ||
| }) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
当前实现中,
suppressBlurHideTransiently直接修改了this.suppressBlurHide共享变量。这会与openPluginInstaller中手动设置的this.suppressBlurHide = true产生冲突和竞态(例如,瞬时抑制定时器触发时会错误地将this.suppressBlurHide置为false,从而提前释放了openPluginInstaller的长时抑制)。为了彻底解决这个问题,建议将瞬时抑制与长时抑制解耦:
suppressBlurHideTransiently中不再修改this.suppressBlurHide。isBlurHideSuppressed()方法(在上方第 162 行),将!!this.transientBlurSuppressTimer纳入判断:这样不仅能避免竞态,还能让
openPluginInstaller直接复用forceActivateWindow(),消除重复代码。