Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/client/InputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export class InputHandler {
private readonly PAN_SPEED = 5;
private readonly ZOOM_SPEED = 10;
private readonly DRAG_THRESHOLD_PX = 10;
private wasFullscreen = false;

private readonly userSettings: UserSettings = new UserSettings();

Expand All @@ -241,6 +242,7 @@ export class InputHandler {

initialize() {
this.keybinds = this.userSettings.keybinds(Platform.isMac);
this.wasFullscreen = !!document.fullscreenElement;

// Listen for warship selection to change cursor
this.eventBus.on(UnitSelectionEvent, (e) => {
Expand Down Expand Up @@ -360,6 +362,9 @@ export class InputHandler {
}
}, 1);

window.addEventListener("keydown", this.handleEscapeCapture, true);
document.addEventListener("fullscreenchange", this.handleFullscreenChange);

window.addEventListener("keydown", (e) => {
const isTextInput = this.isTextInputTarget(e.target);
if (isTextInput && e.code !== "Escape") {
Expand Down Expand Up @@ -848,6 +853,34 @@ export class InputHandler {
this.eventBus.emit(new ContextMenuEvent(event.clientX, event.clientY));
}

private handleEscapeCapture = (event: KeyboardEvent) => {
if (
event.code !== "Escape" ||
this.uiState.ghostStructure === null ||
this.isTextInputTarget(event.target)
) {
return;
}

this.cancelGhostStructure();
event.preventDefault();
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

private handleFullscreenChange = () => {
const isFullscreen = !!document.fullscreenElement;
if (this.wasFullscreen && !isFullscreen) {
this.eventBus.emit(new CloseViewEvent());
if (this.uiState.ghostStructure) {
this.cancelGhostStructure();
}
}
this.wasFullscreen = isFullscreen;
};

private cancelGhostStructure() {
this.setGhostStructure(null);
}

private setGhostStructure(ghostStructure: PlayerBuildableUnitType | null) {
this.uiState.ghostStructure = ghostStructure;
this.eventBus.emit(new GhostStructureChangedEvent(ghostStructure));
Expand Down Expand Up @@ -992,6 +1025,11 @@ export class InputHandler {
if (this.moveInterval !== null) {
clearInterval(this.moveInterval);
}
window.removeEventListener("keydown", this.handleEscapeCapture, true);
document.removeEventListener(
"fullscreenchange",
this.handleFullscreenChange,
);
this.activeKeys.clear();
}

Expand Down
126 changes: 126 additions & 0 deletions tests/InputHandler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
AutoUpgradeEvent,
CloseViewEvent,
ConfirmGhostStructureEvent,
GhostStructureChangedEvent,
InputHandler,
WarshipSelectionBoxCancelEvent,
WarshipSelectionBoxCompleteEvent,
Expand Down Expand Up @@ -590,6 +592,130 @@ describe("InputHandler AutoUpgrade", () => {
});
});

describe("Escape while building", () => {
let uiState: UIState;

beforeEach(() => {
inputHandler.destroy();
uiState = {
attackRatio: 20,
ghostStructure: null,
rocketDirectionUp: true,
overlappingRailroads: [],
ghostRailPaths: [],
} as UIState;
inputHandler = new InputHandler(
mockGameView,
uiState,
mockCanvas,
eventBus,
);
inputHandler.initialize();
});

test("captures Escape and cancels active ghost structure without blocking later handlers", () => {
const mockEmit = vi.spyOn(eventBus, "emit");
const laterHandler = vi.fn();
uiState.ghostStructure = UnitType.City;

window.addEventListener("keydown", laterHandler);
const event = new KeyboardEvent("keydown", {
code: "Escape",
cancelable: true,
});
window.dispatchEvent(event);
window.removeEventListener("keydown", laterHandler);

expect(uiState.ghostStructure).toBeNull();
expect(event.defaultPrevented).toBe(true);
expect(laterHandler).toHaveBeenCalled();
expect(mockEmit).toHaveBeenCalledWith(
expect.any(GhostStructureChangedEvent),
);
});

test("closes views and cancels active ghost structure if fullscreen exits before Escape keydown is delivered", () => {
const mockEmit = vi.spyOn(eventBus, "emit");
const fullscreenDescriptor = Object.getOwnPropertyDescriptor(
document,
"fullscreenElement",
);
let fullscreenElement: Element | null = document.documentElement;
Object.defineProperty(document, "fullscreenElement", {
configurable: true,
get: () => fullscreenElement,
});

try {
inputHandler.destroy();
inputHandler = new InputHandler(
mockGameView,
uiState,
mockCanvas,
eventBus,
);
inputHandler.initialize();
uiState.ghostStructure = UnitType.Factory;

fullscreenElement = null;
document.dispatchEvent(new Event("fullscreenchange"));

expect(uiState.ghostStructure).toBeNull();
expect(mockEmit).toHaveBeenCalledWith(expect.any(CloseViewEvent));
expect(mockEmit).toHaveBeenCalledWith(
expect.any(GhostStructureChangedEvent),
);
} finally {
Reflect.deleteProperty(document, "fullscreenElement");
if (fullscreenDescriptor) {
Object.defineProperty(
document,
"fullscreenElement",
fullscreenDescriptor,
);
}
}
});

test("closes views if fullscreen exits with no active ghost structure", () => {
const mockEmit = vi.spyOn(eventBus, "emit");
const fullscreenDescriptor = Object.getOwnPropertyDescriptor(
document,
"fullscreenElement",
);
let fullscreenElement: Element | null = document.documentElement;
Object.defineProperty(document, "fullscreenElement", {
configurable: true,
get: () => fullscreenElement,
});

try {
inputHandler.destroy();
inputHandler = new InputHandler(
mockGameView,
uiState,
mockCanvas,
eventBus,
);
inputHandler.initialize();

fullscreenElement = null;
document.dispatchEvent(new Event("fullscreenchange"));

expect(mockEmit).toHaveBeenCalledWith(expect.any(CloseViewEvent));
} finally {
Reflect.deleteProperty(document, "fullscreenElement");
if (fullscreenDescriptor) {
Object.defineProperty(
document,
"fullscreenElement",
fullscreenDescriptor,
);
}
}
});
});

describe("Numpad number keys for build keybinds", () => {
beforeEach(() => {
inputHandler.destroy();
Expand Down
Loading