修正解鎖螢幕後不能拖曳、不能右鍵:互動不再依賴 mousedown
根本原因: 2.0.13 的事件紀錄證實:鎖定螢幕再解鎖後,renderer 只收得到 mouseup 與 mousemove,左右鍵的 mousedown 全部被吃掉,直到重啟才恢復(符合 Windows 對 永不啟用視窗的 WM_MOUSEACTIVATE 回傳 MA_NOACTIVATEANDEAT 的行為,只丟掉按下 那一則訊息)。而左鍵靠 mousedown 設 pressed、右鍵靠 mousedown 開選單,兩者 因此同時失效。 影響: 每次鎖定/解鎖後寵物就不能拖曳、不能右鍵,只能重啟。 修法: 右鍵選單改在 mouseup 開(也是 Windows 慣例);mousemove 的 buttons 位元帶著 「左鍵按著」而沒有 pressed 時補一個 synthetic pressed,拖曳照常;沒有 mousedown 的左鍵 mouseup 當成單擊。補上對應單元測試與 README 說明。 版號 2.0.14。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
+21
-3
@@ -676,6 +676,13 @@ function refreshHitTest() {
|
||||
}
|
||||
|
||||
window.addEventListener("mousemove", (e) => {
|
||||
// 鎖定螢幕再解鎖後,Windows/Chromium 會把送給這個視窗的每一個 mousedown 吃掉,
|
||||
// mouseup 與 mousemove 卻照送(實測見事件紀錄)。mousemove 的 buttons 位元仍然帶著
|
||||
// 「左鍵按著」,用它補一個 pressed,拖曳才不會因為少了 down 就失效。
|
||||
if (!pressed && !drag && (e.buttons & 1)) {
|
||||
pressed = { x: e.clientX, y: e.clientY, sx: e.screenX, sy: e.screenY, moved: false, synthetic: true };
|
||||
log(`mousedown missing; synthesized from buttons at ${e.clientX},${e.clientY}`);
|
||||
}
|
||||
if (pressed) {
|
||||
if (!pressed.moved && Math.hypot(e.screenX - pressed.sx, e.screenY - pressed.sy) > 4) {
|
||||
pressed.moved = true;
|
||||
@@ -700,15 +707,25 @@ window.addEventListener("mousedown", (e) => {
|
||||
log(`mousedown b=${e.button} at ${e.clientX},${e.clientY} pressed=${!!pressed} drag=${!!drag}`);
|
||||
if (e.button === 0) {
|
||||
pressed = { x: e.clientX, y: e.clientY, sx: e.screenX, sy: e.screenY, moved: false };
|
||||
} else if (e.button === 2) {
|
||||
window.pet.send("pet:context-menu");
|
||||
}
|
||||
report();
|
||||
});
|
||||
|
||||
// 點擊與右鍵都以 mouseup 為準:解鎖後 mousedown 會被吃掉(見上),mouseup 不會;
|
||||
// 右鍵選單在放開時開也是 Windows 的慣例。
|
||||
window.addEventListener("mouseup", (e) => {
|
||||
log(`mouseup b=${e.button} at ${e.clientX},${e.clientY} pressed=${!!pressed} moved=${!!pressed?.moved}`);
|
||||
if (e.button !== 0 || !pressed) return;
|
||||
if (e.button === 2) {
|
||||
window.pet.send("pet:context-menu");
|
||||
return;
|
||||
}
|
||||
if (e.button !== 0) return;
|
||||
if (!pressed) {
|
||||
log("mouseup without mousedown; treating as click");
|
||||
onClick();
|
||||
report();
|
||||
return;
|
||||
}
|
||||
const wasDrag = pressed.moved;
|
||||
pressed = null;
|
||||
if (wasDrag) {
|
||||
@@ -718,6 +735,7 @@ window.addEventListener("mouseup", (e) => {
|
||||
} else {
|
||||
onClick();
|
||||
}
|
||||
report();
|
||||
});
|
||||
|
||||
window.addEventListener("contextmenu", (e) => e.preventDefault());
|
||||
|
||||
Reference in New Issue
Block a user