## 为什么
用户 2026-10-29 要求:按既定计划完整实现改进,但
① **不得把编译产物拆成 DLL**(项目定位是单文件轻量工具,最终只能有一个 .exe);
② 异常处理改用**预检验**而不是 try/catch(-fno-exceptions 省体积,且 try/catch 本身开销大);
③ 先删除 Documents 内三份 UI 重构文档再按"计划 / 当前进度 / 最终报告"重建;
④ 顺便审计代码,删除无用/过时注释,修复名实不符;
⑤ 提交到本地仓库,期间不要占用用户的鼠标/键盘/屏幕。
## 改了什么
### 1. 体积(909,824 B -> 788,992 B,-120,832 B,-13.3%)
Makefile 新增三个可覆盖开关(默认全为 1):
- `LAYOUT_BOUNDS` 编入/剔除「显示布局边界」叠加层
- `NO_EXCEPTIONS` 追加 `-fno-exceptions`
- `LTO` 追加 `-flto`(编译 + 链接)
派生:`FUNC_SECTIONS`(LTO 下为空)、`GC_SECTIONS`(`-Wl,--gc-sections`)、
`ASM_LISTING`/`ASM_LISTING_C`(LTO 下为空,因为 lto-wrapper 会丢弃 `-Wa` 清单)。
两个实测复现的坑(已写进 Makefile 注释):
- **切换 LTO 必须 `make clean`**:LTO 与非 LTO 目标文件混链会让 GCC 15.2.0 在
`binds_to_current_def_p (symtab.cc:2598)` 处 ICE。
- **`-flto` 与 `-ffunction-sections -fdata-sections` 同时打开也 ICE**(干净重编也必崩,
报错落点是 `DataModel.hpp:393`,但那是 COMDAT 解析落点、不是原因)⇒
LTO 下 `FUNC_SECTIONS` 置空,并加 `-flto-partition=one`:默认 11 个分区的 LTRANS
稳定复现,单分区从未复现。**这是稳定性选项,不是性能选项。**
### 2. 关异常必须成对交付:预检验 + 兜底
新增 `Utilities/Inc/MemProbe.hpp` + `Utilities/Src/MemProbe.cpp`:
- `MemAvailablePhysical()`:`GlobalMemoryStatusEx` + 200ms 缓存
- `MemCanAllocate(bytes)`:预留后仍须保有 1/8 可用内存
- `MemReserveIfRoom(container, n)`:含 `capacity()` 早退与乘法溢出保护
- `MemInstallTerminateHandler()`:OOM 时弹一次带建议的模态提示后 `_Exit`(而不是静默消失)
接入:
- `Utilities/Src/DataModel.cpp`:全仓**唯一**的 `try/catch`(`extremes.reserve` 捕
`std::bad_alloc`)改为 `MemReserveIfRoom(extremes, count / 8)`
- `Components/PlotView/Src/PlotView.cpp`:`AvailablePhysicalBytes()` 原有一份**完整重复**
的 `GlobalMemoryStatusEx` + 缓存实现 → 转发 `MemAvailablePhysical()`
- `Components/Application/Src/Application.cpp`:`Initialize()` 开头安装 terminate handler
### 3. L1 几何产物层
新增 `Components/Shared/Inc/UiBox.hpp` + `Src/UiBox.cpp`:
- `struct UiBox { rect, clip, depth, layer }` —— 纯值类型,不继承任何东西
(继承会和 PlotView 4 层位图缓存 / SignalPanel 行位图缓存的失效体系打架)
- `class UiBoxTable : public UiTextRectSink` —— 只累计几何 + 裁剪/层/遮挡,**不含绘制代码**
`UiComponent::CollectDebugBoxes` → **`virtual void BuildBoxes(UiBoxTable&)`**:
组件从"收集布局边界(调试用)"改为"上报几何产物",不再需要知道"调试"这个概念存在。
`UiDebugBounds.hpp/.cpp` 退化为唯一的消费者(配色 + 包角描边 + 遮挡挖洞)。
除宿主 `ApplicationCommands.cpp` 外,所有组件源码现在只包含 `UiBox.hpp`。
### 4. 审计
- `MemProbe` 里"省 40~60KB"的估算 → 改为实测 27,136 B
- `MemProbe.hpp` 的 `<new>`、`MemProbe.cpp` 的 `<cstdio>`:未使用,删除
- `UiDebugPaintBounds(g, rect, depth)`:零调用者,删除(只保留带 clip 的版本)
- `TabBar`/`Toolbar`/`PlotView`/`SignalPanel` 的 `BuildBoxes` 文档"(调试用)"→"几何 box"
- `PlotView::SetForceFullRepaint` 补写"为什么保留而不优化"
### 5. 文档
`Documents/UI_Refactor_Plan.md` / `_Progress.md` / `_Report.md` 先 `git rm` 再按
「计划 / 当前进度 / 最终报告」重建;`Documents/agent_mem.md` 追加第十三轮记录。
## 怎么验证
```
make clean
make release TARGET_FILE_NAME=JustDataInspector -j16 # 零 error / 零 warning,788,992 B
make debug TARGET_FILE_NAME=JustDataInspectorD -j16 # 零 warning
uicheck 69/69 lbcheck 21/21 lbresid 5/5 lbzorder 4/4 lbcovr 7/7 lbpanel 10/10
rendercheck: 8 个场景哈希与改造前逐一相同 + FUNC 7/7 PASS, exit=0
A FAED59DF284153DB B E6443BFE4E3955F8 C 96A0AA8C402F1729 D E3D5DBD998E43720
E DA0583CB13C26A6F F FFB822159F604D1D G 19F8DE8FE3A8DF18 H DBBDDE4CB2C97C92
```
体积溯源(`objdump -h`,基线 → 最终):
`.text` 735,600 → 657,168;`.rdata` 49,184 → 35,016;
`.xdata` 48,132 → 27,512;`.pdata` 35,424 → 28,524;`.rsrc` 20,656 不变。
## 没做什么(有意为之)
- **没有拆 DLL**(用户明确要求单文件)
- **没有改优化等级**(`-Oz` 保持)
- **L2(`UiDamage` 脏矩形 + 独立叠加层)评估后缓做**:受益方只有"调试开关打开时的帧率",
而正确实现必须把叠加层旧矩形注入底层组件重绘范围,穿透 PlotView 四条快路径 + 两层缓存;
在用户反复报过"拖拽错位 / 贴图残留"的背景下,风险与收益不成比例。理由写在
`Documents/UI_Refactor_Plan.md` §5 与 `PlotView::SetForceFullRepaint` 的文档里。
878 lines
24 KiB
C++
878 lines
24 KiB
C++
#include "TabBar.hpp"
|
|
|
|
#include "UiControls.hpp"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <functional>
|
|
|
|
#include "UiBox.hpp"
|
|
#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; /**< 单个标签最小宽度(逻辑像素)。 */
|
|
|
|
/**
|
|
* @brief 单个标签页控件:只负责"把这一格画出来"。
|
|
*
|
|
* 手势语义(自行判双击 / 拖拽排序 / 关闭 ✕ 的"移开取消")仍留在 `TabBar` 里 ——
|
|
* 那些是多年补丁叠出的隐式约定,迁移控件时**不能顺手重写**。
|
|
*/
|
|
class TabItemWidget : public UiWidget
|
|
{
|
|
public:
|
|
void Configure(const UiFonts* fonts, const Theme* theme, float scale)
|
|
{
|
|
m_fonts = fonts;
|
|
m_theme = theme;
|
|
m_scale = scale;
|
|
}
|
|
|
|
/** @brief 更新这一格的显示状态(名称 / 激活 / 编辑态 / 关闭按钮几何)。 */
|
|
void SetState(std::wstring name, bool active, gd::RectF closeRect, bool editing,
|
|
const TextEdit* edit, gd::RectF editRect)
|
|
{
|
|
const bool changed = (m_name != name) || (m_active != active) || (m_editing != editing);
|
|
m_name = std::move(name);
|
|
m_active = active;
|
|
m_closeRect = closeRect;
|
|
m_editing = editing;
|
|
m_edit = edit;
|
|
m_editRect = editRect;
|
|
if (changed)
|
|
{
|
|
Invalidate(Bounds());
|
|
}
|
|
else
|
|
{
|
|
// 几何/编辑框位置变化也要重画(重命名时标签会随输入变宽)。
|
|
Invalidate(Bounds());
|
|
}
|
|
}
|
|
|
|
/** @brief 关闭按钮矩形(`TabBar` 用于手势命中;绘制与命中同源)。 */
|
|
const gd::RectF& CloseRect() const { return m_closeRect; }
|
|
|
|
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 || m_fonts == nullptr || ctx.g == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
const gd::RectF& rect = Bounds();
|
|
int hoverX = 0;
|
|
int hoverY = 0;
|
|
const bool hovered = HoverPoint(hoverX, hoverY);
|
|
const bool closeHot =
|
|
hovered && UiPointInRect(m_closeRect, static_cast<float>(hoverX),
|
|
static_cast<float>(hoverY));
|
|
|
|
std::uint32_t background = m_theme->panelBg;
|
|
if (m_active)
|
|
{
|
|
background = m_theme->cardBg;
|
|
}
|
|
else if (hovered)
|
|
{
|
|
background = m_theme->hover;
|
|
}
|
|
UiFillRoundRect(*ctx.g, rect, kCorner * m_scale, background);
|
|
|
|
if (!m_active)
|
|
{
|
|
UiDrawRoundRect(*ctx.g, rect, kCorner * m_scale, m_theme->border, 1.0f);
|
|
}
|
|
else
|
|
{
|
|
// 激活页使用顶部强调条指示。
|
|
UiFillRoundRect(*ctx.g,
|
|
gd::RectF(rect.X + kCorner * m_scale, rect.Y,
|
|
rect.Width - kCorner * 2.0f * m_scale, 2.5f * m_scale),
|
|
1.0f * m_scale, m_theme->accent);
|
|
}
|
|
|
|
const float textLeft = rect.X + kTabPadding * m_scale;
|
|
const float textWidth = m_closeRect.X - kCloseGap * m_scale - textLeft;
|
|
|
|
if (m_editing && m_edit != nullptr)
|
|
{
|
|
// 重命名编辑态:在标签本体之上叠加输入框(标签底色与边框仍然保留)。
|
|
UiDrawEditField(*ctx.g, m_editRect, *m_edit, m_fonts->ui, m_scale);
|
|
}
|
|
else
|
|
{
|
|
const std::wstring label =
|
|
UiEllipsize(m_name, m_fonts->ui, std::max(8.0f, textWidth));
|
|
UiText(*ctx.g, label, m_active ? m_fonts->uiBold : m_fonts->ui,
|
|
gd::RectF(textLeft, rect.Y, std::max(8.0f, textWidth), rect.Height),
|
|
m_active ? m_theme->textStrong : m_theme->text, TextAlign::LEFT);
|
|
}
|
|
|
|
// 关闭按钮:悬停时显示底色,图标始终可见。
|
|
const float closeIconSize = 8.0f * m_scale;
|
|
if (closeHot)
|
|
{
|
|
UiFillRoundRect(*ctx.g, m_closeRect, 3.0f * m_scale, m_theme->pressed);
|
|
}
|
|
const float cx = m_closeRect.X + m_closeRect.Width * 0.5f;
|
|
const float cy = m_closeRect.Y + m_closeRect.Height * 0.5f;
|
|
const std::uint32_t iconColor = closeHot
|
|
? m_theme->danger
|
|
: (m_active ? m_theme->text : m_theme->textMuted);
|
|
UiDrawLine(*ctx.g, cx - closeIconSize * 0.5f, cy - closeIconSize * 0.5f,
|
|
cx + closeIconSize * 0.5f, cy + closeIconSize * 0.5f, iconColor,
|
|
1.4f * m_scale);
|
|
UiDrawLine(*ctx.g, cx + closeIconSize * 0.5f, cy - closeIconSize * 0.5f,
|
|
cx - closeIconSize * 0.5f, cy + closeIconSize * 0.5f, iconColor,
|
|
1.4f * m_scale);
|
|
}
|
|
|
|
private:
|
|
const UiFonts* m_fonts = nullptr; /**< 字体。 */
|
|
const Theme* m_theme = nullptr; /**< 主题。 */
|
|
float m_scale = 1.0f; /**< 缩放系数。 */
|
|
std::wstring m_name; /**< 标签名称。 */
|
|
bool m_active = false; /**< 是否激活。 */
|
|
bool m_editing = false; /**< 是否处于重命名编辑态。 */
|
|
const TextEdit* m_edit = nullptr; /**< 编辑模型(编辑态时非空)。 */
|
|
gd::RectF m_editRect; /**< 编辑框矩形。 */
|
|
gd::RectF m_closeRect; /**< 关闭按钮矩形。 */
|
|
};
|
|
|
|
/** @brief 「+」新建按钮控件。 */
|
|
class TabAddWidget : public UiWidget
|
|
{
|
|
public:
|
|
void Configure(const UiFonts* fonts, const Theme* theme, float scale)
|
|
{
|
|
m_fonts = fonts;
|
|
m_theme = theme;
|
|
m_scale = scale;
|
|
}
|
|
|
|
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)
|
|
{
|
|
return;
|
|
}
|
|
const gd::RectF& rect = Bounds();
|
|
const bool hovered = Hovered();
|
|
const std::uint32_t addBg = hovered ? m_theme->hover : m_theme->panelBg;
|
|
UiFillRoundRect(*ctx.g, rect, kCorner * m_scale, addBg);
|
|
UiDrawRoundRect(*ctx.g, rect, kCorner * m_scale, m_theme->border, 1.0f);
|
|
|
|
const float ax = rect.X + rect.Width * 0.5f;
|
|
const float ay = rect.Y + rect.Height * 0.5f;
|
|
const float armLength = 4.5f * m_scale;
|
|
const std::uint32_t addColor = hovered ? m_theme->accent : m_theme->text;
|
|
UiDrawLine(*ctx.g, ax - armLength, ay, ax + armLength, ay, addColor, 1.6f * m_scale);
|
|
UiDrawLine(*ctx.g, ax, ay - armLength, ax, ay + armLength, addColor, 1.6f * m_scale);
|
|
}
|
|
|
|
private:
|
|
const UiFonts* m_fonts = nullptr; /**< 字体(当前未使用,保留给将来的图标)。 */
|
|
const Theme* m_theme = nullptr; /**< 主题。 */
|
|
float m_scale = 1.0f; /**< 缩放系数。 */
|
|
};
|
|
|
|
/**
|
|
* @brief 覆盖层控件:画"拖拽插入指示条 + 底部分隔线"。
|
|
*
|
|
* 它必须**最后绘制**(在标签与「+」之后),因此作为根节点的最后一个子节点,
|
|
* 且**命中透明**(纯装饰,不参与命中与光标链)。
|
|
*/
|
|
class TabOverlayWidget : public UiWidget
|
|
{
|
|
public:
|
|
gd::SizeF OnMeasure(const UiMeasureContext& ctx) override
|
|
{
|
|
(void)ctx;
|
|
return gd::SizeF(Bounds().Width, Bounds().Height);
|
|
}
|
|
|
|
void OnPaint(UiPaintContext& ctx) override
|
|
{
|
|
if (ctx.g == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
if (paintOverlay)
|
|
{
|
|
paintOverlay(*ctx.g, Bounds());
|
|
}
|
|
}
|
|
|
|
/** @brief 由 `TabBar` 注入的覆盖层绘制回调(拖拽指示条 + 分隔线)。 */
|
|
std::function<void(gd::Graphics&, const gd::RectF&)> paintOverlay;
|
|
};
|
|
|
|
/** @brief 标签栏根:只负责底色(其余交给子节点按 z 序绘制)。 */
|
|
class TabRootWidget : public UiWidget
|
|
{
|
|
public:
|
|
void Configure(const Theme* theme)
|
|
{
|
|
m_theme = theme;
|
|
}
|
|
|
|
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;
|
|
}
|
|
UiFillRect(*ctx.g, Bounds(), m_theme->panelBg);
|
|
}
|
|
|
|
private:
|
|
const Theme* m_theme = nullptr; /**< 主题。 */
|
|
};
|
|
|
|
} // namespace
|
|
|
|
TabBar::TabBar() = default;
|
|
|
|
// 控件树用 `std::unique_ptr<UiWidget>` 持有,而具体根类型只在本 TU 可见
|
|
// ⇒ 析构函数必须在这里显式定义。
|
|
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;
|
|
}
|
|
// 标签数量变化 ⇒ 重建控件树(旧控件里缓存的几何与名称全部作废)。
|
|
RebuildWidgets();
|
|
}
|
|
|
|
void TabBar::SetActiveIndex(int index)
|
|
{
|
|
if (m_activeIndex == index)
|
|
{
|
|
return;
|
|
}
|
|
m_activeIndex = index;
|
|
ArrangeWidgets();
|
|
}
|
|
|
|
float TabBar::DesiredHeight() const
|
|
{
|
|
return (kTabHeight + kTopMargin) * m_scale;
|
|
}
|
|
|
|
void TabBar::RebuildWidgets()
|
|
{
|
|
m_root = std::make_unique<TabRootWidget>();
|
|
static_cast<TabRootWidget*>(m_root.get())->Configure(m_theme);
|
|
|
|
for (std::size_t index = 0; index < m_tabs.size(); ++index)
|
|
{
|
|
auto widget = std::make_unique<TabItemWidget>();
|
|
static_cast<TabItemWidget*>(widget.get())->Configure(m_fonts, m_theme, m_scale);
|
|
m_root->AddChild(std::move(widget));
|
|
}
|
|
|
|
auto add = std::make_unique<TabAddWidget>();
|
|
static_cast<TabAddWidget*>(add.get())->Configure(m_fonts, m_theme, m_scale);
|
|
m_root->AddChild(std::move(add));
|
|
|
|
auto overlay = std::make_unique<TabOverlayWidget>();
|
|
TabOverlayWidget* overlayRaw = static_cast<TabOverlayWidget*>(overlay.get());
|
|
overlayRaw->SetHitTransparent(true);
|
|
overlayRaw->paintOverlay = [this](gd::Graphics& g, const gd::RectF& area)
|
|
{
|
|
// 拖拽排序时在插入位置绘制强调色指示条。
|
|
if (m_dragIndex >= 0 && m_dragMoved && !m_tabs.empty() && m_theme != nullptr)
|
|
{
|
|
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 = 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);
|
|
}
|
|
// 底部分隔线。
|
|
if (m_theme != nullptr)
|
|
{
|
|
UiDrawLine(g, area.X, area.Y + area.Height - 1.0f, area.X + area.Width,
|
|
area.Y + area.Height - 1.0f, m_theme->border, 1.0f);
|
|
}
|
|
};
|
|
m_root->AddChild(std::move(overlay));
|
|
}
|
|
|
|
void TabBar::Layout(const gd::RectF& area)
|
|
{
|
|
m_area = area;
|
|
if (m_fonts == nullptr || m_fonts->ui == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
if (m_root == nullptr)
|
|
{
|
|
RebuildWidgets();
|
|
}
|
|
|
|
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);
|
|
|
|
ArrangeWidgets();
|
|
}
|
|
|
|
void TabBar::ArrangeWidgets()
|
|
{
|
|
if (m_root == nullptr || m_root->Children().size() != m_tabs.size() + 2)
|
|
{
|
|
RebuildWidgets();
|
|
}
|
|
if (m_root == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_root->Arrange(m_area);
|
|
const std::vector<std::unique_ptr<UiWidget>>& children = m_root->Children();
|
|
for (std::size_t index = 0; index < m_tabs.size(); ++index)
|
|
{
|
|
TabItemWidget* widget = static_cast<TabItemWidget*>(children[index].get());
|
|
const TabItem& tab = m_tabs[index];
|
|
const bool editing = (static_cast<int>(index) == m_editIndex);
|
|
widget->Arrange(tab.rect);
|
|
widget->SetState(tab.name, static_cast<int>(index) == m_activeIndex, tab.closeRect, editing,
|
|
editing ? &m_edit : nullptr, EditRect());
|
|
}
|
|
static_cast<TabAddWidget*>(children[m_tabs.size()].get())->Arrange(m_addRect);
|
|
static_cast<TabOverlayWidget*>(children[m_tabs.size() + 1].get())->Arrange(m_area);
|
|
}
|
|
|
|
void TabBar::Paint(gd::Graphics& g)
|
|
{
|
|
if (m_theme == nullptr || m_fonts == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
if (m_root == nullptr)
|
|
{
|
|
RebuildWidgets();
|
|
ArrangeWidgets();
|
|
}
|
|
if (m_root == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
UiPaintContext ctx;
|
|
ctx.g = &g;
|
|
ctx.clip = m_area;
|
|
ctx.scale = m_scale;
|
|
m_root->Paint(ctx);
|
|
}
|
|
|
|
bool TabBar::HitTest(int x, int y) const
|
|
{
|
|
return UiPointInRect(m_area, static_cast<float>(x), static_cast<float>(y));
|
|
}
|
|
|
|
void TabBar::BuildBoxes(UiBoxTable& boxes)
|
|
{
|
|
UiComponent::BuildBoxes(boxes);
|
|
if (m_root != nullptr)
|
|
{
|
|
// ⚠️ 跳过根控件:它的矩形与标签栏区域完全重合(已被上一层登记过)。
|
|
// 从控件树的**子节点**(各标签条 / 新建按钮)开始,层级 1。
|
|
for (const std::unique_ptr<UiWidget>& child : m_root->Children())
|
|
{
|
|
if (child != nullptr)
|
|
{
|
|
child->BuildBoxes(boxes, 1);
|
|
}
|
|
}
|
|
}
|
|
// 关闭按钮是「标签条内部的子块」,控件树里没有对应节点(由标签条控件自绘)⇒ 单独登记。
|
|
for (const TabItem& tab : m_tabs)
|
|
{
|
|
boxes.Add(tab.closeRect, 2);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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 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)
|
|
{
|
|
// 关闭 ✕ 只在"按下后仍在 ✕ 上抬起"时生效:按住后移开再松开视为放弃
|
|
// (与信号面板的分组/信号 ✕ 保持一致的交互语义)。
|
|
if (UiPointInRect(m_tabs[static_cast<std::size_t>(index)].closeRect,
|
|
static_cast<float>(x), static_cast<float>(y)))
|
|
{
|
|
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()))
|
|
{
|
|
return gd::RectF();
|
|
}
|
|
const TabItem& tab = m_tabs[static_cast<std::size_t>(m_editIndex)];
|
|
const float pad = 4.0f * m_scale;
|
|
const float textLeft = tab.rect.X + kTabPadding * m_scale;
|
|
const float textRight = tab.closeRect.X - kCloseGap * m_scale;
|
|
return gd::RectF(textLeft, tab.rect.Y + pad, std::max(8.0f, textRight - textLeft),
|
|
tab.rect.Height - pad * 2.0f);
|
|
}
|
|
|
|
bool TabBar::OnEditMouseDown(int x, int y)
|
|
{
|
|
if (m_editIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!UiPointInRect(EditRect(), static_cast<float>(x), static_cast<float>(y)))
|
|
{
|
|
return false;
|
|
}
|
|
UiEditFieldMouseDown(m_fonts->ui, EditRect(), m_edit, m_scale, x, y);
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnEditMouseMove(int x)
|
|
{
|
|
if (m_editIndex < 0)
|
|
{
|
|
return false;
|
|
}
|
|
UiEditFieldMouseMove(m_fonts->ui, EditRect(), m_edit, m_scale, x);
|
|
return true;
|
|
}
|
|
|
|
bool TabBar::OnEditMouseUp()
|
|
{
|
|
if (m_editIndex < 0)
|
|
{
|
|
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_root == nullptr)
|
|
{
|
|
return false;
|
|
}
|
|
UiEvent event;
|
|
event.kind = UiEventKind::MOUSE_LEAVE;
|
|
event.x = 0;
|
|
event.y = 0;
|
|
m_root->Dispatch(event);
|
|
return UiRectHasArea(m_root->TakeDirty());
|
|
}
|
|
|
|
} // namespace jdi
|