#include "UiDebugBounds.hpp" #include #include #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(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(std::lround(x)); out.top = static_cast(std::lround(y)); out.right = static_cast(std::lround(x + width)) - 1; out.bottom = static_cast(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& 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 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