fix(drop): stop the drag-end fallback from eating the drop it waits for

0.1.11 made a dragged row do nothing on every button. The release added to
`dragEnd` landed the gesture immediately, and on macOS the native drop is
reported *after* the button is up: the fallback consumed the gesture with
`dragEnd`'s own position - which Chromium reports as the origin, not as where
the pointer stopped - and the drop that followed was read as a finished
gesture and thrown away. Three buttons, no feedback, on both platforms.

The fallback now waits, and only claims the gesture when it can actually name
a button. A release that aims nowhere stands back, so a real drop that arrives
later still runs, and `landed` keeps the two from both acting. A position of
(0, 0) is treated as no position reported, which is what it is on Chromium, and
answered with the last position the drag moved through.

A row dragged out of the window now carries the file rather than the row. Left
alone the engine photographs whatever is under the pointer, and a Recent row is
a thumbnail, a name, an age, a folder path and five hover buttons - a strip of
the list, which reads as the whole list coming along. The ghost is the file: the
picture the list already decoded, or the extension when there is no picture,
with the name under it, matching the figure the ring draws for a file it is
about to convert.

Rebuilt both 0.1.11 installers from this tree.
main
mula.liu 2026-11-07 00:53:09 +08:00
parent be84d5640f
commit 3057c65d27
6 changed files with 226 additions and 23 deletions

View File

@ -477,6 +477,25 @@ N 个组件」,行内的单个安装按钮保持原样,两种入口调用同
成为兜底:它总会上报,且带 `clientX/clientY`,足够命中按钮;`landed` 标记保证它只在两条
投递路径都没到过的时候执行一次。
**0.1.11 第一次修错了方向。** 那版把兜底写在 `dragEnd` 里立刻执行,而 macOS 的原生
`drop` 是在按钮抬起**之后**才到的:兜底先用 `dragend` 自己的坐标判定(Chromium 在这里报
的是 (0, 0),不是松手位置),把 `landed` 置真,随后真正的那次 drop 被当作"这一笔已经完了"
丢掉——结果从"偶尔不响应"变成"三个按钮全部无反馈"。现在兜底满足两条:
| 约束 | 原因 |
| --- | --- |
| 兜底必须延迟到真正 drop 之后(260 ms),且只由 `landed` 决定谁执行 | 谁先谁是正确答案因引擎而异;macOS 是 drop 在后,页内拖拽在 Chromium 是只有 `dragend`。 |
| 兜底只在**命中按钮**时才落地 | 没命中时它给不出任何 0.1.10 给不了的东西,却可能抢在真正的 drop 之前把这一笔标记完成,把它吃掉。 |
| `dragend` 坐标为 (0, 0) 时改用在途最后一次 `dragover` 的位置 | (0, 0) 是"没报告"而不是"在左上角松手";Chromium 就是这样。 |
| 不要改成"记住离开窗口时就丢掉该位置" | Chromium 在 `dragenter` / `dragleave` 上把 `relatedTarget` 固定为 null,按它清位会让 Windows 的兜底永远拿不到落点——又回到不响应。剩下的误判只剩"从按钮上拖出去"这一种路径,代价远小于什么都不做。 |
### 拖动时的图像是文件,不是那一行
不加 `setDragImage` 时,引擎拍的是指针下的元素本身:缩略图 + 文件名 + 时间 + 目录 + 五个悬停
按钮,看上去像在拖整个列表。`src/lib/dragImage.ts` 造一个和圆环拖拽示意同款的"一个文件 +
名字"的图(图片行复用列表里已经解码好的缩略图,其他行显示扩展名)。它挂在 body 外、屏幕外,
250 ms 后自删:两个引擎拍照的时机不同帧,删早了就拍不到。
### Windows:闲置的圆环会压在按钮上
圆环窗口无边框、`always_on_top`,用完不关就停在原地等点击,而它可能正压在面板的按钮上。

View File

