feat: 重构信号样式区并统一主题化弹出菜单

样式编辑区:
- 移除顶部「样式 + 信号名 + 文件名」标题行,选中信号即代表编辑目标。
- 绘制方式取消阶梯选项,仅保留线段/散点/点线,标签由「方式」改为「样式」。
- 线宽改为「最细预设 + 数值旋钮 + 最粗预设」:中间为带左右增减箭头的输入框,
  步进 0.2px,取值范围 0.5-6.0px,并标注单位为 px。
- 颜色在 10 个预设色块后新增带加号图标的方格,点击展开 RGB 三通道滑块面板,
  可实时预览并显示十六进制值;点击面板外任意位置收起。
- 移除网格归属下拉框,改由每条信号行末尾的 G 徽标承载,点击弹出网格归属菜单。

信号面板与菜单:
- 文件分组行新增关闭按钮,可直接从工作区移除该文件,与整体风格保持一致。
- 新增 PopupMenu 组件:绘制在窗口后备缓冲区内的自绘菜单,含圆角、投影、悬停高亮、
  单选对勾、色块行与分组标题,彻底替换系统默认弹出菜单;
  菜单打开期间接管鼠标与键盘(Esc / 上下键 / 回车),失焦或在菜单外释放时自动关闭。
- 信号、文件、网格、标签页四类右键菜单全部改用该组件。

其他修复:
- 信号名称不再保留尾部单位括号,避免名称后再重复显示单位。
- 数据模型移除 DrawMode::STEP 枚举。
- 标签栏与信号面板弹出菜单后补充重绘请求,修复菜单不显示的问题。
This commit is contained in:
DeepSeek V4.1 Flash committed 2026-09-29 16:30:48 +08:00
1 parent 9c6c374cce
commit eb868b2679
12 files changed
+2148 -531

No files matched your search

