- 图例跟随重命名:绘图区图例文本改用信号当前显示名(Title), 左侧列表改名后网格图例立即同步。 - 标签页重排与改名:新增拖拽排序(按住标签左右拖动并显示插入指示条, 重排后保持原激活页)与双击内联改名,新增 MoveTab 正确处理激活下标。 修复改名后未重新布局导致标签页整条消失的问题(SetTabs 会重置标签矩形)。 双击判定改为自行按双击时间识别,不再依赖系统 WM_LBUTTONDBLCLK。 - 信号拖拽:虚影改用原信号颜色与线宽(不再画成虚线),保持原有线段/散点样式; 横向与纵向均自由跟随鼠标位移;拖拽首帧位移为零,虚影始终贴合在鼠标下方。 - 网格与标尺细化:时间轴与幅值轴期望刻度数提高,并在相邻主刻度之间 补绘 5 等分细线,便于目视估读。 - 图例定位与交互:图例移到各网格左上角,与标尺数字冲突时优先显示图例 (标尺数值主动避让);图例条目支持单击选中信号(与左侧列表联动、 样式区随之切换目标)与双击重命名信号。 - 网格划分:合并网格后新勾选的信号不再被并入那个唯一的合并网格, 而是自动切换为自定义划分并独占一个新网格。 - 工具栏:「每信号一格」改名为「拆分信号网格」。 - 清理:移除已无引用的 UiDrawArrowIcon 与 m_styleContentRect。
506 lines
13 KiB
C++
506 lines
13 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; /**< 单个标签最大宽度(逻辑像素)。 */
|
|
}
|
|
|
|
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 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<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)
|
|
{
|
|
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_editText, m_editFresh, 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_editText = m_tabs[static_cast<std::size_t>(index)].name;
|
|
m_editFresh = true;
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnEditChar(wchar_t character)
|
|
{
|
|
if (m_editIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (character == L'\r' || character == L'\n')
|
|
{
|
|
TabBarResult result;
|
|
OnEditKey(VK_RETURN, result);
|
|
return true;
|
|
}
|
|
if (character == L'\b')
|
|
{
|
|
m_editFresh = false;
|
|
if (!m_editText.empty())
|
|
{
|
|
m_editText.pop_back();
|
|
}
|
|
return true;
|
|
}
|
|
if (character < L' ')
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (m_editFresh)
|
|
{
|
|
m_editText.clear();
|
|
m_editFresh = false;
|
|
}
|
|
if (m_editText.size() < 48)
|
|
{
|
|
m_editText.push_back(character);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnEditKey(WPARAM key, TabBarResult& result)
|
|
{
|
|
result = TabBarResult();
|
|
if (m_editIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
const int index = m_editIndex;
|
|
switch (key)
|
|
{
|
|
case VK_RETURN:
|
|
{
|
|
const std::wstring text = Trim(m_editText);
|
|
m_editIndex = -1;
|
|
m_editFresh = false;
|
|
m_editText.clear();
|
|
if (!text.empty())
|
|
{
|
|
result.type = TabBarResult::Type::RENAME;
|
|
result.index = index;
|
|
result.text = text;
|
|
}
|
|
return true;
|
|
}
|
|
case VK_ESCAPE:
|
|
m_editIndex = -1;
|
|
m_editFresh = false;
|
|
m_editText.clear();
|
|
return true;
|
|
case VK_BACK:
|
|
m_editFresh = false;
|
|
if (!m_editText.empty())
|
|
{
|
|
m_editText.pop_back();
|
|
}
|
|
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
|