Files
Just-DataInspector/Components/Shared/Src/UiDebugBounds.cpp
T
DeepSeek V4.1 Flash 24706c906a feat(UI): 布局边界改为四角粗/四边细包角 + 全元素覆盖 + 几何同源(RowRect O(1))
## 1. 边界观感:四角粗、四边细

用户反馈"边框就是实心矩形、粗细不一,效果不佳"。旧实现是四条等宽实心矩形
(层级 0 用 2px、其余 1px),同一元素的角与边粗细不一致,父子矩形共边时还会叠成更粗的一条。

新规则:
- 四条边固定 1 设备像素(四条独立实心矩形;不用 DrawRectangle,笔宽奇偶会偏半像素);
- 四个角额外加粗:层级 0 = 4px、其余 3px,沿水平/垂直各延伸 cornerLen(≤14px 且 ≤ 短边一半);
- 层级 > 0 整体内缩 1px,父子矩形共边不再叠粗;
- 绝不填充(几十层嵌套填充会把内容糊住);
- 整像素对齐 + PixelOffsetModeNone + 关抗锯齿。

四角粗不只是观感:多条边重合时靠角的形状分辨层次(父=青蓝大包角、子=同位置品红小包角)。

## 2. 协议:组件"收集 box",宿主统一绘制

UiComponent::CollectDebugBoxes(UiDebugBoxList&)  默认只登记 Area()
UiWidget::CollectDebugBoxes(boxes, depth)        递归整棵控件树
Application::PaintLayoutBounds                   收集后按 depth 升序一次性描边

刻意保持 box 为**值类型**而不是让它继承控件基类:PlotView 的 4 层位图缓存、
SignalPanel 的 192 槽行位图缓存都要求"绘制由组件自己负责",几何进继承体系会与
缓存键/失效体系打架。box 不参与绘制,因此能让绘制/命中/调试共用同一份几何。

## 3. 覆盖补齐(此前大量元素被裹在外层单位里)

- Toolbar:区域 / 每个按钮 / **按钮内容(文字或齿轮图标,新增 UiButtonWidget::ContentRect())**
- TabBar:区域 / 标签条 / 新建按钮 / 每个 ✕
- SignalPanel:4 分区 + 筛选输入框/清空✕/全部显示 + 滚动轨道与滑块
  + 样式区 5 行及其 12 色块/自定义/3 模式/3 调整/线宽与透明度轨道和数值
  + 目标行/名称/GX 徽标 + **每个可见行**的折起三角/复选框/色块/名称/GX 徽标/✕
  (组行另有组名/计数)+ 内联编辑框
- PlotView:区域 / 每个网格 / GX 徽标 / 装饰层矩形 / 图例托底+色块+文本
  / **5 类浮动面板全部 20+ 字段**(标题/✕/折起三角/徽标/编辑行/按钮/内容区/滚动轨道与滑块
  /透明度轨道与数值/两个开关/各行复选框与数值与滑轨与复位与名称与✕/分组栏/表头/固定点行)
- PopupMenu:浮层 / 每个条目 / 可滚动可视区 / 轨道 / 滑块 / 二级子菜单与条目
- ColorPickerPopup:面板 / 标题 / 预览 / 色号 / 色域 / 色相条 / 3 条滑轨与数值 / 输入框

标签增加**尺寸门槛**(< 56×16 逻辑像素不画):复选框/色块/✕ 紧邻,逐个小框贴字会糊成一片。

## 4. 设置按钮配色 + 齿轮图标

- ToolbarAction::SETTINGS 改为 primary = true,与「导入文件」同为强调色主按钮。
- 齿轮几何重做:齿占最外一圈(0.66R→1.0R)、外环中心线 0.54R/笔宽 0.24R、轴孔 0.17R。
  旧版让齿从环中心线伸出,齿被环的描边吃掉 ⇒ 看起来只剩一个"甜甜圈"。

## 5. 几何同源与性能

- RowRect(row) 原本每次调用都从头累加行高(O(row)),而画一行要调它 **5 次**
  (✕/徽标/名称/组计数/折起三角各一次)⇒ 200 行只见 30 行时每帧上万次加法,
  命中测试(每次鼠标移动)同样走这条路。
  新增 m_rowTop 前缀和(由 UpdateAnimGeometry() 统一重建)⇒ **O(1)**;
  长度与行集合不一致时自动退回逐行累加(正确性优先)。
- ContentOffsetBeforeRow 同样改用前缀和。
- PanelBounds() 收敛 6 处重复的 (m_clientBounds.Width > 1.0f) ? m_clientBounds : m_area。

## 6. 验证

- make release / make debug 零 error 零 warning;TargetCheck.exe 910,336 B(889 KiB < 900 KiB)。
- .preflight/lbcheck:21 / 21 PASS(含 bounds-off-restores-pixels:关闭后逐像素回到开启前)。
- Tools/rendercheck:「工具栏带以下」区域哈希 8/8 与上一轮**逐一相同**
  (证明本轮改动只落在工具栏带内);FUNC 断言 7/7 PASS。
