Skip to content

feat(keymap): Shortcut 可以绑定鼠标中键和侧键 - #87

Closed
xiaoyumuxi wants to merge 2 commits into
mainfrom
feat/mouse-button-shortcuts-fork
Closed

xiaoyumuxi wants to merge 2 commits into
mainfrom
feat/mouse-button-shortcuts-fork

Conversation

@xiaoyumuxi

Copy link
Copy Markdown

背景

本 PR 与上游 https://github.com/vivaui/viva/pull/533内容相同,按 fork 维护规则同时提交。

Lithe 想做两件事(1lck/Lithe-IDEA#2554):在标签页上按鼠标中键关闭它;按鼠标侧键后退、前进。还想让用户在快捷键表里绑定带修饰键的中键和侧键(比如 Ctrl+中键)。之前 keymap.Chord 只能表示键盘按键,ui.Shortcut 也只响应按键事件,所以应用层没法用同一套快捷键机制处理鼠标按键。

改动

1. keymap:Chord 能表示中键和侧键(3a6db08d)

  • Chord 新增 Button 字段,名字是 mouse_middle、mouse_back、mouse_forward,可以带修饰键(例如 ctrl+mouse_middle)。Parse、Name、Label、Keycaps 都支持。
  • 新增 FromPointer,把一次鼠标按下转成 Chord。
  • 左键和右键永远不能做 Chord:它们负责选择、拖动和打开菜单。没有给它们起名字,FromPointer 遇到左右键返回 ok=false。
  • 更新 keymap/doc.go 的包说明。

2. ui.Shortcut / Box.Shortcut 响应鼠标按键(3a6db08d)

  • 页面级的 Shortcut:在页面任意位置按下时触发,先于指针下的元素拿到事件。
  • 元素自己的 Shortcut:在该元素上或它里面按下时触发,最内层的元素优先。
  • Page 新增 pointerShortcutRoute,和按键的 shortcutRoute 对称;base/accessors.go 补了对应的读取方法。

3. 修复:带名字的容器,它自己的 Shortcut 要作用在容器本身(a8a479bc)

有些容器会命名一个区域,同时自己也有命中区。例如一个标签页,里面有图标和关闭按钮。以前这种容器的 Shortcut 只对区域里面的目标生效:按在标签页本身上,或者焦点在标签页本身上,都会落到页面级的 Shortcut。现在 shortcutEntry 记下容器自己的 ID(own),目标等于这个 ID 时也算按在元素上。这个修复对按键 Shortcut 同样生效:焦点停在带名字的容器本身上时,以前容器的按键 Shortcut 不会触发,现在会触发。

测试

  • keymap/chord_test.go:解析、序列化、标签和键帽的往返;FromPointer 对左右键返回 false;FromPointer。
  • ui/shortcut_test.go:TestShortcutActsOnMouseButtonsWhereThePressLands 覆盖页面级和元素级的中键、侧键,最内层优先,以及容器本身、容器里的按钮两种落点(chip 用例:按在 chip 上关闭;按在 chip 里的按钮上,由 chip 的 Shortcut 处理,按钮不会被点击)。

性能

在同一台机器上比较改动前后(go test -bench):

基准 改动前 改动后
ShortcutPaint — 约 2.8µs,0 次分配
FromEvent 2.3ns 3.0ns
Lookup 12.7ns 13.3ns
Keycaps 11.8ns 13.8ns

已知的既有失败(和本 PR 无关)

在 main 上运行全量检查时,下面这些本来就失败,本 PR 没有改动它们:cmd/viva、components 的 TestDocCatalogCoversEveryComponent、internal/gallery 的 OTP 测试(TestTypingInTheMiddleOfAnOTP,在 bbc1f4b 上也失败)、tools/docs、tools/visualparity。

A Chord may now name the middle button or a side button of a mouse
(mouse_middle, mouse_back, mouse_forward) with modifiers, and FromPointer
reads one from a press. ui.Shortcut and Box.Shortcut take these names:
a page-wide one acts on a press anywhere in the page before the element
under the pointer has it, and an element's own acts on a press on it or
inside it, the innermost winning. The primary and secondary buttons are
never a chord: they select, drag and open menus.
A container that names a place and has a hit of its own, such as a tab
holding an icon and a close button, matched its Shortcut only for targets
inside the place: a press on the tab itself, or the focus on it, fell
through to the page-wide Shortcut. The entry now keeps the container's
identity, and a target equal to it counts as on the element.
@1lck

1lck commented Dec 2, 2026

Copy link
Copy Markdown

已被上游 vivaui#533 (2bff845) 取代,并在 #89 同步进 fork main。

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