@ -49,7 +49,7 @@ import {
import { getInitialLang, publishLang, tLive, useI18n } from "../i18n";
import { api } from "../lib/api";
import { FEEDBACK } from "../lib/feedback";
import { hitTestDropTarget, toWindowPoint } from "../lib/dropTargets";
import { hitTestDropTarget, releasePoint, toWindowPoint } from "../lib/dropTargets";
import {
actionCanServe,
OCR_IMAGE_EXTENSIONS,
@ -70,6 +70,7 @@ import {
timeAgo,
} from "../lib/utils";
import { dragOutPayload } from "../lib/dragOut";
import { fileDragImage } from "../lib/dragImage";
import { history } from "../lib/history";
import type {
AppSettings,
@ -260,6 +261,12 @@ export function MainWindow() {
* once by the native drop events. Only the first of the two may run an action.
*/
const recentDrag = useRef<{ path: string; landed: boolean } | null>(null);
/**
* The last position the drag in flight moved through, in CSS pixels inside this
* window. Chromium reports `dragend` at the origin rather than where the pointer
* stopped, so this is the only aim that engine can be given.
*/
const dragPointer = useRef<{ x: number; y: number } | null>(null);
const runtimeCheckStarted = useRef(false);
async function finishSetup() {
@ -341,6 +348,7 @@ export function MainWindow() {
function actionAt(position: { x: number; y: number }): DropAction | null {
const point = localPoint(position);
if (point) dragPointer.current = point;
return point ? actionAtPoint(point) : null;
}
@ -365,12 +373,15 @@ export function MainWindow() {
/**
* Land a dragged Recent row, at most once per gesture.
*/
function landRecentRow(action: DropAction | null) {
function landRecentRow(action: DropAction | null, token?: { path: string; landed: boolean }) {
const drag = recentDrag.current;
if (!drag || drag.landed) return;
// A timer from the gesture before this one must not land it on the row's behalf.
if (token && token !== drag) return;
drag.landed = true;
setDropAction(null);
// The gesture is over, whether or not `dragend` arrives to say so.
dragPointer.current = null;
setDragPaths(null);
void dropFiles([drag.path], action).catch((error) => flash(tLive(messageFrom(error)), FEEDBACK.error));
}
@ -384,7 +395,9 @@ export function MainWindow() {
if (!recentDrag.current) return;
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
const next = actionAtPoint({ x: event.clientX, y: event.clientY });
const point = { x: event.clientX, y: event.clientY };
dragPointer.current = point;
const next = actionAtPoint(point);
if (dropAction !== next) setDropAction(next);
}
@ -1018,8 +1031,18 @@ export function MainWindow() {
// is what tells our own drop zones the row has come back.
event.dataTransfer.effectAllowed = "copy";
recentDrag.current = { path: item.path, landed: false };
dragPointer.current = null;
setRowDragging(true);
setDragPaths([item.path]);
// Carry the file, not the row. The engine would otherwise photograph
// the row itself - thumbnail, name, age, folder and the five hover
// buttons - which reads as the list being dragged rather than one
// file. The thumbnail the list already decoded is the picture.
fileDragImage(
event.dataTransfer,
basename(item.path),
event.currentTarget.querySelector("img"),
);
// A ring left open from the last drop sits over these buttons,
// borderless and always on top. Windows hands a drag to whichever
// window is under the pointer, so that ring would claim this drop
@ -1031,31 +1054,39 @@ export function MainWindow() {
}}
onDragEnd={(event) => {
const drag = recentDrag.current;
// A row is dragged as a file, and WebView2's OLE drop target only
// accepts `CF_HDROP`, which an in-page drag never carries. So on
// Windows neither the DOM drop nor the native drop is delivered:
// the gesture simply ends. `dragend` still reports where the
// pointer stopped, which is enough to find the button it was aimed
// at, and the `landed` flag keeps this a fallback rather than a
// second execution wherever a drop did arrive.
// `dragend` is the last event of the gesture on some engines and the
// first on others: macOS reports the native drop *after* the button
// is up, so landing here immediately would consume the gesture with
// dragend's own coordinates and then throw away the real drop. Hand
// it to a short timer instead - if the DOM drop or the native drop
// arrives first it marks the gesture landed and this stands down.
// The timer is what makes a row work on Windows, where WebView2's
// OLE target accepts only `CF_HDROP`, so neither drop path is
// delivered for an in-page drag and the gesture would otherwise end
// with nothing at all having happened.
if (drag && !drag.landed) {
const point = { x: event.clientX, y: event.clientY };
// A row released outside the window went to Explorer or a chat
// window, where nothing of ours runs. The hit-test has slack
// around every button, so the edge has to be checked first.
const inside =
point.x >= 0 &&
point.y >= 0 &&
point.x <= window.innerWidth &&
point.y <= window.innerHeight;
landRecentRow(inside ? actionAtPoint(point) : null);
const end = releasePoint(
{ x: event.clientX, y: event.clientY },
dragPointer.current,
{ width: window.innerWidth, height: window.innerHeight },
);
const aimed = end ? actionAtPoint(end) : null;
// Only an aim at a button is worth landing. When the pointer is
// not over one, standing back is right either way: a real drop may
// still be on its way, and a release off a button runs nothing.
if (aimed) {
const token = drag;
window.setTimeout(() => landRecentRow(aimed, token), 260);
}
}
setDropAction(null);
setDragging(false);
setRowDragging(false);
setDragPaths(null);
// Some engines report the native drop only after the button is
// already up, so the gesture stays recognised for a moment.
// The gesture stays recognised a little longer than the fallback
// needs: an engine that reports the drop after `dragend` must still
// find it here, so it marks the gesture landed rather than reading
// the same file as a fresh drop from outside the app.
if (!drag) return;
window.setTimeout(() => {
if (recentDrag.current === drag) recentDrag.current = null;

View File

@ -0,0 +1,40 @@
import { extensionOf } from "./utils.ts";
/**
* The picture the operating system carries while a file is dragged.
*
* Left to itself the engine photographs the element under the pointer, and a Recent row
* is a thumbnail, a name, an age, a folder path and five hover buttons - a piece of the
* list rather than a file, which reads as though the whole list were being dragged. This
* builds the ghost instead: one file, named, the same figure the ring draws for the file
* it is about to convert.
*/
export function fileDragImage(
dataTransfer: Pick<DataTransfer, "setDragImage">,
name: string,
thumbnail: HTMLImageElement | null,
): void {
const ghost = document.createElement("div");
ghost.className = "file-drag-ghost";
const art = document.createElement("div");
art.className = `file-drag-art ${thumbnail ? "is-image" : ""}`;
if (thumbnail) {
// Cloned rather than re-pointed at the same URL: the list has this picture decoded
// already, and a new `img` may not have finished loading by the time the engine
// photographs the ghost, which would ship a blank card.
art.append(thumbnail.cloneNode());
} else {
// Nothing to picture, so name the kind of file the way the list does.
art.textContent = extensionOf(name) || "file";
}
const label = document.createElement("span");
label.textContent = name;
ghost.append(art, label);
document.body.append(ghost);
// Anchored at the pointer, so the file itself is what sits under the cursor.
dataTransfer.setDragImage(ghost, 60, 60);
// The engine photographs the element as part of `dragstart`, and the two engines do
// it on different ticks, so it has to outlive a frame or two. It sits off screen and
// out of the page's flow the whole time, so lingering costs nothing visible.
window.setTimeout(() => ghost.remove(), 250);
}

View File

@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { hitTestDropTarget, toWindowPoint } from "./dropTargets.ts";
import { hitTestDropTarget, releasePoint, toWindowPoint } from "./dropTargets.ts";
const button = { left: 100, top: 200, right: 300, bottom: 260 };
@ -42,6 +42,34 @@ test("a point on no button selects nothing", () => {
);
});
const viewport = { width: 1080, height: 720 };
const lastOnButton = { x: 342, y: 468 };
test("a reported release point is used as it came", () => {
assert.deepEqual(releasePoint({ x: 500, y: 300 }, lastOnButton, viewport), { x: 500, y: 300 });
});
test("a release outside the window runs nothing of ours", () => {
// Dragged out to Explorer or a chat window. The last position inside the window is
// where the pointer left through, which may well have been over a button, so it must
// not stand in here.
assert.equal(releasePoint({ x: 1500, y: 900 }, lastOnButton, viewport), null);
assert.equal(releasePoint({ x: -5, y: 10 }, lastOnButton, viewport), null);
});
test("a zeroed release point falls back to the pointer's last position", () => {
// Some engines report (0, 0) on `dragend` rather than the position the drag ended at,
// which is an absent coordinate rather than a top-left release.
assert.deepEqual(releasePoint({ x: 0, y: 0 }, lastOnButton, viewport), lastOnButton);
});
test("a zeroed release with nothing observed is still a release at the origin", () => {
// With no move ever observed there is nothing better to aim at, and (0, 0) is a
// corner like any other: it simply hits no button.
assert.deepEqual(releasePoint({ x: 0, y: 0 }, null, viewport), { x: 0, y: 0 });
assert.equal(hitTestDropTarget([["formats", button]], { x: 0, y: 0 }), null);
});
test("a near miss still counts as aiming at the button", () => {
assert.equal(hitTestDropTarget([["formats", button]], { x: 95, y: 205 }), "formats");
});

View File

@ -48,3 +48,31 @@ export function hitTestDropTarget<T extends string>(
}
return null;
}
export type Point = { x: number; y: number };
/**
* Where a drag ended, from the one event that is always delivered.
*
* `reported` is `dragend`'s own position. Three cases, in the order they can be told
* apart: a point outside the window means the file was released on another application,
* so nothing of ours runs; a point at the origin means the engine reported no position
* at all, and the last position the drag moved through stands in; anything else is the
* answer.
*/
export function releasePoint(
reported: Point,
lastSeen: Point | null,
viewport: { width: number; height: number },
): Point | null {
if (
reported.x < 0 ||
reported.y < 0 ||
reported.x > viewport.width ||
reported.y > viewport.height
) {
return null;
}
if (reported.x === 0 && reported.y === 0) return lastSeen ?? reported;
return reported;
}

View File

@ -632,6 +632,63 @@ kbd {
white-space: nowrap;
}
/* The figure a Recent row carries while it is dragged: one file with its name, the
same shape the ring draws for the file it is about to convert. Off flow and off
screen, because the engine photographs it at `dragstart` and the page should never
show it. */
.file-drag-ghost {
position: fixed;
top: -240px;
left: 0;
z-index: -1;
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 6px;
pointer-events: none;
}
.file-drag-art {
display: grid;
place-items: center;
width: 104px;
height: 104px;
border: 3px solid rgba(255, 255, 255, 0.9);
border-radius: 18px;
background: linear-gradient(150deg, #efe6ff, #dcd0fb);
box-shadow: 0 16px 34px rgba(54, 28, 16, 0.28);
color: #6b53c8;
font-size: 19px;
font-weight: 750;
letter-spacing: 0.02em;
text-transform: uppercase;
}
.file-drag-art.is-image {
background: #fffdf9;
}
.file-drag-art img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 15px;
}
.file-drag-ghost > span {
max-width: 170px;
overflow: hidden;
padding: 5px 9px;
border-radius: 7px;
color: #fff;
background: #1677ff;
box-shadow: 0 8px 20px rgba(18, 88, 191, 0.25);
font-size: 12px;
font-weight: 690;
letter-spacing: -0.01em;
text-overflow: ellipsis;
white-space: nowrap;
}
.radial-hint {
position: absolute;
left: 50%;