Files
Just-DataInspector/Components/Toolbar/Src/Toolbar.cpp
T
DeepSeek V4.1 Flash 0fffb3ed09 feat(ui): 修复预设色块误跳自定义色、选区 Y 轴自适应改为切换开关、菜单项改名移位
1) 修复样式区"点预设色块直接跳到自定义色块"
- 根因:加载时 customColor 被赋成信号自身的预设色,而样式区按
  "color == customColor 即视为在用自定义色" 判定高亮,于是点自身的预设色时
  高亮立刻跳到自定义色块上(第 N 个信号点第 N 个预设色必然复现)
- customColor 初值改为调色板中"本信号预设色向后递进一格"的颜色
  (palette[(slot + 1) % count]),自定义色块因此显示与预设色块排布连续的下一色
- 同时收紧高亮判定:仅当当前颜色"不属于任何可见预设色"时才认为在用自定义色。
  这样即使自定义色恰好等于某个预设色,点击该预设色也始终高亮预设色块,
  彻底消除"高亮乱跳";用拾色器选出的非预设色仍正常高亮自定义色块

2)「选区 Y 轴自适应」改为切换开关(原为一次性操作)
- Lane 用单一布尔量 fitYToSelection 表示(删除 viewAutoRange/viewAutoMin/viewAutoMax)
- 开启后每次时间轴选区放大(ApplyTimeSelection → ApplySelectionAutoFit)
  都会把该网格 Y 轴匹配到新的可见时间区间
- 任何手动幅值调整(Y 轴选区放大、纵向平移、缩放、直接输入上下限)都会关闭对应网格的开关
- RebuildLanes 按信号继承该开关:勾选/取消勾选信号不再把用户刚打开的开关悄悄清掉

3) 菜单「恢复 Y 轴自适应」移除,原位置新增「选区 Y 轴自适应(全局)」
- 全局项为切换状态:选中即把全部网格的开关置为开,取消即全部置为关;
  任意网格单独关掉后,全局项立即变为未勾选(= AllLanesAutoFitToSelection)
- 工具栏在「适应视图」之后新增「Y 轴自适应」切换按钮,与全局项是同一个开关,
  每帧同步选中态
- 删除了 PLOT_CMD_LANE_AUTO / PlotView::LaneRangeIsAuto / ResetLaneRange 等已无用的接口

4)「时间轴自适应(匹配本网格)」改名为「按网格时间轴展开」并移到分隔线上方
- 新顺序:网格 N / Y 轴最大值 / Y 轴最小值 / 选区 Y 轴自适应 / 按网格时间轴展开 /
  --- / 选区 Y 轴自适应(全局)/ 全部自适应

验证(TestData/[1].CSV 11 信号,窗口 1725x996,125% DPI):
- 颜色:初始高亮在预设色块 1;点预设 1 与预设 2(等于自定义色初值)均保持点中的色块高亮;
  用拾色器改成非预设色后高亮正确落在自定义色块
- 逐网格:菜单项勾选/取消即时反映;开启的网格做时间轴选区放大后 Y 轴自动匹配
  (仅该网格变化,其它网格保持整段范围)
- 全局:工具栏按钮与菜单全局项互相联动;单独取消一个网格后两者都变为未勾选;
  隐藏/显示信号后开关保持
- 「按网格时间轴展开」能把缩放后的时间轴展开回该网格信号范围;「全部自适应」仍正常
- 超大文件回归(VERY_BIG_DATA 1.77GB)菜单/悬停正常;空闲 CPU 0.00%

构建: make clean; make all 无警告无错误
2026-10-07 13:57:46 +08:00

313 lines
8.1 KiB
C++

