Repository navigation
fix(windows): retain editor focus during mouse drag selection - #7554
Conversation
Generated-by: Maka (OpenAI)
M4n5ter
left a comment
There was a problem hiding this comment.
English
Thanks for tracing this regression! The root-cause analysis of how #7365 inverted PressDown / Blur and cleared RichEditor's drag anchor is spot-on.
A couple of suggestions before merging:
1. Drop EventKind::Touch from press_keeps_field and test the real #7365 button path
Right now press_keeps_field checks:
let press_keeps_field = mouse_down.is_some_and(|press| {
press.events.contains(EventKind::Touch) && press.view.state().props.focusable == Some(false)
});Using EventKind::Touch as a discriminator for RichEditor has two issues:
- The
#7365Quote button doesn't take theelse ifbranch at all.MessageQuote.tsx/SelectionToolbar.tsxrender a regular<Button>(nsh-base/src/button.rs), which hasfocusable = Noneand listens to bothEventKind::PressandEventKind::PressDown. Becausekeyboard::is_stopistrue, it entersif let Some(target) = keyboard::focus_target(...) { focus_pointer(...) }and never reaches theelse if !press_keeps_fieldblank-space fallback. - Peer
focusable(false) + PressDowncontrols withoutEventKind::Touchstill get blurred. Every otherfocusable(false) + PressDowncontrol innsh-baseexplicitly opts out of focus so clicking it keeps keyboard focus in the active input/editor (matching macOS and Linux):nsh-base/src/listbox.rs(option rows: "The desktops choose as the button goes down, so a search field keeps the keyboard focus")nsh-base/src/command.rs(searchable command root & option rows, same comment)nsh-base/src/emoji_picker/mod.rs(item rows, same comment)nsh-base/src/editor/completion.rs(completion items)nsh-base/src/editor/view.rs(gutter fold buttons)
For the same reason, in the updated test, turning EventKind::Touch off on content (while keeping focusable = Some(false) and no EventKind::Press) doesn't actually exercise the #7365 focusable-button focus_pointer path, and instead locks in the unwanted blur for focusable(false) + PressDown controls.
Suggested fix:
- Check only
mouse_down.is_some_and(|press| press.view.state().props.focusable == Some(false))(removingpress.events.contains(EventKind::Touch)). Overlay backdrops (Sheet/Popup/Popover/Menu) only attachon_press, notPressDown, so they are unaffected. - In the unit test, add a separate focusable button (
focusable = None,EventKind::Press+EventKind::PressDown) to verify[PressDown, Blur]and focus transfer for the#7365path, verify thatfocusable = Some(false)+PressDown(both with and withoutTouch) keeps the field focused, and keep the blank-space blur assertion.
2. Draft status & verification gap
The PR body notes that the default-feature suite hasn't been verified yet so the PR remains a draft, though GitHub currently shows it as Ready for Review. Note that PR Check only runs fast checks and doesn't compile or test nsh-platform-windows. Before merging, please verify the default-feature Windows test suite and ideally check both Composer drag selection and Quote selection in the Windows desktop app.
Minor note (follow-up)
If a RichEditor / Editor is ever nested inside a focusable ancestor (onPress or focusable = true), focus_hit() and keyboard::focus_target() will still walk past focusable(false) and blur the sibling sink. Maka's current Composer tree doesn't hit this, so it doesn't need to block this PR (and any future change there should be careful to preserve non-searchable Command, whose root is intentionally focusable when clicking an option).
简体中文
感谢定位这个回归!关于 #7365 调整派发顺序后导致 PressDown 后紧跟 Blur、进而清空 RichEditor 拖拽锚点的根因分析很准确。
合入前有两点建议:
1. 去掉 press_keeps_field 里的 EventKind::Touch 限制,并覆盖真实的 #7365 按钮路径
目前的判断条件是:
let press_keeps_field = mouse_down.is_some_and(|press| {
press.events.contains(EventKind::Touch) && press.view.state().props.focusable == Some(false)
});拿 EventKind::Touch 在通用输入层区分 RichEditor 与普通按钮有两个问题:
#7365的 Quote 按钮本来就不走else if分支。MessageQuote.tsx/SelectionToolbar.tsx用的是标准<Button>(nsh-base/src/button.rs),focusable = None且同时监听EventKind::Press和EventKind::PressDown,属于合法焦点停靠点(keyboard::is_stop == true),会直接走前面的if let Some(target) = keyboard::focus_target(...) { focus_pointer(...) },根本不会落到else if !press_keeps_field的空白兜底分支。- 同类的
focusable(false) + PressDown控件(不带Touch)依然会被误失焦。nsh-base里其他所有显式设置focusable(false) + PressDown的桌面控件,本身就是为了“点击时不抢走当前输入框/编辑器的键盘焦点”(与 macOS / Linux 行为一致):nsh-base/src/listbox.rs(选项行,注释明写 “so a search field keeps the keyboard focus”)nsh-base/src/command.rs(可搜索模式下的 root 与选项行,注释同上)nsh-base/src/emoji_picker/mod.rs(选项行,注释同上)nsh-base/src/editor/completion.rs(代码补全项)nsh-base/src/editor/view.rs(行号槽折叠按钮)
同样地,PR 里新增的单测只是把 content(依然是 focusable = Some(false) 且没有 EventKind::Press)的 EventKind::Touch 关掉来断言 [PressDown, Blur],这既没有测到 #7365 真实的可聚焦按钮 focus_pointer 路径,反而把 focusable(false) + PressDown 控件误失焦的行为写死了。
建议修改:
- 去掉
press.events.contains(EventKind::Touch),直接按显式契约mouse_down.is_some_and(|press| press.view.state().props.focusable == Some(false))跳过空白兜底失焦。(Sheet/Popup/Popover/Menu的 backdrop 只挂了on_press,没挂PressDown,不会受影响。) - 调整单测:单独构造一个真实的可聚焦按钮(
focusable = None+EventKind::Press+EventKind::PressDown)验证[PressDown, Blur]和焦点转移;验证focusable = Some(false) + PressDown(无论是否带Touch)都保持输入框焦点;保留点空白处失焦的断言。
2. Draft 状态与验证补齐
PR 描述里提到默认特性套件尚未验证、PR 保持 Draft,但目前 GitHub 上已经处于 Ready for Review 状态。另外 PR Check 只跑快速检查,不会编译和测试 nsh-platform-windows。建议合入前跑通默认特性下的 Windows nsh-platform-windows 测试套件,并尽量在 Windows 桌面端同时确认 Composer 拖选与 Quote selection 都正常。
附注(不阻断本 PR)
如果后续有 RichEditor / Editor 嵌套在可聚焦祖先(带 onPress 或 focusable = true)内部,focus_hit() 和 keyboard::focus_target() 仍会越过 focusable(false) 把焦点转给祖先并导致 sibling sink 失焦。Maka 当前的 Composer 视图树不触发这一路径,本 PR 不必强行扩大范围(后续如果要处理,也需注意不要破坏非搜索模式下 Command 点击选项时聚焦可聚焦 root 的预期行为)。
Generated-by: Maka
|
@M4n5ter, thanks for pointing out that the Quote button takes the focus-target path. Addressed in f5707a1:
|
Summary
Restore mouse drag selection in an already-focused Windows Composer without reverting #7365. RichEditor handles mouse-down on its non-focusable content while a sibling input holds keyboard focus. Treating that press as a click on blank space blurred the input and cleared the drag anchor.
Skip the fallback field blur when the mouse
PressDownowner explicitly setsfocusable = Some(false), whether or not it listens forTouch. A focusable Quote button still receivesPressDownbefore the field receivesBlur, so it can read the selection before taking focus. Clicking outside on blank space still blurs the field.Verification
PressandPressDown) with event order and focus transfer assertions, and blank-space blur.cargo test -p nsh-platform-windowswithCARGO_BUILD_JOBS=4: 428 passed, 0 failed, 3 ignored (426 unit tests and 2 doctests passed).cargo fmt -p nsh-platform-windows -- --checkandgit diff --checkpassed.AI use
Checklist