-
Notifications
You must be signed in to change notification settings - Fork 6
Honor macOS title bar double-click setting #76
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { createTitleBarDragHandlers } from "./title-bar"; | ||
|
|
||
| function mouseEvent( | ||
| detail: number, | ||
| overrides: Partial<{ | ||
| button: number; | ||
| clientX: number; | ||
| clientY: number; | ||
| sameTarget: boolean; | ||
| }> = {}, | ||
| ) { | ||
| const currentTarget = {}; | ||
| return { | ||
| button: overrides.button ?? 0, | ||
| clientX: overrides.clientX ?? 40, | ||
| clientY: overrides.clientY ?? 12, | ||
| currentTarget, | ||
| detail, | ||
| preventDefault: vi.fn(), | ||
| target: overrides.sameTarget === false ? {} : currentTarget, | ||
| } as unknown as Parameters< | ||
| ReturnType<typeof createTitleBarDragHandlers>["onMouseDown"] | ||
| >[0]; | ||
| } | ||
|
|
||
| describe("macOS title bar dragging", () => { | ||
| it("starts dragging on a primary single click of the region itself", () => { | ||
| const actions = { startDragging: vi.fn(), doubleClick: vi.fn() }; | ||
| const handlers = createTitleBarDragHandlers(actions); | ||
| const event = mouseEvent(1); | ||
|
|
||
| handlers.onMouseDown(event); | ||
|
|
||
| expect(event.preventDefault).toHaveBeenCalledOnce(); | ||
| expect(actions.startDragging).toHaveBeenCalledOnce(); | ||
| expect(actions.doubleClick).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("runs the native preference action after an unmoved double click", () => { | ||
| const actions = { startDragging: vi.fn(), doubleClick: vi.fn() }; | ||
| const handlers = createTitleBarDragHandlers(actions); | ||
| const down = mouseEvent(2); | ||
| const up = mouseEvent(2); | ||
|
|
||
| handlers.onMouseDown(down); | ||
| handlers.onMouseUp(up); | ||
|
|
||
| expect(up.preventDefault).toHaveBeenCalledOnce(); | ||
| expect(actions.doubleClick).toHaveBeenCalledOnce(); | ||
| expect(actions.startDragging).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("cancels the double-click action after movement", () => { | ||
| const actions = { startDragging: vi.fn(), doubleClick: vi.fn() }; | ||
| const handlers = createTitleBarDragHandlers(actions); | ||
|
|
||
| handlers.onMouseDown(mouseEvent(2)); | ||
| handlers.onMouseUp(mouseEvent(2, { clientX: 41 })); | ||
|
|
||
| expect(actions.doubleClick).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("ignores interactive descendants and non-primary clicks", () => { | ||
| const actions = { startDragging: vi.fn(), doubleClick: vi.fn() }; | ||
| const handlers = createTitleBarDragHandlers(actions); | ||
|
|
||
| handlers.onMouseDown(mouseEvent(1, { sameTarget: false })); | ||
| handlers.onMouseDown(mouseEvent(1, { button: 1 })); | ||
| handlers.onMouseDown(mouseEvent(2, { sameTarget: false })); | ||
| handlers.onMouseUp(mouseEvent(2, { sameTarget: false })); | ||
|
|
||
| expect(actions.startDragging).not.toHaveBeenCalled(); | ||
| expect(actions.doubleClick).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { MouseEventHandler } from "react"; | ||
| import { invoke } from "@tauri-apps/api/core"; | ||
| import { getCurrentWindow } from "@tauri-apps/api/window"; | ||
|
|
||
| type TitleBarActions = Readonly<{ | ||
| startDragging: () => void; | ||
| doubleClick: () => void; | ||
| }>; | ||
|
|
||
| export type TitleBarDragHandlers = Readonly<{ | ||
| onMouseDown: MouseEventHandler<HTMLElement>; | ||
| onMouseUp: MouseEventHandler<HTMLElement>; | ||
| }>; | ||
|
|
||
| export function createTitleBarDragHandlers( | ||
| actions: TitleBarActions, | ||
| ): TitleBarDragHandlers { | ||
| let doubleClickStart: Readonly<{ x: number; y: number }> | undefined; | ||
|
|
||
| return { | ||
| onMouseDown(event) { | ||
| if ( | ||
| event.button !== 0 || | ||
| event.target !== event.currentTarget || | ||
| (event.detail !== 1 && event.detail !== 2) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| if (event.detail === 2) { | ||
| doubleClickStart = { x: event.clientX, y: event.clientY }; | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| actions.startDragging(); | ||
| }, | ||
| onMouseUp(event) { | ||
| const start = doubleClickStart; | ||
| doubleClickStart = undefined; | ||
| if ( | ||
| event.button !== 0 || | ||
| event.detail !== 2 || | ||
| event.target !== event.currentTarget || | ||
| start?.x !== event.clientX || | ||
| start.y !== event.clientY | ||
|
Comment on lines
+45
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the pointer moves by even one CSS pixel between the second mouse-down and mouse-up, these exact coordinate comparisons suppress the configured title-bar action even though Useful? React with 👍 / 👎. |
||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| event.preventDefault(); | ||
| actions.doubleClick(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| export const macTitleBarDragHandlers = createTitleBarDragHandlers({ | ||
| startDragging: () => { | ||
| void getCurrentWindow().startDragging(); | ||
| }, | ||
| doubleClick: () => { | ||
| void invoke("title_bar_double_click"); | ||
| }, | ||
| }); | ||
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.
The reviewed commit has no
Signed-off-bytrailer, so it violates the repository's DCO requirement and will fail the hosted DCO Check. Ensure this commit carries the verified author's sign-off while preserving its actual authorship before integration.AGENTS.md reference: AGENTS.md:L123-L126
Useful? React with 👍 / 👎.