Add MenuEntry. (#199)

This will address comments from:
https://github.com/ArthurSonzogni/FTXUI/issues/194
This commit is contained in:
Arthur Sonzogni
2021-09-04 18:43:56 +02:00
committed by GitHub
parent 2ccc599db9
commit b99106a7c9
5 changed files with 147 additions and 0 deletions

View File

@@ -149,6 +149,51 @@ Component Menu(ConstStringListRef entries,
return Make<MenuBase>(entries, selected, std::move(option));
}
Component MenuEntry(ConstStringRef label, Ref<MenuEntryOption> option) {
class Impl : public ComponentBase {
public:
Impl(ConstStringRef label, Ref<MenuEntryOption> option)
: label_(std::move(label)), option_(std::move(option)) {}
private:
Element Render() override {
bool focused = Focused();
auto style =
hovered_ ? (focused ? option_->style_selected_focused
: option_->style_selected)
: (focused ? option_->style_focused : option_->style_normal);
auto focus_management = focused ? select : nothing;
auto label = focused ? "> " + (*label_) //
: " " + (*label_);
return text(label) | style | focus_management | reflect(box_);
}
bool Focusable() const override { return true; }
bool OnEvent(Event event) override {
if (!event.is_mouse())
return false;
hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
if (!hovered_)
return false;
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Released) {
TakeFocus();
return true;
}
return false;
}
ConstStringRef label_;
Ref<MenuEntryOption> option_;
Box box_;
bool hovered_ = false;
};
return Make<Impl>(std::move(label), std::move(option));
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.