FTXUI/src/ftxui/component/toggle.cpp

41 lines
1.2 KiB
C++
Raw Normal View History

#include "ftxui/component/toggle.hpp"
2020-03-23 05:32:44 +08:00
#include <algorithm>
namespace ftxui {
Element Toggle::Render() {
2019-01-20 05:06:05 +08:00
bool is_focused = Focused();
2019-01-13 01:24:46 +08:00
Elements children;
for (size_t i = 0; i < entries.size(); ++i) {
2019-01-03 07:35:59 +08:00
// Separator.
if (i != 0)
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);
}
return hbox(std::move(children));
}
2018-10-19 04:58:38 +08:00
bool Toggle::OnEvent(Event event) {
int old_selected = selected;
if (event == Event::ArrowLeft || event == Event::Character('h'))
2019-01-13 05:25:49 +08:00
selected--;
if (event == Event::ArrowRight || event == Event::Character('l'))
2019-01-13 05:25:49 +08:00
selected++;
if (event == Event::Tab && entries.size())
selected = (selected + 1) % entries.size();
if (event == Event::TabReverse && entries.size())
selected = (selected + entries.size() - 1) % entries.size();
selected = std::max(0, std::min(int(entries.size()) - 1, selected));
return old_selected != selected;
}
} // namespace ftxui