From 85de468bc3977ab6181979e94c869ad9d6715158 Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Sat, 7 Nov 2026 12:43:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(drop):=20=E6=8B=92=E7=BB=9D=E6=94=B9?= =?UTF-8?q?=E7=94=B1=E5=85=89=E6=A0=87=E8=A1=A8=E8=BE=BE=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E5=BE=80=E6=8C=89=E9=92=AE=E4=B8=8A=E5=8F=A0=E7=A6=81?= =?UTF-8?q?=E6=AD=A2=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 按钮处理不了这次拖拽时保持 disable,鼠标光标换成禁止符号;能服务的光标仍是 AppKit 画的「+文件」。页面上那一层 .drop-ban 叠加层删掉了。 macOS 上外部文件走原生拖放通道,而 wry 的 draggingEntered:/draggingUpdated: 固定回报 NSDragOperation::Copy,页面改不动那条通道的光标。新增 src-tauri/src/drop_cursor.rs:页面把判定用 set_drop_refusal 交下来,原生侧在 运行时替换 wry 私有类的这两条方法(类名带 crate 版本,按前缀扫描),把 Copy 收窄成 NSDragOperationNone。两次都先调用 wry 自己的实现再改返回值——那次调用 正是 over/dragover 的投递入口,拒绝时提前 return 会让页面再也收不到指针移动, 一次拒绝就粘到整段手势结束。回报 None 时既没有 performDragOperation: 也没有 draggingExited:,所以拒绝期间盯一次鼠标松开,用 drop://cancelled 收起面板状态。 Windows 不需要这套:OLE 的回报跟随页面设置的 dropEffect。 实测:PDF 从「最近使用」和从访达分别拖到「文字识别」,两处都是按钮禁用+光标 禁止、无 hover、松手静默;中途拖到能服务的按钮立刻恢复并正常弹出圆环。 --- README.md | 2 +- README.zh-CN.md | 2 +- docs/ENGINEERING-NOTES.md | 53 +++++--- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 3 + src-tauri/src/drop_cursor.rs | 248 ++++++++++++++++++++++++++++++++++ src-tauri/src/lib.rs | 2 + src/components/MainWindow.tsx | 46 +++++-- src/lib/api.ts | 7 + src/styles.css | 48 ++----- 10 files changed, 342 insertions(+), 70 deletions(-) create mode 100644 src-tauri/src/drop_cursor.rs diff --git a/README.md b/README.md index 6eb47a1..3218919 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Shift lives in the menu bar (macOS) and the notification area (Windows), and it **Use a tool (`Option + Shift`, or `Alt + Shift` on Windows).** Same gesture, and the wedges become tools for that file type instead of formats. While the ring is open, keep `Shift` held and press or release `Option`/`Alt` to flip between the format ring and the tool ring. -**Prefer not to drag?** In the main window, **Convert files** and **File tools** open a picker and then show the same ring for what you chose; **OCR** picks an image and reads it. Dropping a file is **aimed**: the three buttons are the only drop targets, and the button you release on is the action it runs - **Convert files** opens the format ring, **File tools** the tool ring, **OCR** reads the image. All three light up as candidates while a drag is in flight, and only they react: the panel around them stays still. Release anywhere else and nothing happens at all. A button that cannot serve the file you are dragging is **switched off for the length of the gesture** and carries a prohibition sign over its own label - **OCR** for anything that is not an image, **Convert files** for a format with nothing to convert it into. A switched-off button takes no hover feedback at all, and releasing on it does nothing, silently: no empty ring, no handover to another ring, no message. **File tools** never switches off, because its ring ends in Open, Reveal and Send. The same rule, and the same look, decide a drop in every case - whichever platform you are on, and whether the file came from the file manager or from the Recent list. +**Prefer not to drag?** In the main window, **Convert files** and **File tools** open a picker and then show the same ring for what you chose; **OCR** picks an image and reads it. Dropping a file is **aimed**: the three buttons are the only drop targets, and the button you release on is the action it runs - **Convert files** opens the format ring, **File tools** the tool ring, **OCR** reads the image. All three light up as candidates while a drag is in flight, and only they react: the panel around them stays still. Release anywhere else and nothing happens at all. A button that cannot serve the file you are dragging is **switched off for the length of the gesture**, and the cursor turns into a prohibition sign over it - **OCR** for anything that is not an image, **Convert files** for a format with nothing to convert it into. A switched-off button takes no hover feedback at all, and releasing on it does nothing, silently: no empty ring, no handover to another ring, no message. **File tools** never switches off, because its ring ends in Open, Reveal and Send. The same rule, and the same look, decide a drop in every case - whichever platform you are on, and whether the file came from the file manager or from the Recent list. **Several files at once.** Multi-select with `⌘`/`Ctrl`-click, then drag. Every file is processed. With 2+ images or 2+ PDFs selected, a **Merge** wedge appears that combines them into a single PDF. diff --git a/README.zh-CN.md b/README.zh-CN.md index 6e67385..de52a63 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -73,7 +73,7 @@ Shift 常驻菜单栏(macOS)与通知区域(Windows),同时也有普 **使用工具(`Option + Shift`,Windows 为 `Alt + Shift`)** 操作相同,扇区会从格式换成该类型文件可用的工具。圆环打开时,保持按住 `Shift`,再按下或松开 `Option`/`Alt`,即可在格式环与工具环之间来回切换。 -**不想拖动?** 主窗口中的「文件转换」与「文件工具」会先让你挑选文件,然后对选中的文件弹出同一个圆环;「文字识别」则选取图片并识别。拖放文件必须**对准按钮**:三个按钮是唯一的落点,松开在哪个按钮上就执行那个按钮的动作——「文件转换」打开格式环,「文件工具」打开工具环,「文字识别」直接识别图片。拖拽进行中三个按钮会同时显示为候选落点,并且只有按钮会响应,周围的面板保持静止;松开在其他位置什么都不会发生。**处理不了的按钮会在这次拖拽期间被直接禁用,并在按钮上层显示禁止符号**:不是图片的文件会禁用「文字识别」,没有任何可转换目标的格式会禁用「文件转换」。被禁用的按钮没有任何 hover 反馈,松手时什么都不执行、安静地不做,既不会弹出一个空圆环,也不会转交给别的圆环,也不会弹提示。「文件工具」永远不会被禁用,因为它的环里总有打开、在文件夹中显示与发送。同一条规则、同一种观感,在所有平台上、无论文件来自访达/资源管理器还是「最近使用」,判定结果都一致。右侧「最近使用」里的条目同样可以拖到这三个按钮上,用同一次拖拽复用已经处理过的文件。 +**不想拖动?** 主窗口中的「文件转换」与「文件工具」会先让你挑选文件,然后对选中的文件弹出同一个圆环;「文字识别」则选取图片并识别。拖放文件必须**对准按钮**:三个按钮是唯一的落点,松开在哪个按钮上就执行那个按钮的动作——「文件转换」打开格式环,「文件工具」打开工具环,「文字识别」直接识别图片。拖拽进行中三个按钮会同时显示为候选落点,并且只有按钮会响应,周围的面板保持静止;松开在其他位置什么都不会发生。**处理不了的按钮会在这次拖拽期间被直接禁用,光标同时变成禁止符号**:不是图片的文件会禁用「文字识别」,没有任何可转换目标的格式会禁用「文件转换」。被禁用的按钮没有任何 hover 反馈,松手时什么都不执行、安静地不做,既不会弹出一个空圆环,也不会转交给别的圆环,也不会弹提示。「文件工具」永远不会被禁用,因为它的环里总有打开、在文件夹中显示与发送。同一条规则、同一种观感,在所有平台上、无论文件来自访达/资源管理器还是「最近使用」,判定结果都一致。右侧「最近使用」里的条目同样可以拖到这三个按钮上,用同一次拖拽复用已经处理过的文件。 **一次处理多个文件。** 用 `⌘`/`Ctrl` 点选多个文件后一起拖动,每个文件都会被处理。选中 2 张以上图片或 2 个以上 PDF 时,会出现「合并」扇区,把它们合成一个 PDF。 diff --git a/docs/ENGINEERING-NOTES.md b/docs/ENGINEERING-NOTES.md index 8104723..07b92e9 100644 --- a/docs/ENGINEERING-NOTES.md +++ b/docs/ENGINEERING-NOTES.md @@ -23,7 +23,7 @@ | 0.1.8 | 最近文件改为收藏并移除拖回主操作按钮的交互;Windows 增加单实例保护;macOS DOCX→PDF 改用 Quick Look 源,保留页眉、页脚与装订版式。 | | 0.1.9 | 重新支持把「最近使用」拖回三个主操作按钮,并且**按钮是唯一落点**——松开在面板其他位置不执行任何操作(见「『最近使用』拖回主面板的三个按钮」);设置面板字号统一到 `--text-*` 阶梯;取消「截图后」行为选项,固定为结果卡片;修复 macOS「运行组件」把 AppKit 文档渲染器报成 Pandoc 缺失且没有安装入口的回归。 | | 0.1.10 | 拖放反馈收束到按钮:面板自身不再做拖拽动画,松开在非按钮处彻底静默(去掉 0.1.9 的提示浮层);「文字识别」对非图片直接拒绝,不再退回格式环;托盘「识别图片文字…」改为「文字识别」。 | -| 0.1.11 | Windows 实测「最近使用」拖到三个按钮全部完不成:页内文件拖拽在 WebView2 上 DOM 与原生两条投递路径都可能不上报,改由 `dragEnd` 兜底;开始拖动时先让闲置圆环让开,它无边框且始终置顶,正压在按钮上(两节都在「『最近使用』拖回主面板的三个按钮」里)。三个按钮的可用性判定收进 `src/lib/dropCapabilities.ts`,两种语言、两条投递路径共用一条"能处理就处理,不能处理就禁止"的规则。同一节里另有两处:内部拖拽被原生 `enter` 报成"没有文件"、从而三个按钮全部点亮(`dragPathsKnown()` 改为合并),以及禁止态从"变灰+系统光标"改成**按钮禁用加上层禁止符号**(原生通道的光标由 wry 固定成 Copy,页面改不动)。局域网接收的文件从此也受「完成后在访达中显示」控制,一批只在最后一个落地时显示一次;「运行组件」缺失多项时可以一次装完。 | +| 0.1.11 | Windows 实测「最近使用」拖到三个按钮全部完不成:页内文件拖拽在 WebView2 上 DOM 与原生两条投递路径都可能不上报,改由 `dragEnd` 兜底;开始拖动时先让闲置圆环让开,它无边框且始终置顶,正压在按钮上(两节都在「『最近使用』拖回主面板的三个按钮」里)。三个按钮的可用性判定收进 `src/lib/dropCapabilities.ts`,两种语言、两条投递路径共用一条"能处理就处理,不能处理就禁止"的规则。同一节里另有两处:内部拖拽被原生 `enter` 报成"没有文件"、从而三个按钮全部点亮(`dragPathsKnown()` 改为合并),以及禁止态定为**按钮禁用+光标禁止符号**——光标原本由 wry 固定回报 Copy、页面改不动,现在由 `src-tauri/src/drop_cursor.rs` 在运行时接管那两条回报方法,把页面的判定翻译成原生拖放的拒绝(同一节也记了这条路上"被拒绝的手势没有收尾事件"这个坑)。局域网接收的文件从此也受「完成后在访达中显示」控制,一批只在最后一个落地时显示一次;「运行组件」缺失多项时可以一次装完。 | 历史提交可以这样定位: @@ -426,18 +426,37 @@ N 个组件」,行内的单个安装按钮保持原样,两种入口调用同 但那仍是让用户先做错、再由浮层告知。现在拖拽一开始就按携带的文件判定,识别不了的按钮直接进入 不可用态,松手时什么都不发生,也不再有任何提示。 -**不可用态必须写成"禁用 + 按钮上层的禁止符号",不能只靠光标。** 0.1.11 第一次实现时把拒绝交给 -了系统光标(`dropEffect: "none"`)与一圈红边,实测两条投递路径的观感并不一致:页内拖拽能改 -`dropEffect`,而外部文件走的是原生拖放通道,**macOS 下 wry 的 `draggingUpdated` 固定回报 -`NSDragOperation::Copy`**(`wry/src/wkwebview/drag_drop.rs`),页面根本没有机会把光标改成禁止。 -于是同一个 PDF,从列表拖过去是禁止光标、从访达拖过去是"可复制",用户读到的仍是"看着能放、 -松手没反应"。结论:**拒绝必须画在按钮自己身上**,因为按钮的渲染不受拖放通道影响。现在 -`is-drop-unavailable` 直接把按钮 `disabled`,并用 `.drop-ban` 在按钮上层画禁止符号(圆+斜杠用 -CSS 画,不用图标字体,保证任何缩放下都锐利)。 +**不可用态写成"禁用 + 光标禁止符号",不额外画一层符号。** 拒绝要落在光标上:鼠标底下那个 +复制角标本来就是 AppKit 对"这个落点要不要这个文件"的回答,能服务就画 +,不能服务就画禁止, +用户不需要先学会读按钮的配色。0.1.11 中途曾把拒绝画成按钮上层的禁止符号,理由是**页内拖拽改得动 +`dropEffect`,外部文件却改不动光标**:外部文件走原生拖放通道,而 **macOS 下 wry 的 +`draggingEntered:` / `draggingUpdated:` 固定回报 `NSDragOperation::Copy`** +(`wry/src/wkwebview/drag_drop.rs`,它甚至会把 super 返回的 `None` 改写成 `Copy`)。 +现在的结论是:这条路能接管,于是拒绝交还给光标,按钮只保留 `disabled` 与配色弱化,不再叠一层符号。 -**按钮变暗用自身配色,不用 `opacity`。** 全应用统一的禁用样式是 `opacity: var(--dimmed)`,但 -禁止符号是画在按钮之上的,连按钮一起调暗会把符号一起调淡——而符号才是必须被看清的那一层。 -所以 `is-drop-unavailable` 改用 `color` / `background` 弱化,符号保持满不透明度。 +**`src-tauri/src/drop_cursor.rs` 只改"回答",不改"投递"。** 页面是唯一知道"拖的是什么、 +指针停在哪个按钮上"的东西,所以它把判定用 `set_drop_refusal` 命令交下来;原生侧在运行时替换 wry +那个私有类的两条回报方法(类名由 `wry::wkwebview::class::wry_web_view::WryWebView` 前缀扫描得到, +因为真名带 crate 版本,写死会在升级时静默失效),拒绝时把 `Copy` 收窄成 `NSDragOperationNone`。 +**两次都必须先调用 wry 自己的实现再改它的返回值**:那次调用正是 `over` / `dragover` 的投递入口, +拒绝分支若直接 `return`,页面就再也收不到指针移动,一次拒绝会粘到整次手势结束——从不能服务的按钮 +拖到能服务的按钮也不会亮。Windows 不需要这套:OLE 每次移动都会来问本进程,回报跟随页面设的 +`dropEffect`,光标本来就会是禁止。 + +**被拒绝的手势没有收尾事件,所以盯鼠标松开。** 落点回报 `None` 时 AppKit 既不发 +`performDragOperation:` 也不发 `draggingExited:`,按钮会一直停在禁用态、面板看起来卡住了。 +`drop_cursor.rs` 因此在拒绝生效期间起一个线程轮询 `CGEventSourceButtonState`,松手时向主窗口 +发 `drop://cancelled`,页面用它收起 `dragging` / `dropAction` / `dragPaths`。 + +**每次手势只报一次判定,起点必须是"欢迎"。** `draggingEntered:` 会把判定复位成不拒绝——文件还 +没认全就拒绝会废掉一次本来能用的拖拽。而页面是在 `enter` 之后才回答的,所以 `useEffect` 的依赖里 +必须带上一个"第几次拖拽"的计数器(`dragSession`),否则状态没变化的第二次拖拽不会重新上报, +上一次留下的判定会一直生效。 + +**按钮变暗用自身配色,不用全应用的 `opacity: var(--dimmed)`。** 不用 `opacity` 最早是为了 +保住画在按钮之上的禁止符号——符号不能跟着被调淡。符号换成光标之后这条理由已经不在了,配色 +没有再动:整颗按钮调淡会和"还在加载"混在一起,而改用 `color` / `background` 弱化读起来就是 +"这颗关掉了"。留着它也省下一次已经验收过的观感改动。 **`is-drop-unavailable` 必须 `pointer-events: none`。** 落点由坐标命中(`actionAtPoint`)而不是 由"哪个元素回答了事件"决定;禁用按钮若仍占住命中区,`disabled` 后的按钮不再回应拖拽事件, @@ -489,8 +508,8 @@ CSS 画,不用图标字体,保证任何缩放下都锐利)。 | 点亮候选按钮的开关有两个来源 | 外部拖拽靠原生 `enter` 设置的 `dragging`,内部拖拽原生事件不一定到,必须另有 `rowDragging`,否则从列表拖起时按钮毫无反应。 | | 面板不要用 `is-dragging` 自己做拖放反馈 | 内部拖拽拿不到原生事件,只有外部拖拽会点亮面板,两种拖法观感不同;把反馈收在按钮上,两条路径共用同一套 `has-drop-target`。 | | 三个按钮共用 `src/lib/dropCapabilities.ts` 一条规则 | 判定原先只写在「文字识别」上,另外两个按钮照旧执行,于是"能处理就处理、不能处理就禁止"在同一个面板里有两种口径。视觉状态(`spent()`)、`dropFiles` 的实际执行、`aimedAction` 的点亮,三处必须问同一个函数,否则会出现"看着能按、按下去什么都不做"或者反过来。 | -| 「处理不了」不接受兜底,也不解释拒绝 | 按钮说做什么就做什么:既不能转交别的圆环,也不能弹一条"为什么不行"——那等于让用户先做错再被纠正。按钮禁用加上层禁止符号就是全部答复。 | -| 拒绝信号只能画在按钮上,不能交给系统光标 | 外部文件走原生拖放通道,macOS 下 wry 固定回报 `NSDragOperation::Copy`,页面改不动那条通道的光标;只有按钮的渲染两条通道共用。`dropEffectFor()` 仍然服务页内拖拽(Chromium 会真的显示禁止光标),但它只是加分项,不是唯一答复。 | +| 「处理不了」不接受兜底,也不解释拒绝 | 按钮说做什么就做什么:既不能转交别的圆环,也不能弹一条"为什么不行"——那等于让用户先做错再被纠正。按钮禁用加光标禁止符号就是全部答复。 | +| 拒绝交给光标,按钮侧不再叠符号;为此必须接管原生回报 | 光标上的复制角标本来就是"这个落点要不要文件"的答复,两条投递路径都能改到它,观感天然一致;代价是 macOS 要在 `drop_cursor.rs` 里改掉 wry 固定回报的 `NSDragOperation::Copy`。页内拖拽仍走 `dropEffectFor()`,Chromium 会照它画禁止光标。 | | 禁用按钮要 `pointer-events: none` | 落点靠坐标命中,不靠元素应答;禁用后按钮若仍占命中区,拖拽事件不再回到它身上,`dropAction` 会停在上一个应答的按钮上。 | | 内部拖拽的原生 `enter` 不带文件名,要合并不要覆盖 | `collect_paths` 只读文件列表,页内拖拽带的是 `text/uri-list`,于是回报空数组;直接写入会把 `dragstart` 登记的行路径冲掉,三个按钮全点亮、松手又静默。`dragPathsKnown()` 保留已知的那一份。 | | `actionCanServe` 在"文件未知"时必须返回 true | Windows 的拖拽在载荷可读之前可能报告 0 个文件;按空列表判定会把三个按钮全部禁掉,一次本来能用的手势就废了。只有"知道文件、且知道处理不了"才禁止。 | @@ -548,10 +567,12 @@ CSS 画,不用图标字体,保证任何缩放下都锐利)。 1. 「最近使用」某行 → 拖到「文件转换」→ 只弹出一个格式环,且文件是这一行。 2. 同一行 → 拖到「文件工具」→ 只弹出工具环。 -3. 图片行 → 拖到「文字识别」→ 按钮正常点亮,打开编辑器并自动开始识别;非图片行 → 「文字识别」拖拽全程为禁用态、按钮上层显示禁止符号且不会点亮,**hover 完全无反馈**,松手后不打开编辑器、不开环、**不出现任何提示**,另外两个按钮仍可正常落点。**同一条非图片行还要从访达再拖一次**:两条通道的观感必须一致(这是 0.1.11 的主要回归点)。 -3b. `.key` / `.zip` 这类没有可转换目标的行 → 「文件转换」同样全程禁用+禁止符号、松手不执行;「文件工具」始终可用并正常弹出工具环。 +3. 图片行 → 拖到「文字识别」→ 按钮正常点亮,光标带 +,打开编辑器并自动开始识别;非图片行 → 「文字识别」拖拽全程为禁用态、**光标为禁止符号**、不会点亮,**hover 完全无反馈**,松手后不打开编辑器、不开环、**不出现任何提示**,另外两个按钮仍可正常落点。**同一条非图片行还要从访达再拖一次**:两条通道的光标观感必须一致(这是 0.1.11 的主要回归点)。 +3b. `.key` / `.zip` 这类没有可转换目标的行 → 「文件转换」同样全程禁用+光标禁止、松手不执行;「文件工具」始终可用并正常弹出工具环。 3c. **Windows 必须单独跑一遍 1–3**:这一平台两条投递路径都可能不上报,兜底只在 `dragEnd` 里,macOS 通过不代表 Windows 通过。 3d. 先用「文件转换」弹出一个圆环、再拖一行到按钮上 → 圆环不能吃掉这次拖放(Windows 尤其要看这一条)。 +3e. 拒绝之后把文件拖到**能服务的按钮**上 → 光标必须重新变成 +、按钮重新点亮、松手正常执行。原生侧一旦在拒绝时提前返回而跳过 wry 的实现,页面就收不到指针移动,整段手势都会停在拒绝上。 +3f. 连续拖两次同一批文件 → 第二次不能残留第一次的判定(`dragSession` 就是为了让每次手势都重新上报一次)。 4. 同一行 → 拖到面板空白处 / 标题栏 / 列表本身 → 不开环、不执行、**不出现任何提示浮层**。 5. 从访达拖文件到面板空白处 → 同样不执行、不提示;拖到任一按钮 → 执行该按钮的动作。 6. 行拖出到访达/聊天窗口 → 仍是文件本体,没有被 Shift 自己吃掉。 diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 6af6216..17528be 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -3910,6 +3910,7 @@ dependencies = [ "image", "libc", "lopdf 0.45.0", + "objc2", "printpdf", "quick-xml 0.42.0", "serde", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 65b5e4e..0b53ba7 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -59,6 +59,9 @@ windows-sys = { version = "0.59", features = [ "Win32_Networking_WinSock", ] } +[target.'cfg(target_os = "macos")'.dependencies] +objc2 = "0.6" + [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/src-tauri/src/drop_cursor.rs b/src-tauri/src/drop_cursor.rs new file mode 100644 index 0000000..9bc8744 --- /dev/null +++ b/src-tauri/src/drop_cursor.rs @@ -0,0 +1,248 @@ +//! Who decides the drag cursor above a webview. +//! +//! A button that cannot serve the files in flight is switched off for the length of the +//! gesture, and the cursor says it too: the copy badge AppKit paints beside the pointer +//! *is* the answer to "does this destination want the file", so the refusal belongs there +//! rather than in a mark drawn on top of the button. +//! +//! The page cannot put it there for a file from the file manager: that drag never reaches +//! the DOM, wry takes it, and `draggingUpdated:` reports `NSDragOperation::Copy` whatever +//! the page thinks (`wry/src/wkwebview/drag_drop.rs`). So the verdict is handed down here. +//! Both entry points keep calling wry — the events the page listens for and the drop it +//! accepts are wry's business; only the one word that says "welcome" is ours. +//! +//! Windows needs none of this: there the drag goes through OLE, whose report follows the +//! `dropEffect` the page set, so the cursor already answers `none` when asked. + +#[cfg(target_os = "macos")] +use std::ffi::c_void; +#[cfg(target_os = "macos")] +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; +#[cfg(target_os = "macos")] +use std::sync::Once; +#[cfg(target_os = "macos")] +use std::{thread, time::Duration}; + +use tauri::AppHandle; +#[cfg(target_os = "macos")] +use tauri::Emitter; + +/// Whether the button under the pointer refuses the files in flight. +#[cfg(target_os = "macos")] +static REFUSE: AtomicBool = AtomicBool::new(false); +/// One release watcher at a time, and only while a refusal is on screen. +#[cfg(target_os = "macos")] +static WATCHING: AtomicBool = AtomicBool::new(false); +#[cfg(target_os = "macos")] +static INSTALL: Once = Once::new(); +/// wry's own implementations, kept so that only the report changes. +#[cfg(target_os = "macos")] +static WRY_ENTERED: AtomicUsize = AtomicUsize::new(0); +#[cfg(target_os = "macos")] +static WRY_UPDATED: AtomicUsize = AtomicUsize::new(0); + +/// `NSDragOperationNone`. Anything else tells AppKit the drop is welcome, and the cursor +/// of the session follows that report rather than anything the page renders. +#[cfg(target_os = "macos")] +const OPERATION_NONE: usize = 0; + +/// The shape of `draggingEntered:` and `draggingUpdated:`, which both report an +/// `NSDragOperation`: an option set the size of `NSInteger`. +#[cfg(target_os = "macos")] +type DragReport = + unsafe extern "C-unwind" fn(*mut c_void, objc2::runtime::Sel, *mut c_void) -> usize; + +/// Hand the cursor's answer to the native layer. +/// +/// The page is the only thing that knows what the drag carries and which button the +/// pointer sits on; this is the one word it has about the cursor. Fire and forget - the +/// drag moves on while the call is in flight, and the newest report is the one that counts. +#[tauri::command] +pub fn set_drop_refusal(app: AppHandle, refused: bool) { + #[cfg(target_os = "macos")] + { + install(); + let was_refusing = REFUSE.swap(refused, Ordering::AcqRel); + if refused && !was_refusing && !WATCHING.swap(true, Ordering::AcqRel) { + watch_for_release(app); + } + } + #[cfg(not(target_os = "macos"))] + { + // OLE asks this process what the drop is worth on every move, and WebView2's own + // drag controller forwards the effect the page set, so the cursor already answers + // `none` where it was asked to. There is nothing to override. + let _ = (app, refused); + } +} + +/// The end of a refused drag is never delivered to the page: a destination that reports +/// `None` gets no `performDragOperation:`, and no `draggingExited:` either, so its buttons +/// would stay switched off with nothing in the air. The release of the button that started +/// the drag is watched here instead, and only while a refusal is on screen. +#[cfg(target_os = "macos")] +fn watch_for_release(app: AppHandle) { + thread::spawn(move || { + // The end of the gesture, told to the page so it can stand its buttons down. + let finish = || { + WATCHING.store(false, Ordering::Release); + // Only a gesture still being refused needs telling: one that moved onto a + // button that can serve it is over, and its drop is on its way. + if REFUSE.swap(false, Ordering::AcqRel) { + let _ = app.emit_to("main", "drop://cancelled", ()); + } + }; + // A drag presses, so a session where nothing is held is one the page watches end + // by itself; clearing the refusal here would only cut a working gesture short. + if !primary_button_held() { + WATCHING.store(false, Ordering::Release); + return; + } + // The ceiling is a held button for two minutes, which no real gesture reaches. + for _ in 0..4800 { + if !REFUSE.load(Ordering::Acquire) || !primary_button_held() { + break; + } + thread::sleep(Duration::from_millis(25)); + } + finish(); + }); +} + +/// Whether the primary mouse button is held: a drag always is, so its release ends the +/// session. +#[cfg(target_os = "macos")] +fn primary_button_held() -> bool { + /// `kCGEventSourceStateCombinedSessionState` + const SESSION: i32 = -1; + /// `kCGMouseButtonLeft` + const PRIMARY: i32 = 0; + #[link(name = "CoreGraphics", kind = "framework")] + extern "C" { + fn CGEventSourceButtonState(state: i32, button: i32) -> bool; + } + unsafe { CGEventSourceButtonState(SESSION, PRIMARY) } +} + +/// Replace the two report methods of wry's webview class. +/// +/// Once, on first use: the class exists only after a window has been built, and changing +/// an implementation is safe from any thread. +#[cfg(target_os = "macos")] +fn install() { + INSTALL.call_once(|| { + let Some(class) = wry_web_view_class() else { + eprintln!("Shift could not find the webview class; refusals stay on the buttons."); + return; + }; + let entered = swap( + class, + objc2::sel!(draggingEntered:), + dragging_entered, + &WRY_ENTERED, + ); + let updated = swap( + class, + objc2::sel!(draggingUpdated:), + dragging_updated, + &WRY_UPDATED, + ); + // The one way to notice that the swap never happened is that the cursor never + // says no, which is a thing to find in the log rather than by hand. + if entered && updated { + eprintln!( + "Shift answers for the drag cursor: {}", + class.name().to_string_lossy() + ); + } + }); +} + +/// Replace one method and remember what was there before. +#[cfg(target_os = "macos")] +fn swap( + class: &'static objc2::runtime::AnyClass, + selector: objc2::runtime::Sel, + replacement: DragReport, + slot: &AtomicUsize, +) -> bool { + let Some(method) = class.instance_method(selector) else { + return false; + }; + let implementation = + unsafe { std::mem::transmute::(replacement) }; + let previous = unsafe { method.set_implementation(implementation) }; + slot.store(previous as *const c_void as usize, Ordering::Release); + true +} + +/// The class wry builds its webviews from. +/// +/// Its name is generated from the module path plus the crate version, so it is looked for +/// rather than written down: an upgrade that only moves the version keeps working, and the +/// parent view of the same module cannot be mistaken for it. +#[cfg(target_os = "macos")] +fn wry_web_view_class() -> Option<&'static objc2::runtime::AnyClass> { + const NAME: &[u8] = b"wry::wkwebview::class::wry_web_view::WryWebView"; + objc2::runtime::AnyClass::classes() + .iter() + .find(|class| class.name().to_bytes().starts_with(NAME)) + .copied() +} + +/// A session always begins permissive: the page answers a moment later, and refusing +/// before the carried files are known would waste a drag that works. The verdict is read +/// again after wry has run, so an entry that arrives while the page is still deciding +/// welcomes the file like any other. +#[cfg(target_os = "macos")] +unsafe extern "C-unwind" fn dragging_entered( + this: *mut c_void, + selector: objc2::runtime::Sel, + info: *mut c_void, +) -> usize { + REFUSE.store(false, Ordering::Release); + let welcome = report(WRY_ENTERED.load(Ordering::Acquire), this, selector, info); + refuse_if_verdict_says_so(welcome, REFUSE.load(Ordering::Acquire)) +} + +#[cfg(target_os = "macos")] +unsafe extern "C-unwind" fn dragging_updated( + this: *mut c_void, + selector: objc2::runtime::Sel, + info: *mut c_void, +) -> usize { + let welcome = report(WRY_UPDATED.load(Ordering::Acquire), this, selector, info); + refuse_if_verdict_says_so(welcome, REFUSE.load(Ordering::Acquire)) +} + +/// Say no where wry would have said yes, and never the other way round. +/// +/// wry's implementation is always called first, and its answer is the one that gets +/// narrowed rather than replaced: that call is what carries `over` on to the page, and a +/// refusal that skipped it would also stop the page from hearing that the pointer had +/// moved. A destination that refuses once and then never hears again cannot change its +/// mind, and a gesture that started on a button which cannot serve the file would be lost +/// even after it moved onto one that can. +#[cfg(target_os = "macos")] +fn refuse_if_verdict_says_so(welcome: usize, refused: bool) -> usize { + if refused && welcome != OPERATION_NONE { + OPERATION_NONE + } else { + welcome + } +} + +/// Call wry's own implementation, so everything except the report is untouched. +#[cfg(target_os = "macos")] +fn report( + previous: usize, + this: *mut c_void, + selector: objc2::runtime::Sel, + info: *mut c_void, +) -> usize { + if previous == 0 { + return OPERATION_NONE; + } + let previous: DragReport = unsafe { std::mem::transmute(previous) }; + unsafe { previous(this, selector, info) } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 232552f..8c93818 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,6 +1,7 @@ mod autostart; mod capture; mod drag_bridge; +mod drop_cursor; mod editor; mod files; mod history; @@ -426,6 +427,7 @@ pub fn run() { settings::set_setup_reviewed, settings::get_wedge_orders, settings::reveal_output_directory, + drop_cursor::set_drop_refusal, update::update_check, update::update_status, update::update_download, diff --git a/src/components/MainWindow.tsx b/src/components/MainWindow.tsx index 3570064..0ecf862 100644 --- a/src/components/MainWindow.tsx +++ b/src/components/MainWindow.tsx @@ -255,6 +255,13 @@ export function MainWindow() { * on `enter`, a Recent row on `dragstart`. */ const [dragPaths, setDragPaths] = useState(null); + /** + * Counts the drags this window has seen, so the cursor's verdict is handed to the + * native layer once per gesture as well. The native side opens every session permissive, + * which means the report it is given has to come after that - and only the arrival of a + * drag says when that is. + */ + const [dragSession, setDragSession] = useState(0); const [hotkeys, setHotkeys] = useState(null); const dropTargets = useRef>>({}); /** @@ -702,6 +709,7 @@ export function MainWindow() { const drag = recentDrag.current; if (event.payload.type === "enter") { setDragging(true); + setDragSession((value) => value + 1); // `enter` is the first moment the carried files are known, which is // early enough to mark a button that cannot serve them. const carried = event.payload.paths; @@ -735,6 +743,13 @@ export function MainWindow() { } }) .then((unlisten) => unlisteners.push(unlisten)); + // A refused drag ends without a drop and without a leave: the release is the only + // sign it is over, and the native layer is the only thing that sees it. + void listen("drop://cancelled", () => { + setDragging(false); + setDropAction(null); + setDragPaths(null); + }).then((unlisten) => unlisteners.push(unlisten)); void listen("app://error", (event) => flash(tLive(event.payload), FEEDBACK.error)).then((unlisten) => unlisteners.push(unlisten), ); @@ -783,20 +798,27 @@ export function MainWindow() { }, [recent, historyQuery]); // One rule for all three buttons and both platforms: a button that cannot serve the - // files in flight shows spent, and a release on it does nothing. The button under the - // pointer keeps its cursor, and the refusal itself is drawn on the button: a file - // from the file manager arrives through the native drag channel, where macOS fixes the - // drag operation to `copy`, so the cursor cannot carry the answer. The button must. + // files in flight is switched off for the length of the gesture, and a release on it + // does nothing. The cursor says the same thing, because a refusal that only arrives + // after the file is let go reads as a dropped action: the button that can serve the + // drag keeps the copy badge AppKit paints for a willing destination, and the one that + // cannot takes the prohibition sign instead. const spent = (action: DropAction) => dragPaths !== null && !actionCanServe(action, dragPaths); // A button that cannot serve the drag is not a candidate, so it never lights up. const aimedAction = dropAction && actionCanServe(dropAction, dragPaths) ? dropAction : null; - // The prohibition mark reads the same whichever way the file was picked up, and it - // stays for the length of the gesture rather than only while hovered. - const banMark = (action: DropAction) => - spent(action) ? ( -