Skip to content

Commit 0d4aa43

Browse files
committed
Fix click positions
1 parent f1c400c commit 0d4aa43

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

ui/EmulatorTile.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ export function EmulatorTile({ systemId, focused, zoom }: EmulatorTileProps) {
9595
"padding-bottom":0,
9696
overflow: "hidden",
9797
}}
98-
onClick={() => { void plugin.$notify("setFocus", systemId); }}
9998
>
10099
<CanvasAny
101100
ref={canvasRef}

ui/PluginUI.tsx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { View, Render } from "lvgljs-ui";
1+
import { View, Render, Dimensions } from "lvgljs-ui";
22
import { useCallback, useEffect, useRef, useState } from "react";
33
import { createGroup, setKeyboardGroup, on, off } from "lvgljs";
44

@@ -14,6 +14,7 @@ import {
1414
GameboyButton,
1515
KEY_ESCAPE,
1616
KEY_TAB,
17+
MOUSE_BUTTON_LEFT,
1718
MOUSE_BUTTON_RIGHT,
1819
installBindings,
1920
mapGamepadButtonToGameboyButton,
@@ -267,24 +268,36 @@ function PluginUI() {
267268
}
268269
}, []));
269270

270-
// Right-click on a tile: focus the tile + open the menu. Position
271-
// resolution: getTileBounds gives the tile's rect inside the grid; we
272-
// assume window coords ≈ grid coords (true at native zoom on most WMs
273-
// because we drive setWindowSize to match the grid content size).
271+
// Click on a tile: focus it. Right-click also opens the menu. The mouse
272+
// event delivers window-relative coords, but getTileBounds is grid-local.
273+
// The root View flex-centers the grid in the window, so when the
274+
// compositor gives us a window bigger than gridContentSize (tiled WM,
275+
// host-clamped resize) there's a centering offset we must subtract.
274276
useMouse(useCallback((button: number, press: boolean, x: number, y: number) => {
275-
if (button !== MOUSE_BUTTON_RIGHT || !press) return;
277+
if (!press) return;
278+
if (button !== MOUSE_BUTTON_LEFT && button !== MOUSE_BUTTON_RIGHT) return;
276279
if (kitEditorOpenRef.current) return;
280+
if (button === MOUSE_BUTTON_LEFT && menuOpenRef.current) return;
277281
const list = systemsRef.current;
278282
if (list.length === 0) return;
283+
const z = zoomRef.current;
284+
const grid = gridContentSize(list, SystemLayout.Auto, z);
285+
const win = Dimensions.window;
286+
const offX = Math.max(0, (win.width - grid.width) / 2);
287+
const offY = Math.max(0, (win.height - grid.height) / 2);
288+
const gx = x - offX;
289+
const gy = y - offY;
279290
for (let i = 0; i < list.length; i++) {
280-
const b = getTileBounds(i, list.length, SystemLayout.Auto, zoomRef.current);
281-
if (x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h) {
291+
const b = getTileBounds(i, list.length, SystemLayout.Auto, z);
292+
if (gx >= b.x && gx < b.x + b.w && gy >= b.y && gy < b.y + b.h) {
282293
const sys = list[i];
283294
if (sys.id !== focusedIdRef.current) {
284295
setFocusedId(sys.id);
285296
void plugin.$notify("setFocus", sys.id);
286297
}
287-
if (!menuOpenRef.current) setMenuOpen(true);
298+
if (button === MOUSE_BUTTON_RIGHT && !menuOpenRef.current) {
299+
setMenuOpen(true);
300+
}
288301
return;
289302
}
290303
}

0 commit comments

Comments
 (0)