2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/menu.hpp"
|
|
|
|
#include <algorithm>
|
2018-10-19 04:58:38 +08:00
|
|
|
#include <iostream>
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
Element Menu::Render() {
|
2018-10-10 01:06:03 +08:00
|
|
|
std::vector<Element> elements;
|
2019-01-20 05:06:05 +08:00
|
|
|
bool is_focused = Focused();
|
2018-10-10 01:06:03 +08:00
|
|
|
for (size_t i = 0; i < entries.size(); ++i) {
|
2019-01-20 05:06:05 +08:00
|
|
|
auto style = (selected != int(i))
|
|
|
|
? normal_style
|
|
|
|
: is_focused ? focused_style : selected_style;
|
|
|
|
auto focused = (selected != int(i)) ? nothing : is_focused ? focus : select;
|
|
|
|
auto icon = (selected != int(i)) ? L" " : L"> ";
|
|
|
|
elements.push_back(text(icon + entries[i]) | style | focused);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return vbox(std::move(elements));
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Menu::OnEvent(Event event) {
|
2018-10-10 01:06:03 +08:00
|
|
|
if (!Focused())
|
|
|
|
return false;
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
int new_selected = selected;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
2018-10-10 01:06:03 +08:00
|
|
|
new_selected--;
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
2018-10-10 01:06:03 +08:00
|
|
|
new_selected++;
|
2019-01-20 05:06:05 +08:00
|
|
|
new_selected = std::max(0, std::min(int(entries.size()) - 1, new_selected));
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
if (selected != new_selected) {
|
|
|
|
selected = new_selected;
|
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::Return) {
|
2018-10-10 01:06:03 +08:00
|
|
|
on_enter();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|