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
|
#include "cru/ui/components/Menu.h"
#include <functional>
#include "cru/platform/gui/Window.h"
#include "cru/ui/ThemeManager.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 "cru/ui/helper/ClickDetector.h"
#include "cru/ui/host/WindowHost.h"
#include "cru/ui/style/StyleRuleSet.h"
namespace cru::ui::components {
MenuItem::MenuItem() {
container_.SetChild(&text_);
container_.GetStyleRuleSet()->SetParent(
ThemeManager::GetInstance()->GetResourceStyleRuleSet(u"menuitem.style"));
container_.ClickEvent()->AddHandler([this](const helper::ClickEventArgs&) {
if (this->on_click_) this->on_click_();
});
}
MenuItem::MenuItem(String text) : MenuItem() { SetText(std::move(text)); }
MenuItem::~MenuItem() { container_.RemoveFromParent(); }
void MenuItem::SetText(String text) { text_.SetText(std::move(text)); }
Menu::Menu() { container_.SetFlexDirection(controls::FlexDirection::Vertical); }
Menu::~Menu() {
for (auto item : items_) {
delete item;
}
container_.RemoveFromParent();
}
void Menu::AddItem(Component* item, gsl::index index) {
Expects(index >= 0 && index <= GetItemCount());
items_.insert(items_.cbegin() + index, item);
container_.AddChildAt(item->GetRootControl(), index);
}
Component* Menu::RemoveItem(gsl::index index) {
Expects(index >= 0 && index < GetItemCount());
Component* item = items_[index];
items_.erase(items_.cbegin() + index);
container_.RemoveChildAt(index);
return item;
}
void Menu::ClearItems() {
for (auto item : items_) {
delete item;
}
items_.clear();
container_.ClearChildren();
}
void Menu::AddTextItem(String text, gsl::index index,
std::function<void()> on_click) {
MenuItem* item = new MenuItem(std::move(text));
item->SetOnClick([this, index, on_click = std::move(on_click)] {
on_click();
if (on_item_click_) on_item_click_(index);
});
AddItem(item, index);
}
PopupMenu::PopupMenu(controls::Control* attached_control)
: attached_control_(attached_control), popup_(attached_control) {
menu_.SetOnItemClick([this](Index) { this->Close(); });
popup_.AddChildAt(menu_.GetRootControl(), 0);
}
PopupMenu::~PopupMenu() {}
controls::Control* PopupMenu::GetRootControl() { return &popup_; }
void PopupMenu::SetPosition(const Point& position) {
popup_.GetWindowHost()->GetNativeWindow()->SetClientRect(Rect{position, {}});
}
void PopupMenu::Show() {
popup_.GetWindowHost()->RelayoutWithSize(Size::Infinate(), true);
popup_.GetWindowHost()->GetNativeWindow()->SetVisibility(
platform::gui::WindowVisibilityType::Show);
}
void PopupMenu::Close() { popup_.GetWindowHost()->GetNativeWindow()->Close(); }
} // namespace cru::ui::components
|