Skip to content

[6232] 修复目录树开启时 AI 操作栏悬浮错乱 - #4764

Merged
PinkMagicFly merged 3 commits into
mainfrom
pigmagicfly/6232/fix-ai-actions-bar-position
Oct 26, 2026
Merged

PinkMagicFly merged 3 commits into
mainfrom
pigmagicfly/6232/fix-ai-actions-bar-position

Conversation

@PinkMagicFly

@PinkMagicFly PinkMagicFly commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

概述

修复开启左侧目录树(OutlineSidebar)或侧边栏时,选中文本后 AI 操作栏(QTMAiActionsBar)位置严重右偏或完全无法弹出的问题。任务文档见 LiiiLabs/dev-agent 的 devel/6232.stem。

问题原因

  • QTMBasePopup::selectionRectPixels 把选区逻辑坐标换算为视口像素坐标时,水平方向累加了调用链传入的 cached_canvas_x。
  • cached_canvas_x 来源于 get_canvas_x()(SLOT_POSITION),取值是文档绘制表面 surface() 相对顶级主窗口的全局 X 偏移。当左侧停靠了目录树侧边栏(宽约 300px)时,该偏移包含了这 300px 宽度。
  • 而悬浮框以 parentWidget()(QTMScrollView)局部坐标为基准调用 move(x, y),全局偏移混入局部坐标导致右偏约 300px;虚高坐标又使 selectionInView() 把选区误判为超出视口右边界而强行隐藏,并使 cursorNearSelection() 的鼠标距离超出容差门限,present() 判定光标远离选区而直接隐藏。

解决方案

在 QTMBasePopup::selectionRectPixels 中,改用 Qt 控件树内部的局部坐标映射:通过 owner 的 scrollarea 的 surface() 调用 mapTo(parentWidget(), QPoint(0, 0)) 实时取得文档表面相对悬浮框父控件的局部偏移。该映射发生在 scrollarea 内部层级之间,天然隔绝目录树 DockWidget、顶部工具栏及系统窗口边框对局部坐标的干扰;surface 在视口中以水平垂直居中摆放,映射结果自动涵盖视口大于表面时的居中留白,故同时删除 blank_top_offset() 补偿以避免双重计算。共用该基类的 AI 操作栏、图片工具栏、文本工具栏一并修复。

与 #4720 的差异:本 PR 仅保留坐标修复本体,show_ai_actions_bar 等调用链签名与 cached_canvas_x 字段保持不变,保持最小改动面。

验证

  • 新增 C++ 单元测试 tests/Plugins/Qt/qt_base_popup_test.cpp,在普通 QWidget 宿主中的 QTMScrollView 上,以 300×PIXEL 模拟带侧边栏时 get_canvas_x 的全局偏移传入 cachePosition,断言选区像素宽度精确保持、x 坐标等于 surface 相对父控件的实际偏移、selectionInView 判定选区在视口内:
    xmake b qt_base_popup_test && xmake r qt_base_popup_test
    # Totals: 3 passed, 0 failed
  • 回归有效性实证:把公式临时换回旧的 canvas 累加实现后,该测试如预期失败(Compared doubles are not the same),恢复修复后通过。
  • gf fmt --check 通过。
  • 手动 GUI 验收(目录树开启时选中文本、缩放/滚动贴合)待人工执行,步骤见任务文档 3.1。

@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 dad88cf into main Oct 26, 2026
6 checks passed
@PinkMagicFly
PinkMagicFly deleted the pigmagicfly/6232/fix-ai-actions-bar-position branch October 26, 2026 08:54
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