- Tools/uicheck:69 passed / 0 failed。

## 7. 文档

Documents/UI_Refactor_Plan.md / UI_Refactor_Progress.md / UI_Refactor_Report.md 追加第七阶段;
Documents/agent_mem.md 同步变更日志与「2026-10-28 第九轮」硬规则与踩坑记录。
2026-10-28 16:28:57 +08:00

229 lines
8.2 KiB
C++

#include "UiDebugBounds.hpp"
#include <algorithm>
#include <cmath>
#include "PaintUtil.hpp"
namespace jdi
{
namespace
{
/** @brief 四条边的描边宽度(设备像素)。 */
constexpr int kEdgeStroke = 1;
/** @brief 层级 0 的包角粗细(设备像素)。 */
constexpr int kAreaCornerThick = 4;
/** @brief 层级 ≥1 的包角粗细(设备像素)。 */
constexpr int kNodeCornerThick = 3;
/** @brief 包角沿边延伸的长度上限(设备像素;小矩形按比例收缩)。 */
constexpr int kCornerLenMax = 14;
/** @brief 层级 > 0 的矩形内缩量(设备像素):避免与父矩形共边时叠成粗线。 */
constexpr int kNestedInset = 1;
/** @brief 标签底衬的不透明度(保证文字在任何内容上都可读)。 */
constexpr std::uint32_t kTagBackdrop = 0xD9101014u;
/** @brief 标签文字颜色。 */
constexpr std::uint32_t kTagInk = 0xFFFFFFFFu;
/**
* @brief 用整数像素矩形填充(要求调用方已把像素偏移模式设为 `None`)。
*
* 走 GDI+ 而不是 `UiFillRect`:这里需要**精确的 1px 覆盖**,而 `UiFillRect` 的快
* 路径按 `floor/ceil` 取整并可能转交 GDI,混进调试层会带来额外状态切换开销。
*/
void FillPixels(gd::Graphics& g, int x, int y, int width, int height, std::uint32_t argb)
{
if (width <= 0 || height <= 0)
{
return;
}
gd::SolidBrush brush{gd::Color(argb)};
g.FillRectangle(&brush, x, y, width, height);
}
/** @brief 一个已对齐到整像素、可直接填充的矩形。 */
struct PixelRect
{
int left = 0;
int top = 0;
int right = -1;
int bottom = -1;
int Width() const { return right - left + 1; }
int Height() const { return bottom - top + 1; }
bool Valid() const { return right >= left && bottom >= top; }
};
/** @brief 把逻辑矩形对齐到整像素(`inset` 先内缩,用于避免父子共边)。 */
PixelRect AlignToPixels(const gd::RectF& rect, int inset)
{
const float shrink = static_cast<float>(inset);
const float x = rect.X + shrink;
const float y = rect.Y + shrink;
const float width = rect.Width - shrink * 2.0f;
const float height = rect.Height - shrink * 2.0f;
if (!(width > 0.0f) || !(height > 0.0f))
{
return PixelRect();
}
PixelRect out;
out.left = static_cast<int>(std::lround(x));
out.top = static_cast<int>(std::lround(y));
out.right = static_cast<int>(std::lround(x + width)) - 1;
out.bottom = static_cast<int>(std::lround(y + height)) - 1;
if (out.right < out.left)
{
out.right = out.left;
}
if (out.bottom < out.top)
{
out.bottom = out.top;
}
return out;
}
} // namespace
void UiDebugBoxList::Add(const gd::RectF& rect, int depth, std::wstring tag)
{
if (!(rect.Width > 0.0f) || !(rect.Height > 0.0f))
{
return; // 空矩形(未布局 / 该元素当前不显示)不登记。
}
UiDebugBox box;
box.rect = rect;
box.depth = (depth < 0) ? 0 : depth;
box.tag = std::move(tag);
m_boxes.push_back(std::move(box));
}
void UiDebugBoxList::AddAll(const std::vector<gd::RectF>& rects, int depth)
{
for (const gd::RectF& rect : rects)
{
Add(rect, depth);
}
}
std::uint32_t UiDebugBoundsColor(int depth)
{
// 与主题无关的固定配色:调试层必须"永远显眼",不能跟随用户主题变成接近底色。
switch (depth)
{
case 0:
return 0xFF29B6F6u; // 青蓝:顶层组件区域
case 1:
return 0xFFE040FBu; // 品红:组件内部的功能分区
case 2:
return 0xFFFFB300u; // 琥珀:具体控件 / 行
default:
return 0xFF66BB6Au; // 绿:更深的子块(复选框、文本、✕ …)
}
}
void UiDebugPaintBounds(gd::Graphics& g, const gd::RectF& rect, int depth)
{
const int level = (depth < 0) ? 0 : depth;
const int inset = (level > 0) ? kNestedInset : 0;
const PixelRect pixels = AlignToPixels(rect, inset);
if (!pixels.Valid())
{
return;
}
const int width = pixels.Width();
const int height = pixels.Height();
const std::uint32_t color = UiDebugBoundsColor(level);
const int cornerThick = std::min((level == 0) ? kAreaCornerThick : kNodeCornerThick,
std::max(1, std::min(width, height)));
// 包角沿边延伸的长度:默认 14px,但不超过短边的一半(否则四角会连成实线框)。
const int cornerLen = std::max(cornerThick + 1,
std::min(kCornerLenMax, std::max(1, std::min(width, height) / 2)));
const gd::GraphicsState state = g.Save();
// 描边必须锐利:关抗锯齿 + 关半像素偏移,矩形已对齐整像素 ⇒ 1px 就是 1px。
g.SetSmoothingMode(gd::SmoothingModeNone);
g.SetPixelOffsetMode(gd::PixelOffsetModeNone);
// (1) 四边:统一 1px,用实心矩形而不是 `DrawRectangle`
// (后者会因笔宽奇偶而偏移半像素,且抗锯齿会糊成 2px)。
FillPixels(g, pixels.left, pixels.top, width, kEdgeStroke, color); // 上
FillPixels(g, pixels.left, pixels.bottom, width, kEdgeStroke, color); // 下
FillPixels(g, pixels.left, pixels.top, kEdgeStroke, height, color); // 左
FillPixels(g, pixels.right, pixels.top, kEdgeStroke, height, color); // 右
// (2) 四个角:加粗成 ⌜ ⌝ ⌞ ⌟ 包角 —— 这是"一眼定位元素边界"的关键,
// 即使多个矩形共边也能靠角的形状分辨层次。
const int len = std::max(1, std::min(cornerLen, std::max(1, std::min(width, height))));
const int thick = std::min(cornerThick, std::max(1, std::min(width, height)));
// 左上
FillPixels(g, pixels.left, pixels.top, len, thick, color);
FillPixels(g, pixels.left, pixels.top, thick, len, color);
// 右上
FillPixels(g, pixels.right - len + 1, pixels.top, len, thick, color);
FillPixels(g, pixels.right - thick + 1, pixels.top, thick, len, color);
// 左下
FillPixels(g, pixels.left, pixels.bottom - thick + 1, len, thick, color);
FillPixels(g, pixels.left, pixels.bottom - len + 1, thick, len, color);
// 右下
FillPixels(g, pixels.right - len + 1, pixels.bottom - thick + 1, len, thick, color);
FillPixels(g, pixels.right - thick + 1, pixels.bottom - len + 1, thick, len, color);
g.Restore(state);
}
void UiDebugPaintBoxList(gd::Graphics& g, const UiDebugBoxList& boxes,
const UiDebugPaintStyle& style)
{
if (boxes.Empty())
{
return;
}
// 浅层先画、深层后画:深层的包角不会被父级的边盖住。
std::vector<const UiDebugBox*> ordered;
ordered.reserve(boxes.Count());
for (const UiDebugBox& box : boxes.Items())
{
ordered.push_back(&box);
}
std::stable_sort(ordered.begin(), ordered.end(),
[](const UiDebugBox* lhs, const UiDebugBox* rhs)
{ return lhs->depth < rhs->depth; });
for (const UiDebugBox* box : ordered)
{
UiDebugPaintBounds(g, box->rect, box->depth);
if (box->tag.empty() || style.font == nullptr)
{
continue;
}
// ⚠️ **尺寸门槛**:只在"装得下标签"的矩形上画标签。
// 复选框、色块、✕ 这类小方框彼此紧邻,每个都贴一条文字会连成一片糊字,
// 反而看不出边界 —— 它们的归属靠包角形状与颜色层级就能分辨。
const float scale = (style.scale > 0.1f) ? style.scale : 1.0f;
if (box->rect.Width < 56.0f * scale || box->rect.Height < 16.0f * scale)
{
continue;
}
// 标签贴在**左上角内侧**,带深色底衬(内容可能很亮,白字直接压上去会看不清)。
// ⚠️ GDI 文本不受 GDI+ 裁剪约束 ⇒ 用 `UiTextClipped` 明确给出裁剪矩形。
const float textWidth = UiMeasureTextWidth(style.font, box->tag);
const float padX = 3.0f * scale;
const float padY = 1.0f * scale;
const float height = std::round(15.0f * scale);
const gd::RectF backdrop(box->rect.X + 2.0f * scale, box->rect.Y + 2.0f * scale,
textWidth + padX * 2.0f, height);
UiFillRect(g, backdrop, kTagBackdrop);
const gd::RectF text(backdrop.X + padX, backdrop.Y, textWidth,
std::max(1.0f, backdrop.Height - padY * 2.0f));
UiTextClipped(g, box->tag, style.font, text, kTagInk, TextAlign::LEFT, backdrop);
}
}
} // namespace jdi