+1 -2
View File
@@ -18,8 +18,7 @@ namespace jdi
{
LINE = 0, /**< 相邻采样点之间以插值直线连接。 */
SCATTER = 1, /**< 仅绘制采样点散点。 */
LINE_SCATTER = 2, /**< 同时绘制插值直线与采样点散点。 */
STEP = 3 /**< 阶梯保持(零阶保持)连线。 */
LINE_SCATTER = 2 /**< 同时绘制插值直线与采样点散点。 */
};
/**
+66
View File
@@ -23,6 +23,17 @@ namespace jdi
RIGHT = 2 /**< 右对齐。 */
};
/**
* @brief 图标朝向。
*/
enum class IconDirection : int
{
LEFT = 0, /**< 指向左侧。 */
RIGHT = 1, /**< 指向右侧。 */
UP = 2, /**< 指向上方。 */
DOWN = 3 /**< 指向下方。 */
};
/**
* @brief 按 DPI 创建并缓存整套界面字体(GDI 字体句柄)。
*/
@@ -166,6 +177,61 @@ namespace jdi
*/
std::wstring UiEllipsize(gd::Graphics& g, const std::wstring& text, HFONT font, float maxWidth);
/**
* @brief 在矩形中心绘制一个加号图标。
* @param [IN,OUT] g 绘图上下文。
* @param [IN] rect 图标布局矩形。
* @param [IN] argb 线条颜色。
* @param [IN] strokeWidth 线条宽度。
*/
void UiDrawPlusIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth);
/**
* @brief 在矩形中心绘制一个叉号(关闭)图标。
* @param [IN,OUT] g 绘图上下文。
* @param [IN] rect 图标布局矩形。
* @param [IN] argb 线条颜色。
* @param [IN] strokeWidth 线条宽度。
*/
void UiDrawCrossIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth);
/**
* @brief 在矩形中心绘制一个箭头(折线形)图标。
* @param [IN,OUT] g 绘图上下文。
* @param [IN] rect 图标布局矩形。
* @param [IN] argb 线条颜色。
* @param [IN] strokeWidth 线条宽度。
* @param [IN] direction 箭头朝向。
*/
void UiDrawArrowIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth,
IconDirection direction);
/**
* @brief 在矩形中心绘制一个实心三角形指示器。
* @param [IN,OUT] g 绘图上下文。
* @param [IN] rect 图标布局矩形。
* @param [IN] argb 填充颜色。
* @param [IN] direction 三角形朝向。
*/
void UiDrawTriangleIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, IconDirection direction);
/**
* @brief 将滑块轨道上的横坐标换算为取值比例。
* @param [IN] trackRect 轨道矩形。
* @param [IN] x 横坐标。
* @return 取值比例,范围 0-1。
*/
double UiSliderRatioFromX(const gd::RectF& trackRect, float x);
/**
* @brief 绘制一个带边框的复选框。
* @param [IN,OUT] g 绘图上下文。
* @param [IN] rect 复选框矩形。
* @param [IN] state 选中状态:0 未选,1 全选,2 部分选。
* @param [IN] radius 圆角半径。
*/
void UiDrawCheckBox(gd::Graphics& g, const gd::RectF& rect, int state, float radius);
} // namespace jdi
#endif // __PAINT_UTIL_HPP__
+222
View File
@@ -0,0 +1,222 @@
#ifndef __POPUP_MENU_HPP__
#define __POPUP_MENU_HPP__
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
#include <cstdint>
#include <string>
#include <vector>
#include "PaintUtil.hpp"
#include "Theme.hpp"
namespace jdi
{
/**
* @brief 弹出菜单的打开来源,用于把选择结果派发回正确的组件。
*/
enum class MenuScope : int
{
NONE = 0, /**< 无来源。 */
SIGNAL_PANEL = 1, /**< 信号面板。 */
TAB_BAR = 2 /**< 标签栏。 */
};
/**
* @brief 弹出菜单项的显示类型。
*/
enum class MenuItemKind : int
{
NORMAL = 0, /**< 普通可点击项。 */
SEPARATOR = 1, /**< 分隔线。 */
HEADER = 2, /**< 分组标题(不可点击)。 */
SWATCHES = 3 /**< 颜色色块行。 */
};
/**
* @brief 单个弹出菜单项。
*/
struct MenuItem
{
MenuItemKind kind = MenuItemKind::NORMAL; /**< 项目类型。 */
std::wstring label; /**< 显示文本。 */
int id = 0; /**< 命令标识。 */
bool enabled = true; /**< 是否可用。 */
bool checked = false; /**< 是否处于选中(单选)状态。 */
int swatchCount = 0; /**< 色块数量(仅色块行有效)。 */
std::uint32_t swatches[16] = {}; /**< 色块颜色。 */
int checkedSwatch = -1; /**< 被选中的色块下标。 */
};
/**
* @brief 菜单选择结果。
*/
struct MenuResult
{
bool valid = false; /**< 是否产生了有效选择。 */
MenuScope scope = MenuScope::NONE; /**< 来源组件。 */
int id = 0; /**< 被选中项的命令标识。 */
int swatchIndex = -1; /**< 被选中的色块下标(非色块行为 -1)。 */
};
/**
* @brief 与界面主题一致的自绘弹出菜单。
*
* 菜单不创建独立窗口,而是作为宿主窗口绘制流程的最上层内容直接绘制到后备缓冲区,
* 因此外观与主界面完全统一,不会出现系统默认菜单样式。
*/
class PopupMenu
{
public:
PopupMenu();
/**
* @brief 绑定字体与主题资源。
* @param [IN] fonts 字体集合,需在菜单生命周期内保持有效。
* @param [IN] theme 主题配色,需在菜单生命周期内保持有效。
*/
void Setup(const UiFonts* fonts, const Theme* theme);
/**
* @brief 设置 DPI 缩放比例。
* @param [IN] scale 缩放比例(1.0 对应 96 DPI)。
*/
void SetScale(float scale);
/**
* @brief 在指定位置打开菜单。
* @param [IN] scope 来源组件标识。
* @param [IN] bounds 宿主客户区矩形,用于把菜单限制在窗口内。
* @param [IN] x 锚点横坐标(客户区坐标)。
* @param [IN] y 锚点纵坐标(客户区坐标)。
* @param [IN] items 菜单项集合。
*/
void Open(MenuScope scope, const gd::RectF& bounds, int x, int y, const std::vector<MenuItem>& items);
/**
* @brief 关闭菜单。
*/
void Close();
/**
* @brief 判断菜单是否处于打开状态。
* @return 打开返回 true。
*/
bool IsOpen() const { return m_open; }
/**
* @brief 获取当前菜单的来源组件标识。
* @return 来源标识。
*/
MenuScope Scope() const { return m_scope; }
/**
* @brief 绘制菜单(应在宿主绘制的最后一步调用)。
* @param [IN,OUT] g 绘图上下文。
*/
void Paint(gd::Graphics& g);
/**
* @brief 处理鼠标移动。
* @param [IN] x 客户区横坐标。
* @param [IN] y 客户区纵坐标。
* @return 需要重绘返回 true。
*/
bool OnMouseMove(int x, int y);
/**
* @brief 处理鼠标左键按下。
* @param [IN] x 客户区横坐标。
* @param [IN] y 客户区纵坐标。
* @param [OUT] result 在菜单外按下时返回被取消的选择结果。
* @return 事件已被菜单消费返回 true。
*/
bool OnLButtonDown(int x, int y, MenuResult& result);
/**
* @brief 处理鼠标左键抬起。
* @param [IN] x 客户区横坐标。
* @param [IN] y 客户区纵坐标。
* @param [OUT] result 选择结果。
* @return 事件已被菜单消费返回 true。
*/
bool OnLButtonUp(int x, int y, MenuResult& result);
/**
* @brief 处理键盘按键。
* @param [IN] key 虚拟键码。
* @param [OUT] result 选择结果。
* @return 事件已被菜单消费返回 true。
*/
bool OnKeyDown(WPARAM key, MenuResult& result);
/**
* @brief 判断坐标是否落在菜单内部。
* @param [IN] x 客户区横坐标。
* @param [IN] y 客户区纵坐标。
* @return 落在菜单内返回 true。
*/
bool ContainsPoint(int x, int y) const;
private:
/**
* @brief 计算单个菜单项的纵向高度。
* @param [IN] item 菜单项。
* @return 高度(像素)。
*/
float ItemHeight(const MenuItem& item) const;
/**
* @brief 依据当前项目集合计算整体矩形与各项矩形。
* @param [IN] bounds 宿主客户区矩形。
* @param [IN] x 锚点横坐标。
* @param [IN] y 锚点纵坐标。
*/
void BuildLayout(const gd::RectF& bounds, int x, int y);
/**
* @brief 查询坐标命中的菜单项下标。
* @param [IN] x 客户区横坐标。
* @param [IN] y 客户区纵坐标。
* @param [OUT] swatchIndex 命中的色块下标(非色块行为 -1)。
* @return 项目下标,未命中返回 -1。
*/
int ItemAtPoint(int x, int y, int& swatchIndex) const;
/**
* @brief 将悬停项移动到下一个可用项。
* @param [IN] step 步进(+1 向下,-1 向上)。
*/
void MoveHover(int step);
/**
* @brief 确认当前悬停项并生成结果。
* @param [OUT] result 选择结果。
* @return 生成了有效选择返回 true。
*/
bool CommitHover(MenuResult& result);
const UiFonts* m_fonts = nullptr; /**< 字体资源。 */
const Theme* m_theme = nullptr; /**< 主题资源。 */
float m_scale = 1.0f; /**< DPI 缩放比例。 */
bool m_open = false; /**< 是否处于打开状态。 */
MenuScope m_scope = MenuScope::NONE; /**< 来源组件标识。 */
std::vector<MenuItem> m_items; /**< 菜单项集合。 */
gd::RectF m_rect; /**< 菜单整体矩形。 */
std::vector<gd::RectF> m_itemRects; /**< 各项矩形。 */
std::vector<std::vector<gd::RectF>> m_swatchRects; /**< 各项内的色块矩形。 */
int m_hoverIndex = -1; /**< 当前悬停项。 */
int m_hoverSwatch = -1; /**< 当前悬停色块。 */
int m_pressedIndex = -1; /**< 按下时的项目下标。 */
int m_pressedSwatch = -1; /**< 按下时的色块下标。 */
};
} // namespace jdi
#endif // __POPUP_MENU_HPP__
+49 -8
View File
@@ -73,13 +73,12 @@ namespace jdi
}
/**
* @brief 从列名末尾的括号中提取单位(仅接受形如 mA / V / deg/s 的短标记)。
* @param [IN] header 原始列名。
* @brief 从名称末尾的括号中提取单位(仅接受形如 mA / V / deg/s 的短标记)。
* @param [IN] text 已剥离 "[n]" 前缀的名称。
* @return 单位文本,无法识别时返回空字符串。
*/
std::wstring ExtractUnit(const std::wstring& header)
std::wstring ExtractUnit(const std::wstring& text)
{
const std::wstring text = Trim(header);
if (text.size() < 3 || text.back() != L')')
{
return std::wstring();
@@ -110,6 +109,46 @@ namespace jdi
return inner;
}
/**
* @brief 从名称末尾移除已被识别为单位(或描述)的括号片段。
* @param [IN] name 已剥离 "[n]" 前缀的名称。
* @return 移除尾部括号后的名称(括号片段非单位时原样返回)。
*/
std::wstring StripBracketSuffix(const std::wstring& name)
{
if (name.size() < 3 || name.back() != L')')
{
return name;
}
const std::size_t open = name.rfind(L'(');
if (open == std::wstring::npos || open == 0 || open + 2 >= name.size())
{
return name;
}
const std::wstring inner = Trim(name.substr(open + 1, name.size() - open - 2));
if (inner.empty() || inner.size() > 10)
{
return name;
}
// 仅当括号内容为短单位标记时才剥离,避免误伤 "(5 Points)" 这类描述。
for (const wchar_t c : inner)
{
const bool allowed = iswalpha(c) || iswdigit(c) || c == L'%' || c == L'/' || c == L'^' ||
c == L'-' || c == L'.' || c == L'\u00B0' || c == L'\u00B5' ||
c == L'\u03A9' || c == L'\u00B2' || c == L'\u00B3' || c == L'\u00B7';
if (!allowed)
{
return name;
}
}
const std::wstring head = Trim(name.substr(0, open));
return head.empty() ? name : head;
}
/**
* @brief 依据全局信号序号从调色板取色,保证跨标签页配色一致。
* @param [IN] globalIndex 全局信号序号。
@@ -291,8 +330,6 @@ namespace jdi
return L"散点";
case DrawMode::LINE_SCATTER:
return L"点线";
case DrawMode::STEP:
return L"阶梯";
default:
return L"线段";
}
@@ -383,8 +420,12 @@ namespace jdi
SignalData signal;
signal.rawHeader = table.headers[column];
signal.name = MakeDisplayName(signal.rawHeader);
signal.unit = ExtractUnit(signal.rawHeader);
{
// 名称与单位分开保存:名称不再保留尾部单位括号,避免重复显示。
const std::wstring baseName = MakeDisplayName(signal.rawHeader);
signal.unit = ExtractUnit(baseName);
signal.name = StripBracketSuffix(baseName);
}
signal.values.resize(rowCount);
double minValue = std::numeric_limits<double>::infinity();
+132
View File
@@ -288,4 +288,136 @@ namespace jdi
return ellipsis;
}
void UiDrawPlusIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth)
{
const float cx = rect.X + rect.Width * 0.5f;
const float cy = rect.Y + rect.Height * 0.5f;
const float arm = std::min(rect.Width, rect.Height) * 0.3f;
UiDrawLine(g, cx - arm, cy, cx + arm, cy, argb, strokeWidth);
UiDrawLine(g, cx, cy - arm, cx, cy + arm, argb, strokeWidth);
}
void UiDrawCrossIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth)
{
const float cx = rect.X + rect.Width * 0.5f;
const float cy = rect.Y + rect.Height * 0.5f;
const float arm = std::min(rect.Width, rect.Height) * 0.3f;
UiDrawLine(g, cx - arm, cy - arm, cx + arm, cy + arm, argb, strokeWidth);
UiDrawLine(g, cx + arm, cy - arm, cx - arm, cy + arm, argb, strokeWidth);
}
void UiDrawArrowIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, float strokeWidth,
IconDirection direction)
{
const float cx = rect.X + rect.Width * 0.5f;
const float cy = rect.Y + rect.Height * 0.5f;
const float arm = std::min(rect.Width, rect.Height) * 0.26f;
switch (direction)
{
case IconDirection::LEFT:
UiDrawLine(g, cx + arm * 0.6f, cy - arm, cx - arm * 0.6f, cy, argb, strokeWidth);
UiDrawLine(g, cx - arm * 0.6f, cy, cx + arm * 0.6f, cy + arm, argb, strokeWidth);
break;
case IconDirection::RIGHT:
UiDrawLine(g, cx - arm * 0.6f, cy - arm, cx + arm * 0.6f, cy, argb, strokeWidth);
UiDrawLine(g, cx + arm * 0.6f, cy, cx - arm * 0.6f, cy + arm, argb, strokeWidth);
break;
case IconDirection::UP:
UiDrawLine(g, cx - arm, cy + arm * 0.6f, cx, cy - arm * 0.6f, argb, strokeWidth);
UiDrawLine(g, cx, cy - arm * 0.6f, cx + arm, cy + arm * 0.6f, argb, strokeWidth);
break;
case IconDirection::DOWN:
default:
UiDrawLine(g, cx - arm, cy - arm * 0.6f, cx, cy + arm * 0.6f, argb, strokeWidth);
UiDrawLine(g, cx, cy + arm * 0.6f, cx + arm, cy - arm * 0.6f, argb, strokeWidth);
break;
}
}
void UiDrawTriangleIcon(gd::Graphics& g, const gd::RectF& rect, std::uint32_t argb, IconDirection direction)
{
const float cx = rect.X + rect.Width * 0.5f;
const float cy = rect.Y + rect.Height * 0.5f;
const float arm = std::min(rect.Width, rect.Height) * 0.28f;
gd::PointF points[3];
switch (direction)
{
case IconDirection::LEFT:
points[0] = gd::PointF(cx + arm * 0.7f, cy - arm);
points[1] = gd::PointF(cx - arm * 0.7f, cy);
points[2] = gd::PointF(cx + arm * 0.7f, cy + arm);
break;
case IconDirection::RIGHT:
points[0] = gd::PointF(cx - arm * 0.7f, cy - arm);
points[1] = gd::PointF(cx + arm * 0.7f, cy);
points[2] = gd::PointF(cx - arm * 0.7f, cy + arm);
break;
case IconDirection::UP:
points[0] = gd::PointF(cx - arm, cy + arm * 0.7f);
points[1] = gd::PointF(cx, cy - arm * 0.7f);
points[2] = gd::PointF(cx + arm, cy + arm * 0.7f);
break;
case IconDirection::DOWN:
default:
points[0] = gd::PointF(cx - arm, cy - arm * 0.7f);
points[1] = gd::PointF(cx, cy + arm * 0.7f);
points[2] = gd::PointF(cx + arm, cy - arm * 0.7f);
break;
}
gd::SolidBrush brush(ToGdiColor(argb));
g.FillPolygon(&brush, points, 3);
}
double UiSliderRatioFromX(const gd::RectF& trackRect, float x)
{
if (trackRect.Width <= 0.0f)
{
return 0.0;
}
const double ratio = static_cast<double>(x - trackRect.X) / static_cast<double>(trackRect.Width);
if (ratio < 0.0)
{
return 0.0;
}
if (ratio > 1.0)
{
return 1.0;
}
return ratio;
}
void UiDrawCheckBox(gd::Graphics& g, const gd::RectF& rect, int state, float radius)
{
const Theme& theme = SolarizedLight();
const float centerY = rect.Y + rect.Height * 0.5f;
if (state == 1)
{
UiFillRoundRect(g, rect, radius, theme.accent);
gd::Pen pen(ToGdiColor(theme.textOnAccent), std::max(1.4f, rect.Height * 0.11f));
pen.SetStartCap(gd::LineCapRound);
pen.SetEndCap(gd::LineCapRound);
g.DrawLine(&pen, rect.X + rect.Width * 0.22f, centerY, rect.X + rect.Width * 0.42f,
rect.Y + rect.Height * 0.72f);
g.DrawLine(&pen, rect.X + rect.Width * 0.42f, rect.Y + rect.Height * 0.72f,
rect.X + rect.Width * 0.78f, rect.Y + rect.Height * 0.28f);
}
else if (state == 2)
{
UiFillRoundRect(g, rect, radius, theme.accentSoft);
UiDrawRoundRect(g, rect, radius, theme.accent, 1.0f);
UiFillRect(g, gd::RectF(rect.X + rect.Width * 0.22f, centerY - rect.Height * 0.08f,
rect.Width * 0.56f, rect.Height * 0.16f),
theme.accent);
}
else
{
UiFillRoundRect(g, rect, radius, theme.cardBg);
UiDrawRoundRect(g, rect, radius, theme.borderStrong, 1.0f);
}
}
} // namespace jdi
+509
View File
@@ -0,0 +1,509 @@
#include "PopupMenu.hpp"
#include <algorithm>
#include <cmath>
namespace jdi
{
namespace
{
const float kRowHeight = 27.0f; /**< 普通项高度。 */
const float kHeaderHeight = 22.0f; /**< 分组标题高度。 */
const float kSeparatorHeight = 9.0f; /**< 分隔线高度。 */
const float kSwatchRowHeight = 32.0f; /**< 色块行高度。 */
const float kPadV = 5.0f; /**< 菜单上下内边距。 */
const float kPadH = 6.0f; /**< 菜单左右内边距。 */
const float kMarkerColumn = 22.0f; /**< 选中标记列宽度。 */
const float kRightPad = 14.0f; /**< 文本右侧留白。 */
const float kMinWidth = 148.0f; /**< 菜单最小宽度。 */
const float kMaxWidth = 340.0f; /**< 菜单最大宽度。 */
const float kCorner = 6.0f; /**< 菜单圆角半径。 */
const float kSwatchSize = 22.0f; /**< 色块尺寸。 */
const float kSwatchGap = 5.0f; /**< 色块间距。 */
}
PopupMenu::PopupMenu() = default;
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:
return kHeaderHeight * m_scale;
case MenuItemKind::SWATCHES:
return kSwatchRowHeight * m_scale;
case MenuItemKind::NORMAL:
default:
return kRowHeight * m_scale;
}
}
void PopupMenu::BuildLayout(const gd::RectF& bounds, int x, int y)
{
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;
for (std::size_t index = 0; index < m_items.size(); ++index)
{
const MenuItem& item = m_items[index];
totalHeight += ItemHeight(item);
if (item.kind == MenuItemKind::SWATCHES)
{
const int count = std::max(0, std::min(item.swatchCount, 16));
const float width = kPadH * 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 + 8.0f) * m_scale +
UiMeasureTextWidth(m_fonts->uiSmallBold, item.label) + kRightPad * m_scale;
contentWidth = std::max(contentWidth, width);
}
else if (item.kind == MenuItemKind::NORMAL)
{
const float width = (kPadH + kMarkerColumn) * m_scale +
UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kRightPad * m_scale;
contentWidth = std::max(contentWidth, width);
}
}
contentWidth = std::min(contentWidth, kMaxWidth * m_scale);
const float width = contentWidth;
const float height = totalHeight;
float left = static_cast<float>(x);
float top = static_cast<float>(y);
// 优先向右下展开;越界时向左或向上翻转,最后做边界收敛。
if (left + width > bounds.X + bounds.Width)
{
left = static_cast<float>(x) - width;
}
if (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);
float cursorY = top + kPadV * m_scale;
for (std::size_t index = 0; index < m_items.size(); ++index)
{
const MenuItem& item = m_items[index];
const float itemHeight = ItemHeight(item);
m_itemRects[index] = gd::RectF(left + kPadH * m_scale, cursorY,
std::max(0.0f, width - kPadH * 2.0f * m_scale), 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 = cursorY + (itemHeight - kSwatchSize * m_scale) * 0.5f;
const float startX = left + (kPadH + 4.0f) * 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;
}
}
void PopupMenu::Open(MenuScope scope, const gd::RectF& bounds, int x, int y,
const std::vector<MenuItem>& items)
{
m_items = items;
if (m_items.empty())
{
Close();
return;
}
m_scope = scope;
m_open = true;
m_hoverIndex = -1;
m_hoverSwatch = -1;
m_pressedIndex = -1;
m_pressedSwatch = -1;
BuildLayout(bounds, x, y);
}
void PopupMenu::Close()
{
m_open = false;
m_scope = MenuScope::NONE;
m_items.clear();
m_itemRects.clear();
m_swatchRects.clear();
m_hoverIndex = -1;
m_hoverSwatch = -1;
m_pressedIndex = -1;
m_pressedSwatch = -1;
}
bool PopupMenu::ContainsPoint(int x, int y) const
{
if (!m_open)
{
return false;
}
return UiPointInRect(m_rect, static_cast<float>(x), static_cast<float>(y));
}
int PopupMenu::ItemAtPoint(int x, int y, int& swatchIndex) const
{
swatchIndex = -1;
if (!m_open)
{
return -1;
}
const float fx = static_cast<float>(x);
const float fy = static_cast<float>(y);
for (std::size_t index = 0; index < m_items.size(); ++index)
{
const MenuItem& item = m_items[index];
if (item.kind == MenuItemKind::SEPARATOR || item.kind == MenuItemKind::HEADER)
{
continue;
}
if (!UiPointInRect(m_itemRects[index], fx, fy))
{
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)
{
if (UiPointInRect(rects[swatch], fx, fy))
{
swatchIndex = static_cast<int>(swatch);
return static_cast<int>(index);
}
}
// 落在色块行的空白处:整行视为可点击,用于定位提示。
return -1;
}
if (!item.enabled)
{
return -1;
}
return static_cast<int>(index);
}
return -1;
}
void PopupMenu::MoveHover(int step)
{
if (m_items.empty())
{
return;
}
const int count = static_cast<int>(m_items.size());
int index = m_hoverIndex;
for (int attempt = 0; attempt < count; ++attempt)
{
index += step;
if (index < 0)
{
index = count - 1;
}
else if (index >= count)
{
index = 0;
}
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
if (item.kind == MenuItemKind::NORMAL && item.enabled)
{
m_hoverIndex = index;
m_hoverSwatch = -1;
return;
}
}
}
bool PopupMenu::CommitHover(MenuResult& result)
{
if (m_hoverIndex < 0 || m_hoverIndex >= static_cast<int>(m_items.size()))
{
return false;
}
const MenuItem& item = m_items[static_cast<std::size_t>(m_hoverIndex)];
if (item.kind != MenuItemKind::NORMAL || !item.enabled)
{
return false;
}
result.valid = true;
result.scope = m_scope;
result.id = item.id;
result.swatchIndex = -1;
return true;
}
bool PopupMenu::OnMouseMove(int x, int y)
{
if (!m_open)
{
return false;
}
int swatchIndex = -1;
const int index = ItemAtPoint(x, y, swatchIndex);
if (index == m_hoverIndex && swatchIndex == m_hoverSwatch)
{
return false;
}
m_hoverIndex = index;
m_hoverSwatch = swatchIndex;
return true;
}
bool PopupMenu::OnLButtonDown(int x, int y, MenuResult& result)
{
result = MenuResult();
if (!m_open)
{
return false;
}
if (!ContainsPoint(x, y))
{
// 点击菜单外部:关闭菜单,不产生任何命令。
Close();
return true;
}
int swatchIndex = -1;
m_pressedIndex = ItemAtPoint(x, y, swatchIndex);
m_pressedSwatch = swatchIndex;
return true;
}
bool PopupMenu::OnLButtonUp(int x, int y, MenuResult& result)
{
result = MenuResult();
if (!m_open)
{
return false;
}
int swatchIndex = -1;
const int index = ItemAtPoint(x, y, swatchIndex);
const int pressedIndex = m_pressedIndex;
const int pressedSwatch = m_pressedSwatch;
m_pressedIndex = -1;
m_pressedSwatch = -1;
// 在菜单之外释放鼠标时视为取消操作。
if (!ContainsPoint(x, y))
{
Close();
return true;
}
if (index < 0 || index != pressedIndex)
{
return true;
}
const MenuItem& item = m_items[static_cast<std::size_t>(index)];
result.valid = true;
result.scope = m_scope;
result.id = item.id;
result.swatchIndex = -1;
if (item.kind == MenuItemKind::SWATCHES)
{
if (swatchIndex < 0 || swatchIndex != pressedSwatch)
{
result.valid = false;
return true;
}
result.swatchIndex = swatchIndex;
}
Close();
return true;
}
bool PopupMenu::OnKeyDown(WPARAM key, MenuResult& result)
{
result = MenuResult();
if (!m_open)
{
return false;
}
switch (key)
{
case VK_ESCAPE:
Close();
return true;
case VK_DOWN:
MoveHover(1);
return true;
case VK_UP:
MoveHover(-1);
return true;
case VK_RETURN:
{
if (CommitHover(result))
{
Close();
}
return true;
}
default:
break;
}
return true;
}
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);
for (std::size_t index = 0; index < m_items.size(); ++index)
{
const MenuItem& item = m_items[index];
const gd::RectF& rect = m_itemRects[index];
if (item.kind == MenuItemKind::SEPARATOR)
{
const float lineY = rect.Y + rect.Height * 0.5f;
UiDrawLine(g, rect.X + 4.0f * m_scale, lineY, rect.X + rect.Width - 4.0f * 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 + 8.0f * m_scale, rect.Y, rect.Width, rect.Height),
m_theme->textMuted, TextAlign::LEFT);
continue;
}
const bool hovered = (static_cast<int>(index) == m_hoverIndex) &&
(item.kind != MenuItemKind::SWATCHES || m_hoverSwatch < 0);
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 cx = rect.X + kMarkerColumn * m_scale * 0.5f + 2.0f * m_scale;
const float cy = rect.Y + rect.Height * 0.5f;
UiDrawLine(g, cx - 4.0f * m_scale, cy, cx - 1.0f * m_scale, cy + 3.0f * m_scale,
hovered ? m_theme->textOnAccent : m_theme->accent, 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, hovered ? m_theme->textOnAccent : m_theme->accent,
1.8f * m_scale);
}
const float textLeft = rect.X + kMarkerColumn * m_scale;
const float textWidth = rect.Width - kMarkerColumn * m_scale - 4.0f * m_scale;
UiText(g, UiEllipsize(g, 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