// 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(m_items.size()); const int scrollLast = scrollable ? m_pinnedIndex : static_cast(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(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(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(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(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; // 说明文字与普通项使用同一文字列(kItemPad),保证菜单内所有文本左对齐。 const float labelLeft = rect.X + kItemPad * m_scale; const gd::RectF fieldRect = EditFieldRect(static_cast(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(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& 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 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); // 滚动中的子菜单:条目必须裁剪在可视区内(否则会画到面板圆角之外)。 int subClipState = -1; if (m_subScrollEnabled) { subClipState = g.Save(); g.SetClip(gd::RectF(m_subRect.X + 1.0f * m_scale, m_subRect.Y + 1.0f * m_scale, m_subRect.Width - 2.0f * m_scale, m_subRect.Height - 2.0f * m_scale), gd::CombineModeIntersect); } 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(index) == m_subHoverIndex) && item.enabled; if (item.kind == MenuItemKind::EDIT) { // 子菜单中的编辑项:与主菜单一样就地编辑。 if (hovered && 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 + kItemPad * 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(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(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); } if (m_subScrollEnabled) { if (subClipState >= 0) { g.Restore(subClipState); } // 滚动条:轨道 + 滑块(与主菜单同一套配色与算法,粗细取 `kScrollBarWidth`)。 if (m_subScrollBarRect.Width > 0.0f && m_subScrollRegionHeight > 0.0f && m_subViewport.Height > 0.0f) { const float radius = kScrollBarWidth * m_scale * 0.5f; UiFillRoundRect(g, m_subScrollBarRect, radius, m_theme->gridWeak); const float viewportHeight = m_subViewport.Height; const float ratio = std::min(1.0f, viewportHeight / m_subScrollRegionHeight); const float thumbHeight = std::max(kScrollBarMinThumb * m_scale, m_subScrollBarRect.Height * ratio); const float maxScroll = std::max(0.0f, m_subScrollRegionHeight - viewportHeight); const float progress = (maxScroll > 0.0f) ? (m_subScrollOffset / maxScroll) : 0.0f; const float thumbTop = m_subScrollBarRect.Y + std::max(0.0f, std::min(1.0f, progress)) * std::max(0.0f, m_subScrollBarRect.Height - thumbHeight); UiFillRoundRect( g, gd::RectF(m_subScrollBarRect.X, thumbTop, m_subScrollBarRect.Width, thumbHeight), radius, m_theme->borderStrong); } } } } // namespace jdi