mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-07-15 21:03:47 +00:00
Prevent Electron modifier wheel zoom (#1016)
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { installElectronWheelZoomGuard } from "./electron-wheel-zoom";
|
||||
|
||||
const win = window as unknown as Record<string, unknown>;
|
||||
let cleanup: (() => void) | undefined;
|
||||
|
||||
afterEach(() => {
|
||||
cleanup?.();
|
||||
cleanup = undefined;
|
||||
delete win.IS_ELECTRON;
|
||||
delete win.electronAPI;
|
||||
});
|
||||
|
||||
function dispatchWheel(init?: WheelEventInit): WheelEvent {
|
||||
const event = new WheelEvent("wheel", {
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
...init,
|
||||
});
|
||||
window.dispatchEvent(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
describe("installElectronWheelZoomGuard", () => {
|
||||
it("does not block ordinary browser wheel events", () => {
|
||||
cleanup = installElectronWheelZoomGuard();
|
||||
|
||||
expect(dispatchWheel({ ctrlKey: true }).defaultPrevented).toBe(false);
|
||||
});
|
||||
|
||||
it("blocks modifier wheel zoom in Electron", () => {
|
||||
win.electronAPI = { isElectron: true };
|
||||
cleanup = installElectronWheelZoomGuard();
|
||||
|
||||
expect(dispatchWheel({ metaKey: true }).defaultPrevented).toBe(true);
|
||||
expect(dispatchWheel({ ctrlKey: true }).defaultPrevented).toBe(true);
|
||||
expect(dispatchWheel().defaultPrevented).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
import { isElectron } from "./electron";
|
||||
|
||||
export function installElectronWheelZoomGuard(): () => void {
|
||||
if (!isElectron()) return () => {};
|
||||
|
||||
const preventModifierWheelZoom = (event: WheelEvent) => {
|
||||
if (!event.metaKey && !event.ctrlKey) return;
|
||||
event.preventDefault();
|
||||
};
|
||||
|
||||
window.addEventListener("wheel", preventModifierWheelZoom, {
|
||||
capture: true,
|
||||
passive: false,
|
||||
});
|
||||
|
||||
return () =>
|
||||
window.removeEventListener("wheel", preventModifierWheelZoom, {
|
||||
capture: true,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user