diff options
author | crupest <crupest@outlook.com> | 2022-02-15 21:34:21 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-02-15 21:34:21 +0800 |
commit | 24aaa8fbc5da8370506402facdb8ccaf563454e5 (patch) | |
tree | 0075bfbee729b1ccc57966bef97bda4be09cbde9 /src/ui/components/Select.cpp | |
parent | fa6e9a419f688df12a57199aa3b4dce10cc6fd49 (diff) | |
download | cru-24aaa8fbc5da8370506402facdb8ccaf563454e5.tar.gz cru-24aaa8fbc5da8370506402facdb8ccaf563454e5.tar.bz2 cru-24aaa8fbc5da8370506402facdb8ccaf563454e5.zip |
...
Diffstat (limited to 'src/ui/components/Select.cpp')
-rw-r--r-- | src/ui/components/Select.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/ui/components/Select.cpp b/src/ui/components/Select.cpp new file mode 100644 index 00000000..9f9fbfae --- /dev/null +++ b/src/ui/components/Select.cpp @@ -0,0 +1,31 @@ +#include "cru/ui/components/Select.h" + +namespace cru::ui::components { +Select::Select() { + button_.SetChild(&button_text_); + button_.ClickEvent()->AddHandler([this](const helper::ClickEventArgs& args) { + popup_menu_.SetPosition(args.GetDownPoint()); + popup_menu_.Show(); + }); +} + +Select::~Select() { button_.RemoveFromParent(); } + +void Select::SetItems(std::vector<String> items) { + popup_menu_.GetMenu()->ClearItems(); + for (Index i = 0; i < items.size(); i++) { + popup_menu_.GetMenu()->AddTextItem(std::move(items[i]), + [this, i] { SetSelectedIndex(i); }); + } +} + +void Select::SetSelectedIndex(Index index) { + selected_index_ = index; + if (index >= 0 && index < items_.size()) { + button_text_.SetText(items_[index]); + } else { + button_text_.SetText({}); + } + item_selected_event_.Raise(index); +} +} // namespace cru::ui::components |