FTXUI/src/ftxui/component/container.cpp

109 lines
2.3 KiB
C++
Raw Normal View History

2019-01-13 01:24:46 +08:00
#include "ftxui/component/container.hpp"
namespace ftxui {
// static
Container Container::Horizontal() {
2019-01-13 05:25:49 +08:00
Container container;
container.event_handler_ = &Container::HorizontalEvent;
container.render_handler_ = &Container::HorizontalRender;
return container;
2019-01-13 01:24:46 +08:00
}
// static
Container Container::Vertical() {
2019-01-13 05:25:49 +08:00
Container container;
container.event_handler_ = &Container::VerticalEvent;
container.render_handler_ = &Container::VerticalRender;
return container;
2019-01-13 01:24:46 +08:00
}
2019-01-13 05:25:49 +08:00
// static
2019-01-20 05:06:05 +08:00
Container Container::Tab(int* selector) {
2019-01-13 05:25:49 +08:00
Container container;
container.event_handler_ = &Container::TabEvent;
container.render_handler_ = &Container::TabRender;
container.selector_ = selector;
return container;
}
2019-01-13 01:24:46 +08:00
bool Container::OnEvent(Event event) {
if (!Focused())
return false;
if (ActiveChild()->OnEvent(event))
return true;
2019-01-13 05:25:49 +08:00
return (this->*event_handler_)(event);
2019-01-13 01:24:46 +08:00
}
Component* Container::ActiveChild() {
2019-01-13 05:25:49 +08:00
return children_[*selector_ % children_.size()];
2019-01-13 01:24:46 +08:00
}
2019-01-13 05:25:49 +08:00
bool Container::VerticalEvent(Event event) {
2019-01-13 01:24:46 +08:00
selected_ %= children_.size();
// Left pressed ?
if (event == Event::ArrowUp || event == Event::Character('k')) {
if (selected_ != 0) {
selected_--;
return true;
}
}
// Left pressed ?
if (event == Event::ArrowDown || event == Event::Character('j')) {
2019-01-20 05:06:05 +08:00
if (selected_ != int(children_.size()) - 1) {
2019-01-13 01:24:46 +08:00
selected_++;
return true;
}
}
return false;
}
2019-01-13 05:25:49 +08:00
bool Container::HorizontalEvent(Event event) {
2019-01-13 01:24:46 +08:00
selected_ %= children_.size();
// Left pressed ?
if (event == Event::ArrowLeft || event == Event::Character('h')) {
if (selected_ != 0) {
selected_--;
return true;
}
}
// Left pressed ?
if (event == Event::ArrowRight || event == Event::Character('l')) {
2019-01-20 05:06:05 +08:00
if (selected_ != int(children_.size()) - 1) {
2019-01-13 01:24:46 +08:00
selected_++;
return true;
}
}
return false;
}
2019-01-13 05:25:49 +08:00
Element Container::Render() {
return (this->*render_handler_)();
}
Element Container::VerticalRender() {
Elements elements;
for(auto& it : children_)
elements.push_back(it->Render());
return vbox(std::move(elements));
}
Element Container::HorizontalRender() {
Elements elements;
for(auto& it : children_)
elements.push_back(it->Render());
return hbox(std::move(elements));
}
Element Container::TabRender() {
return ActiveChild()->Render();
}
2019-01-13 01:24:46 +08:00
} // namespace ftxui