#include "PopupMenu.hpp" #include #include 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; /**< 色块间距。 */ const float kEditFieldWidth = 88.0f; /**< EDIT 项输入框宽度。 */ const float kEditFieldGap = 6.0f; /**< 说明文字与输入框之间的最小间隔。 */ const float kEditFieldPadV = 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::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) { m_itemRects.assign(m_items.size(), gd::RectF()); m_swatchRects.assign(m_items.size(), std::vector()); 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(count) * kSwatchSize * m_scale + static_cast(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); } else if (item.kind == MenuItemKind::EDIT) { // 说明文字 + 输入框 + 右侧留白;输入框宽度按数值文本自适应(取一个上限)。 const float fieldWidth = std::max(kEditFieldWidth * m_scale, UiMeasureTextWidth(m_fonts->uiSmall, item.value) + 20.0f * m_scale); const float width = (kPadH + 8.0f) * m_scale + UiMeasureTextWidth(m_fonts->uiSmall, item.label) + kEditFieldGap * m_scale + fieldWidth + 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(x); float top = static_cast(y); // 优先向右下展开;越界时向左或向上翻转,最后做边界收敛。 if (left + width > bounds.X + bounds.Width) { left = static_cast(x) - width; } if (top + height > bounds.Y + bounds.Height) { top = static_cast(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& rects = m_swatchRects[index]; rects.reserve(static_cast(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(swatch) * (kSwatchSize + kSwatchGap) * m_scale; rects.push_back(gd::RectF(swatchX, swatchY, kSwatchSize * m_scale, kSwatchSize * m_scale)); } } cursorY += itemHeight; } } gd::RectF PopupMenu::EditFieldRect(int index) const { if (index < 0 || index >= static_cast(m_items.size())) { return gd::RectF(); } if (m_items[static_cast(index)].kind != MenuItemKind::EDIT) { return gd::RectF(); } // 输入框靠右对齐:宽度取固定下限与当前文本宽度中的较大者,但不得超过菜单可用宽度的一半。 const gd::RectF& rect = m_itemRects[static_cast(index)]; const float maxWidth = std::max(40.0f * m_scale, rect.Width * 0.5f); float width = std::max(kEditFieldWidth * m_scale, UiMeasureTextWidth(m_fonts->uiSmall, m_items[static_cast(index)].value) + 20.0f * m_scale); width = std::min(width, maxWidth); const float height = rect.Height - kEditFieldPadV * 2.0f * m_scale; return gd::RectF(rect.X + rect.Width - width - 4.0f * m_scale, rect.Y + (rect.Height - height) * 0.5f, width, height); } int PopupMenu::EditingItemId() const { if (m_editIndex < 0 || m_editIndex >= static_cast(m_items.size())) { return 0; } return m_items[static_cast(m_editIndex)].id; } 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(x), static_cast(y)); } bool PopupMenu::BeginItemEdit(int index) { if (!m_open || index < 0 || index >= static_cast(m_items.size())) { return false; } const MenuItem& item = m_items[static_cast(index)]; if (item.kind != MenuItemKind::EDIT || !item.enabled) { return false; } // 输入框初值取当前显示值,整段选中,便于直接键入新数值。 m_edit.Begin(item.value, 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(m_items.size())) { return true; } MenuItem& item = m_items[static_cast(index)]; // 立即回写显示值,使菜单上的数值与数据层一致(宿主会在派发后再纠正为夹取后的真实值)。 item.value = 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; } BuildLayout(m_buildBounds, m_buildX, m_buildY); } bool PopupMenu::UpdateItemValue(int id, const std::wstring& value) { for (MenuItem& item : m_items) { if (item.id == id && item.kind == MenuItemKind::EDIT) { if (item.value == value) { return true; } item.value = value; 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; } bool PopupMenu::OnEditMouseDown(int x, int y) { if (m_editIndex < 0) { return false; } return UiEditFieldMouseDown(m_fonts->uiSmall, EditFieldRect(m_editIndex), m_edit, m_scale, x, y); } bool PopupMenu::OnEditMouseMove(int x) { if (m_editIndex < 0) { return false; } return UiEditFieldMouseMove(m_fonts->uiSmall, EditFieldRect(m_editIndex), m_edit, m_scale, x); } bool PopupMenu::OnEditMouseUp() { if (!m_edit.Dragging()) { return false; } m_edit.EndDrag(); return true; } bool PopupMenu::OnChar(wchar_t character, MenuResult& result) { result = MenuResult(); if (m_editIndex < 0) { return false; } // 剪贴板与全选快捷键经 WM_CHAR 送达(Ctrl+C/X/V/A 分别对应 3/24/22/1)。 switch (character) { case 0x03: // Ctrl+C m_edit.CopyToClipboard(m_host); return false; case 0x18: // Ctrl+X return m_edit.CutToClipboard(m_host); case 0x16: // Ctrl+V return m_edit.PasteFromClipboard(m_host); case 0x01: // Ctrl+A m_edit.SelectAll(); return true; default: break; } if (character == L'\r' || character == L'\n') { // 回车提交:菜单保持打开,仅把新数值回传宿主。 EndItemEdit(true, result); return true; } // 退格/删除已由 WM_KEYDOWN 处理,此处忽略控制字符避免重复删除。 if (character < 0x20 || character == 0x7F) { return false; } return m_edit.Insert(character); } void PopupMenu::Open(MenuScope scope, const gd::RectF& bounds, int x, int y, const std::vector& 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; m_buildBounds = bounds; m_buildX = x; m_buildY = y; CloseEditState(); 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; CloseEditState(); } void PopupMenu::CloseEditState() { if (m_editIndex >= 0) { m_edit.End(); } m_editIndex = -1; } bool PopupMenu::ContainsPoint(int x, int y) const { if (!m_open) { return false; } return UiPointInRect(m_rect, static_cast(x), static_cast(y)); } int PopupMenu::ItemAtPoint(int x, int y, int& swatchIndex) const { swatchIndex = -1; if (!m_open) { return -1; } const float fx = static_cast(x); const float fy = static_cast(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& rects = m_swatchRects[index]; for (std::size_t swatch = 0; swatch < rects.size(); ++swatch) { if (UiPointInRect(rects[swatch], fx, fy)) { swatchIndex = static_cast(swatch); return static_cast(index); } } // 落在色块行的空白处:整行视为可点击,用于定位提示。 return -1; } if (!item.enabled) { return -1; } return static_cast(index); } return -1; } void PopupMenu::MoveHover(int step) { if (m_items.empty()) { return; } const int count = static_cast(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(index)]; if ((item.kind == MenuItemKind::NORMAL || item.kind == MenuItemKind::EDIT) && item.enabled) { m_hoverIndex = index; m_hoverSwatch = -1; return; } } } bool PopupMenu::CommitHover(MenuResult& result) { if (m_hoverIndex < 0 || m_hoverIndex >= static_cast(m_items.size())) { return false; } const MenuItem& item = m_items[static_cast(m_hoverIndex)]; if (!item.enabled) { return false; } // 编辑项:回车等价于单击进入编辑,菜单不关闭。 if (item.kind == MenuItemKind::EDIT) { BeginItemEdit(m_hoverIndex); return false; } if (item.kind != MenuItemKind::NORMAL) { return false; } result.valid = true; result.scope = m_scope; result.id = item.id; result.swatchIndex = -1; result.itemRect = m_itemRects[static_cast(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)) { // 点击菜单外部:先提交正在编辑的数值,再关闭菜单。 EndItemEdit(true, result); Close(); return true; } // 已经处于编辑状态时:点击当前输入框 → 重新定位插入符; // 点击其它位置 → 先提交本次编辑(菜单不关闭),再按新位置继续处理。 if (m_editIndex >= 0) { if (IsOnEditField(x, y)) { OnEditMouseDown(x, y); m_pressedIndex = m_editIndex; m_pressedSwatch = -1; return true; } EndItemEdit(true, result); } int swatchIndex = -1; m_pressedIndex = ItemAtPoint(x, y, swatchIndex); m_pressedSwatch = swatchIndex; return true; } bool PopupMenu::OnLButtonUp(int x, int y, MenuResult& result) { const MenuResult editResult = result; result = MenuResult(); if (!m_open) { return false; } // 编辑态下松开:先结束拖拽框选,不当作普通项点击。 if (m_editIndex >= 0) { OnEditMouseUp(); m_pressedIndex = -1; m_pressedSwatch = -1; result = editResult; return true; } 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(index)]; // 编辑项:单击进入菜单内联编辑,菜单保持打开。 if (item.kind == MenuItemKind::EDIT) { BeginItemEdit(index); return true; } result.valid = true; result.scope = m_scope; result.id = item.id; result.swatchIndex = -1; result.itemRect = m_itemRects[static_cast(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; } // 编辑态下按键全部交给输入框(回车提交、Esc 取消本次编辑但菜单保持打开)。 if (m_editIndex >= 0) { const bool shift = (GetKeyState(VK_SHIFT) & 0x8000) != 0; switch (key) { case VK_ESCAPE: EndItemEdit(false, result); return true; case VK_RETURN: EndItemEdit(true, result); return true; case VK_BACK: m_edit.Backspace(); return true; case VK_DELETE: m_edit.DeleteForward(); return true; case VK_LEFT: m_edit.MoveCaret(-1, shift); return true; case VK_RIGHT: m_edit.MoveCaret(1, shift); return true; case VK_HOME: m_edit.MoveHome(shift); return true; case VK_END: m_edit.MoveEnd(shift); return true; default: break; } return true; } 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(index) == m_hoverIndex) && (item.kind != MenuItemKind::SWATCHES || m_hoverSwatch < 0); if (item.kind == MenuItemKind::EDIT) { // 编辑项:左侧说明文字 + 右侧内联输入框(与拾色器的 RGB 编辑同一种处理方式, // 菜单始终不关闭,数值就地修改)。 if (hovered && item.enabled && static_cast(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 + 8.0f * m_scale; const gd::RectF fieldRect = EditFieldRect(static_cast(index)); const float labelWidth = std::max(8.0f, fieldRect.X - labelLeft - kEditFieldGap * m_scale); UiText(g, UiEllipsize(g, item.label, m_fonts->uiSmall, labelWidth), m_fonts->uiSmall, gd::RectF(labelLeft, rect.Y, labelWidth, rect.Height), labelColor, TextAlign::LEFT); if (static_cast(index) == m_editIndex) { UiDrawEditField(g, fieldRect, m_edit, m_fonts->uiSmall, m_scale); } 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(g, 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) { const std::vector& 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(index) == m_hoverIndex) && (static_cast(swatch) == m_hoverSwatch); if (static_cast(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(swatch) == item.checkedSwatch) ? m_theme->textStrong : m_theme->borderStrong, (static_cast(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(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