新增 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。
232 lines
5.5 KiB
C++
232 lines
5.5 KiB
C++
#include "UiButtonWidget.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
#include "PaintUtil.hpp"
|
|
#include "Theme.hpp"
|
|
|
|
namespace jdi
|
|
{
|
|
|
|
namespace
|
|
{
|
|
const float kButtonRadius = 5.0f; /**< 按钮圆角半径(逻辑像素)。 */
|
|
|
|
/** @brief 按比例把一个颜色向目标色混合(与旧 Toolbar 的写法保持一致)。 */
|
|
std::uint32_t BlendToward(std::uint32_t base, std::uint32_t target, double ratio)
|
|
{
|
|
return MixColor(base, target, ratio);
|
|
}
|
|
}
|
|
|
|
void UiButtonWidget::Configure(const UiFonts* fonts, const Theme* theme, float scale,
|
|
std::wstring label, bool primary, bool toggle, float textPadding,
|
|
float height)
|
|
{
|
|
m_fonts = fonts;
|
|
m_theme = theme;
|
|
m_scale = (scale > 0.1f) ? scale : 1.0f;
|
|
m_label = std::move(label);
|
|
m_primary = primary;
|
|
m_toggle = toggle;
|
|
m_textPadding = textPadding;
|
|
m_height = height;
|
|
InvalidateMeasure();
|
|
Invalidate(Bounds());
|
|
}
|
|
|
|
void UiButtonWidget::SetLabel(std::wstring label)
|
|
{
|
|
if (m_label == label)
|
|
{
|
|
return;
|
|
}
|
|
m_label = std::move(label);
|
|
InvalidateMeasure();
|
|
Invalidate(Bounds());
|
|
}
|
|
|
|
void UiButtonWidget::SetChecked(bool checked)
|
|
{
|
|
if (m_checked == checked)
|
|
{
|
|
return;
|
|
}
|
|
m_checked = checked;
|
|
Invalidate(Bounds());
|
|
}
|
|
|
|
void UiButtonWidget::SetEnabled(bool enabled)
|
|
{
|
|
if (m_enabled == enabled)
|
|
{
|
|
return;
|
|
}
|
|
m_enabled = enabled;
|
|
Invalidate(Bounds());
|
|
}
|
|
|
|
void UiButtonWidget::CancelPress()
|
|
{
|
|
if (!m_pressed)
|
|
{
|
|
return;
|
|
}
|
|
m_pressed = false;
|
|
ReleaseMouse();
|
|
Invalidate(Bounds());
|
|
}
|
|
|
|
gd::SizeF UiButtonWidget::OnMeasure(const UiMeasureContext& ctx)
|
|
{
|
|
const float scale = (ctx.scale > 0.1f) ? ctx.scale : m_scale;
|
|
const float width = (m_fonts != nullptr && m_fonts->ui != nullptr)
|
|
? (m_textPadding * 2.0f * scale + UiMeasureTextWidth(m_fonts->ui, m_label))
|
|
: 0.0f;
|
|
return gd::SizeF(width, m_height * scale);
|
|
}
|
|
|
|
void UiButtonWidget::OnPaint(UiPaintContext& ctx)
|
|
{
|
|
if (m_theme == nullptr || m_fonts == nullptr || ctx.g == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
const gd::RectF& rect = Bounds();
|
|
const bool hovered = Hovered();
|
|
const bool enabled = m_enabled;
|
|
std::uint32_t background = 0;
|
|
std::uint32_t border = 0;
|
|
std::uint32_t textColor = 0;
|
|
|
|
if (m_primary)
|
|
{
|
|
background = enabled ? m_theme->accent
|
|
: BlendToward(m_theme->accent, m_canvasColor, 0.6);
|
|
if (m_pressed)
|
|
{
|
|
background = BlendToward(m_theme->accent, 0xFF000000u, 0.12);
|
|
}
|
|
else if (enabled && hovered)
|
|
{
|
|
background = BlendToward(m_theme->accent, 0xFFFFFFFFu, 0.12);
|
|
}
|
|
border = background;
|
|
textColor = m_theme->textOnAccent;
|
|
}
|
|
else
|
|
{
|
|
background = (m_toggle && m_checked) ? m_theme->accentSoft : m_canvasColor;
|
|
if (m_pressed)
|
|
{
|
|
background = m_theme->pressed;
|
|
}
|
|
else if (enabled && hovered)
|
|
{
|
|
background = (m_toggle && m_checked)
|
|
? BlendToward(m_theme->accentSoft, m_theme->accent, 0.16)
|
|
: m_theme->hover;
|
|
}
|
|
if (!enabled)
|
|
{
|
|
background = m_canvasColor;
|
|
}
|
|
|
|
if (m_toggle && m_checked && enabled)
|
|
{
|
|
border = m_theme->accent;
|
|
}
|
|
else
|
|
{
|
|
border = BlendToward(m_theme->border, m_canvasColor, enabled ? 0.0 : 0.5);
|
|
}
|
|
textColor = enabled ? ((m_toggle && m_checked) ? m_theme->textStrong : m_theme->text)
|
|
: m_theme->textMuted;
|
|
}
|
|
|
|
if (background != m_canvasColor)
|
|
{
|
|
UiFillRoundRect(*ctx.g, rect, kButtonRadius * m_scale, background);
|
|
}
|
|
if (!m_primary)
|
|
{
|
|
UiDrawRoundRect(*ctx.g, rect, kButtonRadius * m_scale, border, 1.0f);
|
|
}
|
|
|
|
// ⚠️ 与旧实现一致:文字走 `UiText`(GDI,不受 GDI+ 裁剪约束)。
|
|
// 按钮矩形本身就在裁剪区内(外扩 1px 只影响描边),因此不需要 `UiTextClipped`。
|
|
UiText(*ctx.g, m_label, m_fonts->ui, rect, textColor, TextAlign::CENTER);
|
|
}
|
|
|
|
UiEventResult UiButtonWidget::OnEvent(UiEvent& event)
|
|
{
|
|
switch (event.kind)
|
|
{
|
|
case UiEventKind::MOUSE_DOWN:
|
|
if (event.button != UiMouseButton::LEFT || !m_enabled)
|
|
{
|
|
return UiEventResult();
|
|
}
|
|
m_pressed = true;
|
|
CaptureMouse();
|
|
{
|
|
UiEventResult result = UiEventResult::Handled();
|
|
result.dirty = Bounds();
|
|
return result;
|
|
}
|
|
case UiEventKind::MOUSE_UP:
|
|
if (event.button != UiMouseButton::LEFT || !m_pressed)
|
|
{
|
|
return UiEventResult();
|
|
}
|
|
// 只有"在自身范围内松开"才算点击(按住移开再松开 = 放弃)。
|
|
m_pressed = false;
|
|
ReleaseMouse();
|
|
{
|
|
UiEventResult result = UiEventResult::Handled();
|
|
result.dirty = Bounds();
|
|
if (Contains(event.x, event.y) && m_enabled && onClick)
|
|
{
|
|
onClick();
|
|
}
|
|
return result;
|
|
}
|
|
case UiEventKind::MOUSE_LEAVE:
|
|
if (m_pressed)
|
|
{
|
|
m_pressed = false;
|
|
ReleaseMouse();
|
|
UiEventResult result = UiEventResult::Handled();
|
|
result.dirty = Bounds();
|
|
return result;
|
|
}
|
|
return UiEventResult();
|
|
default:
|
|
return UiEventResult();
|
|
}
|
|
}
|
|
|
|
HCURSOR UiButtonWidget::OnQueryCursor(int x, int y)
|
|
{
|
|
(void)x;
|
|
(void)y;
|
|
// 与旧实现一致:工具栏按钮不改变光标形态(箭头)。
|
|
return nullptr;
|
|
}
|
|
|
|
std::uint64_t UiButtonWidget::ContentKey() const
|
|
{
|
|
std::uint64_t key = BaseContentKey();
|
|
key = UiMixKey(key, static_cast<std::uint64_t>(m_label.size()));
|
|
key = UiMixKey(key, m_primary ? 1u : 0u);
|
|
key = UiMixKey(key, m_toggle ? 1u : 0u);
|
|
key = UiMixKey(key, m_checked ? 1u : 0u);
|
|
key = UiMixKey(key, m_enabled ? 1u : 0u);
|
|
key = UiMixKey(key, m_pressed ? 1u : 0u);
|
|
key = UiMixKey(key, Hovered() ? 1u : 0u);
|
|
return key;
|
|
}
|
|
|
|
} // namespace jdi
|