Files
Just-DataInspector/Components/Toolbar/Src/Toolbar.cpp
T
DeepSeek V4.1 Flash 53b981a2a9 全项目大检查:修复数据层/UI 真 BUG、清理死代码与过期注释、并评估二进制体积
按「性能 / BUG / 错误 / 过期注释 / 命名与实际含义不符」四项对全仓做了一次审查,
四个只读子审查交叉核对后逐条回到源码确认再改。

一、数据层真 BUG
- MatH5:U8/U16/U32/U64 用逐字节 Cached() 拼装,而 Cached 只服务「完全落在同一个
  64KB 块内」的请求 => 跨块时静默读成 0(U32 整段为 0、U64 高 32 位为 0),分块 B-tree
  的 chunk 偏移/子节点地址被破坏且无任何报错。新增 ScalarLE():命中缓存用缓存,
  否则退回一次 ReadAt;U64 顺带从 8 次缓存查找降到 1 次。
- Mf4Loader::IsBlock 未校验链接表是否落在块内 => DataOffset 越界,随后
  block_len - (data - offset) 在 uint64 上下溢,得到一个约 2^64 的"可用字节数",
  下游 vector(usable+1) 直接 bad_alloc。同时把 offset + bytes > size 改成不产生回绕的写法。
- DecodeRawValue 的整数路径:bit_offset%8 != 0 且位宽 64 时需 9 个字节,先拼成 64 位
  再整体右移会把最高字节静默丢弃 => 改为逐字节带位偏移累加。
- cg_cycle_count 加「数据段总字节 / 记录字节」上限(压缩段按 deflate 最大膨胀比估算)。
- CsvLoader 表头分支同样跳过 #/% 注释行(否则整份文件按 1 列解析并被丢弃)。
- MatLoader:double -> int 的越界转换加了范围检查;普通对象递归加深度上限与成环检测。
- Utf8Util::BuildTicks:double -> long long 越界转换前先判商是否落在 ±9e18 内。

二、界面 BUG
- 工具栏按钮永远不显示悬停高亮:Paint 读的 Button::hover 全仓从未赋值,改为按 m_hoverIndex 判断。
- 菜单打开时"取消底层拖拽"对每个消息都执行,把菜单自己的 Target::EDIT_FIELD 也清掉
  => 菜单内联输入框无法拖拽框选。排除 EDIT_FIELD。
- GdiplusShutdown 之前未释放 PlotView 的 GDI+ 位图(Application 是进程单例,成员析构在
  WinMain 之后)=> 新增 PlotView::ReleaseGraphicsResources() 并在 Shutdown 里调用。
- WM_KILLFOCUS 现在会提交标签页/绘图区编辑并复位捕获与拖拽状态。
- WM_LBUTTONUP 不再抢走中键平移的鼠标捕获。
- TextEdit:被过滤器拒绝的字符返回 false;无选区时 Ctrl+X 只复制、不再清空整段文本。
- SignalPanel::InvalidateRows 作废编辑/待确认/改名计时等行下标状态。
- EndFilterEdit 改走 ApplyFilterText(否则输入纯空格后回车,列表仍停在筛选结果)。
- 新增 SignalBadgeVisible 作为唯一判据(原来命中只看"可见",绘制还要求"已分配网格")。
- 折起动画结束后夹取滚动量;双击分组名改名时清掉该组正在播放的折起动画。
- ScrollToSignal 复用 SignalScrollTarget(含悬浮组名避让)。
- RowNameRect 与绘制使用同一最小宽度(原来退化时返回空矩形)。
- 分隔条拖拽上限改用与 Relayout 同一公式;UpdateDragCursor 补 SPLITTER/EDIT_FIELD。
- SignalPanel 里几个守卫改为判断真正被解引用的指针;分组行 ✕ 也返回手型光标。

三、PlotView
- FindStyleOnlyLanes 改用完整的 SceneStyleEntry::operator==(原来只比 4 个字段,
  另一个网格同时改"调整"时带着旧像素被当成最新)。
- 宽基底图层不再烘焙拖拽光晕(LaneLayerHash 不含它),改由 PaintHoverHighlight 每帧叠加。
- PaintLaneLegend 提前返回前清掉本网格的命中矩形(否则能点中看不见的图例条目)。
- 面板主体改为完全不透明(消除平移后 4% 透出的旧波形,零额外代价)。
- PAN / Shift+滚轮改走 SetTimeView,使"自动点线"判据随平移重算。
- 内联输入框命中字体与绘制统一为 monoSmall。
- 派生曲线取值与窗口按 SourceIndexFor 映射(水平镜像时不再读到镜像另一端)。
- EdgePanelWidth 补 style/axis 空指针守卫;二分结果夹到 count。

