UI 重构 P3(2):TabBar 迁移到控件树(逐像素 0 差异 + 4 条交互断言)
TabBar 的标签 / 「+」/ 覆盖层拆成控件,手势语义原样保留(自行判双击、3px 拖拽阈值、
关闭 ✕ 的「移开取消」、拖拽排序的 drop 位置),公开 API 未变:
- 新增(TabBar.cpp 内实现的内部控件)
* UiTabItemWidget:单格标签的绘制(底色/描边/激活指示条/文本或重命名输入框/关闭 ✕),
关闭按钮热区由基类的悬停点自行判定。
* UiAddWidget:「+」按钮绘制与悬停配色。
* UiOverlayWidget:命中透明的覆盖层,最后绘制「拖拽插入指示条 + 底部分隔线」
(必须晚于标签与「+」,否则会被它们压住)。
* UiTabRootWidget:只铺标签栏底色。
- TabBar 移除 m_hoverIndex / m_hoverClose / m_hoverAdd 三个手工下标,
改为 dispatch MOUSE_MOVE 后由 root->TakeDirty() 判断是否需要重绘。
- 绘制顺序保持:底色 → 各标签 → 「+」→ 拖拽指示条 → 底部分隔线(与旧实现逐像素一致)。
- UiWidget 新增 HoverPoint(x,y):派生控件据此自行判断「悬停在自身内部哪个热区」。
验证:
- 像素:8 场景哈希与迁移前逐一相同(A 2166933…、H 23F390D…)。
- 交互断言(全部 PASS):
* tabbar-hover-marks-dirty (1,0) —— 进入标签只标记一次脏区;
* tabbar-add-creates-tab (1 -> 2) —— 「+」新建标签;
* tabbar-click-selects-tab (active=0) —— 点击标签切换激活页;
* tabbar-close-removes-tab (2 -> 1) —— 点击 ✕ 关闭标签。
- Tools/uicheck 69 passed / 0 failed。
- make release / make debug 零警告;Target.exe 886,784 B(< 900KB)。
This commit is contained in:
1 parent
c4faa930e2
commit
c3b79a6d2c
6 files changed
+429
-127
No files matched your search
@@ -244,6 +244,13 @@ namespace jdi
|
||||
UiWidget* HoverWidget() const;
|
||||
/** @brief 本控件当前是否被悬停(由根节点的悬停链维护)。 */
|
||||
bool Hovered() const { return m_hovered; }
|
||||
/**
|
||||
* @brief 本控件最近一次收到的悬停点(客户区坐标)。
|
||||
*
|
||||
* 派生控件据此自行判断"悬停在自身内部的哪个热区"(例如标签的关闭 ✕);
|
||||
* 未悬停时返回 false,出参不变。
|
||||
*/
|
||||
bool HoverPoint(int& x, int& y) const;
|
||||
|
||||
/** @brief 请求键盘焦点(根节点记录;键盘/字符事件沿焦点链派发)。 */
|
||||
void RequestFocus();
|
||||
@@ -326,6 +333,8 @@ namespace jdi
|
||||
bool m_hitTransparent = false; /**< 是否命中透明。 */
|
||||
bool m_measureValid = false; /**< 期望尺寸是否有效。 */
|
||||
bool m_hovered = false; /**< 是否被悬停(悬停链写入)。 */
|
||||
int m_hoverX = 0; /**< 最近一次悬停点横坐标(悬停链写入)。 */
|
||||
int m_hoverY = 0; /**< 最近一次悬停点纵坐标(悬停链写入)。 */
|
||||
|
||||
// 根节点专用状态。
|
||||
UiWidget* m_capture = nullptr; /**< 鼠标捕获者。 */
|
||||
|
||||
@@ -481,6 +481,8 @@ namespace jdi
|
||||
if (target != nullptr)
|
||||
{
|
||||
target->m_hovered = true;
|
||||
target->m_hoverX = x;
|
||||
target->m_hoverY = y;
|
||||
target->OnHoverChanged(true);
|
||||
target->Invalidate(target->m_bounds);
|
||||
}
|
||||
@@ -543,6 +545,17 @@ namespace jdi
|
||||
return (root != nullptr) ? root->m_hover : nullptr;
|
||||
}
|
||||
|
||||
bool UiWidget::HoverPoint(int& x, int& y) const
|
||||
{
|
||||
if (!m_hovered)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
x = m_hoverX;
|
||||
y = m_hoverY;
|
||||
return true;
|
||||
}
|
||||
|
||||
void UiWidget::RequestFocus()
|
||||
{
|
||||
UiWidget* root = Root();
|
||||
|
||||
@@ -5,11 +5,13 @@
|
||||
#include <objidl.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "PaintUtil.hpp"
|
||||
#include "Theme.hpp"
|
||||
#include "UiWidget.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
@@ -48,6 +50,7 @@ namespace jdi
|
||||
{
|
||||
public:
|
||||
TabBar();
|
||||
~TabBar();
|
||||
|
||||
/**
|
||||
* @brief 绑定字体与主题资源。
|
||||
@@ -240,14 +243,11 @@ namespace jdi
|
||||
const Theme* m_theme = nullptr; /**< 主题资源。 */
|
||||
float m_scale = 1.0f; /**< DPI 缩放比例。 */
|
||||
|
||||
std::vector<TabItem> m_tabs; /**< 标签页布局集合。 */
|
||||
std::vector<TabItem> m_tabs; /**< 标签页数据(名称 + 几何;几何由控件回写)。 */
|
||||
gd::RectF m_addRect; /**< 新建按钮矩形。 */
|
||||
gd::RectF m_area; /**< 标签栏整体区域。 */
|
||||
|
||||
int m_activeIndex = -1; /**< 当前激活标签页下标。 */
|
||||
int m_hoverIndex = -1; /**< 当前悬停标签页下标。 */
|
||||
bool m_hoverClose = false; /**< 是否悬停在关闭按钮上。 */
|
||||
bool m_hoverAdd = false; /**< 是否悬停在新建按钮上。 */
|
||||
int m_pressedIndex = -1; /**< 当前按下的标签页下标。 */
|
||||
bool m_pressedClose = false; /**< 是否按在关闭按钮上。 */
|
||||
bool m_pressedAdd = false; /**< 是否按在新建按钮上。 */
|
||||
@@ -263,6 +263,13 @@ namespace jdi
|
||||
|
||||
int m_lastClickIndex = -1; /**< 上一次单击的标签页下标(用于自行判定双击)。 */
|
||||
DWORD m_lastClickTick = 0; /**< 上一次单击的时刻。 */
|
||||
|
||||
/** 控件树根:负责底色与底部分隔线,子节点是各标签与「+」按钮。 */
|
||||
std::unique_ptr<UiWidget> m_root;
|
||||
/** 重建控件树(标签数量变化时调用)。 */
|
||||
void RebuildWidgets();
|
||||
/** 把标签数据/激活态/编辑态同步给控件并摆放。 */
|
||||
void ArrangeWidgets();
|
||||
};
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
+370
-122
@@ -3,6 +3,7 @@
|
||||
#include "UiControls.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <functional>
|
||||
|
||||
#include "Utf8Util.hpp"
|
||||
|
||||
@@ -23,10 +24,250 @@ namespace jdi
|
||||
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;
|
||||
@@ -50,13 +291,18 @@ namespace jdi
|
||||
{
|
||||
m_activeIndex = m_tabs.empty() ? -1 : static_cast<int>(m_tabs.size()) - 1;
|
||||
}
|
||||
m_hoverIndex = -1;
|
||||
m_hoverClose = false;
|
||||
// 标签数量变化 ⇒ 重建控件树(旧控件里缓存的几何与名称全部作废)。
|
||||
RebuildWidgets();
|
||||
}
|
||||
|
||||
void TabBar::SetActiveIndex(int index)
|
||||
{
|
||||
if (m_activeIndex == index)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_activeIndex = index;
|
||||
ArrangeWidgets();
|
||||
}
|
||||
|
||||
float TabBar::DesiredHeight() const
|
||||
@@ -64,6 +310,52 @@ namespace jdi
|
||||
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;
|
||||
@@ -71,6 +363,10 @@ namespace jdi
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
RebuildWidgets();
|
||||
}
|
||||
|
||||
const float tabHeight = kTabHeight * m_scale;
|
||||
const float top = area.Y + kTopMargin * m_scale;
|
||||
@@ -129,6 +425,34 @@ namespace jdi
|
||||
|
||||
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)
|
||||
@@ -137,103 +461,21 @@ namespace jdi
|
||||
{
|
||||
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)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
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)
|
||||
{
|
||||
UiDrawEditField(g, EditRect(), m_edit, m_fonts->ui, m_scale);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::wstring label = UiEllipsize(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);
|
||||
RebuildWidgets();
|
||||
ArrangeWidgets();
|
||||
}
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 拖拽排序时在插入位置绘制强调色指示条。
|
||||
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);
|
||||
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
|
||||
@@ -257,7 +499,7 @@ namespace jdi
|
||||
|
||||
bool TabBar::OnMouseMove(int x, int y)
|
||||
{
|
||||
// 拖动排序中:只跟踪鼠标位置,由界面层绘制插入指示。
|
||||
// 拖动排序中:只跟踪鼠标位置,由覆盖层绘制插入指示。
|
||||
if (m_dragIndex >= 0)
|
||||
{
|
||||
if (x != m_dragMouseX)
|
||||
@@ -269,20 +511,18 @@ namespace jdi
|
||||
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)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_hoverIndex = index;
|
||||
m_hoverClose = overClose;
|
||||
m_hoverAdd = overAdd;
|
||||
return true;
|
||||
UiEvent event;
|
||||
event.kind = UiEventKind::MOUSE_MOVE;
|
||||
event.x = x;
|
||||
event.y = y;
|
||||
m_root->Dispatch(event);
|
||||
// 悬停链切换会通过 `Invalidate` 冒泡到根 ⇒ 用它判断是否需要重绘
|
||||
//(与旧实现"悬停下标/关闭/新建变化才重绘"等价,但不再手工维护三个下标)。
|
||||
return UiRectHasArea(m_root->TakeDirty());
|
||||
}
|
||||
|
||||
bool TabBar::OnLButtonDown(int x, int y)
|
||||
@@ -535,16 +775,16 @@ namespace jdi
|
||||
|
||||
gd::RectF TabBar::EditRect() const
|
||||
{
|
||||
if (m_editIndex < 0 || m_editIndex >= static_cast<int>(m_tabs.size()) || m_fonts == nullptr)
|
||||
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 textWidth = tab.closeRect.X - kCloseGap * m_scale - textLeft;
|
||||
return gd::RectF(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);
|
||||
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)
|
||||
@@ -553,7 +793,12 @@ namespace jdi
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return UiEditFieldMouseDown(m_fonts->ui, EditRect(), m_edit, m_scale, x, y);
|
||||
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)
|
||||
@@ -562,12 +807,13 @@ namespace jdi
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return UiEditFieldMouseMove(m_fonts->ui, EditRect(), m_edit, m_scale, x);
|
||||
UiEditFieldMouseMove(m_fonts->ui, EditRect(), m_edit, m_scale, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TabBar::OnEditMouseUp()
|
||||
{
|
||||
if (!m_edit.Dragging())
|
||||
if (m_editIndex < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -594,14 +840,16 @@ namespace jdi
|
||||
|
||||
bool TabBar::OnMouseLeave()
|
||||
{
|
||||
if (m_hoverIndex < 0 && !m_hoverClose && !m_hoverAdd)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_hoverIndex = -1;
|
||||
m_hoverClose = false;
|
||||
m_hoverAdd = false;
|
||||
return true;
|
||||
UiEvent event;
|
||||
event.kind = UiEventKind::MOUSE_LEAVE;
|
||||
event.x = 0;
|
||||
event.y = 0;
|
||||
m_root->Dispatch(event);
|
||||
return UiRectHasArea(m_root->TakeDirty());
|
||||
}
|
||||
|
||||
} // namespace jdi
|
||||
Reference in new issue
Block a user