#include "TabBar.hpp" #include #include #include "Utf8Util.hpp" namespace jdi { namespace { const float kTabHeight = 32.0f; /**< 标签高度(逻辑像素)。 */ const float kTopMargin = 4.0f; /**< 标签顶部留白。 */ const float kLeftPadding = 8.0f; /**< 左侧留白。 */ const float kTabPadding = 14.0f; /**< 标签文本左右内边距。 */ const float kCloseSize = 16.0f; /**< 关闭按钮尺寸。 */ const float kCloseGap = 6.0f; /**< 文本与关闭按钮间距。 */ const float kAddSize = 24.0f; /**< 新建按钮尺寸。 */ const float kAddGap = 6.0f; /**< 新建按钮与末个标签的间距。 */ const float kCorner = 5.0f; /**< 标签圆角半径。 */ const float kMaxTabWidth = 200.0f; /**< 单个标签最大宽度(逻辑像素)。 */ } TabBar::TabBar() = default; void TabBar::Setup(const UiFonts* fonts, const Theme* theme) { m_fonts = fonts; m_theme = theme; } void TabBar::SetScale(float scale) { m_scale = (scale > 0.1f) ? scale : 1.0f; } void TabBar::SetTabs(const std::vector& names) { m_tabs.clear(); m_tabs.resize(names.size()); for (std::size_t index = 0; index < names.size(); ++index) { m_tabs[index].name = names[index]; } if (m_activeIndex >= static_cast(m_tabs.size())) { m_activeIndex = m_tabs.empty() ? -1 : static_cast(m_tabs.size()) - 1; } m_hoverIndex = -1; m_hoverClose = false; } void TabBar::SetActiveIndex(int index) { m_activeIndex = index; } float TabBar::DesiredHeight() const { return (kTabHeight + kTopMargin) * m_scale; } void TabBar::Layout(const gd::RectF& area) { m_area = area; if (m_fonts == nullptr || m_fonts->ui == nullptr) { return; } const float tabHeight = kTabHeight * m_scale; const float top = area.Y + kTopMargin * m_scale; const float closeSize = kCloseSize * m_scale; const float maxWidth = kMaxTabWidth * m_scale; const float minWidth = 70.0f * m_scale; float cursorX = area.X + kLeftPadding * m_scale; for (TabItem& tab : m_tabs) { const float textWidth = UiMeasureTextWidth(m_fonts->ui, tab.name); float width = kTabPadding * 2.0f * m_scale + textWidth + kCloseGap * m_scale + closeSize; width = std::max(minWidth, std::min(width, maxWidth)); tab.rect = gd::RectF(cursorX, top, width, tabHeight); tab.closeRect = gd::RectF(cursorX + width - kTabPadding * m_scale - closeSize, top + (tabHeight - closeSize) * 0.5f, closeSize, closeSize); cursorX += width + kAddGap * m_scale; } const float addSize = kAddSize * m_scale; m_addRect = gd::RectF(cursorX, top + (tabHeight - addSize) * 0.5f, addSize, addSize); } void TabBar::Paint(gd::Graphics& g) { if (m_theme == nullptr || m_fonts == nullptr) { return; } UiFillRect(g, m_area, m_theme->panelBg); const float closeIconSize = 8.0f * m_scale; for (std::size_t index = 0; index < m_tabs.size(); ++index) { const TabItem& tab = m_tabs[index]; const bool active = (static_cast(index) == m_activeIndex); const bool hovered = (static_cast(index) == m_hoverIndex); std::uint32_t background = m_theme->panelBg; if (active) { background = m_theme->cardBg; } else if (hovered) { background = m_theme->hover; } UiFillRoundRect(g, tab.rect, kCorner * m_scale, background); if (!active) { UiDrawRoundRect(g, tab.rect, kCorner * m_scale, m_theme->border, 1.0f); } else { // 激活页使用顶部强调条指示。 UiFillRoundRect(g, gd::RectF(tab.rect.X + kCorner * m_scale, tab.rect.Y, tab.rect.Width - kCorner * 2.0f * m_scale, 2.5f * m_scale), 1.0f * m_scale, m_theme->accent); } const float textLeft = tab.rect.X + kTabPadding * m_scale; const float textWidth = tab.closeRect.X - kCloseGap * m_scale - textLeft; // 重命名编辑态:在标签本体之上叠加输入框(标签底色与边框仍然保留)。 if (static_cast(index) == m_editIndex) { const gd::RectF editRect(textLeft - 3.0f * m_scale, tab.rect.Y + 3.0f * m_scale, std::max(40.0f, textWidth + 6.0f * m_scale), tab.rect.Height - 6.0f * m_scale); UiDrawEditField(g, editRect, m_edit, m_fonts->ui, m_scale); } else { const std::wstring label = UiEllipsize(g, tab.name, m_fonts->ui, std::max(8.0f, textWidth)); UiText(g, label, active ? m_fonts->uiBold : m_fonts->ui, gd::RectF(textLeft, tab.rect.Y, std::max(8.0f, textWidth), tab.rect.Height), active ? m_theme->textStrong : m_theme->text, TextAlign::LEFT); } // 关闭按钮:悬停时显示底色,图标始终可见。 const bool closeHot = hovered && m_hoverClose; if (closeHot) { UiFillRoundRect(g, tab.closeRect, 3.0f * m_scale, m_theme->pressed); } const float cx = tab.closeRect.X + tab.closeRect.Width * 0.5f; const float cy = tab.closeRect.Y + tab.closeRect.Height * 0.5f; const std::uint32_t iconColor = closeHot ? m_theme->danger : (active ? m_theme->text : m_theme->textMuted); UiDrawLine(g, cx - closeIconSize * 0.5f, cy - closeIconSize * 0.5f, cx + closeIconSize * 0.5f, cy + closeIconSize * 0.5f, iconColor, 1.4f * m_scale); UiDrawLine(g, cx + closeIconSize * 0.5f, cy - closeIconSize * 0.5f, cx - closeIconSize * 0.5f, cy + closeIconSize * 0.5f, iconColor, 1.4f * m_scale); } // 拖拽排序时在插入位置绘制强调色指示条。 if (m_dragIndex >= 0 && m_dragMoved && !m_tabs.empty()) { const int drop = DropIndexAt(m_dragMouseX); float markerX = m_addRect.X; if (drop < static_cast(m_tabs.size())) { markerX = m_tabs[static_cast(drop)].rect.X - kAddGap * m_scale * 0.5f; } const float top = m_area.Y + kTopMargin * m_scale; const float bottom = top + kTabHeight * m_scale; UiFillRoundRect(g, gd::RectF(markerX - 1.5f * m_scale, top, 3.0f * m_scale, bottom - top), 1.5f * m_scale, m_theme->accent); } // 新建按钮。 const std::uint32_t addBg = m_hoverAdd ? m_theme->hover : m_theme->panelBg; UiFillRoundRect(g, m_addRect, kCorner * m_scale, addBg); UiDrawRoundRect(g, m_addRect, kCorner * m_scale, m_theme->border, 1.0f); const float ax = m_addRect.X + m_addRect.Width * 0.5f; const float ay = m_addRect.Y + m_addRect.Height * 0.5f; const float armLength = 4.5f * m_scale; const std::uint32_t addColor = m_hoverAdd ? m_theme->accent : m_theme->text; UiDrawLine(g, ax - armLength, ay, ax + armLength, ay, addColor, 1.6f * m_scale); UiDrawLine(g, ax, ay - armLength, ax, ay + armLength, addColor, 1.6f * m_scale); // 底部分隔线。 UiDrawLine(g, m_area.X, m_area.Y + m_area.Height - 1.0f, m_area.X + m_area.Width, m_area.Y + m_area.Height - 1.0f, m_theme->border, 1.0f); } bool TabBar::HitTest(int x, int y) const { return UiPointInRect(m_area, static_cast(x), static_cast(y)); } int TabBar::TabAtPoint(int x, int y) const { const float fx = static_cast(x); const float fy = static_cast(y); for (std::size_t index = 0; index < m_tabs.size(); ++index) { if (UiPointInRect(m_tabs[index].rect, fx, fy)) { return static_cast(index); } } return -1; } bool TabBar::OnMouseMove(int x, int y) { // 拖动排序中:只跟踪鼠标位置,由界面层绘制插入指示。 if (m_dragIndex >= 0) { if (x != m_dragMouseX) { m_dragMouseX = x; m_dragMoved = true; return true; } return false; } const int index = TabAtPoint(x, y); const bool overClose = (index >= 0 && UiPointInRect(m_tabs[static_cast(index)].closeRect, static_cast(x), static_cast(y))); const bool overAdd = UiPointInRect(m_addRect, static_cast(x), static_cast(y)); if (index == m_hoverIndex && overClose == m_hoverClose && overAdd == m_hoverAdd) { return false; } m_hoverIndex = index; m_hoverClose = overClose; m_hoverAdd = overAdd; return true; } bool TabBar::OnLButtonDown(int x, int y) { m_pressedIndex = -1; m_pressedClose = false; m_pressedAdd = false; // 编辑态下点击其它位置先提交重命名。 if (IsEditing()) { TabBarResult ignored; OnEditKey(VK_RETURN, ignored); } if (UiPointInRect(m_addRect, static_cast(x), static_cast(y))) { m_pressedAdd = true; return true; } const int index = TabAtPoint(x, y); if (index < 0) { return false; } m_pressedIndex = index; m_dragIndex = index; m_dragMouseX = x; m_pressedX = x; m_dragMoved = false; m_pressedClose = UiPointInRect(m_tabs[static_cast(index)].closeRect, static_cast(x), static_cast(y)); // 自行判定双击:不依赖系统 WM_LBUTTONDBLCLK,行为在不同环境下更一致。 const DWORD now = GetTickCount(); const bool sameTab = (index == m_lastClickIndex); const DWORD elapsed = now - m_lastClickTick; if (!m_pressedClose && sameTab && elapsed <= GetDoubleClickTime()) { m_lastClickIndex = -1; OnDoubleClick(x, y); return true; } m_lastClickIndex = m_pressedClose ? -1 : index; m_lastClickTick = now; return true; } bool TabBar::OnLButtonUp(int x, int y, TabBarResult& result) { result = TabBarResult(); if (m_pressedAdd) { m_pressedAdd = false; if (UiPointInRect(m_addRect, static_cast(x), static_cast(y))) { result.type = TabBarResult::Type::ADD; } return true; } if (m_pressedIndex < 0) { m_dragIndex = -1; return false; } const int index = m_pressedIndex; const bool wasClose = m_pressedClose; const bool dragged = m_dragMoved && (std::abs(x - m_pressedX) > 3); m_pressedIndex = -1; m_pressedClose = false; m_dragIndex = -1; m_dragMoved = false; // 拖动超过阈值:按释放位置重新排序。 if (dragged && !wasClose) { const int target = DropIndexAt(x); if (target >= 0 && target != index && target != index + 1) { result.type = TabBarResult::Type::REORDER; result.index = index; result.targetIndex = target; } return true; } if (TabAtPoint(x, y) != index) { return true; } if (wasClose) { result.type = TabBarResult::Type::CLOSE; result.index = index; } else { result.type = TabBarResult::Type::SELECT; result.index = index; } return true; } int TabBar::DropIndexAt(int x) const { const float fx = static_cast(x); for (std::size_t index = 0; index < m_tabs.size(); ++index) { const gd::RectF& rect = m_tabs[index].rect; if (fx < rect.X + rect.Width * 0.5f) { return static_cast(index); } } return static_cast(m_tabs.size()); } bool TabBar::OnDoubleClick(int x, int y) { const int index = TabAtPoint(x, y); if (index < 0) { return false; } // 双击关闭按钮不进入重命名。 if (UiPointInRect(m_tabs[static_cast(index)].closeRect, static_cast(x), static_cast(y))) { return false; } m_editIndex = index; m_edit.Begin(m_tabs[static_cast(index)].name, true, EditFilter::ANY, 48); return true; } bool TabBar::OnEditChar(wchar_t character) { 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') { TabBarResult result; OnEditKey(VK_RETURN, result); return true; } if (character == L'\b') { return m_edit.Backspace(); } if (character == 0x7F) { return m_edit.DeleteForward(); } if (character < L' ') { return false; } return m_edit.Insert(character); } bool TabBar::OnEditKey(WPARAM key, TabBarResult& result) { result = TabBarResult(); if (m_editIndex < 0) { return false; } const int index = m_editIndex; const bool shift = (GetKeyState(VK_SHIFT) & 0x8000) != 0; const bool control = (GetKeyState(VK_CONTROL) & 0x8000) != 0; switch (key) { case VK_RETURN: { const std::wstring text = Trim(m_edit.Text()); m_editIndex = -1; m_edit.End(); if (!text.empty()) { result.type = TabBarResult::Type::RENAME; result.index = index; result.text = text; } return true; } case VK_ESCAPE: m_editIndex = -1; m_edit.End(); return true; case VK_BACK: return m_edit.Backspace(); case VK_DELETE: return m_edit.DeleteForward(); case VK_LEFT: return m_edit.MoveCaret(-1, shift); case VK_RIGHT: return m_edit.MoveCaret(1, shift); case VK_HOME: return m_edit.MoveHome(shift); case VK_END: return m_edit.MoveEnd(shift); case 'A': if (control) { m_edit.SelectAll(); } return true; default: break; } return true; } bool TabBar::OnRButtonDown(int x, int y, int screenX, int screenY, TabBarResult& result) { result = TabBarResult(); const int index = TabAtPoint(x, y); if (index < 0) { return false; } result.type = TabBarResult::Type::CONTEXT; result.index = index; result.screenX = screenX; result.screenY = screenY; return true; } bool TabBar::OnMouseLeave() { if (m_hoverIndex < 0 && !m_hoverClose && !m_hoverAdd) { return false; } m_hoverIndex = -1; m_hoverClose = false; m_hoverAdd = false; return true; } } // namespace jdi