Skip to content

feat(ui): let a text field look at a paste before it takes it - #76

Closed
xiaoyumuxi wants to merge 1 commit into
mainfrom
feat/text-paste-hook
Closed

xiaoyumuxi wants to merge 1 commit into
mainfrom
feat/text-paste-hook

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 28, 2026 •

Copy link
Copy Markdown

关联 Issue:无(在 Lithe 的 Agent 对话框里发现:粘贴文件管理器复制的文件或截图时,应用没有机会把它们作为附件,只能看着文件名被当成文字贴进去)

上游 PR:vivaui#505(同一个提交,cherry-pick 到上游 main)

改了什么

TextField 和 TextArea 新增 Box.OnPaste(handler func(text string) bool)。每次往这个输入框粘贴,不管来自键盘(⌘V / Ctrl+V)、右键编辑菜单的“粘贴”还是系统菜单的“粘贴”,输入框接收之前都会先交给处理函数:

  • 处理函数拿到这次粘贴会插入的文字。单行输入框拿到的是折叠成一行之后的文字,和它实际会插入的一致;剪贴板里没有文字(比如只有一张图片)时拿到空串。
  • 返回 true 表示由处理函数接管:输入框内容不变、不进撤销历史,窗口重绘一次,让处理函数改动的页面状态(比如新加的附件)马上显示出来。
  • 返回 false 时照常粘贴,可以撤销。
  • 剪切不经过处理函数。
  • 不设 OnPaste 时,行为和原来完全一样。

文件管理器复制文件时,通常也会把文件名作为文字放进剪贴板,所以处理函数应该先问 ClipboardFiles,再看文字。文档里的示例就是这么写的。

各层的改动

  • window/text_input.go:新增 TextInput.Paste func(text string) bool 字段和文档。
  • app/painter.go(applyTextEvent):在单行折叠之后、交给 Editor 或 OnEvent 之前调用 Paste。只对两类事件调用:带文字的 TextPaste,以及 TextPasteNoText;explicit 的粘贴不调用。返回 true 时不改编辑器,并把 Redraw 置为真。
  • base/field.go:
    • 新增 Box.OnPaste。它只能用在页面上的 TextField 或 TextArea 上,不能在 Memo 里用,处理函数也不能为 nil;违反任意一条都会 panic,并说明原因。
    • 新增 Page.fieldPaste。
  • base/names.go:新增 fieldEntry.paste。
  • base/page.go:TextInput 把输入框的处理函数交给原生文本输入。
  • base/edit_menu.go:编辑菜单的“粘贴”也是先折叠单行、再交给处理函数,和键盘粘贴一致。
  • cmd/viva/initfiles/AGENTS.md.txt:补一条 .OnPaste 的用法,TestTheGuideMentionsEveryMethod 要求每个方法都写进指南。
  • ui/field_paste_test.go:新增四个测试。
    • TestTextAreaOnPasteTakesOrLeavesThePaste:覆盖文件被接管、图片作为空串交给处理函数、文字照常粘贴且能撤销、剪切不经过处理函数,以及接管后页面重绘。
    • TestTextFieldOnPasteSeesTheTextTheFieldWouldTake:单行输入框拿到的是折叠后的文字。
    • TestEditMenuPasteIsOfferedToOnPaste:编辑菜单的“粘贴”也会交给处理函数。
    • TestOnPasteNeedsATextFieldAndAHandler:用在非输入框上,或处理函数为 nil 时会 panic。

为什么

聊天类输入框要像 Claude Code、Codex CLI 那样,把粘贴进来的文件或截图作为附件,而不是把文件名当文字贴进去。之前应用拿不到“即将粘贴”的时机:

  • 快捷键在原生文本输入里处理;
  • 菜单的粘贴直接写进编辑器;
  • 应用只能等粘贴完成后比对文字差异、再撤销。这样既不可靠,还会把文件名留在撤销历史里。

怎么验证的

在 macOS(Apple Silicon)上运行:

  • gofmt -l app base ui window:没有输出
  • go vet ./app/ ./base/ ./ui/ ./window/;同样的 vet 在 GOOS=windows 和 GOOS=linux 下也跑了;GOOS=js GOARCH=wasm 下 vet 了 ./base/ ./ui/ ./window/
  • go test ./ui/ ./base/ ./app/ ./window/ ./components/ -count=1 全部通过。新增的测试在没有这个钩子时会失败。
  • cmd/viva 的打包测试:本机工具链的默认部署目标和测试里的 min_system 不一致,属于环境问题,和本改动无关
  • 应用侧:Lithe 的 Agent 输入框用这个钩子实现了几种粘贴:粘贴复制的文件变成附件、粘贴截图后保存并作为附件、粘贴文字照常输入,并用 headless driver 测试覆盖
  • 真实窗口:没有跑

边界在哪

这是框架能力:粘贴是输入框的通用行为,“粘贴前先看一眼”不属于某个应用的流程。

A chat box that attaches what was copied (the files a file manager
copied, a screenshot) had no way to see a paste before the field
inserted the file names: TextPasteNoText reaches only an application
that owns the editor through OnEvent, and a TextField or TextArea of a
page never shows its editor's events to the application.

TextInput.Paste is offered every paste into the field first, from the
keyboard, the edit menu or the system's menu, with the text the paste
would insert (folded into one line for a single-line field, as macOS
hands it) or nothing when the clipboard has no text. Returning true
takes the paste: the editor is left alone and the window paints again.
A cut is not offered. Box.OnPaste declares it on a TextField or a
TextArea of a page.
@xiaoyumuxi

Copy link
Copy Markdown
Author

上游已以 vivaui#505(086247ad)合入同一改动,#78 把它同步进 fork main(7a73f1ba),合并后这几处文件与本 PR 逐字相同,关闭。

@xiaoyumuxi xiaoyumuxi closed this Nov 29, 2026
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.

1 participant