mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-20 19:20:27 +00:00
Fix Ctrl+Click for Macs, and alphabetize gitignore (#2720)
Resolves #2719 - [X] Ran `npm run format` - [X] Ran `npm run lint:fix` - [X] Ran `npm test` - [X] Manually tested fixes ## Description: ### What problem(s) was I solving? On Mac, `Ctrl+Click` is commonly used as a substitute for right-click to open context menus. However, in OpenFront, `Ctrl+Click` was triggering both the context menu, AND causing an attack. This effectively prevented Mac users from being able to ally with bots/nations, as trying to ally would cause you to attack them. ### What changes did I make? - `Ctrl+Click` on Mac no longer triggers an attack, allowing players to properly interact with the alliance system ### How I implemented it 1. Added an `isMac()` method to `InputHandler` which encapsulates the "is mac" logic, and lets it be mocked during tests 2. In the `onPointerUp` handler, added a check: if on Mac and `ControlLeft` is held, emit a `ContextMenuEvent`, and then return (instead of continuing to also create a `MouseUpEvent`) 3. Extracted magic numbers for mouse button checks into descriptive helper methods (`isMiddleMouseButton`, `isNonLeftMouseButton`) for improved code clarity 4. Added clarifying comments throughout the pointer event handlers Last, alphabetized the `.gitignore` file and organized it into "Folders" and "Files" sections to make it easier to read. ### How to verify it #### Manual Testing - [X] On a Mac, hold `Ctrl` and left-click on another nation - verify the context menu opens (not an attack) - [X] On a Mac, right-click should still open the context menu as expected - [X] On Windows/Linux, `Ctrl+Click` continue to work as before (modifier key for build menu if configured) - [X] Regular left-click still triggers attacks/interactions as expected #### Automated Testing - [x] New unit test added: `Mac Ctrl+Click Context Menu` - verifies that `Ctrl+Click` on Mac emits `ContextMenuEvent` instead of `MouseUpEvent` ### Description for the changelog Fixed `Ctrl+Click` on Mac to properly open the context menu instead of triggering an attack, restoring the ability for Mac users to form alliances with other nations. ## Please complete the following: - [X] I have added screenshots for all UI updates - [X] I process any text displayed to the user through translateText() and I've added it to the en.json file - [X] I have added relevant tests to the test directory - [X] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: Terekhov
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { AutoUpgradeEvent, InputHandler } from "../src/client/InputHandler";
|
||||
import {
|
||||
AutoUpgradeEvent,
|
||||
ContextMenuEvent,
|
||||
InputHandler,
|
||||
MouseUpEvent,
|
||||
} from "../src/client/InputHandler";
|
||||
import { EventBus } from "../src/core/EventBus";
|
||||
|
||||
class MockPointerEvent {
|
||||
@@ -452,4 +457,46 @@ describe("InputHandler AutoUpgrade", () => {
|
||||
spy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("Mac Ctrl+Click Context Menu", () => {
|
||||
test("should create context menu with Ctrl+Click on Mac, but not attack", () => {
|
||||
// Mock isMac() to return true
|
||||
vi.spyOn(inputHandler as any, "isMac").mockReturnValue(true);
|
||||
const mockEmit = vi.spyOn(eventBus, "emit");
|
||||
|
||||
// Simulate ControlLeft being held
|
||||
inputHandler["activeKeys"].add("ControlLeft");
|
||||
|
||||
// Simulate a pointer down first (to set pointerDown state)
|
||||
const pointerDownEvent = new PointerEvent("pointerdown", {
|
||||
button: 0,
|
||||
clientX: 100,
|
||||
clientY: 200,
|
||||
pointerId: 1,
|
||||
});
|
||||
inputHandler["onPointerDown"](pointerDownEvent);
|
||||
mockEmit.mockClear();
|
||||
|
||||
// Now trigger pointer up
|
||||
const pointerUpEvent = new PointerEvent("pointerup", {
|
||||
button: 0,
|
||||
clientX: 100,
|
||||
clientY: 200,
|
||||
pointerId: 1,
|
||||
});
|
||||
inputHandler["onPointerUp"](pointerUpEvent);
|
||||
|
||||
// Verify ContextMenuEvent was emitted with correct coordinates
|
||||
expect(mockEmit).toHaveBeenCalledTimes(1);
|
||||
// If MouseUp is fired, that would cause an attack - which we do not want.
|
||||
expect(mockEmit).not.toHaveBeenCalledWith(expect.any(MouseUpEvent));
|
||||
expect(mockEmit).toHaveBeenCalledWith(expect.any(ContextMenuEvent));
|
||||
expect(mockEmit).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
x: 100,
|
||||
y: 200,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user