From 24aaa8fbc5da8370506402facdb8ccaf563454e5 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 15 Feb 2022 21:34:21 +0800 Subject: ... --- src/ui/components/Select.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/ui/components/Select.cpp (limited to 'src/ui/components/Select.cpp') 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 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 -- cgit v1.2.3