blob: 7dc5a5d7a847e37e1a6b74cc00daa01c5991bd11 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#include "cru/ui/controls/button.hpp"
#include <memory>
#include "../helper.hpp"
#include "cru/platform/graph/brush.hpp"
#include "cru/platform/native/cursor.hpp"
#include "cru/platform/native/ui_application.hpp"
#include "cru/ui/render/border_render_object.hpp"
#include "cru/ui/ui_manager.hpp"
#include "cru/ui/window.hpp"
namespace cru::ui::controls {
using cru::platform::native::SystemCursorType;
namespace {
void Set(render::BorderRenderObject* o, const ButtonStateStyle& s) {
o->SetBorderBrush(s.border_brush);
o->SetBorderThickness(s.border_thickness);
o->SetBorderRadius(s.border_radius);
o->SetForegroundBrush(s.foreground_brush);
o->SetBackgroundBrush(s.background_brush);
}
std::shared_ptr<platform::native::ICursor> GetSystemCursor(
SystemCursorType type) {
return GetUiApplication()->GetCursorManager()->GetSystemCursor(type);
}
} // namespace
Button::Button() : click_detector_(this) {
style_ = UiManager::GetInstance()->GetThemeResources()->button_style;
render_object_ = std::make_unique<render::BorderRenderObject>();
render_object_->SetAttachedControl(this);
Set(render_object_.get(), style_.normal);
render_object_->SetBorderEnabled(true);
click_detector_.StateChangeEvent()->AddHandler(
[this](const ClickState& state) {
switch (state) {
case ClickState::None:
Set(render_object_.get(), style_.normal);
SetCursor(GetSystemCursor(SystemCursorType::Arrow));
break;
case ClickState::Hover:
Set(render_object_.get(), style_.hover);
SetCursor(GetSystemCursor(SystemCursorType::Hand));
break;
case ClickState::Press:
Set(render_object_.get(), style_.press);
SetCursor(GetSystemCursor(SystemCursorType::Hand));
break;
case ClickState::PressInactive:
Set(render_object_.get(), style_.press_cancel);
SetCursor(GetSystemCursor(SystemCursorType::Arrow));
break;
}
});
}
render::RenderObject* Button::GetRenderObject() const {
return render_object_.get();
}
void Button::OnChildChanged(Control* old_child, Control* new_child) {
if (old_child != nullptr) render_object_->RemoveChild(0);
if (new_child != nullptr)
render_object_->AddChild(new_child->GetRenderObject(), 0);
}
} // namespace cru::ui::controls
|