四、清理
- 删除 Toolbar::ActionLabel、Theme::statusBg、PopupMenu 的 editResult 死机制、
  Toolbar::Layout 的重复 clear()、HitRow 的恒假分支、OnDoubleClick 的重复判定、
  PaintUtil 里孤立的注释块。
- 修正一批过期/错误注释(包络带"不做加宽"、LaneLayerHash 的图例说法、
  均值滤波曲线的窗口说法、样式区行数与「调整」按钮顺序、FIT_ALL 退出缩放模式、
  RemoveSignal 的 ✕ 位置、.mat/.dat 格式文档、MatH5 的"映射视图"、DataModel 的
  "点线/被移除文件"、SignalPanel 的"行高连续缩小"、TextEdit 的 PaintUtil 引用等)。

五、二进制体积评估(按要求只评估,未做压缩修改)
- release 809,472 字节;.text 652KB、.xdata+.pdata 约 75KB、.rdata 42KB、
  .rsrc 20KB、.idata 11.5KB。
- 去掉 -ld3d11 -ldwmapi -ld3dcompiler(仓内无任何调用)实测 0 字节收益
  (静态库只拉取被引用的对象)=> 删链接库不是压缩手段,只是配置整洁。
- 仓内已无 <iostream>/<sstream>/<filesystem>/<regex>/<locale> 等体积炸弹。
- 结论:已无明显的浪费可捡;要显著变小只能改变取舍(动态 CRT / UPX / 砍功能),
  需要用户决策,本轮未动。

验证
- editcheck 20/20、round3check 31/31、round4check 34/34、round5check 5/5、convcheck 18/18。
- check 136/137:唯一失败项经 git stash 回到改动前复现同一数值,属该探针自身的既有
  不稳定项(取决于前置用例留下的缓存/时序),与本轮改动无关。
- 真实数据:4 个 MF4 => 283/8/18/24 个信号;.mat 样例 8 信号、
  带无障碍对照-防夹力曲线数据 8 信号、BIG_MAT.mat 5 信号(私有内存 +741MB)。
- make release / make debug 零警告。

文档:Documents/agent_mem.md 同步 §10 变更日志、§11.2 第 171-180 条硬约束
与文末本轮小节(含体积评估数据)。
2026-10-20 16:10:29 +08:00

381 lines
10 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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;
// 重新排布会重建全部按钮:先记下当前各 toggle 的勾选状态,
// 否则每次因文本变化(如「光标x2」)触发的 Layout 会把按钮选中态全部清掉。
std::vector<std::pair<ToolbarAction, bool>> previousChecked;
for (const Button& button : m_buttons)
{
previousChecked.emplace_back(button.action, button.checked);
}
const auto inheritedChecked = [&previousChecked](ToolbarAction action)
{
for (const std::pair<ToolbarAction, bool>& entry : previousChecked)
{
if (entry.first == action)
{
return entry.second;
}
}
return false;
};
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_SELECT, L"选区放大", 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()` 并做过 `previousChecked` 快照,这里只需预留容量。)
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.checked = inheritedChecked(spec.action);
// 动态文本覆盖(例如「光标」按当前模式显示 光标 / 光标x1 / 光标x2)。
for (const std::pair<ToolbarAction, std::wstring>& entry : m_labelOverrides)
{
if (entry.first == spec.action)
{
button.label = entry.second;
break;
}
}
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;
}
}
}
bool Toolbar::SetActionLabel(ToolbarAction action, const std::wstring& label)
{
for (std::pair<ToolbarAction, std::wstring>& entry : m_labelOverrides)
{
if (entry.first == action)
{
if (entry.second == label)
{
return false;
}
entry.second = label;
// 已排布过时同步更新按钮文本,宿主随后会调用 Layout() 重新计算宽度。
for (Button& button : m_buttons)
{
if (button.action == action)
{
button.label = label;
}
}
return true;
}
}
m_labelOverrides.emplace_back(action, label);
for (Button& button : m_buttons)
{
if (button.action == action)
{
button.label = label;
}
}
return true;
}
void Toolbar::Paint(gd::Graphics& g)
{
if (m_theme == nullptr || m_fonts == nullptr)
{
return;
}
UiFillRect(g, m_area, m_theme->toolbarBg);
for (std::size_t buttonIndex = 0; buttonIndex < m_buttons.size(); ++buttonIndex)
{
const Button& button = m_buttons[buttonIndex];
const bool enabled = IsEnabled(button);
// ⚠️ 悬停态的唯一来源是 `m_hoverIndex`(`Button::hover` 字段从未被赋值,
// 旧实现读它 ⇒ 鼠标移到按钮上永远不高亮)。
const bool hovered = (static_cast<int>(buttonIndex) == m_hoverIndex);
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 && hovered)
{
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 && hovered)
{
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