feat(UI): 显示布局边界(Android 风格)+ 工具栏设置菜单(齿轮)+ 三处可见收益优化
## 1. 新功能:齿轮按钮 -> 应用设置菜单 -> 显示布局边界 - ToolbarAction::SETTINGS 位于「导入文件」左侧;工具栏布局改为 「左侧固定簇(kLeftClusterCount = 2:齿轮 + 导入文件)+ 右侧按钮组」。 - 齿轮是矢量化绘制(8 齿 + 外环 + 轴孔,全部按 half 的比例推导), 不用字体字符「⚙」——该字符在不同回退字体里大小与基线不一致,会忽大忽小、垂直不居中。 UiButtonWidget 新增 UiButtonIcon(NONE / GEAR)与 SetIcon。 - MenuScope::APP_SETTINGS(新增)+ 设置菜单:HEADER「调试」+ 勾选切换项「显示布局边界」 (keepOpen:点击后菜单保持打开,勾选态由 UpdateItemLabel 回写)。 锚点取 Toolbar::ButtonRect(SETTINGS) 的控件实测矩形,宿主不再自己推算按钮几何。 ## 2. 布局边界:基类给协议,组件自己上报 - UiComponent 新增 virtual PaintDebugBounds(g, scale),默认只画 Area()。 - Toolbar / TabBar 覆盖:跳过与区域重合的根控件,遍历 m_root->Children()(层级 1); TabBar 另上报每个 ✕(层级 2)。 - SignalPanel 覆盖:表头 / 筛选框 / 列表 / 样式区(1)+ 滚动条、样式行、当前可见列表行(2)。 - PlotView 覆盖:每个 LaneVisual::rect(1);不上报随数据变化的瞬态几何(曲线/胶囊/提示)。 - UiWidget 新增 PaintDebugBounds(g, scale, depth) 递归整棵控件树 —— 已上树的控件零成本获得边界。 - 宿主 PaintLayoutBounds 补客户区外框、分隔条、打开的菜单/拾色器。 视觉与实现约束(Android「Show layout bounds」风): - 只描边、不填充(几十个嵌套矩形叠加填充会把内容糊住); - 层级配色 青蓝(2px) / 品红 / 琥珀 / 绿(1px); - 线宽按设备像素固定(不乘 scale),临时 PixelOffsetModeNone + 关抗锯齿 + 整像素对齐, 保证 1px 就是 1px; - ⚠️ 只在 Render 最后一步叠加,绝不进入任何位图缓存(PlotView 4 层缓存 / SignalPanel 行位图缓存), 否则关掉开关后线条会残留; - ⚠️ fullFrame 并入 m_showLayoutBounds:开启时必须整帧回送,否则「只回送绘图区」的快路径 会让装饰区边界停在旧帧上。 ## 3. 可见收益优化(行为不变) - Toolbar::Layout:控件树改为按需重建(m_treeDirty),只在规格/字体/主题/缩放/动态文本 变化时重建。拖动面板分隔条时 Relayout 每帧调用一次 -> 不再每帧 10 次堆分配 + 8 次测量缓存失效。 - Toolbar::SetActionLabel:只写 m_labelOverrides(唯一权威)+ 置树脏,消除「覆盖表与控件文本 双写」可能不同步的问题。 - UiWidget::DispatchRedraw(event):新增;收敛 Toolbar / TabBar 里重复的 Dispatch(); return UiRectHasArea(TakeDirty());(TakeDirty 会清零,漏一次就把下一次真实变化 误判成「无变化」)。 ## 4. 验证 - make release / make debug:零 error 零 warning;TargetCheck.exe 897,024 B(< 900 KB)。 - 新增离屏探针 .preflight/lbcheck:21 / 21 PASS (齿轮在导入文件左侧/同高/更窄;开启后青蓝 24,930 px、品红 19,734 px;关闭后逐像素还原; 菜单 scope、锚定贴合按钮、宽度未被撑大、条目初始未勾选、点击翻转开关、菜单保持打开)。 - Tools/rendercheck:8 个场景的「工具栏带以下区域」哈希与改造前**逐一相同** (证明改动像素被完全限制在工具栏带内);整帧哈希因新增按钮整体变化,属预期。 FUNC 断言 7 / 7 PASS。 - Tools/uicheck:69 passed / 0 failed。 ## 5. 文档 Documents/UI_Refactor_Plan.md / UI_Refactor_Progress.md / UI_Refactor_Report.md 追加第六阶段; Documents/agent_mem.md 同步变更日志与「2026-10-28 第八轮」硬约束与验证坑。
This commit is contained in:
1 parent
0ec8055e95
commit
ab19a6e72e
26 files changed
+997
-55
No files matched your search
@@ -168,6 +168,13 @@ namespace jdi
|
||||
*/
|
||||
HCURSOR QueryCursor(int x, int y) const;
|
||||
|
||||
/**
|
||||
* @brief 拾色器面板整体矩形(客户区坐标;未打开时为空矩形)。
|
||||
*
|
||||
* 只读用途:宿主的「显示布局边界」需要把浮层区域也上报出来。
|
||||
*/
|
||||
const gd::RectF& Rect() const { return m_rect; }
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 面板内可进行内联文本输入的字段。
|
||||
|
||||
@@ -24,7 +24,8 @@ namespace jdi
|
||||
NONE = 0, /**< 无来源。 */
|
||||
SIGNAL_PANEL = 1, /**< 信号面板。 */
|
||||
TAB_BAR = 2, /**< 标签栏。 */
|
||||
PLOT_VIEW = 3 /**< 波形绘图区。 */
|
||||
PLOT_VIEW = 3, /**< 波形绘图区。 */
|
||||
APP_SETTINGS = 4 /**< 应用设置菜单(工具栏齿轮按钮):由宿主自己处理,不下发到组件。 */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -223,6 +224,13 @@ namespace jdi
|
||||
*/
|
||||
bool ContainsPoint(int x, int y) const;
|
||||
|
||||
/**
|
||||
* @brief 菜单整体矩形(客户区坐标;未打开时为空矩形)。
|
||||
*
|
||||
* 只读用途:宿主的「显示布局边界」需要把浮层区域也上报出来。
|
||||
*/
|
||||
const gd::RectF& Rect() const { return m_rect; }
|
||||
|
||||
/**
|
||||
* @brief 菜单是否因内容过长而启用了滚动(条目区可滚动、前导标题与尾部条目固定)。
|
||||
* @return 启用滚动返回 true。
|
||||
|
||||
@@ -9,6 +9,18 @@
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief 按钮图标(非 `NONE` 时按钮**只画图标**,忽略文本)。
|
||||
*
|
||||
* 图标用矢量绘制(不用字体里的符号):`⚙` 这类字符在中文/西文回退字体里
|
||||
* 宽高与基线各不相同,会出现"图标忽大忽小、垂直不居中"的问题。
|
||||
*/
|
||||
enum class UiButtonIcon : int
|
||||
{
|
||||
NONE = 0, /**< 无图标(只画文本)。 */
|
||||
GEAR = 1 /**< 齿轮:设置菜单入口。 */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief 一个"文字按钮"控件(工具栏按钮的原语)。
|
||||
*
|
||||
@@ -29,6 +41,11 @@ namespace jdi
|
||||
void Configure(const UiFonts* fonts, const Theme* theme, float scale, std::wstring label,
|
||||
bool primary, bool toggle, float textPadding, float height);
|
||||
|
||||
/** @brief 设置图标(`NONE` 表示回到纯文本按钮)。 */
|
||||
void SetIcon(UiButtonIcon icon);
|
||||
/** @brief 当前图标。 */
|
||||
UiButtonIcon Icon() const { return m_icon; }
|
||||
|
||||
/** @brief 修改文本(宽度会在下一次 `InvalidateMeasure` 后重新实测)。 */
|
||||
void SetLabel(std::wstring label);
|
||||
/** @brief 文本(只读)。 */
|
||||
@@ -69,6 +86,7 @@ namespace jdi
|
||||
const Theme* m_theme = nullptr; /**< 主题。 */
|
||||
float m_scale = 1.0f; /**< 缩放系数。 */
|
||||
std::wstring m_label; /**< 按钮文本。 */
|
||||
UiButtonIcon m_icon = UiButtonIcon::NONE; /**< 按钮图标(非 `NONE` 时忽略文本)。 */
|
||||
bool m_primary = false; /**< 主按钮(强调色实底)。 */
|
||||
bool m_toggle = false; /**< 是否可切换。 */
|
||||
bool m_checked = false; /**< 可切换按钮的选中态。 */
|
||||
|
||||
@@ -73,6 +73,23 @@ namespace jdi
|
||||
(void)y;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 绘制自身的**布局边界**(调试用,Android「Show layout bounds」风格)。
|
||||
*
|
||||
* 默认实现只画 `Area()` 一个矩形;自身内部还有可辨识子块(控件树节点、列表行、
|
||||
* 网格区域……)的组件可以覆盖它,先调本基类再补画**层级更深**的矩形。
|
||||
*
|
||||
* 之所以把入口放在这里而不是让宿主逐个点名:宿主只持有 `UiComponent*` 的表,
|
||||
* 遍历一次就能覆盖全部顶层区域;组件内部结构由组件自己上报(它才知道几何)。
|
||||
*
|
||||
* ⚠️ 只在宿主开启「显示布局边界」时才被调用 —— 平时**零开销**(不进任何绘制路径、
|
||||
* 不进任何缓存)。实现时**禁止**写进自持位图缓存,否则关掉开关后线条会残留。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文(宿主绘制的最后一步,覆盖在所有内容之上)。
|
||||
* @param [IN] scale DPI 缩放系数。
|
||||
*/
|
||||
virtual void PaintDebugBounds(gd::Graphics& g, float scale);
|
||||
};
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef __UI_DEBUG_BOUNDS_HPP__
|
||||
#define __UI_DEBUG_BOUNDS_HPP__
|
||||
|
||||
#include <windows.h>
|
||||
#include <objidl.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "Theme.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief 布局边界描边颜色(按层级取色,风格参考 Android 的「Show layout bounds」)。
|
||||
*
|
||||
* 层级只用于**区分归属**,不代表重要性:层级越深说明越靠近叶子控件。
|
||||
*
|
||||
* @param [IN] depth 层级(0 = 顶层组件区域,≥1 = 内部控件)。
|
||||
* @return ARGB 颜色(不透明)。
|
||||
*/
|
||||
std::uint32_t UiDebugBoundsColor(int depth);
|
||||
|
||||
/**
|
||||
* @brief 绘制一个矩形的布局边界。
|
||||
*
|
||||
* 视觉规则(Android「Show layout bounds」风):
|
||||
* * 层级 0(顶层组件区域):2 设备像素描边 + 青蓝;
|
||||
* * 层级 1(控件节点):1 设备像素描边 + 品红;
|
||||
* * 层级 ≥2(内部子块):1 设备像素描边 + 琥珀 / 绿。
|
||||
* * **只描边、不填充** —— 填充会在几十个嵌套矩形上层层叠加,把内容糊成一片。
|
||||
*
|
||||
* ⚠️ **描边宽度按设备像素固定**(不乘 `scale`):这类调试线一旦随 DPI 变粗就会盖住内容。
|
||||
* ⚠️ 描绘边时**临时切到 `PixelOffsetModeNone` + 无抗锯齿**并把矩形对齐到整像素,
|
||||
* 否则 1px 线会被抗锯齿摊成 2px 灰边(难以判断真实边界)。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] rect 目标矩形(客户区坐标)。
|
||||
* @param [IN] depth 层级(决定颜色与粗细,见上)。
|
||||
* @param [IN] scale DPI 缩放系数(只用于对齐,不用于线宽)。
|
||||
*/
|
||||
void UiDebugPaintBounds(gd::Graphics& g, const gd::RectF& rect, int depth, float scale);
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
#endif // __UI_DEBUG_BOUNDS_HPP__
|
||||
@@ -194,6 +194,31 @@ namespace jdi
|
||||
/** @brief 绘制自身与全部子节点(内部处理裁剪下探与脏区跳过)。 */
|
||||
void Paint(UiPaintContext& ctx);
|
||||
|
||||
/**
|
||||
* @brief 派发一个事件,并回答"是否需要重绘"。
|
||||
*
|
||||
* 等价于 `Dispatch(event); return UiRectHasArea(TakeDirty());` —— 这是
|
||||
* `Toolbar` / `TabBar` 等容器**唯一**需要的"事件后是否需要重绘"判断。
|
||||
* 收敛成一个函数是为了避免"忘记取脏区"(取脏区同时会清零,漏掉一次就会
|
||||
* 把下一次真实变化误判成"无变化")。
|
||||
*
|
||||
* @param [IN,OUT] event 事件(`QUERY_CURSOR` 的结果写回 `event.cursor`)。
|
||||
* @return 事件产生了需要重绘的脏区返回 true。
|
||||
*/
|
||||
bool DispatchRedraw(UiEvent& event);
|
||||
|
||||
/**
|
||||
* @brief 递归绘制本节点与全部子节点的布局边界(调试用)。
|
||||
*
|
||||
* 只读 `m_bounds`,不参与脏区、不进缓存 ⇒ 关闭开关时完全不执行。
|
||||
* 悬停/按下/选中态一律不改变边界(那是外观而非布局),因此不需要额外状态。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] scale DPI 缩放系数(只用于对齐,不用于线宽)。
|
||||
* @param [IN] depth 层级(0 = 调用者自身,子节点依次 +1,决定描边颜色)。
|
||||
*/
|
||||
void PaintDebugBounds(gd::Graphics& g, float scale, int depth = 0) const;
|
||||
|
||||
/** @brief 局部失效:与自身矩形求交后向根冒泡。 */
|
||||
virtual void Invalidate(const gd::RectF& rect);
|
||||
/** @brief 整棵子树失效。 */
|
||||
|
||||
@@ -12,12 +12,67 @@ namespace jdi
|
||||
namespace
|
||||
{
|
||||
const float kButtonRadius = 5.0f; /**< 按钮圆角半径(逻辑像素)。 */
|
||||
/** @brief 图标按钮的宽度(逻辑像素):与其他按钮同高、略窄。 */
|
||||
const float kIconButtonWidth = 40.0f;
|
||||
/** @brief 图标自身占用的边长(逻辑像素)。 */
|
||||
const float kIconSize = 18.0f;
|
||||
|
||||
/** @brief 按比例把一个颜色向目标色混合(与旧 Toolbar 的写法保持一致)。 */
|
||||
std::uint32_t BlendToward(std::uint32_t base, std::uint32_t target, double ratio)
|
||||
{
|
||||
return MixColor(base, target, ratio);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 画一个齿轮图标(矢量:8 个齿 + 外环 + 轴孔环)。
|
||||
*
|
||||
* 走 GDI+ 矢量而不是字体符号 `⚙`:该字符在不同回退字体里的大小与基线不一致,
|
||||
* 会出现"图标忽大忽小、垂直不居中"。抗锯齿保持开启(圆环/斜齿需要它才不难看)。
|
||||
*
|
||||
* 几何全部按「半径占 `half` 的比例」推导,因此任意 DPI 下形状一致:
|
||||
* 齿从环中心线(0.70)伸到最外圈(1.0),环描边宽度(0.30)刚好与齿根相接。
|
||||
*/
|
||||
void PaintGearIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb)
|
||||
{
|
||||
const float cx = rect.X + rect.Width * 0.5f;
|
||||
const float cy = rect.Y + rect.Height * 0.5f;
|
||||
const float half = std::min(rect.Width, rect.Height) * 0.5f;
|
||||
if (half < 4.0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const float ringRadius = half * 0.70f; // 外环的中心线半径
|
||||
const float ringWidth = half * 0.30f; // 外环描边宽度(与齿根相接)
|
||||
const float toothWidth = half * 0.32f; // 齿宽
|
||||
const float hubRadius = half * 0.28f; // 轴孔中心线半径
|
||||
const float hubWidth = std::max(1.0f, half * 0.22f);
|
||||
const gd::Color color(argb);
|
||||
|
||||
const gd::GraphicsState state = g.Save();
|
||||
g.SetSmoothingMode(gd::SmoothingModeAntiAlias);
|
||||
g.TranslateTransform(cx, cy);
|
||||
|
||||
// 齿:从环中心线向外伸到最外圈(`-half` 到 `-ringRadius`)。
|
||||
gd::SolidBrush brush(color);
|
||||
const gd::RectF tooth(-toothWidth * 0.5f, -half, toothWidth, half - ringRadius);
|
||||
for (int index = 0; index < 8; ++index)
|
||||
{
|
||||
g.FillRectangle(&brush, tooth);
|
||||
g.RotateTransform(45.0f);
|
||||
}
|
||||
g.ResetTransform();
|
||||
|
||||
gd::Pen pen(color, ringWidth);
|
||||
g.DrawEllipse(&pen, cx - ringRadius, cy - ringRadius, ringRadius * 2.0f,
|
||||
ringRadius * 2.0f);
|
||||
|
||||
gd::Pen hubPen(color, hubWidth);
|
||||
g.DrawEllipse(&hubPen, cx - hubRadius, cy - hubRadius, hubRadius * 2.0f,
|
||||
hubRadius * 2.0f);
|
||||
|
||||
g.Restore(state);
|
||||
}
|
||||
}
|
||||
|
||||
void UiButtonWidget::Configure(const UiFonts* fonts, const Theme* theme, float scale,
|
||||
@@ -47,6 +102,18 @@ namespace jdi
|
||||
Invalidate(Bounds());
|
||||
}
|
||||
|
||||
void UiButtonWidget::SetIcon(UiButtonIcon icon)
|
||||
{
|
||||
if (m_icon == icon)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_icon = icon;
|
||||
// 图标按钮与文本按钮的期望宽度不同 ⇒ 必须让测量失效(宿主随后会重新 `Layout`)。
|
||||
InvalidateMeasure();
|
||||
Invalidate(Bounds());
|
||||
}
|
||||
|
||||
void UiButtonWidget::SetChecked(bool checked)
|
||||
{
|
||||
if (m_checked == checked)
|
||||
@@ -81,6 +148,11 @@ namespace jdi
|
||||
gd::SizeF UiButtonWidget::OnMeasure(const UiMeasureContext& ctx)
|
||||
{
|
||||
const float scale = (ctx.scale > 0.1f) ? ctx.scale : m_scale;
|
||||
if (m_icon != UiButtonIcon::NONE)
|
||||
{
|
||||
// 图标按钮宽度固定:不依赖字体测量,避免图标被文本内边距拉宽。
|
||||
return gd::SizeF(kIconButtonWidth * scale, m_height * scale);
|
||||
}
|
||||
const float width = (m_fonts != nullptr && m_fonts->ui != nullptr)
|
||||
? (m_textPadding * 2.0f * scale + UiMeasureTextWidth(m_fonts->ui, m_label))
|
||||
: 0.0f;
|
||||
@@ -156,6 +228,14 @@ namespace jdi
|
||||
|
||||
// ⚠️ 与旧实现一致:文字走 `UiText`(GDI,不受 GDI+ 裁剪约束)。
|
||||
// 按钮矩形本身就在裁剪区内(外扩 1px 只影响描边),因此不需要 `UiTextClipped`。
|
||||
if (m_icon != UiButtonIcon::NONE)
|
||||
{
|
||||
const float side = kIconSize * m_scale;
|
||||
const gd::RectF iconRect(rect.X + (rect.Width - side) * 0.5f,
|
||||
rect.Y + (rect.Height - side) * 0.5f, side, side);
|
||||
PaintGearIcon(*ctx.g, iconRect, textColor);
|
||||
return;
|
||||
}
|
||||
UiText(*ctx.g, m_label, m_fonts->ui, rect, textColor, TextAlign::CENTER);
|
||||
}
|
||||
|
||||
@@ -219,6 +299,7 @@ namespace jdi
|
||||
{
|
||||
std::uint64_t key = BaseContentKey();
|
||||
key = UiMixKey(key, static_cast<std::uint64_t>(m_label.size()));
|
||||
key = UiMixKey(key, static_cast<std::uint64_t>(static_cast<int>(m_icon)));
|
||||
key = UiMixKey(key, m_primary ? 1u : 0u);
|
||||
key = UiMixKey(key, m_toggle ? 1u : 0u);
|
||||
key = UiMixKey(key, m_checked ? 1u : 0u);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "UiComponent.hpp"
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
// 默认实现放在 .cpp 而不是头文件内联:`UiComponent.hpp` 是四个大组件都要包含的
|
||||
// 公共头,不应把 `UiDebugBounds.hpp`(GDI+ 绘制辅助)也拉进它们的编译单元。
|
||||
void UiComponent::PaintDebugBounds(gd::Graphics& g, float scale)
|
||||
{
|
||||
UiDebugPaintBounds(g, Area(), 0, scale);
|
||||
}
|
||||
|
||||
} // namespace jdi
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
/** @brief 顶层组件区域的描边宽度(设备像素)。 */
|
||||
const int kAreaStroke = 2;
|
||||
/** @brief 内部控件 / 子块的描边宽度(设备像素)。 */
|
||||
const int kNodeStroke = 1;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
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, float scale)
|
||||
{
|
||||
(void)scale; // 线宽刻意不随 DPI 变化(见头文件说明),此参数保留以便将来做对齐细分。
|
||||
|
||||
if (!(rect.Width > 0.0f) || !(rect.Height > 0.0f))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 对齐到整像素:把矩形四边四舍五入,保证 1px 描边正好压住一格像素。
|
||||
const int left = static_cast<int>(std::lround(rect.X));
|
||||
const int top = static_cast<int>(std::lround(rect.Y));
|
||||
int right = static_cast<int>(std::lround(rect.X + rect.Width)) - 1;
|
||||
int bottom = static_cast<int>(std::lround(rect.Y + rect.Height)) - 1;
|
||||
if (right < left)
|
||||
{
|
||||
right = left;
|
||||
}
|
||||
if (bottom < top)
|
||||
{
|
||||
bottom = top;
|
||||
}
|
||||
|
||||
const int width = right - left + 1;
|
||||
const int height = bottom - top + 1;
|
||||
const int level = (depth < 0) ? 0 : depth;
|
||||
const std::uint32_t color = UiDebugBoundsColor(level);
|
||||
const int stroke = (level == 0) ? kAreaStroke : kNodeStroke;
|
||||
|
||||
const gd::GraphicsState state = g.Save();
|
||||
// 描边必须锐利:关抗锯齿 + 关半像素偏移,矩形已对齐整像素 ⇒ 1px 就是 1px。
|
||||
g.SetSmoothingMode(gd::SmoothingModeNone);
|
||||
g.SetPixelOffsetMode(gd::PixelOffsetModeNone);
|
||||
|
||||
// ⚠️ **只描边、不填充**(与 Android「Show layout bounds」一致):
|
||||
// 填充会在几十个嵌套矩形上层层叠加,把内容糊成一片,反而看不出真实边界。
|
||||
|
||||
// 四条边(用实心矩形而不是 `DrawRectangle`:后者会因笔宽奇偶而偏移半像素)。
|
||||
const int thick = (stroke * 2 <= width) ? stroke : 1;
|
||||
const int thickY = (stroke * 2 <= height) ? stroke : 1;
|
||||
FillPixels(g, left, top, width, thickY, color); // 上
|
||||
FillPixels(g, left, bottom - thickY + 1, width, thickY, color); // 下
|
||||
FillPixels(g, left, top, thick, height, color); // 左
|
||||
FillPixels(g, right - thick + 1, top, thick, height, color); // 右
|
||||
|
||||
g.Restore(state);
|
||||
}
|
||||
|
||||
} // namespace jdi
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
@@ -247,6 +249,32 @@ namespace jdi
|
||||
ctx.g->Restore(saveState);
|
||||
}
|
||||
|
||||
bool UiWidget::DispatchRedraw(UiEvent& event)
|
||||
{
|
||||
Dispatch(event);
|
||||
return UiRectHasArea(TakeDirty());
|
||||
}
|
||||
|
||||
void UiWidget::PaintDebugBounds(gd::Graphics& g, float scale, int depth) const
|
||||
{
|
||||
if (!m_visible || !UiRectHasArea(m_bounds))
|
||||
{
|
||||
// 不可见 / 未摆放的节点没有边界可画,其子树也不会被 `Paint` 画出来 ⇒ 一并跳过。
|
||||
return;
|
||||
}
|
||||
|
||||
UiDebugPaintBounds(g, m_bounds, depth, scale);
|
||||
|
||||
// 子节点按 z 序递归(末尾最上层)—— 与 `Paint` 的顺序一致,重叠时上层压住下层。
|
||||
for (const std::unique_ptr<UiWidget>& child : m_children)
|
||||
{
|
||||
if (child != nullptr)
|
||||
{
|
||||
child->PaintDebugBounds(g, scale, depth + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UiWidget::Invalidate(const gd::RectF& rect)
|
||||
{
|
||||
if (!UiRectHasArea(rect))
|
||||
|
||||
Reference in new issue
Block a user