blob: 32936f841474b7b79fd5776a7b8ee7ab3da09a82 (
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
#pragma once
#include "Component.h"
#include "cru/common/Base.h"
#include "cru/ui/controls/Button.h"
#include "cru/ui/controls/Control.h"
#include "cru/ui/controls/FlexLayout.h"
#include "cru/ui/controls/Popup.h"
#include "cru/ui/controls/TextBlock.h"
#include <functional>
#include <vector>
namespace cru::ui::components {
class CRU_UI_API MenuItem : public Component {
public:
MenuItem();
explicit MenuItem(String text);
CRU_DELETE_COPY(MenuItem)
CRU_DELETE_MOVE(MenuItem)
~MenuItem();
public:
controls::Control* GetRootControl() override { return container_; }
void SetText(String text);
void SetOnClick(std::function<void()> on_click) {
on_click_ = std::move(on_click);
}
private:
controls::Button* container_;
controls::TextBlock* text_;
std::function<void()> on_click_;
};
class CRU_UI_API Menu : public Component {
public:
Menu();
CRU_DELETE_COPY(Menu)
CRU_DELETE_MOVE(Menu)
~Menu();
public:
controls::Control* GetRootControl() override { return container_; }
gsl::index GetItemCount() const {
return static_cast<gsl::index>(items_.size());
}
void AddItem(Component* component) { AddItem(component, GetItemCount()); }
void AddItem(Component* component, gsl::index index);
Component* RemoveItem(gsl::index index);
void ClearItems();
void AddTextItem(String text, std::function<void()> on_click) {
AddTextItem(std::move(text), GetItemCount(), std::move(on_click));
}
void AddTextItem(String text, Index index, std::function<void()> on_click);
void SetOnItemClick(std::function<void(Index)> on_item_click) {
on_item_click_ = std::move(on_item_click);
}
private:
controls::FlexLayout* container_;
std::vector<Component*> items_;
std::function<void(Index)> on_item_click_;
};
class CRU_UI_API PopupMenu : public Component {
public:
explicit PopupMenu(controls::Control* attached_control = nullptr);
CRU_DELETE_COPY(PopupMenu)
CRU_DELETE_MOVE(PopupMenu)
~PopupMenu();
public:
controls::Control* GetRootControl() override;
controls::Popup* GetPopup() { return popup_; }
Menu* GetMenu() { return menu_; }
// position relative to screen left top.
void SetPosition(const Point& position);
void Show();
// position relative to screen left top.
void Show(const Point& position) {
SetPosition(position);
Show();
}
void Close();
private:
controls::Control* attached_control_;
controls::Popup* popup_;
Menu* menu_;
};
} // namespace cru::ui::components
|