重构(UI): 统一组件协议 + 去手写链路 + 大文件按职责拆分 + 代码审查 + 剥离 Tools
## 1. 新增 UiComponent 顶层组件协议(不是负优化)
Components/Shared/Inc/UiComponent.hpp:纯接口、无状态,8 个方法
SetScale / Layout / Area / Paint / HitTest / OnMouseLeave / IsEditing / QueryCursor。
Toolbar / TabBar / SignalPanel / PlotView 本来就各自拥有完全同名同签名的成员,
因此改造 = 加 override + 补 Area()(PlotView 另加一个非 const 的 QueryCursor 转发,
因为它自己的查询会惰性建立行缓存),实现代码零改动、静态派发、体积无增长。
刻意不把 UiWidget 当作它的基类:UiWidget 是"树节点"(父给尺寸、父下探脏区、
命中沿树链派发),而 PlotView(4 层位图缓存)/ SignalPanel(行位图缓存)是
"地盘宿主"。强行上树会推翻"父脏则子全脏"的前提,历史上正是这类耦合导致过
"后台渲染覆盖平移结果""命中矩形错位"的事故。头文件里已写明这条理由。
Toolbar/TabBar 是两者的交集:对外实现 UiComponent,对内持有一棵 UiWidget 树。
## 2. 宿主侧:四条手写链 -> 组件表
Application 新增 ComponentEntry{ component, target, chrome } 与 m_components
(底->顶:plot -> panel -> toolbar -> tabBar),BuildComponentTable() 装配一次。
- ApplyDpi 6 行逐个 SetScale -> 1 个循环
- Relayout 手算三列 x 坐标 -> UiStackRects(内容区, HORIZONTAL, {panelW, splitW, AUTO})
- WM_MOUSELEAVE 逐个 OnMouseLeave -> 循环累积 chromeChanged / plotChanged
- WM_SETCURSOR if/else if 链 -> 从表尾向前找首个命中者,取其 QueryCursor
顺带兑现历史缺口:UiLayout::UiStackRects 自引入以来第一次有了生产调用点。
有意保留显式的三条链(WM_LBUTTONDOWN / WM_LBUTTONUP / WM_MOUSEMOVE):它们各自带
组件专属后置动作(提交重命名、构建派生信号、面板联动滚动、更新拖拽光标、
plotOnlyMove 决定局部失效还是整帧失效)。塞进表里只能靠 lambda 字段,
可读性反而下降 —— 属于用户明确禁止的"过度抽象",因此不做。
## 3. 大文件按职责拆分(纯整函数搬移,逐像素 0 差异)
SignalPanelInteraction.cpp 2329 -> 831 行,拆出:
SignalPanelEdit.cpp 616 内联编辑(筛选框、组名/信号名、数值、颜色与透明度)
SignalPanelScroll.cpp 205 动画与滚动(折起、联动滚动、滚动到目标信号)
SignalPanelMenu.cpp 527 弹出菜单(系数/偏移、镜像、右键菜单与命令回执)
Shared/PopupMenu.cpp 1901 -> 706 行,拆出:
PopupMenuLayout.cpp 753 尺寸测量、项矩形排版、滚动可见性
PopupMenuPaint.cpp 397 背景/阴影/项绘制
Inc/PopupMenuInternal.hpp 103 pmdetail 常量 + inline 辅助(跨 TU 安全)
拆分判据是"职责"而非行数;共享常量一律收敛到 *Internal.hpp。
## 4. 代码审查(修 5 处)
- SignalPanelInteraction.cpp 头注释仍写"4 片",实际 6 片 -> 改为完整清单
- PopupMenuLayout.cpp 机械搬移时把构造函数一起搬走 -> 重复定义,只保留在交互分片
- PopupMenu.cpp 的 #include "PopupMenuInternal.hpp" 落进 namespace jdi 内 -> 移到文件头
- PopupMenuPaint.cpp 缺 UiControls.hpp(UiDrawEditFieldContent 未声明)-> 补 include
- 全仓复查历史删除符号残留引用(m_paintPlot / ClampCursorsAfterPan /
PaintFreeMarkerTooltip / CaptureDecorCache / FitSinglePointSignal / CsvTable /
DataFile / .preflight / structcheck)-> 零命中
## 5. Tools/ 剥离
git rm -r --cached Tools + .gitignore 新增 Tools/(本地文件保留,可随时复跑)。
ui_refactor 分支为全本地分支,另做 filter-branch 清除历史中的该目录。
## 6. 验证
- make release / make debug:零 error 零 warning
- rendercheck:A-H 八个场景哈希与改造前逐一相同
(A 2166933039626F66 B C444ECFEB18FD73D C 7984446D8E7A4418 D BF855879CA12F289
E 1C663E4118616016 F 0535292D8E07AE18 G 7A58172C46577729 H 23F390DD76141113)
FUNC 7/7 PASS
- uicheck:69 passed, 0 failed
- TargetCheck.exe 891,392 B(< 900 KB 阈值)
## 7. 文档
Documents/UI_Refactor_Plan.md / UI_Refactor_Progress.md / UI_Refactor_Report.md
新增第五阶段章节;Documents/agent_mem.md 同步变更日志与「第七轮」硬约束。
This commit is contained in:
1 parent
675c2a5f54
commit
0ec8055e95
20 files changed
+3287
-2678
No files matched your search
@@ -0,0 +1,103 @@
|
||||
#ifndef __POPUP_MENU_INTERNAL_HPP__
|
||||
#define __POPUP_MENU_INTERNAL_HPP__
|
||||
|
||||
// PopupMenu 的内部共享定义(尺寸常量与绘制辅助函数)。
|
||||
//
|
||||
// 实现按职责分片后(PopupMenu.cpp = 交互与生命周期 / PopupMenuLayout.cpp = 几何与排版 /
|
||||
// PopupMenuPaint.cpp = 绘制),三片都要用到同一批尺寸常量与「还原」图标绘制,
|
||||
// 因此统一放在这里:命名空间 `pmdetail`、全部 `inline` / `constexpr`,跨 TU 安全。
|
||||
//
|
||||
// ⚠️ 这里的常量同时决定**排版与绘制**:改数值会同时影响两边(这是刻意的,避免两套真相)。
|
||||
|
||||
#include <windows.h>
|
||||
#include <objidl.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
#include "Theme.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
namespace pmdetail
|
||||
{
|
||||
constexpr float kRowHeight = 27.0f; /**< 普通项高度。 */
|
||||
constexpr float kHeaderHeight = 22.0f; /**< 分组标题高度。 */
|
||||
constexpr float kSeparatorHeight = 9.0f; /**< 分隔线高度。 */
|
||||
constexpr float kSwatchRowHeight = 32.0f; /**< 色块行高度。 */
|
||||
constexpr float kPadV = 5.0f; /**< 菜单上下内边距。 */
|
||||
constexpr float kPadH = 5.0f; /**< 菜单左右内边距。 */
|
||||
constexpr float kItemPad = 8.0f; /**< 条目内文本的左右内缩(所有条目统一)。 */
|
||||
constexpr float kMarkerColumn = 18.0f; /**< 条目**右侧**的选中标记列宽度(对勾画在这里)。 */
|
||||
constexpr float kMarkerGap = 4.0f; /**< 文本与右侧标记列之间的间隔。 */
|
||||
constexpr float kMinWidth = 148.0f; /**< 菜单最小宽度。 */
|
||||
constexpr float kMaxWidth = 340.0f; /**< 菜单最大宽度。 */
|
||||
constexpr float kCorner = 6.0f; /**< 菜单圆角半径。 */
|
||||
constexpr float kSwatchSize = 22.0f; /**< 色块尺寸。 */
|
||||
constexpr float kSwatchGap = 5.0f; /**< 色块间距。 */
|
||||
constexpr float kEditFieldWidth = 88.0f; /**< EDIT 项输入框宽度。 */
|
||||
constexpr float kEditFieldGap = 6.0f; /**< 说明文字与输入框之间的最小间隔。 */
|
||||
constexpr float kEditFieldPadV = 5.0f; /**< 输入框上下内缩。 */
|
||||
constexpr float kSubArrowSize = 4.5f; /**< 子菜单指示箭头尺寸。 */
|
||||
constexpr float kSubOverlap = 2.0f; /**< 子菜单与主菜单重叠的宽度(避免中间露出缝隙)。 */
|
||||
constexpr float kHeaderIconSize = 16.0f; /**< `HEADER_ACTION` 右侧图标按钮的边长。 */
|
||||
constexpr float kHeaderIconGap = 6.0f; /**< 标题文字与图标按钮之间的最小间隔。 */
|
||||
constexpr float kRowFieldWidth = 132.0f; /**< 整行输入框(无说明文字)的默认宽度。 */
|
||||
constexpr float kScrollBarWidth = 6.0f; /**< 可滚动菜单的滚动条宽度。 */
|
||||
constexpr float kScrollBarGap = 4.0f; /**< 滚动条与条目内容之间的间距。 */
|
||||
constexpr float kScrollBarMinThumb = 22.0f; /**< 滚动条滑块的最小高度。 */
|
||||
|
||||
/**
|
||||
* @brief 绘制「还原/重置」图标(逆时针回环箭头)。
|
||||
*
|
||||
* 用一段圆弧 + 一个三角箭头拼成,与界面里其它自绘图标(加号、叉号)保持同一笔画风格。
|
||||
* 不依赖任何字体或资源,因此在任何 DPI 下都清晰。
|
||||
*
|
||||
* @param [IN,OUT] g 绘图上下文。
|
||||
* @param [IN] rect 图标外接矩形(正方形)。
|
||||
* @param [IN] color 笔画颜色。
|
||||
* @param [IN] scale DPI 缩放比例。
|
||||
*/
|
||||
inline void PaintResetIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t color, float scale)
|
||||
{
|
||||
const float inset = 2.5f * scale;
|
||||
gd::RectF box(rect.X + inset, rect.Y + inset, rect.Width - inset * 2.0f,
|
||||
rect.Height - inset * 2.0f);
|
||||
if (box.Width <= 1.0f || box.Height <= 1.0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
gd::Pen pen(gd::Color((color >> 24) & 0xFFu, (color >> 16) & 0xFFu, (color >> 8) & 0xFFu,
|
||||
color & 0xFFu),
|
||||
std::max(1.0f, 1.5f * scale));
|
||||
pen.SetStartCap(gd::LineCapRound);
|
||||
pen.SetEndCap(gd::LineCapRound);
|
||||
// 缺口留在右上角(从 300° 到 20° 处断开),与常见的"重置/刷新"图标一致。
|
||||
g.DrawArc(&pen, box.X, box.Y, box.Width, box.Height, 300.0f, 300.0f);
|
||||
|
||||
// 箭头:位于缺口末端(约 300° 方向),指向前进方向。
|
||||
const float cx = box.X + box.Width * 0.5f;
|
||||
const float cy = box.Y + box.Height * 0.5f;
|
||||
const float radius = box.Width * 0.5f;
|
||||
const float angle = 300.0f * 3.14159265f / 180.0f;
|
||||
const float tipX = cx + std::cos(angle) * radius;
|
||||
const float tipY = cy + std::sin(angle) * radius;
|
||||
const float size = std::max(2.5f, 3.2f * scale);
|
||||
gd::PointF arrow[3];
|
||||
arrow[0] = gd::PointF(tipX, tipY);
|
||||
arrow[1] = gd::PointF(tipX - size, tipY - size * 0.25f);
|
||||
arrow[2] = gd::PointF(tipX - size * 0.25f, tipY - size);
|
||||
gd::SolidBrush brush(gd::Color((color >> 24) & 0xFFu, (color >> 16) & 0xFFu,
|
||||
(color >> 8) & 0xFFu, color & 0xFFu));
|
||||
g.FillPolygon(&brush, arrow, 3);
|
||||
}
|
||||
} // namespace pmdetail
|
||||
|
||||
using namespace pmdetail;
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
#endif // __POPUP_MENU_INTERNAL_HPP__
|
||||
@@ -0,0 +1,80 @@
|
||||
#ifndef __UI_COMPONENT_HPP__
|
||||
#define __UI_COMPONENT_HPP__
|
||||
|
||||
#include <windows.h>
|
||||
#include <objidl.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
#include "Theme.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief 顶层 UI 组件的统一协议(**纯接口,无状态**)。
|
||||
*
|
||||
* ## 为什么需要它
|
||||
*
|
||||
* 旧实现里宿主(`Application`)用**手写 if 链**逐个点名四个大组件,导致同一件事
|
||||
* (设置缩放 / 摆放 / 悬停离开 / 光标仲裁 / 绘制)要在宿主的每个消息分支里各写一遍,
|
||||
* 新增组件必须记得改 5 处。这里把它们收敛成一张**有序组件表**,
|
||||
* 宿主只按表遍历,不再点名。
|
||||
*
|
||||
* ## 为什么不是 `UiWidget`
|
||||
*
|
||||
* `UiWidget` 的契约是"**树上的一个节点**":尺寸由父节点 `Measure` 决定、脏区由父节点下探、
|
||||
* 命中走树的命中链、`OnPaint` 只画自己不许遍历子节点。而顶层大组件的现实是:
|
||||
* * 区域由宿主给定,且**内部还要再切分**;
|
||||
* * `PlotView` 有 4 层自持位图缓存、`SignalPanel` 有行位图缓存 —— 与"父脏下探"是另一套失效体系;
|
||||
* * `PlotView` 一旦成为树节点,它的 `Paint` 就得接受父级 `dirty` 并与自己的缓存对齐,
|
||||
* 这正是历史上"后台渲染覆盖用户平移 / 命中错位"那类事故的温床。
|
||||
*
|
||||
* ⇒ 两者是**兄弟关系**而不是父子关系:
|
||||
* `UiWidget`(树内节点,管**一块区域内部的**布局/命中/绘制)
|
||||
* `UiComponent`(顶层宿主,管**自己这一整块地盘**:区域、缓存、局部重绘策略)。
|
||||
* `Toolbar` / `TabBar` 同时是两者 —— 对外实现本协议,对内持有一棵 `UiWidget` 树。
|
||||
*
|
||||
* ## 实现要求
|
||||
*
|
||||
* * 所有方法都必须**幂等且可在未布局时安全调用**(宿主会在首次 `Layout` 之前调 `SetScale`)。
|
||||
* * `HitTest` 必须与 `Layout` 接受的区域一致(绘制与命中同源)。
|
||||
* * `Paint` 必须把内容严格限制在 `Area()` 内(宿主可能只回送部分矩形)。
|
||||
*/
|
||||
class UiComponent
|
||||
{
|
||||
public:
|
||||
virtual ~UiComponent() = default;
|
||||
|
||||
/** @brief 设置 DPI 缩放系数(> 0)。 */
|
||||
virtual void SetScale(float scale) = 0;
|
||||
|
||||
/** @brief 在宿主给定的区域内摆放自身(内部布局由组件自己决定)。 */
|
||||
virtual void Layout(const gd::RectF& area) = 0;
|
||||
|
||||
/** @brief 当前占用区域(客户区坐标);未布局时返回空矩形。 */
|
||||
virtual gd::RectF Area() const = 0;
|
||||
|
||||
/** @brief 绘制自身(必须裁剪在 `Area()` 内)。 */
|
||||
virtual void Paint(gd::Graphics& g) = 0;
|
||||
|
||||
/** @brief 坐标是否落在本组件内(与 `Area()` 同源)。 */
|
||||
virtual bool HitTest(int x, int y) const = 0;
|
||||
|
||||
/** @brief 指针离开窗口/组件:清掉本组件的悬停态。返回是否需要重绘。 */
|
||||
virtual bool OnMouseLeave() = 0;
|
||||
|
||||
/** @brief 是否处于内联编辑态(宿主据此把鼠标/键盘优先交给它)。 */
|
||||
virtual bool IsEditing() const { return false; }
|
||||
|
||||
/** @brief 光标仲裁:返回 `nullptr` 表示不认领(由下层或宿主决定)。 */
|
||||
virtual HCURSOR QueryCursor(int x, int y)
|
||||
{
|
||||
(void)x;
|
||||
(void)y;
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
#endif // __UI_COMPONENT_HPP__
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,753 @@
|
||||
// PopupMenu 实现分片 —— 几何与排版(BuildLayout / 子菜单 / 滚动区 / 内联输入框位置)。
|
||||
//
|
||||
// 本文件只包含 PopupMenu 的成员函数定义(从 PopupMenu.cpp 机械搬移,行为不变)。
|
||||
//
|
||||
// ⚠️ 这里是菜单**唯一**的几何来源:`Paint` 与所有命中测试都读它产出的矩形,
|
||||
// 不得在别处再反推一遍(否则会出现"看得见点不到")。
|
||||
// ⚠️ `NaturalEditFieldWidth` / `EditSuffixWidth` 同时参与排版与绘制,改公式必须两处一致。
|
||||
|
||||
#include "PopupMenu.hpp"
|
||||
|
||||
#include "PopupMenuInternal.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
void PopupMenu::Setup(const UiFonts* fonts, const Theme* theme)
|
||||
{
|
||||
m_fonts = fonts;
|
||||
m_theme = theme;
|
||||
}
|
||||
|
||||
void PopupMenu::SetScale(float scale)
|
||||
{
|
||||
m_scale = (scale > 0.1f) ? scale : 1.0f;
|
||||
}
|
||||
|
||||
float PopupMenu::ItemHeight(const MenuItem& item) const
|
||||
{
|
||||
switch (item.kind)
|
||||
{
|
||||
case MenuItemKind::SEPARATOR:
|
||||
return kSeparatorHeight * m_scale;
|
||||
case MenuItemKind::HEADER:
|
||||
case MenuItemKind::HEADER_ACTION:
|
||||
return kHeaderHeight * m_scale;
|
||||
case MenuItemKind::SWATCHES:
|
||||
return kSwatchRowHeight * m_scale;
|
||||
case MenuItemKind::EDIT:
|
||||
// 编辑项比普通项略高,给输入框留出上下呼吸空间。
|
||||
return (kRowHeight + 4.0f) * m_scale;
|
||||
case MenuItemKind::NORMAL:
|
||||
default:
|
||||
return kRowHeight * m_scale;
|
||||
}
|
||||
}
|
||||
|
||||
void PopupMenu::BuildLayout(const gd::RectF& bounds, int x, int y, bool expandUp)
|
||||
{
|
||||
m_itemRects.assign(m_items.size(), gd::RectF());
|
||||
m_swatchRects.assign(m_items.size(), std::vector<gd::RectF>());
|
||||
|
||||
if (m_fonts == nullptr)
|
||||
{
|
||||
m_rect = gd::RectF();
|
||||
return;
|
||||
}
|
||||
|
||||
float contentWidth = kMinWidth * m_scale;
|
||||
float totalHeight = kPadV * 2.0f * m_scale;
|
||||
|
||||
std::vector<float> heights(m_items.size(), 0.0f);
|
||||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||||
{
|
||||
const MenuItem& item = m_items[index];
|
||||
heights[index] = ItemHeight(item);
|
||||
totalHeight += heights[index];
|
||||
|
||||
if (item.kind == MenuItemKind::SWATCHES)
|
||||
{
|
||||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||||
static_cast<float>(count) * kSwatchSize * m_scale +
|
||||
static_cast<float>(std::max(0, count - 1)) * kSwatchGap * m_scale;
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::HEADER || item.kind == MenuItemKind::HEADER_ACTION)
|
||||
{
|
||||
// 标题与普通项共用同一文字列与右侧标记列,保证整个菜单的左右边距一致。
|
||||
const float iconExtra = (item.kind == MenuItemKind::HEADER_ACTION)
|
||||
? (kHeaderIconSize + kHeaderIconGap) * m_scale
|
||||
: 0.0f;
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||||
kMarkerGap * m_scale + iconExtra +
|
||||
UiMeasureTextWidth(m_fonts->uiSmallBold, item.label);
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::NORMAL)
|
||||
{
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||||
kMarkerGap * m_scale + UiMeasureTextWidth(m_fonts->uiSmall, item.label);
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::EDIT)
|
||||
{
|
||||
// 说明文字 + 输入框;输入框宽度按数值文本自适应(与 EditFieldRect 保持一致)。
|
||||
const float fieldWidth = NaturalEditFieldWidth(item);
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||||
UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale +
|
||||
fieldWidth;
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
}
|
||||
|
||||
contentWidth = std::min(contentWidth, kMaxWidth * m_scale);
|
||||
const float width = contentWidth;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// 超长菜单:限制高度 + 条目区可滚动 + 顶部/尾部固定项(见 `MenuLayoutConstraint`)。
|
||||
// * 顶部固定 = **前导的 `HEADER` / `HEADER_ACTION`**(典型:「所属网格」标题)——
|
||||
// 用户 2026-10-16 要求:滚动时标题必须留在最上方不动;
|
||||
// * 尾部固定 = **最后一个分隔线及其之后的条目**(典型:「新建网格」)钉在底部;
|
||||
// * 中间是滚动区(鼠标滚轮 / 键盘 / 滚动条),右侧画一根滚动条。
|
||||
// ------------------------------------------------------------------
|
||||
int pinnedFrom = -1;
|
||||
int scrollFirst = 0;
|
||||
if (m_constraint.maxHeight > 0.0f || m_constraint.maxScrollRows > 0)
|
||||
{
|
||||
for (int index = static_cast<int>(m_items.size()) - 1; index > 0; --index)
|
||||
{
|
||||
if (m_items[static_cast<std::size_t>(index)].kind == MenuItemKind::SEPARATOR)
|
||||
{
|
||||
pinnedFrom = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (scrollFirst < pinnedFrom &&
|
||||
(m_items[static_cast<std::size_t>(scrollFirst)].kind == MenuItemKind::HEADER ||
|
||||
m_items[static_cast<std::size_t>(scrollFirst)].kind == MenuItemKind::HEADER_ACTION))
|
||||
{
|
||||
++scrollFirst; // 前导标题固定在顶部,不参与滚动
|
||||
}
|
||||
}
|
||||
|
||||
float headHeight = 0.0f;
|
||||
float scrollRegionHeight = 0.0f;
|
||||
float tailHeight = 0.0f;
|
||||
if (pinnedFrom > 0)
|
||||
{
|
||||
for (int index = 0; index < static_cast<int>(m_items.size()); ++index)
|
||||
{
|
||||
if (index >= pinnedFrom)
|
||||
{
|
||||
tailHeight += heights[static_cast<std::size_t>(index)];
|
||||
}
|
||||
else if (index < scrollFirst)
|
||||
{
|
||||
headHeight += heights[static_cast<std::size_t>(index)];
|
||||
}
|
||||
else
|
||||
{
|
||||
scrollRegionHeight += heights[static_cast<std::size_t>(index)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float height = totalHeight;
|
||||
m_scrollEnabled = false;
|
||||
m_scrollRegionHeight = 0.0f;
|
||||
m_scrollFirst = 0;
|
||||
m_pinnedIndex = -1;
|
||||
m_scrollViewport = gd::RectF();
|
||||
m_scrollBarRect = gd::RectF();
|
||||
// ⚠️ **不要在这里复位 `m_scrollBarDragging`**:拖拽滚动条时每一帧都会 `RebuildLayout()`,
|
||||
// 在这里清零会把拖拽态当场吃掉(拖一下断一下)。该状态只在 `Open` / `Close` 里复位。
|
||||
// ⚠️ **不要在这里无条件清零 `m_scrollOffset`**:`RebuildLayout()`(滚轮 / 数值回写 / 拖滚动条)
|
||||
// 会重新走一遍本函数,清零就等于把滚动位置吃掉(滚轮“滚不动”)。只有在菜单**不再需要**
|
||||
// 滚动时才复位(`Open` 里已经为每次打开重置过)。
|
||||
if (pinnedFrom > 0)
|
||||
{
|
||||
// 滚动视口高度上限:`maxHeight`(客户区像素 → 换算成视口可用高)与
|
||||
// `maxScrollRows`(标准行数)**取更严格的那个**;0 = 该约束不生效。
|
||||
float viewportLimit = 0.0f;
|
||||
if (m_constraint.maxHeight > 0.0f)
|
||||
{
|
||||
// 夹到宿主矩形以内(不盖住窗口边框);小到放不下时的上限直接视作“不限制”,
|
||||
// 交给下方的边界收敛处理,避免算出一个高度为 0 的菜单。
|
||||
const float menuLimit = std::min(m_constraint.maxHeight, bounds.Height - 4.0f * m_scale);
|
||||
if (menuLimit >= 40.0f * m_scale)
|
||||
{
|
||||
viewportLimit = std::max(
|
||||
0.0f, menuLimit - kPadV * 2.0f * m_scale - headHeight - tailHeight);
|
||||
}
|
||||
}
|
||||
if (m_constraint.maxScrollRows > 0)
|
||||
{
|
||||
const float rows =
|
||||
static_cast<float>(m_constraint.maxScrollRows) * kRowHeight * m_scale;
|
||||
viewportLimit = (viewportLimit > 0.0f) ? std::min(viewportLimit, rows) : rows;
|
||||
}
|
||||
|
||||
if (viewportLimit >= kRowHeight * m_scale && scrollRegionHeight > viewportLimit + 0.5f)
|
||||
{
|
||||
const float viewportHeight = std::min(scrollRegionHeight, viewportLimit);
|
||||
height = kPadV * 2.0f * m_scale + headHeight + viewportHeight + tailHeight;
|
||||
m_scrollEnabled = true;
|
||||
m_scrollFirst = scrollFirst;
|
||||
m_pinnedIndex = pinnedFrom;
|
||||
m_scrollRegionHeight = scrollRegionHeight;
|
||||
m_scrollOffset = std::max(
|
||||
0.0f, std::min(m_scrollOffset, std::max(0.0f, scrollRegionHeight - viewportHeight)));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_scrollOffset = 0.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_scrollOffset = 0.0f;
|
||||
}
|
||||
|
||||
// 带说明文字的 EDIT 项共用**同一个输入框宽度**:取其中最大的自然宽度,
|
||||
// 再按“给最宽的说明文字留位 / 不超过内容区 3/4”夹取。
|
||||
// ⚠️ 若每项各自按自己的文本长度算,同一面板里就会出现一个宽一个窄
|
||||
// (时间轴行的文本更长、还带单位后缀,于是它的输入框比 Y 轴行宽一截)。
|
||||
float labeledFieldWidth = 0.0f;
|
||||
float widestLabel = 0.0f;
|
||||
for (const MenuItem& item : m_items)
|
||||
{
|
||||
if (item.kind != MenuItemKind::EDIT || item.label.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
labeledFieldWidth = std::max(labeledFieldWidth, NaturalEditFieldWidth(item));
|
||||
widestLabel = std::max(widestLabel,
|
||||
UiMeasureTextWidth(m_fonts->uiSmall, item.label) +
|
||||
kEditFieldGap * m_scale);
|
||||
}
|
||||
if (labeledFieldWidth > 0.0f)
|
||||
{
|
||||
const float rowContent =
|
||||
std::max(40.0f * m_scale, (width - 2.0f * (kPadH + kItemPad) * m_scale));
|
||||
const float limit = std::max(40.0f * m_scale,
|
||||
std::min(rowContent * 0.75f, rowContent - widestLabel));
|
||||
labeledFieldWidth = std::min(labeledFieldWidth, limit);
|
||||
}
|
||||
m_editFieldWidth = labeledFieldWidth;
|
||||
|
||||
float left = static_cast<float>(x);
|
||||
// `expandUp`:锚点是菜单的**左下角**(向上展开);否则锚点是左上角(向右下展开)。
|
||||
float top = expandUp ? (static_cast<float>(y) - height) : static_cast<float>(y);
|
||||
|
||||
// 优先向右展开(向左下/右下);越界时向左或向上翻转,最后做边界收敛。
|
||||
if (left + width > bounds.X + bounds.Width)
|
||||
{
|
||||
left = static_cast<float>(x) - width;
|
||||
}
|
||||
if (!expandUp && top + height > bounds.Y + bounds.Height)
|
||||
{
|
||||
top = static_cast<float>(y) - height;
|
||||
}
|
||||
left = std::max(bounds.X + 2.0f, std::min(left, bounds.X + bounds.Width - width - 2.0f));
|
||||
top = std::max(bounds.Y + 2.0f, std::min(top, bounds.Y + bounds.Height - height - 2.0f));
|
||||
|
||||
m_rect = gd::RectF(left, top, width, height);
|
||||
|
||||
// 条目排布:头部固定区从菜单顶往下排;滚动区条目整体上移 `m_scrollOffset`;
|
||||
// 尾部固定区从菜单底部往上对齐。
|
||||
const float bodyTop = top + kPadV * m_scale;
|
||||
const float tailTop = top + height - kPadV * m_scale - tailHeight;
|
||||
const float scrollBarReserve = m_scrollEnabled ? (kScrollBarWidth + kScrollBarGap) * m_scale : 0.0f;
|
||||
m_scrollViewport =
|
||||
m_scrollEnabled
|
||||
? gd::RectF(left + kPadH * m_scale, bodyTop + headHeight,
|
||||
std::max(0.0f, width - kPadH * 2.0f * m_scale),
|
||||
std::max(0.0f, height - kPadV * 2.0f * m_scale - headHeight - tailHeight))
|
||||
: gd::RectF();
|
||||
|
||||
float cursorY = bodyTop;
|
||||
float headCursorY = bodyTop;
|
||||
float scrollCursorY = bodyTop + headHeight - m_scrollOffset;
|
||||
float tailCursorY = (m_scrollEnabled) ? tailTop : bodyTop;
|
||||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||||
{
|
||||
const MenuItem& item = m_items[index];
|
||||
const float itemHeight = heights[index];
|
||||
|
||||
float rectY = cursorY;
|
||||
float rectWidth = std::max(0.0f, width - kPadH * 2.0f * m_scale);
|
||||
if (m_scrollEnabled)
|
||||
{
|
||||
const int itemIndex = static_cast<int>(index);
|
||||
if (itemIndex < m_scrollFirst)
|
||||
{
|
||||
rectY = headCursorY;
|
||||
headCursorY += itemHeight;
|
||||
}
|
||||
else if (itemIndex < m_pinnedIndex)
|
||||
{
|
||||
rectY = scrollCursorY;
|
||||
scrollCursorY += itemHeight;
|
||||
// 滚动区条目让出右侧滚动条的位置(对勾/文本不会压在滑块上)。
|
||||
rectWidth = std::max(0.0f, rectWidth - scrollBarReserve);
|
||||
}
|
||||
else
|
||||
{
|
||||
rectY = tailCursorY;
|
||||
tailCursorY += itemHeight;
|
||||
}
|
||||
}
|
||||
m_itemRects[index] =
|
||||
gd::RectF(left + kPadH * m_scale, rectY, rectWidth, itemHeight);
|
||||
|
||||
if (item.kind == MenuItemKind::SWATCHES)
|
||||
{
|
||||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||||
std::vector<gd::RectF>& rects = m_swatchRects[index];
|
||||
rects.reserve(static_cast<std::size_t>(count));
|
||||
const float swatchY = rectY + (itemHeight - kSwatchSize * m_scale) * 0.5f;
|
||||
const float startX = left + (kPadH + kItemPad) * m_scale;
|
||||
for (int swatch = 0; swatch < count; ++swatch)
|
||||
{
|
||||
const float swatchX =
|
||||
startX + static_cast<float>(swatch) * (kSwatchSize + kSwatchGap) * m_scale;
|
||||
rects.push_back(gd::RectF(swatchX, swatchY, kSwatchSize * m_scale, kSwatchSize * m_scale));
|
||||
}
|
||||
}
|
||||
|
||||
cursorY += itemHeight;
|
||||
}
|
||||
|
||||
// 滚动条轨道:贴在可滚动条目区的右内侧(只在条目区旁边,不会盖住固定区)。
|
||||
if (m_scrollEnabled)
|
||||
{
|
||||
const float thickness = kScrollBarWidth * m_scale;
|
||||
m_scrollBarRect = gd::RectF(left + width - kPadH * m_scale - thickness,
|
||||
m_scrollViewport.Y, thickness, m_scrollViewport.Height);
|
||||
}
|
||||
}
|
||||
|
||||
bool PopupMenu::ScrollRegion(int& first, int& last) const
|
||||
{
|
||||
if (!m_scrollEnabled || m_pinnedIndex < 0)
|
||||
{
|
||||
first = 0;
|
||||
last = static_cast<int>(m_items.size());
|
||||
return false;
|
||||
}
|
||||
first = m_scrollFirst;
|
||||
last = m_pinnedIndex;
|
||||
return true;
|
||||
}
|
||||
|
||||
gd::RectF PopupMenu::ScrollBarThumbRect() const
|
||||
{
|
||||
if (!m_scrollEnabled || m_scrollBarRect.Height <= 0.0f || m_scrollRegionHeight <= 0.0f)
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
const float viewport = m_scrollViewport.Height;
|
||||
if (viewport <= 0.0f)
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
const float ratio = std::min(1.0f, viewport / m_scrollRegionHeight);
|
||||
const float thumbHeight = std::max(kScrollBarMinThumb * m_scale, m_scrollBarRect.Height * ratio);
|
||||
const float maxScroll = std::max(0.0f, m_scrollRegionHeight - viewport);
|
||||
const float progress = (maxScroll > 0.0f) ? (m_scrollOffset / maxScroll) : 0.0f;
|
||||
const float thumbY =
|
||||
m_scrollBarRect.Y +
|
||||
std::max(0.0f, std::min(1.0f, progress)) * std::max(0.0f, m_scrollBarRect.Height - thumbHeight);
|
||||
return gd::RectF(m_scrollBarRect.X, thumbY, m_scrollBarRect.Width, thumbHeight);
|
||||
}
|
||||
|
||||
float PopupMenu::ScrollOffsetForThumbTop(float thumbTop) const
|
||||
{
|
||||
if (!m_scrollEnabled || m_scrollBarRect.Height <= 0.0f)
|
||||
{
|
||||
return m_scrollOffset;
|
||||
}
|
||||
const float viewport = m_scrollViewport.Height;
|
||||
const float maxScroll = std::max(0.0f, m_scrollRegionHeight - viewport);
|
||||
if (maxScroll <= 0.0f)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
const float track = m_scrollBarRect.Height;
|
||||
const float thumbHeight = std::max(kScrollBarMinThumb * m_scale, track * std::min(1.0f, viewport / std::max(1.0f, m_scrollRegionHeight)));
|
||||
const float travel = std::max(0.0f, track - thumbHeight);
|
||||
if (travel <= 0.0f)
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
const float relative = thumbTop - m_scrollBarRect.Y;
|
||||
const float progress = std::max(0.0f, std::min(1.0f, relative / travel));
|
||||
return progress * maxScroll;
|
||||
}
|
||||
|
||||
bool PopupMenu::EnsureItemVisible(int index)
|
||||
{
|
||||
if (!m_scrollEnabled || index < m_scrollFirst || index >= m_pinnedIndex ||
|
||||
m_scrollViewport.Height <= 0.0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const gd::RectF& rect = m_itemRects[static_cast<std::size_t>(index)];
|
||||
const float relative = rect.Y - m_scrollViewport.Y;
|
||||
float delta = 0.0f;
|
||||
if (relative < 0.0f)
|
||||
{
|
||||
delta = relative;
|
||||
}
|
||||
else if (relative + rect.Height > m_scrollViewport.Height)
|
||||
{
|
||||
delta = relative + rect.Height - m_scrollViewport.Height;
|
||||
}
|
||||
if (std::fabs(delta) < 0.5f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_scrollOffset += delta;
|
||||
// 重新排布(内部会重新夹取 `m_scrollOffset`)。
|
||||
RebuildLayout();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PopupMenu::OnMouseWheel(int delta)
|
||||
{
|
||||
if (!m_open || !m_scrollEnabled || delta == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const float previous = m_scrollOffset;
|
||||
// 向上滚(`delta > 0`)= 看更靠前的条目 ⇒ 偏移减小。
|
||||
m_scrollOffset -= static_cast<float>(delta) / 120.0f * 54.0f * m_scale;
|
||||
// 重新排布:条目矩形依赖滚动偏移,同时会夹取到合法范围。
|
||||
RebuildLayout();
|
||||
return std::fabs(m_scrollOffset - previous) > 0.01f;
|
||||
}
|
||||
|
||||
float PopupMenu::NaturalEditFieldWidth(const MenuItem& item) const
|
||||
{
|
||||
const std::wstring& widest =
|
||||
(item.editValue.size() > item.value.size()) ? item.editValue : item.value;
|
||||
const float natural = std::max(kEditFieldWidth * m_scale,
|
||||
UiMeasureTextWidth(m_fonts->uiSmall, widest) + 20.0f * m_scale);
|
||||
const float withSuffix = natural + EditSuffixWidth(item);
|
||||
// 无说明文字 = 整行输入框(如「系数调整」面板):给一个更宽的默认值,视觉上独占整行。
|
||||
return item.label.empty() ? std::max(withSuffix, kRowFieldWidth * m_scale) : withSuffix;
|
||||
}
|
||||
|
||||
void PopupMenu::CloseSubmenu()
|
||||
{
|
||||
m_subOpen = false;
|
||||
m_subParentIndex = -1;
|
||||
m_subRect = gd::RectF();
|
||||
m_subItems.clear();
|
||||
m_subItemRects.clear();
|
||||
m_subHoverIndex = -1;
|
||||
m_subPressedIndex = -1;
|
||||
}
|
||||
|
||||
void PopupMenu::BuildSubLayout(int parentIndex)
|
||||
{
|
||||
CloseSubmenu();
|
||||
if (parentIndex < 0 || parentIndex >= static_cast<int>(m_items.size()))
|
||||
{
|
||||
return;
|
||||
}
|
||||
const MenuItem& parent = m_items[static_cast<std::size_t>(parentIndex)];
|
||||
if (parent.kind != MenuItemKind::SUBMENU || parent.submenu.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_subItems = parent.submenu;
|
||||
|
||||
// 与主菜单一致:宽度取条目中的最大者并夹在上下限之间。
|
||||
float contentWidth = kMinWidth * m_scale;
|
||||
float totalHeight = kPadV * 2.0f * m_scale;
|
||||
for (const MenuItem& item : m_subItems)
|
||||
{
|
||||
totalHeight += ItemHeight(item);
|
||||
if (item.kind == MenuItemKind::SWATCHES)
|
||||
{
|
||||
const int count = std::max(0, std::min(item.swatchCount, 16));
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||||
static_cast<float>(count) * kSwatchSize * m_scale +
|
||||
static_cast<float>(std::max(0, count - 1)) * kSwatchGap * m_scale;
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::HEADER)
|
||||
{
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||||
kMarkerGap * m_scale +
|
||||
UiMeasureTextWidth(m_fonts->uiSmallBold, item.label);
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::EDIT)
|
||||
{
|
||||
const float fieldWidth = NaturalEditFieldWidth(item);
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale +
|
||||
UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale +
|
||||
fieldWidth;
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
else if (item.kind == MenuItemKind::NORMAL || item.kind == MenuItemKind::SUBMENU)
|
||||
{
|
||||
const float width = (kPadH + kItemPad) * 2.0f * m_scale + kMarkerColumn * m_scale +
|
||||
kMarkerGap * m_scale + UiMeasureTextWidth(m_fonts->uiSmall, item.label);
|
||||
contentWidth = std::max(contentWidth, width);
|
||||
}
|
||||
}
|
||||
contentWidth = std::min(contentWidth, kMaxWidth * m_scale);
|
||||
|
||||
const float width = contentWidth;
|
||||
const float height = totalHeight;
|
||||
const gd::RectF& parentRect = m_itemRects[static_cast<std::size_t>(parentIndex)];
|
||||
const gd::RectF& bounds = m_buildBounds;
|
||||
|
||||
// 默认贴主菜单右缘并向上与父项顶端对齐;右侧空间不足时翻到左侧。
|
||||
float left = m_rect.X + m_rect.Width - kSubOverlap * m_scale;
|
||||
if (left + width > bounds.X + bounds.Width - 2.0f)
|
||||
{
|
||||
left = m_rect.X - width + kSubOverlap * m_scale;
|
||||
}
|
||||
left = std::max(bounds.X + 2.0f, std::min(left, bounds.X + bounds.Width - width - 2.0f));
|
||||
|
||||
float top = parentRect.Y - kPadV * m_scale;
|
||||
top = std::max(bounds.Y + 2.0f, std::min(top, bounds.Y + bounds.Height - height - 2.0f));
|
||||
|
||||
m_subRect = gd::RectF(left, top, width, height);
|
||||
m_subItemRects.assign(m_subItems.size(), gd::RectF());
|
||||
|
||||
float cursorY = top + kPadV * m_scale;
|
||||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||||
{
|
||||
const float itemHeight = ItemHeight(m_subItems[index]);
|
||||
m_subItemRects[index] = gd::RectF(left + kPadH * m_scale, cursorY,
|
||||
std::max(0.0f, width - kPadH * 2.0f * m_scale), itemHeight);
|
||||
cursorY += itemHeight;
|
||||
}
|
||||
|
||||
m_subParentIndex = parentIndex;
|
||||
m_subOpen = true;
|
||||
m_subHoverIndex = -1;
|
||||
m_subPressedIndex = -1;
|
||||
}
|
||||
|
||||
int PopupMenu::SubItemAtPoint(int x, int y) const
|
||||
{
|
||||
if (!m_subOpen)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
const float fx = static_cast<float>(x);
|
||||
const float fy = static_cast<float>(y);
|
||||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||||
{
|
||||
const MenuItem& item = m_subItems[index];
|
||||
if (item.kind == MenuItemKind::SEPARATOR || item.kind == MenuItemKind::HEADER)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!item.enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (UiPointInRect(m_subItemRects[index], fx, fy))
|
||||
{
|
||||
return static_cast<int>(index);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
float PopupMenu::EditSuffixWidth(const MenuItem& item) const
|
||||
{
|
||||
if (item.kind != MenuItemKind::EDIT || item.editSuffix.empty())
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
return UiMeasureTextWidth(m_fonts->uiSmall, item.editSuffix) + 8.0f * m_scale;
|
||||
}
|
||||
|
||||
gd::RectF PopupMenu::EditFieldRect(int index) const
|
||||
{
|
||||
if (index < 0 || index >= static_cast<int>(m_items.size()))
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||||
if (item.kind != MenuItemKind::EDIT)
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
|
||||
// 输入框靠右对齐到条目内容区右边界;宽度取自然宽度,但必须给说明文字留出自然宽度,
|
||||
// 避免菜单被最大宽度夹取后输入框把左侧标签挤成省略号。
|
||||
// ⚠️ 说明文字为空(例如「系数调整」面板的整行输入框)时不设 3/4 上限,
|
||||
// 让输入框独占整行。
|
||||
// ⚠️ 带说明文字的项统一使用排布阶段算好的 `m_editFieldWidth`:
|
||||
// 同一面板里所有这类输入框必须一样宽、右边界对齐。
|
||||
const gd::RectF& rect = m_itemRects[static_cast<std::size_t>(index)];
|
||||
const float contentWidth = std::max(40.0f * m_scale, rect.Width - 2.0f * kItemPad * m_scale);
|
||||
const float labelWidth = UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale;
|
||||
float width = NaturalEditFieldWidth(item);
|
||||
width = std::min(width, std::max(40.0f * m_scale, contentWidth - labelWidth));
|
||||
if (item.label.empty())
|
||||
{
|
||||
// 整行输入框(无说明文字):占满内容区,视觉上就是一个独立的一行输入框。
|
||||
width = contentWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_editFieldWidth > 0.0f)
|
||||
{
|
||||
width = m_editFieldWidth;
|
||||
}
|
||||
width = std::min(width, std::max(40.0f * m_scale, contentWidth - labelWidth));
|
||||
width = std::min(width, contentWidth * 0.75f);
|
||||
}
|
||||
|
||||
const float height = rect.Height - kEditFieldPadV * 2.0f * m_scale;
|
||||
return gd::RectF(rect.X + rect.Width - kItemPad * m_scale - width,
|
||||
rect.Y + (rect.Height - height) * 0.5f, width, height);
|
||||
}
|
||||
|
||||
gd::RectF PopupMenu::HeaderIconRect(int index) const
|
||||
{
|
||||
if (index < 0 || index >= static_cast<int>(m_items.size()))
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||||
if (item.kind != MenuItemKind::HEADER_ACTION)
|
||||
{
|
||||
return gd::RectF();
|
||||
}
|
||||
const gd::RectF& rect = m_itemRects[static_cast<std::size_t>(index)];
|
||||
const float size = std::min(kHeaderIconSize * m_scale, std::max(6.0f, rect.Height - 4.0f * m_scale));
|
||||
return gd::RectF(rect.X + rect.Width - kItemPad * m_scale - size,
|
||||
rect.Y + (rect.Height - size) * 0.5f, size, size);
|
||||
}
|
||||
|
||||
bool PopupMenu::IsOnEditField(int x, int y) const
|
||||
{
|
||||
if (m_editIndex < 0)
|
||||
{ return false;
|
||||
}
|
||||
const gd::RectF rect = EditFieldRect(m_editIndex);
|
||||
return UiPointInRect(rect, static_cast<float>(x), static_cast<float>(y));
|
||||
}
|
||||
|
||||
bool PopupMenu::BeginItemEdit(int index)
|
||||
{
|
||||
if (!m_open || index < 0 || index >= static_cast<int>(m_items.size()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||||
if (item.kind != MenuItemKind::EDIT || !item.enabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 输入框初值取「编辑初值」(未设置时用显示值),整段选中,便于直接键入新数值。
|
||||
m_edit.Begin(item.editValue.empty() ? item.value : item.editValue, true, item.filter, 0u);
|
||||
m_editIndex = index;
|
||||
m_hoverIndex = index;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PopupMenu::EndItemEdit(bool commit, MenuResult& result)
|
||||
{
|
||||
if (m_editIndex < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const int index = m_editIndex;
|
||||
const std::wstring text = m_edit.Text();
|
||||
m_editIndex = -1;
|
||||
m_edit.End();
|
||||
|
||||
if (!commit || index >= static_cast<int>(m_items.size()))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
MenuItem& item = m_items[static_cast<std::size_t>(index)];
|
||||
// 立即回写显示值,使菜单上的数值与数据层一致(宿主会在派发后再纠正为夹取后的真实值);
|
||||
// 键入文本同时记为**编辑初值**(`value` 与 `editValue` 不一致的行也能正确回显上次输入)。
|
||||
item.value = text;
|
||||
item.editValue = text;
|
||||
|
||||
result.valid = true;
|
||||
result.scope = m_scope;
|
||||
result.id = item.id;
|
||||
result.editCommit = true;
|
||||
result.editText = text;
|
||||
return true;
|
||||
}
|
||||
|
||||
void PopupMenu::RebuildLayout()
|
||||
{
|
||||
if (!m_open)
|
||||
{
|
||||
return;
|
||||
}
|
||||
const int subParent = m_subParentIndex;
|
||||
BuildLayout(m_buildBounds, m_buildX, m_buildY, m_buildExpandUp);
|
||||
// 主菜单重新排布后子菜单位置会失效:若之前是展开的则按新几何重建。
|
||||
if (subParent >= 0)
|
||||
{
|
||||
BuildSubLayout(subParent);
|
||||
}
|
||||
}
|
||||
|
||||
bool PopupMenu::UpdateItemValue(int id, const std::wstring& value, const std::wstring& editValue)
|
||||
{
|
||||
for (MenuItem& item : m_items)
|
||||
{
|
||||
if (item.id == id && item.kind == MenuItemKind::EDIT)
|
||||
{
|
||||
const bool valueSame = (item.value == value);
|
||||
const bool editSame = editValue.empty() || (item.editValue == editValue);
|
||||
if (valueSame && editSame)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
item.value = value;
|
||||
if (!editValue.empty())
|
||||
{
|
||||
item.editValue = editValue;
|
||||
}
|
||||
RebuildLayout();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PopupMenu::UpdateItemLabel(int id, const std::wstring& label, bool checked, bool enabled)
|
||||
{
|
||||
for (MenuItem& item : m_items)
|
||||
{
|
||||
if (item.id == id && item.kind == MenuItemKind::NORMAL)
|
||||
{
|
||||
if (item.label == label && item.checked == checked && item.enabled == enabled)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
item.label = label;
|
||||
item.checked = checked;
|
||||
item.enabled = enabled;
|
||||
RebuildLayout();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace jdi
|
||||
@@ -0,0 +1,400 @@
|
||||
// PopupMenu 实现分片 —— 绘制(主菜单 + 二级子菜单)。
|
||||
//
|
||||
// 本文件只包含 PopupMenu 的成员函数定义(从 PopupMenu.cpp 机械搬移,行为不变)。
|
||||
//
|
||||
// ⚠️ 菜单是叠在最上层的手绘浮层:文字走 `UiText`(GDI,不认 GDI+ 裁剪区),
|
||||
// 因此所有文字必须由排版阶段算好的矩形约束,不能指望裁剪。
|
||||
|
||||
#include "PopupMenu.hpp"
|
||||
|
||||
#include "PopupMenuInternal.hpp"
|
||||
#include "UiControls.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
void PopupMenu::Paint(gd::Graphics& g)
|
||||
{
|
||||
if (!m_open || m_theme == nullptr || m_fonts == nullptr || m_rect.Width <= 0.0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 投影:用两层递减透明度的圆角矩形模拟,避免引入系统阴影 API。
|
||||
UiFillRoundRect(g, gd::RectF(m_rect.X + 1.0f * m_scale, m_rect.Y + 2.0f * m_scale, m_rect.Width,
|
||||
m_rect.Height),
|
||||
kCorner * m_scale, 0x14000000u);
|
||||
UiFillRoundRect(g, gd::RectF(m_rect.X + 2.0f * m_scale, m_rect.Y + 4.0f * m_scale, m_rect.Width,
|
||||
m_rect.Height),
|
||||
kCorner * m_scale, 0x10000000u);
|
||||
|
||||
UiFillRoundRect(g, m_rect, kCorner * m_scale, m_theme->cardBg);
|
||||
UiDrawRoundRect(g, m_rect, kCorner * m_scale, m_theme->borderStrong, 1.0f);
|
||||
|
||||
// 条目文字统一走本 lambda:可滚动区里的条目必须夹到**滚动视口** ——
|
||||
// `UiText` 走 GDI `DrawTextW`,**不遵循 GDI+ 的 `SetClip`**,不显式裁剪就会画到固定区上。
|
||||
//(GDI+ 图元 —— 行底色 / 圆角 / 分隔线 / 对勾 / 子菜单三角 —— 受 `SetClip` 约束,无需额外处理。)
|
||||
const auto itemText = [this, &g](const std::wstring& text, HFONT font, const gd::RectF& rect,
|
||||
std::uint32_t argb, TextAlign align, bool clipped)
|
||||
{
|
||||
if (clipped)
|
||||
{
|
||||
UiTextClipped(g, text, font, rect, argb, align, m_scrollViewport);
|
||||
}
|
||||
else
|
||||
{
|
||||
UiText(g, text, font, rect, argb, align);
|
||||
}
|
||||
};
|
||||
|
||||
// 可滚动菜单的条目分三段:
|
||||
// * `[0, m_scrollFirst)`:**前导标题**(「所属网格」这类)—— 钉在菜单顶部,**不参与滚动**;
|
||||
// * `[m_scrollFirst, m_pinnedIndex)`:滚动区(裁剪到滚动视口,右侧有滚动条);
|
||||
// * `[m_pinnedIndex, end)`:末尾固定项(最后一个分隔线 + 「新建网格」)钉在菜单底部。
|
||||
const bool scrollable = m_scrollEnabled && m_pinnedIndex > 0;
|
||||
const int scrollFirst = scrollable ? m_scrollFirst : static_cast<int>(m_items.size());
|
||||
const int scrollLast = scrollable ? m_pinnedIndex : static_cast<int>(m_items.size());
|
||||
int clipState = 0;
|
||||
bool clipActive = false;
|
||||
|
||||
for (std::size_t index = 0; index < m_items.size(); ++index)
|
||||
{
|
||||
const MenuItem& item = m_items[index];
|
||||
const gd::RectF& rect = m_itemRects[index];
|
||||
const int itemIndex = static_cast<int>(index);
|
||||
// 进入/离开滚动区:按需开启/撤销 GDI+ 裁剪(固定区永远完整可见)。
|
||||
if (scrollable && itemIndex == scrollFirst && !clipActive)
|
||||
{
|
||||
clipState = g.Save();
|
||||
g.SetClip(m_scrollViewport, gd::CombineModeIntersect);
|
||||
clipActive = true;
|
||||
}
|
||||
else if (scrollable && itemIndex == scrollLast && clipActive)
|
||||
{
|
||||
g.Restore(clipState);
|
||||
clipActive = false;
|
||||
}
|
||||
const bool inScrollRegion = itemIndex >= scrollFirst && itemIndex < scrollLast;
|
||||
// 滚动区里完全不可见的条目直接跳过:长菜单(上百条)逐条绘制会明显卡顿。
|
||||
if (scrollable && inScrollRegion &&
|
||||
(rect.Y + rect.Height <= m_scrollViewport.Y ||
|
||||
rect.Y >= m_scrollViewport.Y + m_scrollViewport.Height))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
const bool clipped = scrollable && inScrollRegion;
|
||||
|
||||
if (item.kind == MenuItemKind::SEPARATOR)
|
||||
{
|
||||
const float lineY = rect.Y + rect.Height * 0.5f;
|
||||
UiDrawLine(g, rect.X + kItemPad * m_scale, lineY,
|
||||
rect.X + rect.Width - kItemPad * m_scale, lineY, m_theme->border, 1.0f);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.kind == MenuItemKind::HEADER)
|
||||
{
|
||||
// 标题与普通项共用同一文字列,保证菜单内所有文本严格左对齐。
|
||||
itemText(item.label, m_fonts->uiSmallBold,
|
||||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, rect.Width, rect.Height),
|
||||
m_theme->textMuted, TextAlign::LEFT, clipped);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.kind == MenuItemKind::HEADER_ACTION)
|
||||
{
|
||||
// 标题 + 右侧「还原」图标按钮:文字与普通标题同一列;图标可点击(悬停高亮)。
|
||||
const gd::RectF icon = HeaderIconRect(static_cast<int>(index));
|
||||
const float labelWidth =
|
||||
std::max(8.0f, icon.X - rect.X - kItemPad * m_scale - kHeaderIconGap * m_scale);
|
||||
itemText(UiEllipsize(item.label, m_fonts->uiSmallBold, labelWidth), m_fonts->uiSmallBold,
|
||||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, labelWidth, rect.Height),
|
||||
m_theme->textMuted, TextAlign::LEFT, clipped);
|
||||
if (icon.Width > 0.0f)
|
||||
{
|
||||
const bool iconHovered = (static_cast<int>(index) == m_hoverIconIndex);
|
||||
if (iconHovered)
|
||||
{
|
||||
UiFillRoundRect(g, gd::RectF(icon.X - 2.0f * m_scale, icon.Y - 2.0f * m_scale,
|
||||
icon.Width + 4.0f * m_scale, icon.Height + 4.0f * m_scale),
|
||||
4.0f * m_scale, WithAlpha(m_theme->accent, 0x33));
|
||||
}
|
||||
PaintResetIcon(g, icon, iconHovered ? m_theme->accent : m_theme->text, m_scale);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool hovered = (static_cast<int>(index) == m_hoverIndex) &&
|
||||
(item.kind != MenuItemKind::SWATCHES || m_hoverSwatch < 0);
|
||||
|
||||
if (item.kind == MenuItemKind::EDIT)
|
||||
{
|
||||
// 编辑项:左侧说明文字 + 右侧内联输入框(与拾色器的 RGB 编辑同一种处理方式,
|
||||
// 菜单始终不关闭,数值就地修改)。
|
||||
if (hovered && item.enabled && static_cast<int>(index) != m_editIndex)
|
||||
{
|
||||
UiFillRoundRect(g, rect, 4.0f * m_scale, WithAlpha(m_theme->accent, 0x22));
|
||||
}
|
||||
|
||||
const std::uint32_t labelColor = item.enabled ? m_theme->text : m_theme->textMuted;
|
||||
// 说明文字与普通项使用同一文字列(kItemPad),保证菜单内所有文本左对齐。
|
||||
const float labelLeft = rect.X + kItemPad * m_scale;
|
||||
const gd::RectF fieldRect = EditFieldRect(static_cast<int>(index));
|
||||
const float labelWidth = std::max(8.0f, fieldRect.X - labelLeft - kEditFieldGap * m_scale);
|
||||
itemText(UiEllipsize(item.label, m_fonts->uiSmall, labelWidth), m_fonts->uiSmall,
|
||||
gd::RectF(labelLeft, rect.Y, labelWidth, rect.Height), labelColor, TextAlign::LEFT,
|
||||
clipped);
|
||||
|
||||
const float suffix = EditSuffixWidth(item);
|
||||
if (static_cast<int>(index) == m_editIndex)
|
||||
{
|
||||
// 编辑态:输入框底板覆盖整行,正文区向右让出后缀宽度,
|
||||
// 后缀(单位提示)靠右对齐画在它右侧。
|
||||
// ⚠️ 必须用 `UiDrawEditFieldContent`(只画内容)——`UiDrawEditField`
|
||||
// 会把底板/描边按传入矩形绘制,那样带后缀时外框会短一截。
|
||||
const gd::RectF textRect =
|
||||
(suffix > 0.0f)
|
||||
? gd::RectF(fieldRect.X, fieldRect.Y, std::max(12.0f, fieldRect.Width - suffix),
|
||||
fieldRect.Height)
|
||||
: fieldRect;
|
||||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->cardBg);
|
||||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->accent, 1.6f);
|
||||
UiDrawEditFieldContent(g, textRect, m_edit, m_fonts->uiSmall, m_scale);
|
||||
if (suffix > 0.0f)
|
||||
{
|
||||
const float suffixLeft = textRect.X + textRect.Width;
|
||||
itemText(item.editSuffix, m_fonts->uiSmall,
|
||||
gd::RectF(suffixLeft, fieldRect.Y,
|
||||
std::max(4.0f, fieldRect.X + fieldRect.Width - suffixLeft -
|
||||
4.0f * m_scale),
|
||||
fieldRect.Height),
|
||||
m_theme->textMuted, TextAlign::RIGHT, clipped);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->pressed);
|
||||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->border, 1.0f);
|
||||
const std::wstring text =
|
||||
UiEllipsize(item.value, m_fonts->uiSmall, fieldRect.Width - 12.0f * m_scale);
|
||||
itemText(text, m_fonts->uiSmall,
|
||||
gd::RectF(fieldRect.X + 6.0f * m_scale, fieldRect.Y,
|
||||
std::max(8.0f, fieldRect.Width - 12.0f * m_scale), fieldRect.Height),
|
||||
item.enabled ? m_theme->textStrong : m_theme->textMuted, TextAlign::LEFT, clipped);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.kind == MenuItemKind::SWATCHES)
|
||||
{
|
||||
const std::vector<gd::RectF>& rects = m_swatchRects[index];
|
||||
for (std::size_t swatch = 0; swatch < rects.size(); ++swatch)
|
||||
{
|
||||
const gd::RectF& swatchRect = rects[swatch];
|
||||
UiFillRoundRect(g, swatchRect, 4.0f * m_scale, item.swatches[swatch]);
|
||||
|
||||
const bool swatchHover = (static_cast<int>(index) == m_hoverIndex) &&
|
||||
(static_cast<int>(swatch) == m_hoverSwatch);
|
||||
if (static_cast<int>(swatch) == item.checkedSwatch || swatchHover)
|
||||
{
|
||||
const gd::RectF ring(swatchRect.X - 2.0f * m_scale, swatchRect.Y - 2.0f * m_scale,
|
||||
swatchRect.Width + 4.0f * m_scale,
|
||||
swatchRect.Height + 4.0f * m_scale);
|
||||
UiDrawRoundRect(g, ring, 5.0f * m_scale,
|
||||
(static_cast<int>(swatch) == item.checkedSwatch) ? m_theme->textStrong
|
||||
: m_theme->borderStrong,
|
||||
(static_cast<int>(swatch) == item.checkedSwatch) ? 1.8f : 1.0f);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// 普通项。
|
||||
std::uint32_t background = 0;
|
||||
if (hovered && item.enabled)
|
||||
{
|
||||
background = m_theme->accent;
|
||||
}
|
||||
else if (static_cast<int>(index) == m_pressedIndex)
|
||||
{
|
||||
background = m_theme->pressed;
|
||||
}
|
||||
if (background != 0)
|
||||
{
|
||||
UiFillRoundRect(g, rect, 4.0f * m_scale, background);
|
||||
}
|
||||
|
||||
const std::uint32_t textColor = !item.enabled ? m_theme->textMuted
|
||||
: ((hovered && item.enabled) ? m_theme->textOnAccent
|
||||
: m_theme->text);
|
||||
|
||||
if (item.checked)
|
||||
{
|
||||
// 单选标记:对勾固定画在条目**右侧**的标记列内,左侧只留统一的小边距,
|
||||
// 这样菜单文本左边距不会因为“可能存在的对勾”而被整体推右。
|
||||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||||
const float cy = rect.Y + rect.Height * 0.5f;
|
||||
const std::uint32_t markerColor = hovered ? m_theme->textOnAccent : m_theme->accent;
|
||||
UiDrawLine(g, cx - 4.0f * m_scale, cy, cx - 1.0f * m_scale, cy + 3.0f * m_scale,
|
||||
markerColor, 1.8f * m_scale);
|
||||
UiDrawLine(g, cx - 1.0f * m_scale, cy + 3.0f * m_scale, cx + 4.5f * m_scale,
|
||||
cy - 4.0f * m_scale, markerColor, 1.8f * m_scale);
|
||||
}
|
||||
|
||||
// 带子菜单的项:在右侧标记列画一个指向右的三角,提示可以继续展开。
|
||||
if (item.kind == MenuItemKind::SUBMENU)
|
||||
{
|
||||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||||
const float cy = rect.Y + rect.Height * 0.5f;
|
||||
const float size = kSubArrowSize * m_scale;
|
||||
gd::PointF arrow[3];
|
||||
arrow[0] = gd::PointF(cx - size * 0.5f, cy - size);
|
||||
arrow[1] = gd::PointF(cx - size * 0.5f, cy + size);
|
||||
arrow[2] = gd::PointF(cx + size * 0.7f, cy);
|
||||
gd::SolidBrush brush(ToGdiColor(textColor));
|
||||
g.FillPolygon(&brush, arrow, 3);
|
||||
}
|
||||
|
||||
const float textLeft = rect.X + kItemPad * m_scale;
|
||||
const float textWidth =
|
||||
rect.Width - 2.0f * kItemPad * m_scale - kMarkerColumn * m_scale - kMarkerGap * m_scale;
|
||||
itemText(UiEllipsize(item.label, m_fonts->uiSmall, textWidth), m_fonts->uiSmall,
|
||||
gd::RectF(textLeft, rect.Y, std::max(8.0f, textWidth), rect.Height), textColor,
|
||||
TextAlign::LEFT, clipped);
|
||||
}
|
||||
|
||||
// 恢复滚动区的裁剪(若已因固定区而恢复过则 `Restore` 会成对生效)。
|
||||
if (clipActive)
|
||||
{
|
||||
g.Restore(clipState);
|
||||
clipActive = false;
|
||||
}
|
||||
|
||||
// 滚动条:轨道 + 滑块(画在条目之上,仅在可滚动菜单里出现)。
|
||||
// 位置限制在可滚动条目区旁边,因此不会盖住顶部标题与底部「新建网格」。
|
||||
if (scrollable && m_scrollBarRect.Width > 0.0f)
|
||||
{
|
||||
const float radius = kScrollBarWidth * m_scale * 0.5f;
|
||||
UiFillRoundRect(g, m_scrollBarRect, radius, m_theme->gridWeak);
|
||||
const gd::RectF thumb = ScrollBarThumbRect();
|
||||
if (thumb.Width > 0.0f)
|
||||
{
|
||||
UiFillRoundRect(g, thumb, radius, m_theme->borderStrong);
|
||||
}
|
||||
}
|
||||
|
||||
// 二级子菜单最后绘制,保证覆盖在主菜单之上。
|
||||
PaintSubmenu(g);
|
||||
}
|
||||
|
||||
void PopupMenu::PaintSubmenu(gd::Graphics& g)
|
||||
{
|
||||
if (!m_subOpen || m_subRect.Width <= 0.0f || m_subRect.Height <= 0.0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UiFillRoundRect(g, gd::RectF(m_subRect.X + 1.0f * m_scale, m_subRect.Y + 2.0f * m_scale,
|
||||
m_subRect.Width, m_subRect.Height),
|
||||
kCorner * m_scale, 0x14000000u);
|
||||
UiFillRoundRect(g, gd::RectF(m_subRect.X + 2.0f * m_scale, m_subRect.Y + 4.0f * m_scale,
|
||||
m_subRect.Width, m_subRect.Height),
|
||||
kCorner * m_scale, 0x10000000u);
|
||||
|
||||
UiFillRoundRect(g, m_subRect, kCorner * m_scale, m_theme->cardBg);
|
||||
UiDrawRoundRect(g, m_subRect, kCorner * m_scale, m_theme->borderStrong, 1.0f);
|
||||
|
||||
for (std::size_t index = 0; index < m_subItems.size(); ++index)
|
||||
{
|
||||
const MenuItem& item = m_subItems[index];
|
||||
const gd::RectF& rect = m_subItemRects[index];
|
||||
|
||||
if (item.kind == MenuItemKind::SEPARATOR)
|
||||
{
|
||||
const float lineY = rect.Y + rect.Height * 0.5f;
|
||||
UiDrawLine(g, rect.X + kItemPad * m_scale, lineY,
|
||||
rect.X + rect.Width - kItemPad * m_scale, lineY, m_theme->border, 1.0f);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.kind == MenuItemKind::HEADER)
|
||||
{
|
||||
UiText(g, item.label, m_fonts->uiSmallBold,
|
||||
gd::RectF(rect.X + kItemPad * m_scale, rect.Y, rect.Width, rect.Height),
|
||||
m_theme->textMuted, TextAlign::LEFT);
|
||||
continue;
|
||||
}
|
||||
|
||||
const bool hovered = (static_cast<int>(index) == m_subHoverIndex) && item.enabled;
|
||||
|
||||
if (item.kind == MenuItemKind::EDIT)
|
||||
{
|
||||
// 子菜单中的编辑项:与主菜单一样就地编辑。
|
||||
if (hovered && static_cast<int>(index) != m_editIndex)
|
||||
{
|
||||
UiFillRoundRect(g, rect, 4.0f * m_scale, WithAlpha(m_theme->accent, 0x22));
|
||||
}
|
||||
const std::uint32_t labelColor = item.enabled ? m_theme->text : m_theme->textMuted;
|
||||
const float labelLeft = rect.X + kItemPad * m_scale;
|
||||
const gd::RectF fieldRect = EditFieldRect(static_cast<int>(index));
|
||||
const float labelWidth = std::max(8.0f, fieldRect.X - labelLeft - kEditFieldGap * m_scale);
|
||||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, labelWidth), m_fonts->uiSmall,
|
||||
gd::RectF(labelLeft, rect.Y, labelWidth, rect.Height), labelColor, TextAlign::LEFT);
|
||||
UiFillRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->pressed);
|
||||
UiDrawRoundRect(g, fieldRect, 3.0f * m_scale, m_theme->border, 1.0f);
|
||||
const std::wstring text =
|
||||
UiEllipsize(item.value, m_fonts->uiSmall, fieldRect.Width - 12.0f * m_scale);
|
||||
UiText(g, text, m_fonts->uiSmall,
|
||||
gd::RectF(fieldRect.X + 6.0f * m_scale, fieldRect.Y,
|
||||
std::max(8.0f, fieldRect.Width - 12.0f * m_scale), fieldRect.Height),
|
||||
item.enabled ? m_theme->textStrong : m_theme->textMuted, TextAlign::LEFT);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (item.kind == MenuItemKind::SWATCHES)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
std::uint32_t background = 0;
|
||||
if (hovered)
|
||||
{
|
||||
background = m_theme->accent;
|
||||
}
|
||||
else if (static_cast<int>(index) == m_subPressedIndex)
|
||||
{
|
||||
background = m_theme->pressed;
|
||||
}
|
||||
if (background != 0)
|
||||
{
|
||||
UiFillRoundRect(g, rect, 4.0f * m_scale, background);
|
||||
}
|
||||
|
||||
const std::uint32_t textColor = !item.enabled ? m_theme->textMuted
|
||||
: (hovered ? m_theme->textOnAccent
|
||||
: m_theme->text);
|
||||
|
||||
// 选中标记(对勾):与主菜单一致画在右侧标记列内。
|
||||
if (item.checked)
|
||||
{
|
||||
const float markerRight = rect.X + rect.Width - kItemPad * m_scale;
|
||||
const float cx = markerRight - kMarkerColumn * m_scale * 0.5f;
|
||||
const float cy = rect.Y + rect.Height * 0.5f;
|
||||
const std::uint32_t markerColor = hovered ? m_theme->textOnAccent : m_theme->accent;
|
||||
UiDrawLine(g, cx - 4.0f * m_scale, cy, cx - 1.0f * m_scale, cy + 3.0f * m_scale,
|
||||
markerColor, 1.8f * m_scale);
|
||||
UiDrawLine(g, cx - 1.0f * m_scale, cy + 3.0f * m_scale, cx + 4.5f * m_scale,
|
||||
cy - 4.0f * m_scale, markerColor, 1.8f * m_scale);
|
||||
}
|
||||
|
||||
const float textLeft = rect.X + kItemPad * m_scale;
|
||||
const float textWidth =
|
||||
rect.Width - 2.0f * kItemPad * m_scale - kMarkerColumn * m_scale - kMarkerGap * m_scale;
|
||||
UiText(g, UiEllipsize(item.label, m_fonts->uiSmall, textWidth), m_fonts->uiSmall,
|
||||
gd::RectF(textLeft, rect.Y, std::max(8.0f, textWidth), rect.Height), textColor,
|
||||
TextAlign::LEFT);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace jdi
|
||||
Reference in new issue
Block a user