UI 重构 P3:Toolbar 迁移到控件树(新增 UiButtonWidget,逐像素 0 差异 + 功能断言)
新增 Components/Shared/{Inc,Src}/UiButtonWidget.*:通用「文字按钮」控件,视觉规则与旧 Toolbar
按钮逐像素一致(主按钮强调色实底 / 普通按钮 1px 描边 / 可切换选中态 / 禁用态配色);
交互由控件自己处理:按下即 CaptureMouse 进入按下态,在自身范围内松开才触发 onClick,
指针离开(不会再收到抬起)时 CancelPress。
Toolbar 内部重写,公开 API 一字未改:
- Button 只保留静态配置 + 控件指针;矩形与外观状态归控件。
- 删除 m_hoverIndex / m_pressedIndex 与死字段 Button::hover,改由 UiWidget 的悬停链与 capture 维护。
- Paint 改为「根控件(底色 + 底部分隔线)+ 按钮子节点」,一次 root->Paint(ctx)。
- OnMouseMove/Down/Up/Leave 统一翻译成 UiEvent 后 root->Dispatch;动作经 onClick 回调登记、抬起后执行。
框架约定调整:UiWidget::Paint 的裁剪从「严格等于自身矩形」改为「自身矩形外扩 1px(kUiClipBleed)」。
边框/描边以矩形边界为中心绘制,严格裁剪会切掉外半侧使控件变细、与旧绘制不再像素一致。
uicheck 对应断言改为「裁剪 ⊇ 自身矩形且不超出外扩范围」。
验证:
- make release / make debug 零警告;Target.exe 881,664 B(< 900KB)。
- 逐像素 A/B(8 场景:全览 / 深放大+自由标记+滤波+弱峰+光标 / 平移 / 悬停按钮 / 按下 / 松开 /
悬停主按钮 / 移出工具栏):迁移前后哈希逐一相同
A 2166933039626F66 B C444ECFEB18FD73D C 7984446D8E7A4418 D BF855879CA12F289
E 1C663E4118616016 F 0535292D8E07AE18 G 7A58172C46577729 H 23F390DD76141113。
- 功能断言:locate-cursor-button PASS (1291,73);click-cursor-changes-mode PASS (1 -> 2);
release-outside-does-not-fire PASS (2 -> 2)。
- Tools/uicheck 62 passed / 0 failed。
This commit is contained in:
1 parent
7e1852c2d2
commit
25264f9074
9 files changed
+663
-223
No files matched your search
@@ -5,11 +5,15 @@
|
||||
#include <objidl.h>
|
||||
#include <gdiplus.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "PaintUtil.hpp"
|
||||
#include "Theme.hpp"
|
||||
#include "UiButtonWidget.hpp"
|
||||
#include "UiWidget.hpp"
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
@@ -37,6 +41,7 @@ namespace jdi
|
||||
{
|
||||
public:
|
||||
Toolbar();
|
||||
~Toolbar();
|
||||
|
||||
/**
|
||||
* @brief 绑定字体与主题资源。
|
||||
@@ -135,19 +140,17 @@ namespace jdi
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief 单个工具栏按钮的运行时状态。
|
||||
* @brief 单个工具栏按钮的**静态配置**(矩形与外观状态由控件自己维护)。
|
||||
*/
|
||||
struct Button
|
||||
{
|
||||
ToolbarAction action = ToolbarAction::NONE; /**< 按钮动作。 */
|
||||
std::wstring label; /**< 按钮文本。 */
|
||||
gd::RectF rect; /**< 按钮矩形。 */
|
||||
float width = 0.0f; /**< 按钮宽度。 */
|
||||
UiButtonWidget* widget = nullptr; /**< 对应控件(由 `m_root` 拥有)。 */
|
||||
bool primary = false; /**< 是否为主按钮(强调色填充)。 */
|
||||
bool needsDocument = true; /**< 是否依赖已加载数据。 */ bool toggle = false; /**< 是否为可切换按钮。 */
|
||||
bool needsDocument = true; /**< 是否依赖已加载数据。 */
|
||||
bool toggle = false; /**< 是否为可切换按钮。 */
|
||||
bool checked = false; /**< 可切换按钮的选中状态。 */
|
||||
bool hover = false; /**< 悬停状态(当前由 `Toolbar::m_hoverIndex` 派生,字段保留供未来使用)。 */
|
||||
bool pressed = false; /**< 按下状态。 */
|
||||
float width = 0.0f; /**< 按钮宽度(由控件实测得出)。 */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -157,15 +160,20 @@ namespace jdi
|
||||
*/
|
||||
bool IsEnabled(const Button& button) const;
|
||||
|
||||
/** @brief 按固定规格表重建控件树(文本变化/首次布局时调用)。 */
|
||||
void RebuildWidgets();
|
||||
/** @brief 把可用性同步到控件(未加载数据时禁用依赖数据的按钮)。 */
|
||||
void SyncEnabledState();
|
||||
|
||||
const UiFonts* m_fonts = nullptr; /**< 字体资源。 */
|
||||
const Theme* m_theme = nullptr; /**< 主题资源。 */
|
||||
float m_scale = 1.0f; /**< DPI 缩放比例。 */
|
||||
bool m_documentLoaded = false; /**< 是否已加载数据。 */
|
||||
std::vector<Button> m_buttons; /**< 按钮集合。 */
|
||||
std::vector<Button> m_buttons; /**< 按钮配置集合。 */
|
||||
std::vector<std::pair<ToolbarAction, std::wstring>> m_labelOverrides; /**< 动态文本覆盖表。 */
|
||||
gd::RectF m_area; /**< 工具栏整体区域。 */
|
||||
int m_hoverIndex = -1; /**< 当前悬停按钮下标。 */
|
||||
int m_pressedIndex = -1; /**< 当前按下按钮下标。 */
|
||||
std::unique_ptr<UiWidget> m_root; /**< 控件树根(负责底色与底部分隔线)。 */
|
||||
ToolbarAction m_pendingAction = ToolbarAction::NONE; /**< 最近一次成功点击的动作。 */
|
||||
};
|
||||
|
||||
} // namespace jdi
|
||||
|
||||
+244
-199
@@ -1,8 +1,8 @@
|
||||
#include "Toolbar.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
#include "Utf8Util.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace jdi
|
||||
{
|
||||
@@ -13,13 +13,78 @@ namespace jdi
|
||||
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; /**< 工具栏上下留白。 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @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; /**< 是否可切换。 */
|
||||
};
|
||||
|
||||
const ButtonSpec kButtonSpecs[] = {
|
||||
{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},
|
||||
};
|
||||
} // 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;
|
||||
@@ -33,7 +98,12 @@ namespace jdi
|
||||
|
||||
void Toolbar::SetDocumentLoaded(bool hasDocument)
|
||||
{
|
||||
if (m_documentLoaded == hasDocument)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_documentLoaded = hasDocument;
|
||||
SyncEnabledState();
|
||||
}
|
||||
|
||||
float Toolbar::DesiredHeight() const
|
||||
@@ -46,92 +116,111 @@ namespace jdi
|
||||
return button.needsDocument ? m_documentLoaded : true;
|
||||
}
|
||||
|
||||
void Toolbar::Layout(const gd::RectF& area)
|
||||
void Toolbar::SyncEnabledState()
|
||||
{
|
||||
m_area = area;
|
||||
|
||||
// 重新排布会重建全部按钮:先记下当前各 toggle 的勾选状态,
|
||||
// 否则每次因文本变化(如「光标x2」)触发的 Layout 会把按钮选中态全部清掉。
|
||||
std::vector<std::pair<ToolbarAction, bool>> previousChecked;
|
||||
for (const Button& button : m_buttons)
|
||||
for (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 (button.widget != nullptr)
|
||||
{
|
||||
if (entry.first == action)
|
||||
{
|
||||
return entry.second;
|
||||
}
|
||||
button.widget->SetEnabled(IsEnabled(button));
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void Toolbar::RebuildWidgets()
|
||||
{
|
||||
m_root = std::make_unique<ToolbarRootWidget>();
|
||||
static_cast<ToolbarRootWidget*>(m_root.get())->Configure(m_theme, m_scale);
|
||||
|
||||
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` 快照,这里只需预留容量。)
|
||||
const std::size_t specCount = sizeof(kButtonSpecs) / sizeof(kButtonSpecs[0]);
|
||||
m_buttons.reserve(specCount);
|
||||
|
||||
for (const Spec& spec : specs)
|
||||
for (std::size_t index = 0; index < specCount; ++index)
|
||||
{
|
||||
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);
|
||||
|
||||
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)
|
||||
{
|
||||
button.label = entry.second;
|
||||
label = entry.second;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
button.width = horizontalPadding + UiMeasureTextWidth(m_fonts->ui, button.label);
|
||||
m_buttons.push_back(std::move(button));
|
||||
Button button;
|
||||
button.action = spec.action;
|
||||
button.primary = spec.primary;
|
||||
button.needsDocument = spec.needsDocument;
|
||||
button.toggle = spec.toggle;
|
||||
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);
|
||||
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 的勾选状态,否则每次因文本变化(如「光标x2」)
|
||||
// 触发的 Layout 会把按钮选中态全部清掉。
|
||||
std::vector<std::pair<ToolbarAction, bool>> previousChecked;
|
||||
previousChecked.reserve(m_buttons.size());
|
||||
for (const Button& button : m_buttons)
|
||||
{
|
||||
previousChecked.emplace_back(button.action, button.checked);
|
||||
}
|
||||
|
||||
RebuildWidgets();
|
||||
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 groupWidth = 0.0f;
|
||||
for (std::size_t index = 1; index < m_buttons.size(); ++index)
|
||||
{
|
||||
@@ -143,13 +232,13 @@ namespace jdi
|
||||
}
|
||||
|
||||
float cursorX = leftX;
|
||||
m_buttons[0].rect = gd::RectF(cursorX, top, m_buttons[0].width, height);
|
||||
m_buttons[0].widget->Arrange(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);
|
||||
m_buttons[index].widget->Arrange(gd::RectF(cursorX, top, m_buttons[index].width, height));
|
||||
cursorX += m_buttons[index].width + kButtonGap * m_scale;
|
||||
}
|
||||
}
|
||||
@@ -161,6 +250,10 @@ namespace jdi
|
||||
if (button.action == action && button.toggle)
|
||||
{
|
||||
button.checked = checked;
|
||||
if (button.widget != nullptr)
|
||||
{
|
||||
button.widget->SetChecked(checked);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -177,12 +270,11 @@ namespace jdi
|
||||
return false;
|
||||
}
|
||||
entry.second = label;
|
||||
// 已排布过时同步更新按钮文本,宿主随后会调用 Layout() 重新计算宽度。
|
||||
for (Button& button : m_buttons)
|
||||
{
|
||||
if (button.action == action)
|
||||
if (button.action == action && button.widget != nullptr)
|
||||
{
|
||||
button.label = label;
|
||||
button.widget->SetLabel(label);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -191,9 +283,9 @@ namespace jdi
|
||||
m_labelOverrides.emplace_back(action, label);
|
||||
for (Button& button : m_buttons)
|
||||
{
|
||||
if (button.action == action)
|
||||
if (button.action == action && button.widget != nullptr)
|
||||
{
|
||||
button.label = label;
|
||||
button.widget->SetLabel(label);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -201,84 +293,16 @@ namespace jdi
|
||||
|
||||
void Toolbar::Paint(gd::Graphics& g)
|
||||
{
|
||||
if (m_theme == nullptr || m_fonts == nullptr)
|
||||
if (m_theme == nullptr || m_fonts == nullptr || m_root == 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);
|
||||
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
|
||||
@@ -288,44 +312,42 @@ namespace jdi
|
||||
|
||||
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)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_hoverIndex = hoverIndex;
|
||||
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 Toolbar::OnLButtonDown(int x, int y, ToolbarAction& action)
|
||||
{
|
||||
action = ToolbarAction::NONE;
|
||||
|
||||
for (std::size_t index = 0; index < m_buttons.size(); ++index)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
Button& button = m_buttons[index];
|
||||
if (!UiPointInRect(button.rect, static_cast<float>(x), static_cast<float>(y)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!IsEnabled(button))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
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();
|
||||
|
||||
m_pressedIndex = static_cast<int>(index);
|
||||
button.pressed = true;
|
||||
action = button.action;
|
||||
return true;
|
||||
for (const Button& button : m_buttons)
|
||||
{
|
||||
if (button.widget != nullptr && button.widget->Pressed())
|
||||
{
|
||||
action = button.action;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -333,47 +355,70 @@ namespace jdi
|
||||
bool Toolbar::OnLButtonUp(int x, int y, ToolbarAction& action)
|
||||
{
|
||||
action = ToolbarAction::NONE;
|
||||
if (m_pressedIndex < 0)
|
||||
if (m_root == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const int index = m_pressedIndex;
|
||||
m_pressedIndex = -1;
|
||||
if (index >= static_cast<int>(m_buttons.size()))
|
||||
// 旧语义:没有按下过就不进入抬起处理。
|
||||
bool hadPressed = false;
|
||||
for (const Button& button : m_buttons)
|
||||
{
|
||||
if (button.widget != nullptr && button.widget->Pressed())
|
||||
{
|
||||
hadPressed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hadPressed)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Button& button = m_buttons[static_cast<std::size_t>(index)];
|
||||
button.pressed = 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);
|
||||
|
||||
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;
|
||||
}
|
||||
// 旧实现会在抬起时按落点重算悬停态;派发一条 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;
|
||||
if (m_hoverIndex >= 0)
|
||||
for (Button& button : m_buttons)
|
||||
{
|
||||
m_hoverIndex = -1;
|
||||
changed = true;
|
||||
}
|
||||
if (m_pressedIndex >= 0)
|
||||
{
|
||||
if (m_pressedIndex < static_cast<int>(m_buttons.size()))
|
||||
if (button.widget != nullptr && button.widget->Pressed())
|
||||
{
|
||||
m_buttons[static_cast<std::size_t>(m_pressedIndex)].pressed = false;
|
||||
button.widget->CancelPress();
|
||||
changed = true;
|
||||
}
|
||||
m_pressedIndex = -1;
|
||||
changed = true;
|
||||
}
|
||||
changed = UiRectHasArea(m_root->TakeDirty()) || changed;
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user