2018-10-10 01:06:03 +08:00
|
|
|
#include "ftxui/component/toggle.hpp"
|
|
|
|
|
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 Toggle::Render() {
|
2019-01-20 05:06:05 +08:00
|
|
|
bool is_focused = Focused();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
Elements children;
|
2019-01-13 05:25:49 +08:00
|
|
|
for(size_t i = 0; i<entries.size(); ++i) {
|
2019-01-03 07:35:59 +08:00
|
|
|
// Separator.
|
|
|
|
if (i != 0)
|
2019-01-05 09:03:49 +08:00
|
|
|
children.push_back(separator());
|
2019-01-03 07:35:59 +08:00
|
|
|
|
|
|
|
// Entry.
|
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;
|
|
|
|
children.push_back(text(entries[i]) | style | focused);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
return hbox(std::move(children));
|
|
|
|
}
|
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Toggle::OnEvent(Event event) {
|
2019-01-13 05:25:49 +08:00
|
|
|
if (selected > 0 &&
|
2019-01-03 07:35:59 +08:00
|
|
|
(event == Event::ArrowLeft || event == Event::Character('h'))) {
|
2019-01-13 05:25:49 +08:00
|
|
|
selected--;
|
2019-01-03 07:35:59 +08:00
|
|
|
on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-20 05:06:05 +08:00
|
|
|
if (selected < int(entries.size()) - 1 &&
|
2019-01-03 07:35:59 +08:00
|
|
|
(event == Event::ArrowRight || event == Event::Character('l'))) {
|
2019-01-13 05:25:49 +08:00
|
|
|
selected++;
|
2019-01-03 07:35:59 +08:00
|
|
|
on_change();
|
|
|
|
return true;
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|