两处独立收益: 1. Toolbar::BuildBoxes 里唯一的 dynamic_cast 是**多余强转**(Button::widget 本来就是 UiButtonWidget*),却让全项目必须开 RTTI。改为直接遍历 m_buttons(与控件树同源)。 最小复现程序实测:39,936 B -> 115,200 B(+75,264 B)。 2. RTTI 关掉后 cp-demangle.o 仍然在链接里 —— 真凶是 libstdc++ 的 vterminate.o (引用 __cxa_demangle)。本文件自行定义 __gnu_cxx::__verbose_terminate_handler(), 让它不再被拉入(本应用启动后会换成自己的 terminate 处理,行为等效)。 Makefile 新增 NO_RTTI=1(默认开启)-> CXXFLAGS += -fno-rtti。 实测:Release 890,880 -> 829,440 B(-61,440);.text 753,136 -> 703,616(-49,520); .rdata 36,936 -> 26,616(-10,320);Debug 1,801,617 -> 1,734,578(-67,039)。 release/debug 全量重建零警告。
492 lines
14 KiB
C++
492 lines
14 KiB
C++
#include "Toolbar.hpp"
|
||
|
||
#include <algorithm>
|
||
#include <cmath>
|
||
#include <utility>
|
||
|
||
#include "UiBox.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 kSidePadding = 12.0f; /**< 工具栏左右留白。 */
|
||
const float kTopPadding = 8.0f; /**< 工具栏上下留白。 */
|
||
|
||
/**
|
||
* @brief 工具栏的根控件:只负责"整条底色 + 底部分隔线"。
|
||
*
|
||
* 按钮是它的子节点(`UiButtonWidget`),由 `Toolbar::Layout` 逐个 `Arrange`。
|
||
* 之所以把根做成控件而不是让 `Toolbar` 自己遍历按钮绘制:命中 / 悬停 / 按下的仲裁
|
||
* 全部交给 `UiWidget` 的派发链与悬停链,`Toolbar` 只做"规格表 → 控件"的翻译。
|
||
*/
|
||
class ToolbarRootWidget : public UiWidget
|
||
{
|
||
public:
|
||
void Configure(const Theme* theme, float scale)
|
||
{
|
||
m_theme = theme;
|
||
m_scale = scale;
|
||
}
|
||
|
||
/** @brief 根尺寸由宿主(`Toolbar::Layout`)给定,不参与父级测量。 */
|
||
gd::SizeF OnMeasure(const UiMeasureContext& ctx) override
|
||
{
|
||
(void)ctx;
|
||
return gd::SizeF(Bounds().Width, Bounds().Height);
|
||
}
|
||
|
||
void OnPaint(UiPaintContext& ctx) override
|
||
{
|
||
if (m_theme == nullptr || ctx.g == nullptr || !UiRectHasArea(Bounds()))
|
||
{
|
||
return;
|
||
}
|
||
const gd::RectF& area = Bounds();
|
||
UiFillRect(*ctx.g, area, m_theme->toolbarBg);
|
||
// 底部分隔线:按钮不会覆盖到这一行,因此"底色 + 分隔线先画"与旧实现像素一致。
|
||
UiDrawLine(*ctx.g, area.X, area.Y + area.Height - 1.0f, area.X + area.Width,
|
||
area.Y + area.Height - 1.0f, m_theme->border, 1.0f);
|
||
}
|
||
|
||
private:
|
||
const Theme* m_theme = nullptr; /**< 主题。 */
|
||
float m_scale = 1.0f; /**< 缩放系数。 */
|
||
};
|
||
|
||
/** @brief 按钮规格(顺序即绘制顺序)。 */
|
||
struct ButtonSpec
|
||
{
|
||
ToolbarAction action; /**< 动作。 */
|
||
const wchar_t* label; /**< 默认文本(图标按钮留空)。 */
|
||
bool primary; /**< 是否主按钮。 */
|
||
bool needsDocument; /**< 是否依赖已加载数据。 */
|
||
bool toggle; /**< 是否可切换。 */
|
||
UiButtonIcon icon; /**< 图标(`NONE` = 纯文本按钮)。 */
|
||
};
|
||
|
||
// ⚠️ **前两项构成"左侧固定簇"**(设置齿轮 + 导入文件),其余按钮作为整体靠右。
|
||
// `Layout` 里用 `kLeftClusterCount` 引用这个约定的长度,增删左侧按钮时必须同步。
|
||
// 齿轮与「导入文件」**同为强调色主按钮**(用户 2026-10-28 要求配色一致),
|
||
// 两者视觉上构成一组"文件/设置"入口,因此 primary 都为 true。
|
||
const ButtonSpec kButtonSpecs[] = {
|
||
{ToolbarAction::SETTINGS, L"", true, false, false, UiButtonIcon::GEAR},
|
||
{ToolbarAction::OPEN_FILE, L"导入文件", true, false, false, UiButtonIcon::NONE},
|
||
{ToolbarAction::FIT_ALL, L"适应视图", false, true, false, UiButtonIcon::NONE},
|
||
{ToolbarAction::AUTO_FIT_Y, L"Y 轴自适应", false, true, true, UiButtonIcon::NONE},
|
||
{ToolbarAction::ZOOM_SELECT, L"选区放大", false, true, true, UiButtonIcon::NONE},
|
||
{ToolbarAction::CURSOR, L"光标", false, true, true, UiButtonIcon::NONE},
|
||
{ToolbarAction::MERGE_LANES, L"合并网格", false, true, false, UiButtonIcon::NONE},
|
||
{ToolbarAction::SPLIT_LANES, L"拆分网格", false, true, false, UiButtonIcon::NONE},
|
||
{ToolbarAction::CLEAR_ALL, L"清空网格", false, true, false, UiButtonIcon::NONE},
|
||
};
|
||
|
||
/** @brief 左侧固定簇的按钮数量(从 `kButtonSpecs` 开头算起)。 */
|
||
constexpr std::size_t kLeftClusterCount = 2;
|
||
} // namespace
|
||
|
||
Toolbar::Toolbar() = default;
|
||
|
||
// 控件树用 `std::unique_ptr<UiWidget>` 持有,而具体根类型只在本 TU 可见
|
||
// ⇒ 析构函数必须在这里显式定义(否则其它 TU 里实例化会因类型不完整而编译失败)。
|
||
Toolbar::~Toolbar() = default;
|
||
|
||
void Toolbar::Setup(const UiFonts* fonts, const Theme* theme)
|
||
{
|
||
m_fonts = fonts;
|
||
m_theme = theme;
|
||
m_treeDirty = true;
|
||
}
|
||
|
||
void Toolbar::SetScale(float scale)
|
||
{
|
||
const float next = (scale > 0.1f) ? scale : 1.0f;
|
||
if (next == m_scale)
|
||
{
|
||
return;
|
||
}
|
||
m_scale = next;
|
||
// 控件里缓存了旧的缩放(字体实测宽度、图标尺寸)⇒ 必须重建。
|
||
m_treeDirty = true;
|
||
}
|
||
|
||
void Toolbar::SetDocumentLoaded(bool hasDocument)
|
||
{
|
||
if (m_documentLoaded == hasDocument)
|
||
{
|
||
return;
|
||
}
|
||
m_documentLoaded = hasDocument;
|
||
SyncEnabledState();
|
||
}
|
||
|
||
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::SyncEnabledState()
|
||
{
|
||
for (Button& button : m_buttons)
|
||
{
|
||
if (button.widget != nullptr)
|
||
{
|
||
button.widget->SetEnabled(IsEnabled(button));
|
||
}
|
||
}
|
||
}
|
||
|
||
void Toolbar::RebuildWidgets()
|
||
{
|
||
m_root = std::make_unique<ToolbarRootWidget>();
|
||
static_cast<ToolbarRootWidget*>(m_root.get())->Configure(m_theme, m_scale);
|
||
|
||
m_buttons.clear();
|
||
const std::size_t specCount = sizeof(kButtonSpecs) / sizeof(kButtonSpecs[0]);
|
||
m_buttons.reserve(specCount);
|
||
|
||
for (std::size_t index = 0; index < specCount; ++index)
|
||
{
|
||
const ButtonSpec& spec = kButtonSpecs[index];
|
||
// 动态文本覆盖(例如「光标」按当前模式显示 光标 / 光标x1 / 光标x2)。
|
||
std::wstring label = spec.label;
|
||
for (const std::pair<ToolbarAction, std::wstring>& entry : m_labelOverrides)
|
||
{
|
||
if (entry.first == spec.action)
|
||
{
|
||
label = entry.second;
|
||
break;
|
||
}
|
||
}
|
||
|
||
Button button;
|
||
button.action = spec.action;
|
||
button.primary = spec.primary;
|
||
button.needsDocument = spec.needsDocument;
|
||
button.toggle = spec.toggle;
|
||
button.icon = spec.icon;
|
||
button.checked = false;
|
||
|
||
auto widget = std::make_unique<UiButtonWidget>();
|
||
UiButtonWidget* raw = widget.get();
|
||
raw->Configure(m_fonts, m_theme, m_scale, label, spec.primary, spec.toggle, kButtonPadding,
|
||
kButtonHeight);
|
||
raw->SetIcon(spec.icon);
|
||
if (m_theme != nullptr)
|
||
{
|
||
raw->SetCanvasColor(m_theme->toolbarBg);
|
||
}
|
||
raw->SetEnabled(IsEnabled(button));
|
||
// 点击回调只登记动作,由宿主在鼠标抬起后统一执行(与旧语义一致)。
|
||
raw->onClick = [this, action = spec.action]() { m_pendingAction = action; };
|
||
|
||
button.widget = raw;
|
||
m_root->AddChild(std::move(widget));
|
||
m_buttons.push_back(button);
|
||
}
|
||
}
|
||
|
||
void Toolbar::Layout(const gd::RectF& area)
|
||
{
|
||
m_area = area;
|
||
|
||
if (m_fonts == nullptr || m_fonts->ui == nullptr || m_theme == nullptr)
|
||
{
|
||
return;
|
||
}
|
||
|
||
// 控件树**只在必要时重建**(先记下各 toggle 的勾选状态,否则重建会把选中态清掉)。
|
||
// 纯粹的尺寸变化不走重建:按钮几何由 `Arrange` 决定,与树结构无关。
|
||
std::vector<std::pair<ToolbarAction, bool>> previousChecked;
|
||
if (m_treeDirty || m_root == nullptr)
|
||
{
|
||
previousChecked.reserve(m_buttons.size());
|
||
for (const Button& button : m_buttons)
|
||
{
|
||
previousChecked.emplace_back(button.action, button.checked);
|
||
}
|
||
RebuildWidgets();
|
||
m_treeDirty = false;
|
||
}
|
||
m_root->Arrange(area);
|
||
|
||
UiMeasureContext measure;
|
||
measure.theme = m_theme;
|
||
measure.fonts = m_fonts;
|
||
measure.scale = m_scale;
|
||
|
||
// 宽度由控件实测(`内边距 × 2 + 文本宽`),与旧公式完全一致;中文标签因此不会被裁切。
|
||
for (Button& button : m_buttons)
|
||
{
|
||
button.width = button.widget->Measure(measure).Width;
|
||
for (const std::pair<ToolbarAction, bool>& entry : previousChecked)
|
||
{
|
||
if (entry.first == button.action)
|
||
{
|
||
button.checked = entry.second;
|
||
button.widget->SetChecked(entry.second);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
// 左侧固定簇(设置齿轮 + 导入文件)靠左,其余按钮作为整体靠右。
|
||
const float top = area.Y + kTopPadding * m_scale;
|
||
const float height = kButtonHeight * m_scale;
|
||
const float leftX = area.X + kSidePadding * m_scale;
|
||
|
||
float leftWidth = 0.0f;
|
||
for (std::size_t index = 0; index < kLeftClusterCount && index < m_buttons.size(); ++index)
|
||
{
|
||
leftWidth += m_buttons[index].width;
|
||
if (index > 0)
|
||
{
|
||
leftWidth += kButtonGap * m_scale;
|
||
}
|
||
}
|
||
|
||
float cursorX = leftX;
|
||
for (std::size_t index = 0; index < kLeftClusterCount && index < m_buttons.size(); ++index)
|
||
{
|
||
m_buttons[index].widget->Arrange(gd::RectF(cursorX, top, m_buttons[index].width, height));
|
||
cursorX += m_buttons[index].width + kButtonGap * m_scale;
|
||
}
|
||
|
||
float groupWidth = 0.0f;
|
||
for (std::size_t index = kLeftClusterCount; index < m_buttons.size(); ++index)
|
||
{
|
||
groupWidth += m_buttons[index].width;
|
||
if (index + 1 < m_buttons.size())
|
||
{
|
||
groupWidth += kButtonGap * m_scale;
|
||
}
|
||
}
|
||
|
||
const float rightEdge = area.X + area.Width - kSidePadding * m_scale;
|
||
cursorX = std::max(leftX + leftWidth + kGroupGap * m_scale, rightEdge - groupWidth);
|
||
for (std::size_t index = kLeftClusterCount; index < m_buttons.size(); ++index)
|
||
{
|
||
m_buttons[index].widget->Arrange(gd::RectF(cursorX, top, m_buttons[index].width, height));
|
||
cursorX += m_buttons[index].width + kButtonGap * m_scale;
|
||
}
|
||
}
|
||
|
||
bool Toolbar::ButtonRect(ToolbarAction action, gd::RectF& rect) const
|
||
{
|
||
for (const Button& button : m_buttons)
|
||
{
|
||
if (button.action == action && button.widget != nullptr)
|
||
{
|
||
rect = button.widget->Bounds();
|
||
return UiRectHasArea(rect);
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
void Toolbar::SetToggleState(ToolbarAction action, bool checked)
|
||
{
|
||
for (Button& button : m_buttons)
|
||
{
|
||
if (button.action == action && button.toggle)
|
||
{
|
||
button.checked = checked;
|
||
if (button.widget != nullptr)
|
||
{
|
||
button.widget->SetChecked(checked);
|
||
}
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool Toolbar::SetActionLabel(ToolbarAction action, const std::wstring& label)
|
||
{
|
||
// ⚠️ 只更新**唯一权威来源**(`m_labelOverrides`)并置树脏,不再逐个回写控件:
|
||
// 旧实现既要写覆盖表又要写控件,两处一旦不同步,下一次重建就会把文本退回去。
|
||
// 调用方拿到 true 后会重排(`Relayout`),届时由 `RebuildWidgets` 统一生效。
|
||
for (std::pair<ToolbarAction, std::wstring>& entry : m_labelOverrides)
|
||
{
|
||
if (entry.first == action)
|
||
{
|
||
if (entry.second == label)
|
||
{
|
||
return false;
|
||
}
|
||
entry.second = label;
|
||
m_treeDirty = true;
|
||
return true;
|
||
}
|
||
}
|
||
m_labelOverrides.emplace_back(action, label);
|
||
m_treeDirty = true;
|
||
return true;
|
||
}
|
||
|
||
void Toolbar::Paint(gd::Graphics& g)
|
||
{
|
||
if (m_theme == nullptr || m_fonts == nullptr || m_root == nullptr)
|
||
{
|
||
return;
|
||
}
|
||
|
||
UiPaintContext ctx;
|
||
ctx.g = &g;
|
||
ctx.clip = m_area;
|
||
ctx.scale = m_scale;
|
||
m_root->Paint(ctx);
|
||
}
|
||
|
||
bool Toolbar::HitTest(int x, int y) const
|
||
{
|
||
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
||
}
|
||
|
||
void Toolbar::BuildBoxes(UiBoxTable& boxes)
|
||
{
|
||
UiComponent::BuildBoxes(boxes);
|
||
if (m_root == nullptr)
|
||
{
|
||
return;
|
||
}
|
||
// ⚠️ 跳过根控件:它的矩形与工具栏区域完全重合(已经被上一层登记过)。
|
||
//
|
||
// ⚠️⚠️ **这里绝不能出现 `dynamic_cast`**:旧写法用它把 `UiWidget*` 转成
|
||
// `UiButtonWidget*`,但 `Button::widget` 本来就是 `UiButtonWidget*`。
|
||
// 一个多余的转换会链入 RTTI 元数据 + `cp-demangle.o`(Release map 里
|
||
// **56,320 B**;最小复现程序实测 **+75,264 B**),并逼得全项目无法用
|
||
// `-fno-rtti`。现在改为直接遍历 `m_buttons`(与控件树同源,且无需转换)。
|
||
for (const Button& spec : m_buttons)
|
||
{
|
||
if (spec.widget == nullptr)
|
||
{
|
||
continue;
|
||
}
|
||
boxes.Add(spec.widget->Bounds(), 1);
|
||
// 按钮**内容**(文本或齿轮图标)单独上报:它是居中绘制的,与按钮矩形不同。
|
||
boxes.Add(spec.widget->ContentRect(), 2);
|
||
}
|
||
}
|
||
|
||
bool Toolbar::OnMouseMove(int x, int y)
|
||
{
|
||
if (m_root == nullptr)
|
||
{
|
||
return false;
|
||
}
|
||
UiEvent event;
|
||
event.kind = UiEventKind::MOUSE_MOVE;
|
||
event.x = x;
|
||
event.y = y;
|
||
// 悬停链切换会通过 `Invalidate` 把矩形冒泡到根 ⇒ 用脏区判断"是否需要重绘"
|
||
//(与旧实现"悬停下标变化才重绘"等价,但不再手工维护下标)。
|
||
return m_root->DispatchRedraw(event);
|
||
}
|
||
|
||
bool Toolbar::OnLButtonDown(int x, int y, ToolbarAction& action)
|
||
{
|
||
action = ToolbarAction::NONE;
|
||
if (m_root == nullptr)
|
||
{
|
||
return false;
|
||
}
|
||
UiEvent event;
|
||
event.kind = UiEventKind::MOUSE_DOWN;
|
||
event.x = x;
|
||
event.y = y;
|
||
event.button = UiMouseButton::LEFT;
|
||
m_root->Dispatch(event);
|
||
(void)m_root->TakeDirty();
|
||
|
||
for (const Button& button : m_buttons)
|
||
{
|
||
if (button.widget != nullptr && button.widget->Pressed())
|
||
{
|
||
action = button.action;
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
bool Toolbar::OnLButtonUp(int x, int y, ToolbarAction& action)
|
||
{
|
||
action = ToolbarAction::NONE;
|
||
if (m_root == nullptr)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 旧语义:没有按下过就不进入抬起处理。
|
||
bool hadPressed = false;
|
||
for (const Button& button : m_buttons)
|
||
{
|
||
if (button.widget != nullptr && button.widget->Pressed())
|
||
{
|
||
hadPressed = true;
|
||
break;
|
||
}
|
||
}
|
||
if (!hadPressed)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
m_pendingAction = ToolbarAction::NONE;
|
||
UiEvent event;
|
||
event.kind = UiEventKind::MOUSE_UP;
|
||
event.x = x;
|
||
event.y = y;
|
||
event.button = UiMouseButton::LEFT;
|
||
m_root->Dispatch(event);
|
||
|
||
// 旧实现会在抬起时按落点重算悬停态;派发一条 MOUSE_MOVE 让悬停链同步。
|
||
UiEvent sync;
|
||
sync.kind = UiEventKind::MOUSE_MOVE;
|
||
sync.x = x;
|
||
sync.y = y;
|
||
m_root->Dispatch(sync);
|
||
(void)m_root->TakeDirty();
|
||
|
||
action = m_pendingAction;
|
||
m_pendingAction = ToolbarAction::NONE;
|
||
return true;
|
||
}
|
||
|
||
bool Toolbar::OnMouseLeave()
|
||
{
|
||
if (m_root == nullptr)
|
||
{
|
||
return false;
|
||
}
|
||
UiEvent event;
|
||
event.kind = UiEventKind::MOUSE_LEAVE;
|
||
event.x = 0;
|
||
event.y = 0;
|
||
m_root->Dispatch(event);
|
||
|
||
// 指针离开窗口后不会再收到 MOUSE_UP ⇒ 在这里放弃"按下"(与旧实现一致)。
|
||
bool changed = false;
|
||
for (Button& button : m_buttons)
|
||
{
|
||
if (button.widget != nullptr && button.widget->Pressed())
|
||
{
|
||
button.widget->CancelPress();
|
||
changed = true;
|
||
}
|
||
}
|
||
changed = UiRectHasArea(m_root->TakeDirty()) || changed;
|
||
return changed;
|
||
}
|
||
|
||
} // namespace jdi
|