FTXUI/ftxui/src/ftxui/component/toggle.cpp

43 lines
888 B
C++
Raw Normal View History

#include "ftxui/component/toggle.hpp"
namespace ftxui {
Toggle::Toggle(Delegate* delegate) : Component(delegate) {}
Element Toggle::Render() {
auto highlight = Focused() ? inverted : bold;
Children children;
2019-01-03 07:35:59 +08:00
for(size_t i = 0; i<options.size(); ++i) {
// Separator.
if (i != 0)
children.push_back(separator());
2019-01-03 07:35:59 +08:00
// Entry.
auto style = i == activated ? highlight : dim;
children.push_back(style(text(options[i])));
}
return hbox(std::move(children));
}
2018-10-19 04:58:38 +08:00
bool Toggle::OnEvent(Event event) {
2019-01-03 07:35:59 +08:00
if (activated > 0 &&
(event == Event::ArrowLeft || event == Event::Character('h'))) {
activated--;
on_change();
return true;
}
if (activated < options.size() - 1 &&
(event == Event::ArrowRight || event == Event::Character('l'))) {
activated++;
on_change();
return true;
}
return false;
}
} // namespace ftxui