#include "Toolbar.hpp"
#include <cmath>
#include "Utf8Util.hpp"
namespace jdi
{
namespace
{
const float kButtonHeight = 30.0f; /**< 按钮高度(逻辑像素)。 */
const float kButtonGap = 6.0f; /**< 按钮间距。 */
const float kButtonPadding = 14.0f; /**< 按钮左右内边距。 */
const float kGroupGap = 18.0f; /**< 分组间距。 */
const float kButtonRadius = 5.0f; /**< 按钮圆角半径。 */
const float kSidePadding = 12.0f; /**< 工具栏左右留白。 */
const float kTopPadding = 8.0f; /**< 工具栏上下留白。 */
}
Toolbar::Toolbar() = default;
void Toolbar::Setup(const UiFonts* fonts, const Theme* theme)
{
m_fonts = fonts;
m_theme = theme;
}
void Toolbar::SetScale(float scale)
{
m_scale = (scale > 0.1f) ? scale : 1.0f;
}
void Toolbar::SetDocumentLoaded(bool hasDocument)
{
m_documentLoaded = hasDocument;
}
float Toolbar::DesiredHeight() const
{
return (kButtonHeight + kTopPadding * 2.0f) * m_scale;
}
bool Toolbar::IsEnabled(const Button& button) const
{
return button.needsDocument ? m_documentLoaded : true;
}
void Toolbar::Layout(const gd::RectF& area)
{
m_area = area;
m_buttons.clear();
if (m_fonts == nullptr || m_fonts->ui == nullptr)
{
return;
}
struct Spec
{
ToolbarAction action;
const wchar_t* label;
bool primary;
bool needsDocument;
bool toggle;
};
const Spec specs[] = {
{ToolbarAction::OPEN_FILE, L"导入文件", true, false, false},
{ToolbarAction::FIT_ALL, L"适应视图", false, true, false},
{ToolbarAction::AUTO_FIT_Y, L"Y 轴自适应", false, true, true},
{ToolbarAction::ZOOM_TIME, L"时间轴选区", false, true, true},
{ToolbarAction::ZOOM_VALUE, L"Y 轴选区", false, true, true},
{ToolbarAction::CURSOR, L"光标", false, true, true},
{ToolbarAction::MERGE_LANES, L"合并网格", false, true, false},
{ToolbarAction::SPLIT_LANES, L"拆分网格", false, true, false},
{ToolbarAction::CLEAR_ALL, L"清空网格", false, true, false},
};
const std::size_t specCount = sizeof(specs) / sizeof(specs[0]);
// 按钮宽度按实际文本像素宽 + 固定内边距计算,避免中文标签被裁切。
const float top = area.Y + kTopPadding * m_scale;
const float height = kButtonHeight * m_scale;
const float horizontalPadding = kButtonPadding * 2.0f * m_scale;
// 先建立全部按钮并测量宽度,再决定“导入文件”固定在左、其余按钮整体靠右。
m_buttons.clear();
m_buttons.reserve(specCount);
for (const Spec& spec : specs)
{
Button button;
button.action = spec.action;
button.label = spec.label;
button.primary = spec.primary;
button.needsDocument = spec.needsDocument;
button.toggle = spec.toggle;
button.width = horizontalPadding + UiMeasureTextWidth(m_fonts->ui, button.label);
m_buttons.push_back(std::move(button));
}
// 首按钮(导入文件)靠左;其余按钮作为整体靠右,并留出一组额外间距。
const float leftX = area.X + kSidePadding * m_scale;
float groupWidth = 0.0f;
for (std::size_t index = 1; index < m_buttons.size(); ++index)
{
groupWidth += m_buttons[index].width;
if (index + 1 < m_buttons.size())
{
groupWidth += kButtonGap * m_scale;
}
}
float cursorX = leftX;
m_buttons[0].rect = gd::RectF(cursorX, top, m_buttons[0].width, height);
const float rightEdge = area.X + area.Width - kSidePadding * m_scale;
cursorX = std::max(leftX + m_buttons[0].width + kGroupGap * m_scale, rightEdge - groupWidth);
for (std::size_t index = 1; index < m_buttons.size(); ++index)
{
m_buttons[index].rect = gd::RectF(cursorX, top, m_buttons[index].width, height);
cursorX += m_buttons[index].width + kButtonGap * m_scale;
}
}
void Toolbar::SetToggleState(ToolbarAction action, bool checked)
{
for (Button& button : m_buttons)
{
if (button.action == action && button.toggle)
{
button.checked = checked;
return;
}
}
}
void Toolbar::Paint(gd::Graphics& g)
{
if (m_theme == nullptr || m_fonts == nullptr)
{
return;
}
UiFillRect(g, m_area, m_theme->toolbarBg);
for (const Button& button : m_buttons)
{
const bool enabled = IsEnabled(button);
std::uint32_t background = 0;
std::uint32_t border = 0;
std::uint32_t textColor = 0;
if (button.primary)
{
background = enabled ? m_theme->accent : MixColor(m_theme->accent, m_theme->toolbarBg, 0.6);
if (button.pressed)
{
background = MixColor(m_theme->accent, 0xFF000000u, 0.12);
}
else if (enabled && button.hover)
{
background = MixColor(m_theme->accent, 0xFFFFFFFFu, 0.12);
}
border = background;
textColor = m_theme->textOnAccent;
}
else
{
// 可切换按钮在选中时用强调色浅底 + 强调描边,与普通按钮区分。
background = (button.toggle && button.checked) ? m_theme->accentSoft : m_theme->toolbarBg;
if (button.pressed)
{
background = m_theme->pressed;
}
else if (enabled && button.hover)
{
background = (button.toggle && button.checked) ? MixColor(m_theme->accentSoft, m_theme->accent,
0.16)
: m_theme->hover;
}
if (!enabled)
{
background = m_theme->toolbarBg;
}
if (button.toggle && button.checked && enabled)
{
border = m_theme->accent;
}
else
{
border = MixColor(m_theme->border, m_theme->toolbarBg, enabled ? 0.0 : 0.5);
}
textColor = enabled ? ((button.toggle && button.checked) ? m_theme->textStrong : m_theme->text)
: m_theme->textMuted;
}
if (background != m_theme->toolbarBg)
{
UiFillRoundRect(g, button.rect, kButtonRadius * m_scale, background);
}
if (!button.primary)
{
UiDrawRoundRect(g, button.rect, kButtonRadius * m_scale, border, 1.0f);
}
UiText(g, button.label, m_fonts->ui, button.rect, textColor, TextAlign::CENTER);
}
// 底部分隔线。
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 Toolbar::HitTest(int x, int y) const
{
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
}
bool Toolbar::OnMouseMove(int x, int y)
{
int hoverIndex = -1;
for (std::size_t index = 0; index < m_buttons.size(); ++index)
{
if (UiPointInRect(m_buttons[index].rect, static_cast<float>(x), static_cast<float>(y)))
{
hoverIndex = static_cast<int>(index);
break;
}
}
if (hoverIndex == m_hoverIndex)
{
return false;
}
m_hoverIndex = hoverIndex;
return true;
}
bool Toolbar::OnLButtonDown(int x, int y, ToolbarAction& action)
{
action = ToolbarAction::NONE;
for (std::size_t index = 0; index < m_buttons.size(); ++index)
{
Button& button = m_buttons[index];
if (!UiPointInRect(button.rect, static_cast<float>(x), static_cast<float>(y)))
{
continue;
}
if (!IsEnabled(button))
{
return false;
}
m_pressedIndex = static_cast<int>(index);
button.pressed = true;
action = button.action;
return true;
}
return false;
}
bool Toolbar::OnLButtonUp(int x, int y, ToolbarAction& action)
{
action = ToolbarAction::NONE;
if (m_pressedIndex < 0)
{
return false;
}
const int index = m_pressedIndex;
m_pressedIndex = -1;
if (index >= static_cast<int>(m_buttons.size()))
{
return false;
}
Button& button = m_buttons[static_cast<std::size_t>(index)];
button.pressed = false;
const bool inside = UiPointInRect(button.rect, static_cast<float>(x), static_cast<float>(y));
m_hoverIndex = inside ? index : -1;
if (inside && IsEnabled(button))
{
action = button.action;
}
return true;
}
bool Toolbar::OnMouseLeave()
{
bool changed = false;
if (m_hoverIndex >= 0)
{
m_hoverIndex = -1;
changed = true;
}
if (m_pressedIndex >= 0)
{
if (m_pressedIndex < static_cast<int>(m_buttons.size()))
{
m_buttons[static_cast<std::size_t>(m_pressedIndex)].pressed = false;
}
m_pressedIndex = -1;
changed = true;
}
return changed;
}
} // namespace jdi