Skip to content

[0986] 选中文字后下一行出现 AI 操作栏(AI翻译/AI润色/AI对话,一键翻译第一步) - #4555

Merged
PinkMagicFly merged 17 commits into
mainfrom
pigmagicfly/0986/ai-translate
Sep 14, 2026
Merged

[0986] 选中文字后下一行出现 AI 操作栏(AI翻译/AI润色/AI对话,一键翻译第一步)#4555
PinkMagicFly merged 17 commits into
mainfrom
pigmagicfly/0986/ai-translate

Conversation

@PinkMagicFly

@PinkMagicFly PinkMagicFly commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

一键 AI 翻译 · 第一步:选区下方 AI 操作栏

任务文档:devel/0986.md。整个功能分两个 PR,本 PR 为第一步(仅悬浮 UI),
点击按钮后的提示词拼接与侧边栏自动发送在第二步实现。

What

选中文字后,在选中文字的下一行出现一行 AI 操作栏:

  • 三个纯文字按钮(无图标):「AI翻译」「AI润色」「AI对话」
  • 位置规则:始终与选区最末行(get_selection_last_rect:屏幕最下方、同行
    最右的选区矩形)左缘对齐、位于其下方;下方放不下时退到选区上方,始终
    裁剪在视口内
  • 按钮字号与整体尺寸跟随选区内最小文字的字号(渲染高度),含文档缩放因子
  • 中文界面下按钮文案为「AI翻译」「AI润色」「AI对话」(词典新增
    ("ai translate" "AI翻译")("ai polish" "AI润色")("ai chat" "AI对话")
  • Esc、选区取消、选区移出视口或点击选区外时隐藏;滚动时跟随选区;点击按钮
    (或按 Esc)后本次选区内不再弹出,选区变化后自动复位
  • 聊天输入框等 tmfs:// 内嵌页面、数学/代码/verbatim 上下文不弹出
  • 点击按钮的动作在第二步接入,当前仅关闭操作栏

How

复用现有悬浮 popup 体系(QTMBasePopup/QTMTextPopup 家族):

  • 新增 QTMAiTranslatePopup(继承 QTMBasePopup):一行三个 QPushButton
    getCachedPosition 定位到选区下一行;autoSize 按选区最小文字渲染高度
    缩放字号与内边距
  • QTMBasePopup 新增 blank_top_offset() 助手,消除「视口高于画布表面时
    顶部居中留白补偿」的三处复制
  • 选区几何统一由 get_selection_geometry 单次 search_selection 遍历产出
    (最末矩形 + 最小高度),避免每次更新遍历两遍排版树;顺带修复了最小高度
    计算的符号错误(选区矩形不变式 y1 < y2,原 y1 - y2 恒为负导致恒退回
    默认字号)
  • 不复用文本工具栏的 USE_TEXT_TOOLBAR 编译宏(该宏默认关闭,且是另一套
    按钮组),AI 操作栏独立默认开启
  • 触发点与 text popup 并行挂接:选区变化(apply_changes)、键盘(含 Esc
    dismiss)、鼠标移动、滚动(QTMWidget::scrollContentsBy)、点击选区外隐藏;
    选区移出视口由 popup 侧 selectionInView 判定隐藏

单元测试

  • editor_rep 基类新增带默认实现的虚函数 get_selection_min_height /
    selection_last_rect_array(仿 is_pre_editing 模式),经
    glue_editor.lua 暴露为 (selection-min-height) / (selection-last-rect)
  • 曾配集成测试 TeXmacs/tests/0986.scm 钉选区几何语义,但 headless 下断言
    依赖异步排版、无法稳定同步执行(还触发过游标路径崩溃),已移除;几何
    语义(末行定位、最小字号联动)由手动 GUI 验证覆盖

验证

  • xmake b stem 构建通过(含 glue 重新生成)
  • gf fmt --changed-since=main 已跑,格式化改动仅落在本任务文件
  • xmake b qml_load_test && xmake r qml_load_test:24 用例全过,含
    test_ai_actions_bar_loads(QML 操作栏加载)与 test_ai_actions_bar_hover
    (无按键 move 直接点亮/熄灭按钮胶囊——纯悬浮链路回归用例)
  • 操作栏的弹出位置、尺寸联动、显隐时机为 GUI 行为,待手动 GUI 验证

更新:修复按钮纯悬浮不点亮

  • 早期版本给 popup 设了 WA_NativeWindow:macOS 上原生子窗口收不到无按键
    mouseMoved,按钮纯悬浮不点亮、按住拖动/点击才反应。已去掉该属性(圆角
    透明经顶层 backing store 合成即可),并补交此前未入库的
    AiActionsBar.qml 与图标资源(此前远端分支缺这两个资源,无法正常弹出)
  • showPopup 显示后按当前光标位置向离屏 scene 发一次 HoverMove
    syncHover),纠正上次会话残留的 hover 态(弹窗可能在静止光标正下方
    弹出/重定位,不会有鼠标事件到来)
  • 后续清理:software 场景图后端与主题 context 收敛为 qt_utilities 共享助手
    qt_use_software_scene_graph / qt_inject_theme_context /
    qt_is_dark_theme,QTMQmlDialog 与 qt_utilities 原四处内联判定同步替换);
    autoSize 按字号缓存去重,选区存续期间的高频 showPopup 不再重复触发 QML
    重排与定尺寸;测试侧 AiActionsBar 两用例共用 make_ai_actions_bar 脚手架

🤖 Generated with Claude Code

@PinkMagicFly
PinkMagicFly force-pushed the pigmagicfly/0986/ai-translate branch from e5d8b9e to 9ebb181 Compare September 14, 2026 03:43
@PinkMagicFly PinkMagicFly changed the title [0986] 选中文字后右侧出现 AI翻译 悬浮按钮(一键翻译第一步) [0986] 选中文字后下一行出现 AI 操作栏(AI翻译/AI润色/AI对话,一键翻译第一步) Sep 14, 2026

@da-liii da-liii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@MoonL79 MoonL79 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@PinkMagicFly
PinkMagicFly merged commit 5d60bf9 into main Sep 14, 2026
6 checks passed
@PinkMagicFly
PinkMagicFly deleted the pigmagicfly/0986/ai-translate branch September 14, 2026 09:36
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.

3 participants