Skip to content

fix(windows): retain editor focus during mouse drag selection - #7554

Merged
M4n5ter merged 2 commits into
apache:mainfrom
ARE404:fix/windows-rich-editor-drag-blur
Dec 3, 2026
Merged

M4n5ter merged 2 commits into
apache:mainfrom
ARE404:fix/windows-rich-editor-drag-blur

Conversation

@ARE404

@ARE404 ARE404 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

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 PressDown owner explicitly sets focusable = Some(false), whether or not it listens for Touch. A focusable Quote button still receives PressDown before the field receives Blur, so it can read the selection before taking focus. Clicking outside on blank space still blurs the field.

Verification

  • The Windows input regression test covers drag selection's focus prerequisite with a Touch listener, focus retention without one, a separate Quote-style button (default focusability, Press and PressDown) with event order and focus transfer assertions, and blank-space blur.
  • Windows/MSVC, default features: cargo test -p nsh-platform-windows with CARGO_BUILD_JOBS=4: 428 passed, 0 failed, 3 ignored (426 unit tests and 2 doctests passed).
  • cargo fmt -p nsh-platform-windows -- --check and git diff --check passed.
  • Composer drag selection and Quote selection have not been manually verified in the Windows desktop app.

AI use

  • Generative tooling made a substantive contribution: Maka (OpenAI) traced the regression and prepared the patch, test and PR.

Checklist

  • Regression test verified to fail with the old fallback blur behavior.
  • Formatting and the affected Windows unit suite pass locally.
  • No breaking change or persisted-data migration.

@ARE404
ARE404 marked this pull request as ready for review December 3, 2026 01:53

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 #7365 Quote button doesn't take the else if branch at all. MessageQuote.tsx / SelectionToolbar.tsx render a regular <Button> (nsh-base/src/button.rs), which has focusable = None and listens to both EventKind::Press and EventKind::PressDown. Because keyboard::is_stop is true, it enters if let Some(target) = keyboard::focus_target(...) { focus_pointer(...) } and never reaches the else if !press_keeps_field blank-space fallback.
  • Peer focusable(false) + PressDown controls without EventKind::Touch still get blurred. Every other focusable(false) + PressDown control in nsh-base explicitly 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)) (removing press.events.contains(EventKind::Touch)). Overlay backdrops (Sheet / Popup / Popover / Menu) only attach on_press, not PressDown, 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 #7365 path, verify that focusable = Some(false) + PressDown (both with and without Touch) 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 的预期行为)。

@ARE404

ARE404 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@M4n5ter, thanks for pointing out that the Quote button takes the focus-target path. Addressed in f5707a1:

  • Removed the Touch check. An explicitly non-focusable mouse PressDown owner now keeps the field focused with or without a touch listener.
  • The regression test covers both cases and uses a separate button with default focusability, Press, and PressDown to verify both [PressDown, Blur] and focus transfer to the button. The blank-space blur assertion is retained. Nested focusable ancestors are unchanged.
  • The full default-feature Windows suite passed: 428 passed, 0 failed, 3 ignored (including 2 doctests). Formatting and git diff --check passed. I updated the PR description; Composer drag selection and Quote selection have not been manually verified in the desktop app.

@M4n5ter
M4n5ter merged commit 81b6463 into apache:main Dec 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants