- 输入框:支持鼠标左键点击定位插入符、拖拽框选区域,并且不再限制字符长度 - 输入框:长文本使用 ETO_CLIPPED 裁剪绘制,避免字符溢出到编辑区域之外 - 图例:信号名称超过 200px 时省略号截断,不再挤压标尺区域 - 选中状态从 TabConfig 迁移到 DataSet,切换视图后信号选中状态保持一致 - 网格右键菜单:Y 轴最大值/最小值直接显示当前值,点击后原位置弹出内联编辑框 - 标尺数值改为绘制在图例下层,不再因避让图例而隐藏 - 移除已无用的 LegendBandRect,新增共享的编辑框布局与命中检测工具 - .gitignore 忽略 TestData 测试数据目录
512 lines
14 KiB
C++
512 lines
14 KiB
C++
#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;
|
|
result.itemRect = m_itemRects[static_cast<std::size_t>(m_hoverIndex)];
|
|
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;
|
|
result.itemRect = m_itemRects[static_cast<std::size_t>(index)];
|
|
|
|
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
|