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
@@ -387,6 +387,26 @@ namespace jdi
|
||||
*/
|
||||
void PaintLoadingOverlay(gd::Graphics& g, int width, int height);
|
||||
|
||||
/**
|
||||
* @brief 展开「应用设置」菜单(工具栏齿轮按钮)。
|
||||
*
|
||||
* 锚点取齿轮按钮的**实际矩形**(由控件实测),因此菜单总能贴在按钮下方,
|
||||
* 不会因按钮文本宽度变化而错位。
|
||||
*/
|
||||
void OpenSettingsMenu();
|
||||
|
||||
/**
|
||||
* @brief 绘制全部 UI 控件的布局边界(调试开关开启时在 Render 的最后一步调用)。
|
||||
*
|
||||
* 遍历组件表(底 → 顶)并让每个组件上报自己的内部结构;随后补上不属于
|
||||
* 任何组件的两块区域(面板分隔条、客户区外框)。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] width 客户区宽度。
|
||||
* @param [IN] height 客户区高度。
|
||||
*/
|
||||
void PaintLayoutBounds(gd::Graphics& g, int width, int height);
|
||||
|
||||
/**
|
||||
* @brief 处理客户区鼠标移动。
|
||||
* @param [IN] x 客户区横坐标。
|
||||
@@ -449,6 +469,15 @@ namespace jdi
|
||||
*/
|
||||
std::vector<ComponentEntry> m_components;
|
||||
|
||||
/**
|
||||
* @brief 是否显示**布局边界**(设置菜单里的调试开关)。
|
||||
*
|
||||
* 开启时 `Render` 在最后一步遍历组件表让每个组件上报边界(见 `PaintLayoutBounds`),
|
||||
* 并强制整帧回送(局部回送只会更新绘图区,装饰区的边界会留在旧帧上)。
|
||||
* 关闭时**完全不执行**,对正常绘制零影响。
|
||||
*/
|
||||
bool m_showLayoutBounds = false;
|
||||
|
||||
/** @brief 填充组件表(`Initialize` 里 `Setup` 之后调用一次)。 */
|
||||
void BuildComponentTable();
|
||||
|
||||
|
||||
@@ -176,6 +176,9 @@ namespace jdi
|
||||
inline const int kMenuTabClose = 2;
|
||||
inline const int kMenuTabCloseOthers = 3;
|
||||
|
||||
/** @brief 应用设置菜单(工具栏齿轮)命令 ID。 */
|
||||
inline const int kAppCmdShowLayoutBounds = 1;
|
||||
|
||||
/**
|
||||
* @brief 从应用资源中按指定尺寸加载图标。
|
||||
* @param [IN] cx 期望宽度(像素)。
|
||||
|
||||
@@ -506,14 +506,15 @@ namespace jdi
|
||||
// 仍需要“重绘装饰区域”这个判定(决定回送整窗还是只回送绘图区)。
|
||||
const bool chromeDirty = fullChrome || panelOnly;
|
||||
// 提示浮层淡出由定时器驱动,其绘制范围局限在绘图区,但为稳妥起见整帧回送。
|
||||
const bool fullFrame = chromeDirty || m_plot.HintNeedsAnimation();
|
||||
// ⚠️ 开启「显示布局边界」后同样必须**整帧回送**:边界跨所有装饰区且比组件矩形外扩
|
||||
// 1px,走"只回送绘图区"的快路径会让装饰区的边界停留在旧帧上(看起来像没开)。
|
||||
const bool fullFrame = chromeDirty || m_plot.HintNeedsAnimation() || m_showLayoutBounds;
|
||||
|
||||
{
|
||||
gd::Graphics g(m_backDc);
|
||||
g.SetSmoothingMode(gd::SmoothingModeAntiAlias);
|
||||
g.SetPixelOffsetMode(gd::PixelOffsetModeHalf);
|
||||
g.SetTextRenderingHint(gd::TextRenderingHintClearTypeGridFit);
|
||||
|
||||
// 信号列表 / 样式区「目标」行的悬停 → 绘图区联动高亮(网格变色 + 该信号曲线高亮)。
|
||||
// 用户 2026-10-15 要求;鼠标在网格里移动**不再**触发网格变色高亮。
|
||||
// ⚠️ 每帧同步(而不是只在 WM_MOUSEMOVE 里同步)才能覆盖全部改变悬停的路径:
|
||||
@@ -550,6 +551,13 @@ namespace jdi
|
||||
|
||||
// 加载遮罩层在一切内容之上(包括菜单与拾色器),形成模态观感。
|
||||
PaintLoadingOverlay(g, width, height);
|
||||
|
||||
// 布局边界是**纯调试叠加层**:必须在所有内容(含遮罩)之上、且绝不进入任何
|
||||
// 位图缓存 —— 否则关掉开关后线条会残留在缓存里。
|
||||
if (m_showLayoutBounds)
|
||||
{
|
||||
PaintLayoutBounds(g, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
HDC windowDc = GetDC(m_hwnd);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// Application 实现分片 —— 本文件只包含 Application 的**成员函数定义**(纯代码搬移,行为与拆分前完全一致)。
|
||||
// Application 实现分片 —— 本文件包含:标签页管理、工具栏动作、设置菜单、键盘、菜单结果派发。
|
||||
//
|
||||
// 共用的常量与自由函数在 ApplicationInternal.hpp(命名空间 `appdetail`,全部 inline)。
|
||||
// 分片按“职责”划分:核心(状态/布局/几何)、渲染、交互与编辑,见各文件名。
|
||||
// 共用的常量与工具函数在 ApplicationInternal.hpp(命名空间 `appdetail`,全部 inline)。
|
||||
// 分片按"职责"划分:核心(状态/布局)、渲染、命令与菜单、加载。
|
||||
#include "ApplicationInternal.hpp"
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
@@ -305,6 +307,15 @@ namespace jdi
|
||||
{
|
||||
switch (action)
|
||||
{
|
||||
case ToolbarAction::SETTINGS:
|
||||
// 齿轮按钮只负责**展开菜单**:不改变任何业务状态,因此不必走后面的"整帧失效",
|
||||
// 只需要把浮层画出来(菜单打开后 `Render` 会因 `overlaysVisible` 自动整帧重绘)。
|
||||
OpenSettingsMenu();
|
||||
if (m_hwnd != nullptr)
|
||||
{
|
||||
InvalidateRect(m_hwnd, nullptr, TRUE);
|
||||
}
|
||||
return;
|
||||
case ToolbarAction::OPEN_FILE:
|
||||
OpenFileDialog();
|
||||
break;
|
||||
@@ -1106,6 +1117,70 @@ namespace jdi
|
||||
m_plot.AllLanesAutoFitToSelection(), true);
|
||||
}
|
||||
|
||||
void Application::OpenSettingsMenu()
|
||||
{
|
||||
// 锚点取齿轮按钮的**实际矩形**:按钮宽度由控件实测,宿主不应自己推算几何。
|
||||
gd::RectF anchor;
|
||||
if (!m_toolbar.ButtonRect(ToolbarAction::SETTINGS, anchor))
|
||||
{
|
||||
// 尚未布局(极端情形):退化为从菜单快捷键位置展开,而不是什么都不发生。
|
||||
anchor = m_toolbarRect;
|
||||
}
|
||||
|
||||
std::vector<MenuItem> items;
|
||||
|
||||
// ── 调试 ──────────────────────────────────────────────
|
||||
MenuItem debugHeader;
|
||||
debugHeader.kind = MenuItemKind::HEADER;
|
||||
debugHeader.label = L"调试";
|
||||
items.push_back(debugHeader);
|
||||
|
||||
MenuItem boundsItem;
|
||||
boundsItem.label = L"显示布局边界";
|
||||
boundsItem.id = kAppCmdShowLayoutBounds;
|
||||
boundsItem.checked = m_showLayoutBounds;
|
||||
// 切换项:点击后菜单保持打开,勾选态由宿主回写(与既有「镜像」等切换项一致)。
|
||||
boundsItem.keepOpen = true;
|
||||
items.push_back(boundsItem);
|
||||
|
||||
RECT client = {};
|
||||
GetClientRect(m_hwnd, &client);
|
||||
const gd::RectF bounds(0.0f, 0.0f, static_cast<float>(client.right - client.left),
|
||||
static_cast<float>(client.bottom - client.top));
|
||||
|
||||
// 右下展开,锚在按钮**下沿**(留 4 逻辑像素,视觉上不贴着按钮边框)。
|
||||
const int x = static_cast<int>(anchor.X);
|
||||
const int y = static_cast<int>(anchor.Y + anchor.Height + 4.0f * m_scale);
|
||||
m_menu.Open(MenuScope::APP_SETTINGS, bounds, x, y, items);
|
||||
}
|
||||
|
||||
void Application::PaintLayoutBounds(gd::Graphics& g, int width, int height)
|
||||
{
|
||||
// 客户区外框:便于确认"窗口边缘 == 最外层边界"。
|
||||
UiDebugPaintBounds(g, gd::RectF(0.0f, 0.0f, static_cast<float>(width),
|
||||
static_cast<float>(height)), 0, m_scale);
|
||||
|
||||
// 组件区域(底 → 顶;组件自己负责再上报内部结构)。
|
||||
for (const ComponentEntry& entry : m_components)
|
||||
{
|
||||
if (entry.component != nullptr)
|
||||
{
|
||||
entry.component->PaintDebugBounds(g, m_scale);
|
||||
}
|
||||
}
|
||||
|
||||
// 不属于任何组件的两块:面板分隔条(由宿主直接填充色块)、菜单/拾色器(浮层)。
|
||||
UiDebugPaintBounds(g, m_splitterRect, 0, m_scale);
|
||||
if (m_menu.IsOpen())
|
||||
{
|
||||
UiDebugPaintBounds(g, m_menu.Rect(), 0, m_scale);
|
||||
}
|
||||
if (m_colorPicker.IsOpen())
|
||||
{
|
||||
UiDebugPaintBounds(g, m_colorPicker.Rect(), 0, m_scale);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::DispatchMenuResult(const MenuResult& result)
|
||||
{
|
||||
if (!result.valid)
|
||||
@@ -1115,6 +1190,20 @@ namespace jdi
|
||||
|
||||
switch (result.scope)
|
||||
{
|
||||
case MenuScope::APP_SETTINGS:
|
||||
{
|
||||
// 应用设置菜单:全部条目都是**带勾选态的切换项**,菜单保持打开,
|
||||
// 勾选变化通过 `UpdateItemLabel` 立即回写(连续切换多项时不用反复打开菜单)。
|
||||
if (result.id == kAppCmdShowLayoutBounds)
|
||||
{
|
||||
m_showLayoutBounds = !m_showLayoutBounds;
|
||||
m_menu.UpdateItemLabel(kAppCmdShowLayoutBounds, L"显示布局边界", m_showLayoutBounds);
|
||||
// 边界比组件矩形外扩 1px 且跨越所有装饰区 ⇒ 必须整帧重绘。
|
||||
m_chromeDirty = true;
|
||||
}
|
||||
InvalidateRect(m_hwnd, nullptr, TRUE);
|
||||
break;
|
||||
}
|
||||
case MenuScope::SIGNAL_PANEL:
|
||||
{
|
||||
const bool needRelayout = m_panel.OnMenuCommand(result);
|
||||
|
||||
@@ -204,6 +204,17 @@ namespace jdi
|
||||
*/
|
||||
void Paint(gd::Graphics& g) override;
|
||||
|
||||
/**
|
||||
* @brief 绘制绘图区区域与**每个网格波形区**的布局边界(调试用)。
|
||||
*
|
||||
* 只上报“布局结构”(区域 + 网格),不上报曲线/胶囊/提示这类随数据变化的瞬态几何:
|
||||
* 后者每帧不同,画出来只是噪声。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] scale DPI 缩放系数。
|
||||
*/
|
||||
void PaintDebugBounds(gd::Graphics& g, float scale) override;
|
||||
|
||||
/**
|
||||
* @brief 处理鼠标移动(含拖拽)。
|
||||
* @param [IN] x 客户区横坐标。
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
// ⚠️ 新增函数按**职责**放入对应分片;`PlotView::Paint/PaintLane` 仍只允许定义在本文件。
|
||||
#include "PlotViewInternal.hpp"
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
@@ -560,6 +562,17 @@ namespace jdi
|
||||
UiText(g, message, m_fonts->ui, m_area, m_theme->textMuted, TextAlign::CENTER);
|
||||
}
|
||||
|
||||
void PlotView::PaintDebugBounds(gd::Graphics& g, float scale)
|
||||
{
|
||||
UiComponent::PaintDebugBounds(g, scale);
|
||||
for (const LaneVisual& lane : m_lanes)
|
||||
{
|
||||
// 层级 1:网格波形区。不画曲线/取值胶囊/提示这类随数据变化的瞬态几何
|
||||
//(它们每帧都不同,叠上去只是噪声)。
|
||||
UiDebugPaintBounds(g, lane.rect, 1, scale);
|
||||
}
|
||||
}
|
||||
|
||||
void PlotView::Paint(gd::Graphics& g)
|
||||
{
|
||||
if (m_theme == nullptr || m_fonts == nullptr)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -363,6 +363,17 @@ namespace jdi
|
||||
*/
|
||||
HCURSOR QueryCursor(int x, int y) override;
|
||||
|
||||
/**
|
||||
* @brief 绘制面板区域与其内部结构(表头/筛选框/列表/样式区/可见行)的布局边界。
|
||||
*
|
||||
* 面板的行位图缓存与本函数**无关**:调试边界只画进宿主后备缓冲的最后一步,
|
||||
* 不进任何缓存 ⇒ 关掉开关后不会留下残影。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] scale DPI 缩放系数。
|
||||
*/
|
||||
void PaintDebugBounds(gd::Graphics& g, float scale) override;
|
||||
|
||||
/**
|
||||
* @brief 将列表滚动到指定信号所在位置(无动画,立即到位)。
|
||||
* @param [IN] ref 目标信号引用。
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
// SignalPanel 实现分片 —— 本文件只包含 SignalPanel 的**成员函数定义**(纯代码搬移,行为与拆分前完全一致)。
|
||||
// SignalPanel 实现分片 —— 核心:状态、行集合构建、布局与几何、分组/信号可见性。
|
||||
//
|
||||
// 共用的常量与自由函数在 SignalPanelInternal.hpp(命名空间 `spdetail`,全部 inline)。
|
||||
// 分片按“职责”划分:核心(状态/布局/几何)、渲染、交互与编辑,见各文件名。
|
||||
// 共用的常量与工具函数在 SignalPanelInternal.hpp(命名空间 `spdetail`,全部 inline)。
|
||||
// 六个分片按**职责**划分:
|
||||
// SignalPanel.cpp 核心:状态、行集合、布局与几何、可见性(**本文件**)
|
||||
// SignalPanelPaint.cpp 绘制:列表行(含行位图缓存)、样式区、筛选框、滚动条
|
||||
// SignalPanelInteraction.cpp 交互:命中测试、鼠标/键盘派发、光标形状
|
||||
// SignalPanelEdit.cpp 内联编辑:筛选框、组名/信号名、数值输入、颜色与透明度
|
||||
// SignalPanelScroll.cpp 动画与滚动:分组折起、联动滚动、"滚动到目标信号"
|
||||
// SignalPanelMenu.cpp 弹出菜单:系数/偏移、镜像、右键菜单与命令回执
|
||||
#include "SignalPanelInternal.hpp"
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
#include "UiLayout.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
@@ -63,6 +72,47 @@ namespace jdi
|
||||
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
||||
}
|
||||
|
||||
void SignalPanel::PaintDebugBounds(gd::Graphics& g, float scale)
|
||||
{
|
||||
UiComponent::PaintDebugBounds(g, scale);
|
||||
|
||||
if (m_fonts == nullptr || !UiRectHasArea(m_area))
|
||||
{
|
||||
return; // 尚未完成布局:内部矩形全是空的,画出来只会误导。
|
||||
}
|
||||
|
||||
// 层级 1:面板内部的功能分区。
|
||||
UiDebugPaintBounds(g, m_headerRect, 1, scale);
|
||||
UiDebugPaintBounds(g, m_filterRect, 1, scale);
|
||||
UiDebugPaintBounds(g, m_listRect, 1, scale);
|
||||
UiDebugPaintBounds(g, m_styleRect, 1, scale);
|
||||
|
||||
// 层级 2:滚动条与样式区的每一行。
|
||||
UiDebugPaintBounds(g, m_scrollBarRect, 2, scale);
|
||||
for (const gd::RectF& row : m_styleRowRects)
|
||||
{
|
||||
UiDebugPaintBounds(g, row, 2, scale);
|
||||
}
|
||||
|
||||
// 层级 2:列表里**当前可见**的行(被滚出列表的行不画,否则边界会糊出列表区)。
|
||||
const float listTop = m_listRect.Y;
|
||||
const float listBottom = m_listRect.Y + m_listRect.Height;
|
||||
const int rowCount = static_cast<int>(m_rows.size());
|
||||
for (int row = 0; row < rowCount; ++row)
|
||||
{
|
||||
const gd::RectF rect = RowRect(row);
|
||||
if (!UiRectHasArea(rect))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (rect.Y + rect.Height <= listTop || rect.Y >= listBottom)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
UiDebugPaintBounds(g, rect, 2, scale);
|
||||
}
|
||||
}
|
||||
|
||||
int SignalPanel::TotalSignalCount() const
|
||||
{
|
||||
if (m_dataSet == nullptr)
|
||||
|
||||
@@ -224,6 +224,13 @@ namespace jdi
|
||||
*/
|
||||
bool HitTest(int x, int y) const override;
|
||||
|
||||
/**
|
||||
* @brief 绘制标签栏区域与**每个标签条/新建按钮**的布局边界(调试用)。
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] scale DPI 缩放系数。
|
||||
*/
|
||||
void PaintDebugBounds(gd::Graphics& g, float scale) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 单个标签页的布局信息。
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
#include "Utf8Util.hpp"
|
||||
|
||||
namespace jdi
|
||||
@@ -483,6 +484,28 @@ namespace jdi
|
||||
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
||||
}
|
||||
|
||||
void TabBar::PaintDebugBounds(gd::Graphics& g, float scale)
|
||||
{
|
||||
UiComponent::PaintDebugBounds(g, scale);
|
||||
if (m_root != nullptr)
|
||||
{
|
||||
// ⚠️ 跳过根控件:它的矩形与标签栏区域完全重合(已被上一层画过)。
|
||||
// 从控件树的**子节点**(各标签条 / 新建按钮,层级 1)开始上报。
|
||||
for (const std::unique_ptr<UiWidget>& child : m_root->Children())
|
||||
{
|
||||
if (child != nullptr)
|
||||
{
|
||||
child->PaintDebugBounds(g, scale, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// 关闭按钮是「标签条内部的子块」,控件树里没有对应节点(由标签条控件自绘)⇒ 单独上报。
|
||||
for (const TabItem& tab : m_tabs)
|
||||
{
|
||||
UiDebugPaintBounds(g, tab.closeRect, 2, scale);
|
||||
}
|
||||
}
|
||||
|
||||
int TabBar::TabAtPoint(int x, int y) const
|
||||
{
|
||||
const float fx = static_cast<float>(x);
|
||||
@@ -519,10 +542,9 @@ namespace jdi
|
||||
event.kind = UiEventKind::MOUSE_MOVE;
|
||||
event.x = x;
|
||||
event.y = y;
|
||||
m_root->Dispatch(event);
|
||||
// 悬停链切换会通过 `Invalidate` 冒泡到根 ⇒ 用它判断是否需要重绘
|
||||
// 悬停链切换会通过 `Invalidate` 冒泡到根 ⇒ 用脏区判断是否需要重绘
|
||||
//(与旧实现"悬停下标/关闭/新建变化才重绘"等价,但不再手工维护三个下标)。
|
||||
return UiRectHasArea(m_root->TakeDirty());
|
||||
return m_root->DispatchRedraw(event);
|
||||
}
|
||||
|
||||
bool TabBar::OnLButtonDown(int x, int y)
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace jdi
|
||||
enum class ToolbarAction : int
|
||||
{
|
||||
NONE = 0, /**< 未触发任何动作。 */
|
||||
SETTINGS, /**< **展开应用设置菜单**(齿轮图标,位于「导入文件」左侧)。 */
|
||||
OPEN_FILE, /**< 打开数据文件。 */
|
||||
FIT_ALL, /**< 视图自适应到可见信号范围(**不**退出选区放大模式)。 */
|
||||
AUTO_FIT_Y, /**< 切换「选区 Y 轴自适应」全局开关:时间轴选区放大后自动匹配各网格 Y 轴。 */
|
||||
@@ -86,6 +87,17 @@ namespace jdi
|
||||
*/
|
||||
float DesiredHeight() const;
|
||||
|
||||
/**
|
||||
* @brief 查询某个按钮当前的屏幕矩形(用于把弹出菜单锚定到按钮下方)。
|
||||
*
|
||||
* 几何的唯一权威来源是控件本身(按钮宽度由文本实测得出),因此这里读控件而不是重算。
|
||||
*
|
||||
* @param [IN] action 目标按钮动作。
|
||||
* @param [OUT] rect 按钮矩形(客户区坐标)。
|
||||
* @return 找到且已摆放返回 true。
|
||||
*/
|
||||
bool ButtonRect(ToolbarAction action, gd::RectF& rect) const;
|
||||
|
||||
/**
|
||||
* @brief 按给定区域重新排布按钮。
|
||||
* @param [IN] area 工具栏可用区域。
|
||||
@@ -141,6 +153,13 @@ namespace jdi
|
||||
*/
|
||||
bool HitTest(int x, int y) const override;
|
||||
|
||||
/**
|
||||
* @brief 绘制工具栏区域与**每个按钮**的布局边界(调试用)。
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] scale DPI 缩放系数。
|
||||
*/
|
||||
void PaintDebugBounds(gd::Graphics& g, float scale) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 单个工具栏按钮的**静态配置**(矩形与外观状态由控件自己维护)。
|
||||
@@ -149,6 +168,7 @@ namespace jdi
|
||||
{
|
||||
ToolbarAction action = ToolbarAction::NONE; /**< 按钮动作。 */
|
||||
UiButtonWidget* widget = nullptr; /**< 对应控件(由 `m_root` 拥有)。 */
|
||||
UiButtonIcon icon = UiButtonIcon::NONE; /**< 图标(非 `NONE` 时按钮只画图标)。 */
|
||||
bool primary = false; /**< 是否为主按钮(强调色填充)。 */
|
||||
bool needsDocument = true; /**< 是否依赖已加载数据。 */
|
||||
bool toggle = false; /**< 是否为可切换按钮。 */
|
||||
@@ -163,7 +183,14 @@ namespace jdi
|
||||
*/
|
||||
bool IsEnabled(const Button& button) const;
|
||||
|
||||
/** @brief 按固定规格表重建控件树(文本变化/首次布局时调用)。 */
|
||||
/**
|
||||
* @brief 按固定规格表重建控件树。
|
||||
*
|
||||
* ⚠️ 只在 `m_treeDirty` 为真时由 `Layout` 调用:规格表、字体、主题、缩放、动态文本
|
||||
* 任何一项变化都会置脏,而**纯粹的尺寸变化不会**(按钮几何由 `Arrange` 决定)。
|
||||
* 旧实现每次 `Layout` 都无条件重建(拖动面板分隔条时每帧一次)⇒ 每帧 9 次堆分配
|
||||
* 加 8 次测量缓存失效。
|
||||
*/
|
||||
void RebuildWidgets();
|
||||
/** @brief 把可用性同步到控件(未加载数据时禁用依赖数据的按钮)。 */
|
||||
void SyncEnabledState();
|
||||
@@ -177,6 +204,7 @@ namespace jdi
|
||||
gd::RectF m_area; /**< 工具栏整体区域。 */
|
||||
std::unique_ptr<UiWidget> m_root; /**< 控件树根(负责底色与底部分隔线)。 */
|
||||
ToolbarAction m_pendingAction = ToolbarAction::NONE; /**< 最近一次成功点击的动作。 */
|
||||
bool m_treeDirty = true; /**< 控件树是否需要重建(见 `RebuildWidgets`)。 */
|
||||
};
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <cmath>
|
||||
#include <utility>
|
||||
|
||||
#include "UiDebugBounds.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
@@ -61,22 +63,29 @@ namespace jdi
|
||||
struct ButtonSpec
|
||||
{
|
||||
ToolbarAction action; /**< 动作。 */
|
||||
const wchar_t* label; /**< 默认文本。 */
|
||||
const wchar_t* label; /**< 默认文本(图标按钮留空)。 */
|
||||
bool primary; /**< 是否主按钮。 */
|
||||
bool needsDocument; /**< 是否依赖已加载数据。 */
|
||||
bool toggle; /**< 是否可切换。 */
|
||||
UiButtonIcon icon; /**< 图标(`NONE` = 纯文本按钮)。 */
|
||||
};
|
||||
|
||||
// ⚠️ **前两项构成"左侧固定簇"**(设置齿轮 + 导入文件),其余按钮作为整体靠右。
|
||||
// `Layout` 里用 `kLeftClusterCount` 引用这个约定的长度,增删左侧按钮时必须同步。
|
||||
const ButtonSpec kButtonSpecs[] = {
|
||||
{ToolbarAction::OPEN_FILE, L"导入文件", true, false, false},
|
||||
{ToolbarAction::FIT_ALL, L"适应视图", false, true, false},
|
||||
{ToolbarAction::AUTO_FIT_Y, L"Y 轴自适应", false, true, true},
|
||||
{ToolbarAction::ZOOM_SELECT, L"选区放大", false, true, true},
|
||||
{ToolbarAction::CURSOR, L"光标", false, true, true},
|
||||
{ToolbarAction::MERGE_LANES, L"合并网格", false, true, false},
|
||||
{ToolbarAction::SPLIT_LANES, L"拆分网格", false, true, false},
|
||||
{ToolbarAction::CLEAR_ALL, L"清空网格", false, true, false},
|
||||
{ToolbarAction::SETTINGS, L"", false, false, false, UiButtonIcon::GEAR},
|
||||
{ToolbarAction::OPEN_FILE, L"导入文件", true, false, false, UiButtonIcon::NONE},
|
||||
{ToolbarAction::FIT_ALL, L"适应视图", false, true, false, UiButtonIcon::NONE},
|
||||
{ToolbarAction::AUTO_FIT_Y, L"Y 轴自适应", false, true, true, UiButtonIcon::NONE},
|
||||
{ToolbarAction::ZOOM_SELECT, L"选区放大", false, true, true, UiButtonIcon::NONE},
|
||||
{ToolbarAction::CURSOR, L"光标", false, true, true, UiButtonIcon::NONE},
|
||||
{ToolbarAction::MERGE_LANES, L"合并网格", false, true, false, UiButtonIcon::NONE},
|
||||
{ToolbarAction::SPLIT_LANES, L"拆分网格", false, true, false, UiButtonIcon::NONE},
|
||||
{ToolbarAction::CLEAR_ALL, L"清空网格", false, true, false, UiButtonIcon::NONE},
|
||||
};
|
||||
|
||||
/** @brief 左侧固定簇的按钮数量(从 `kButtonSpecs` 开头算起)。 */
|
||||
constexpr std::size_t kLeftClusterCount = 2;
|
||||
} // namespace
|
||||
|
||||
Toolbar::Toolbar() = default;
|
||||
@@ -89,11 +98,19 @@ namespace jdi
|
||||
{
|
||||
m_fonts = fonts;
|
||||
m_theme = theme;
|
||||
m_treeDirty = true;
|
||||
}
|
||||
|
||||
void Toolbar::SetScale(float scale)
|
||||
{
|
||||
m_scale = (scale > 0.1f) ? scale : 1.0f;
|
||||
const float next = (scale > 0.1f) ? scale : 1.0f;
|
||||
if (next == m_scale)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_scale = next;
|
||||
// 控件里缓存了旧的缩放(字体实测宽度、图标尺寸)⇒ 必须重建。
|
||||
m_treeDirty = true;
|
||||
}
|
||||
|
||||
void Toolbar::SetDocumentLoaded(bool hasDocument)
|
||||
@@ -155,12 +172,14 @@ namespace jdi
|
||||
button.primary = spec.primary;
|
||||
button.needsDocument = spec.needsDocument;
|
||||
button.toggle = spec.toggle;
|
||||
button.icon = spec.icon;
|
||||
button.checked = false;
|
||||
|
||||
auto widget = std::make_unique<UiButtonWidget>();
|
||||
UiButtonWidget* raw = widget.get();
|
||||
raw->Configure(m_fonts, m_theme, m_scale, label, spec.primary, spec.toggle, kButtonPadding,
|
||||
kButtonHeight);
|
||||
raw->SetIcon(spec.icon);
|
||||
if (m_theme != nullptr)
|
||||
{
|
||||
raw->SetCanvasColor(m_theme->toolbarBg);
|
||||
@@ -184,16 +203,19 @@ namespace jdi
|
||||
return;
|
||||
}
|
||||
|
||||
// 重建控件树:先记下各 toggle 的勾选状态,否则每次因文本变化(如「光标x2」)
|
||||
// 触发的 Layout 会把按钮选中态全部清掉。
|
||||
// 控件树**只在必要时重建**(先记下各 toggle 的勾选状态,否则重建会把选中态清掉)。
|
||||
// 纯粹的尺寸变化不走重建:按钮几何由 `Arrange` 决定,与树结构无关。
|
||||
std::vector<std::pair<ToolbarAction, bool>> previousChecked;
|
||||
previousChecked.reserve(m_buttons.size());
|
||||
for (const Button& button : m_buttons)
|
||||
if (m_treeDirty || m_root == nullptr)
|
||||
{
|
||||
previousChecked.emplace_back(button.action, button.checked);
|
||||
previousChecked.reserve(m_buttons.size());
|
||||
for (const Button& button : m_buttons)
|
||||
{
|
||||
previousChecked.emplace_back(button.action, button.checked);
|
||||
}
|
||||
RebuildWidgets();
|
||||
m_treeDirty = false;
|
||||
}
|
||||
|
||||
RebuildWidgets();
|
||||
m_root->Arrange(area);
|
||||
|
||||
UiMeasureContext measure;
|
||||
@@ -216,13 +238,30 @@ namespace jdi
|
||||
}
|
||||
}
|
||||
|
||||
// 首按钮(导入文件)靠左;其余按钮作为整体靠右,并留出一组额外间距。
|
||||
// 左侧固定簇(设置齿轮 + 导入文件)靠左,其余按钮作为整体靠右。
|
||||
const float top = area.Y + kTopPadding * m_scale;
|
||||
const float height = kButtonHeight * m_scale;
|
||||
const float leftX = area.X + kSidePadding * m_scale;
|
||||
|
||||
float leftWidth = 0.0f;
|
||||
for (std::size_t index = 0; index < kLeftClusterCount && index < m_buttons.size(); ++index)
|
||||
{
|
||||
leftWidth += m_buttons[index].width;
|
||||
if (index > 0)
|
||||
{
|
||||
leftWidth += kButtonGap * m_scale;
|
||||
}
|
||||
}
|
||||
|
||||
float cursorX = leftX;
|
||||
for (std::size_t index = 0; index < kLeftClusterCount && index < m_buttons.size(); ++index)
|
||||
{
|
||||
m_buttons[index].widget->Arrange(gd::RectF(cursorX, top, m_buttons[index].width, height));
|
||||
cursorX += m_buttons[index].width + kButtonGap * m_scale;
|
||||
}
|
||||
|
||||
float groupWidth = 0.0f;
|
||||
for (std::size_t index = 1; index < m_buttons.size(); ++index)
|
||||
for (std::size_t index = kLeftClusterCount; index < m_buttons.size(); ++index)
|
||||
{
|
||||
groupWidth += m_buttons[index].width;
|
||||
if (index + 1 < m_buttons.size())
|
||||
@@ -231,18 +270,27 @@ namespace jdi
|
||||
}
|
||||
}
|
||||
|
||||
float cursorX = leftX;
|
||||
m_buttons[0].widget->Arrange(gd::RectF(cursorX, top, m_buttons[0].width, height));
|
||||
|
||||
const float rightEdge = area.X + area.Width - kSidePadding * m_scale;
|
||||
cursorX = std::max(leftX + m_buttons[0].width + kGroupGap * m_scale, rightEdge - groupWidth);
|
||||
for (std::size_t index = 1; index < m_buttons.size(); ++index)
|
||||
cursorX = std::max(leftX + leftWidth + kGroupGap * m_scale, rightEdge - groupWidth);
|
||||
for (std::size_t index = kLeftClusterCount; index < m_buttons.size(); ++index)
|
||||
{
|
||||
m_buttons[index].widget->Arrange(gd::RectF(cursorX, top, m_buttons[index].width, height));
|
||||
cursorX += m_buttons[index].width + kButtonGap * m_scale;
|
||||
}
|
||||
}
|
||||
|
||||
bool Toolbar::ButtonRect(ToolbarAction action, gd::RectF& rect) const
|
||||
{
|
||||
for (const Button& button : m_buttons)
|
||||
{
|
||||
if (button.action == action && button.widget != nullptr)
|
||||
{
|
||||
rect = button.widget->Bounds();
|
||||
return UiRectHasArea(rect);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void Toolbar::SetToggleState(ToolbarAction action, bool checked)
|
||||
{
|
||||
for (Button& button : m_buttons)
|
||||
@@ -261,6 +309,9 @@ namespace jdi
|
||||
|
||||
bool Toolbar::SetActionLabel(ToolbarAction action, const std::wstring& label)
|
||||
{
|
||||
// ⚠️ 只更新**唯一权威来源**(`m_labelOverrides`)并置树脏,不再逐个回写控件:
|
||||
// 旧实现既要写覆盖表又要写控件,两处一旦不同步,下一次重建就会把文本退回去。
|
||||
// 调用方拿到 true 后会重排(`Relayout`),届时由 `RebuildWidgets` 统一生效。
|
||||
for (std::pair<ToolbarAction, std::wstring>& entry : m_labelOverrides)
|
||||
{
|
||||
if (entry.first == action)
|
||||
@@ -270,24 +321,12 @@ namespace jdi
|
||||
return false;
|
||||
}
|
||||
entry.second = label;
|
||||
for (Button& button : m_buttons)
|
||||
{
|
||||
if (button.action == action && button.widget != nullptr)
|
||||
{
|
||||
button.widget->SetLabel(label);
|
||||
}
|
||||
}
|
||||
m_treeDirty = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
m_labelOverrides.emplace_back(action, label);
|
||||
for (Button& button : m_buttons)
|
||||
{
|
||||
if (button.action == action && button.widget != nullptr)
|
||||
{
|
||||
button.widget->SetLabel(label);
|
||||
}
|
||||
}
|
||||
m_treeDirty = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -310,6 +349,24 @@ namespace jdi
|
||||
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
||||
}
|
||||
|
||||
void Toolbar::PaintDebugBounds(gd::Graphics& g, float scale)
|
||||
{
|
||||
UiComponent::PaintDebugBounds(g, scale);
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// ⚠️ 跳过根控件:它的矩形与工具栏区域完全重合(已经被上一层画过),
|
||||
// 再画一次只会多出一条压住自己的粗框。直接从**按钮**(层级 1)开始上报。
|
||||
for (const std::unique_ptr<UiWidget>& button : m_root->Children())
|
||||
{
|
||||
if (button != nullptr)
|
||||
{
|
||||
button->PaintDebugBounds(g, scale, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Toolbar::OnMouseMove(int x, int y)
|
||||
{
|
||||
if (m_root == nullptr)
|
||||
@@ -320,10 +377,9 @@ namespace jdi
|
||||
event.kind = UiEventKind::MOUSE_MOVE;
|
||||
event.x = x;
|
||||
event.y = y;
|
||||
m_root->Dispatch(event);
|
||||
// 悬停链切换会通过 `Invalidate` 把矩形冒泡到根 ⇒ 用它判断"是否需要重绘"
|
||||
// 悬停链切换会通过 `Invalidate` 把矩形冒泡到根 ⇒ 用脏区判断"是否需要重绘"
|
||||
//(与旧实现"悬停下标变化才重绘"等价,但不再手工维护下标)。
|
||||
return UiRectHasArea(m_root->TakeDirty());
|
||||
return m_root->DispatchRedraw(event);
|
||||
}
|
||||
|
||||
bool Toolbar::OnLButtonDown(int x, int y, ToolbarAction& action)
|
||||
|
||||
Reference in new issue
Block a user