Files
Just-DataInspector/Components/TabBar/Src/TabBar.cpp
T
DeepSeek V4.1 Flash 12b3531485 输入框鼠标编辑、图例省略、信号跨视图共享选择与网格菜单布局修复
- 输入框:支持鼠标左键点击定位插入符、拖拽框选区域,并且不再限制字符长度
- 输入框:长文本使用 ETO_CLIPPED 裁剪绘制,避免字符溢出到编辑区域之外
- 图例:信号名称超过 200px 时省略号截断,不再挤压标尺区域
- 选中状态从 TabConfig 迁移到 DataSet,切换视图后信号选中状态保持一致
- 网格右键菜单:Y 轴最大值/最小值直接显示当前值,点击后原位置弹出内联编辑框
- 标尺数值改为绘制在图例下层,不再因避让图例而隐藏
- 移除已无用的 LegendBandRect,新增共享的编辑框布局与命中检测工具
- .gitignore 忽略 TestData 测试数据目录
2026-09-30 17:10:23 +08:00

601 lines
16 KiB
C++

#include "TabBar.hpp"
#include <algorithm>
#include <cmath>
#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; /**< 单个标签最大宽度(逻辑像素)。 */
const float kEditingTabWidth = 320.0f; /**< 重命名输入时标签的最大宽度(便于看清长名称)。 */
const float kMinTabWidth = 70.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<std::wstring>& 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<int>(m_tabs.size()))
{
m_activeIndex = m_tabs.empty() ? -1 : static_cast<int>(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 minWidth = kMinTabWidth * m_scale;
const float addSize = kAddSize * m_scale;
// 标签宽度按「加粗字体」测量:激活页用 uiBold 绘制,
// 若按常规字体测量,加粗后的长名称会在激活时被省略号截断(预览过短)。
std::vector<float> desired;
desired.reserve(m_tabs.size());
float total = kLeftPadding * m_scale;
for (std::size_t index = 0; index < m_tabs.size(); ++index)
{
const bool editing = (static_cast<int>(index) == m_editIndex);
// 重命名过程中按「正在输入的文本」测量,标签才能随输入逐步变宽;
// 否则始终按已提交的旧名称算宽度,长名称会被挤成很短的一截(预览过短)。
const std::wstring& measureText = editing ? m_edit.Text() : m_tabs[index].name;
const float boldWidth = UiMeasureTextWidth(m_fonts->uiBold, measureText);
const float regularWidth = UiMeasureTextWidth(m_fonts->ui, measureText);
const float textWidth = std::max(boldWidth, regularWidth);
const float maxWidth = (editing ? kEditingTabWidth : kMaxTabWidth) * m_scale;
float width = kTabPadding * 2.0f * m_scale + textWidth + kCloseGap * m_scale + closeSize;
width = std::max(minWidth, std::min(width, maxWidth));
desired.push_back(width);
total += width + kAddGap * m_scale;
}
total += addSize;
// 标签总宽超出可用宽度时整体等比收缩,保证「+」按钮始终可见。
const float available = area.Width - kLeftPadding * m_scale;
float shrink = 1.0f;
if (total > available && total > 0.0f)
{
shrink = available / total;
}
float cursorX = area.X + kLeftPadding * m_scale;
for (std::size_t index = 0; index < m_tabs.size(); ++index)
{
TabItem& tab = m_tabs[index];
float width = std::max(minWidth * shrink, desired[index] * shrink);
// 收缩后仍要保证「文本 + 关闭按钮」有最小可用空间。
width = std::max(width, closeSize + kTabPadding * m_scale + 8.0f * m_scale);
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;
}
m_addRect = gd::RectF(std::min(cursorX, area.X + area.Width - addSize - 2.0f * m_scale),
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<int>(index) == m_activeIndex);
const bool hovered = (static_cast<int>(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<int>(index) == m_editIndex)
{
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<int>(m_tabs.size()))
{
markerX = m_tabs[static_cast<std::size_t>(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<float>(x), static_cast<float>(y));
}
int TabBar::TabAtPoint(int x, int y) const
{
const float fx = static_cast<float>(x);
const float fy = static_cast<float>(y);
for (std::size_t index = 0; index < m_tabs.size(); ++index)
{
if (UiPointInRect(m_tabs[index].rect, fx, fy))
{
return static_cast<int>(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<std::size_t>(index)].closeRect,
static_cast<float>(x), static_cast<float>(y)));
const bool overAdd = UiPointInRect(m_addRect, static_cast<float>(x), static_cast<float>(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<float>(x), static_cast<float>(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<std::size_t>(index)].closeRect,
static_cast<float>(x), static_cast<float>(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<float>(x), static_cast<float>(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<float>(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<int>(index);
}
}
return static_cast<int>(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<std::size_t>(index)].closeRect, static_cast<float>(x),
static_cast<float>(y)))
{
return false;
}
m_editIndex = index;
m_edit.Begin(m_tabs[static_cast<std::size_t>(index)].name, true, EditFilter::ANY, 0u);
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;
}
// 退格/删除已由 WM_KEYDOWN 的 OnEditKey 处理,此处忽略全部控制字符。
if (character < L' ' || character == 0x7F)
{
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::CollapseEditSelectionToEnd()
{
if (m_editIndex < 0)
{
return false;
}
return m_edit.DeselectToEnd();
}
gd::RectF TabBar::EditRect() const
{
if (m_editIndex < 0 || m_editIndex >= static_cast<int>(m_tabs.size()) || m_fonts == nullptr)
{
return gd::RectF();
}
const TabItem& tab = m_tabs[static_cast<std::size_t>(m_editIndex)];
const float textLeft = tab.rect.X + kTabPadding * m_scale;
const float textWidth = tab.closeRect.X - kCloseGap * m_scale - textLeft;
return gd::RectF(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);
}
bool TabBar::OnEditMouseDown(int x, int y)
{
if (m_editIndex < 0)
{
return false;
}
return UiEditFieldMouseDown(m_fonts->ui, EditRect(), m_edit, m_scale, x, y);
}
bool TabBar::OnEditMouseMove(int x)
{
if (m_editIndex < 0)
{
return false;
}
return UiEditFieldMouseMove(m_fonts->ui, EditRect(), m_edit, m_scale, x);
}
bool TabBar::OnEditMouseUp()
{
if (!m_edit.Dragging())
{
return false;
}
m_edit.EndDrag();
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