|
|
|
@@ -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
|