2020-04-20 03:00:37 +08:00
|
|
|
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
#include "ftxui/component/container.hpp"
|
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
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-02-03 01:41:07 +08:00
|
|
|
if (children_.size() == 0)
|
|
|
|
return nullptr;
|
2020-04-17 07:15:17 +08:00
|
|
|
|
|
|
|
int selected = selector_ ? *selector_ : selected_;
|
|
|
|
return children_[selected % children_.size()];
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
bool Container::VerticalEvent(Event event) {
|
2019-11-02 02:52:41 +08:00
|
|
|
int old_selected = selected_;
|
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k'))
|
|
|
|
selected_--;
|
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j'))
|
|
|
|
selected_++;
|
|
|
|
if (event == Event::Tab && children_.size())
|
|
|
|
selected_ = (selected_ + 1) % children_.size();
|
|
|
|
if (event == Event::TabReverse && children_.size())
|
|
|
|
selected_ = (selected_ + children_.size() - 1) % children_.size();
|
|
|
|
|
|
|
|
selected_ = std::max(0, std::min(int(children_.size()) - 1, selected_));
|
|
|
|
return old_selected != selected_;
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
bool Container::HorizontalEvent(Event event) {
|
2019-11-02 02:52:41 +08:00
|
|
|
int old_selected = selected_;
|
|
|
|
if (event == Event::ArrowLeft || event == Event::Character('h'))
|
|
|
|
selected_--;
|
|
|
|
if (event == Event::ArrowRight || event == Event::Character('l'))
|
|
|
|
selected_++;
|
|
|
|
if (event == Event::Tab && children_.size())
|
|
|
|
selected_ = (selected_ + 1) % children_.size();
|
|
|
|
if (event == Event::TabReverse && children_.size())
|
|
|
|
selected_ = (selected_ + children_.size() - 1) % children_.size();
|
|
|
|
|
|
|
|
selected_ = std::max(0, std::min(int(children_.size()) - 1, selected_));
|
|
|
|
return old_selected != selected_;
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
Element Container::Render() {
|
|
|
|
return (this->*render_handler_)();
|
|
|
|
}
|
|
|
|
|
|
|
|
Element Container::VerticalRender() {
|
|
|
|
Elements elements;
|
2019-11-02 02:52:41 +08:00
|
|
|
for (auto& it : children_)
|
2019-01-13 05:25:49 +08:00
|
|
|
elements.push_back(it->Render());
|
2019-02-03 01:41:07 +08:00
|
|
|
if (elements.size() == 0)
|
|
|
|
return text(L"Empty container");
|
2019-01-13 05:25:49 +08:00
|
|
|
return vbox(std::move(elements));
|
|
|
|
}
|
|
|
|
|
|
|
|
Element Container::HorizontalRender() {
|
|
|
|
Elements elements;
|
2019-11-02 02:52:41 +08:00
|
|
|
for (auto& it : children_)
|
2019-01-13 05:25:49 +08:00
|
|
|
elements.push_back(it->Render());
|
2019-02-03 01:41:07 +08:00
|
|
|
if (elements.size() == 0)
|
|
|
|
return text(L"Empty container");
|
2019-01-13 05:25:49 +08:00
|
|
|
return hbox(std::move(elements));
|
|
|
|
}
|
|
|
|
|
|
|
|
Element Container::TabRender() {
|
2019-02-03 01:41:07 +08:00
|
|
|
Component* active_child = ActiveChild();
|
|
|
|
if (active_child)
|
|
|
|
return active_child->Render();
|
|
|
|
return text(L"Empty container");
|
2019-01-13 05:25:49 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
} // namespace ftxui
|