2021-05-10 02:32:27 +08:00
|
|
|
#include <stddef.h> // for size_t
|
|
|
|
#include <algorithm> // for max, min
|
|
|
|
#include <ext/alloc_traits.h> // for __alloc_traits<>::value_type
|
|
|
|
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, __shared_ptr_access<>::element_type, allocator_traits<>::value_type
|
|
|
|
#include <utility> // for move
|
|
|
|
#include <vector> // for vector, allocator
|
2019-01-13 01:24:46 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/container.hpp"
|
2020-03-23 05:32:44 +08:00
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
namespace ftxui {
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
Component ContainerVertical(Components children) {
|
|
|
|
return Container::Vertical(std::move(children));
|
|
|
|
}
|
|
|
|
|
|
|
|
Component ContainerHorizontal(Components children) {
|
|
|
|
return Container::Horizontal(std::move(children));
|
|
|
|
}
|
|
|
|
|
|
|
|
Component ContainerTab(int* selector, Components children) {
|
|
|
|
return Container::Tab(selector, std::move(children));
|
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
// static
|
2021-05-10 02:32:27 +08:00
|
|
|
Component Container::Vertical() {
|
|
|
|
return Vertical({});
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Component Container::Vertical(Components children) {
|
|
|
|
auto container = std::make_shared<Container>();
|
|
|
|
container->event_handler_ = &Container::VerticalEvent;
|
|
|
|
container->render_handler_ = &Container::VerticalRender;
|
|
|
|
for (Component& child : children)
|
|
|
|
container->Add(std::move(child));
|
2019-01-13 05:25:49 +08:00
|
|
|
return container;
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2021-05-10 02:32:27 +08:00
|
|
|
Component Container::Horizontal() {
|
|
|
|
return Horizontal({});
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Component Container::Horizontal(Components children) {
|
|
|
|
auto container = std::make_shared<Container>();
|
|
|
|
container->event_handler_ = &Container::HorizontalEvent;
|
|
|
|
container->render_handler_ = &Container::HorizontalRender;
|
|
|
|
for (Component& child : children)
|
|
|
|
container->Add(std::move(child));
|
2019-01-13 05:25:49 +08:00
|
|
|
return container;
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
// static
|
2021-05-10 02:32:27 +08:00
|
|
|
Component Container::Tab(int* selector) {
|
|
|
|
return Tab(selector, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
Component Container::Tab(int* selector, Components children) {
|
|
|
|
auto container = std::make_shared<Container>();
|
|
|
|
container->selector_ = selector;
|
|
|
|
container->event_handler_ = &Container::TabEvent;
|
|
|
|
container->render_handler_ = &Container::TabRender;
|
|
|
|
for (Component& child : children)
|
|
|
|
container->Add(std::move(child));
|
2019-01-13 05:25:49 +08:00
|
|
|
return container;
|
|
|
|
}
|
2019-01-13 01:24:46 +08:00
|
|
|
|
|
|
|
bool Container::OnEvent(Event event) {
|
2021-04-19 04:33:41 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
if (!Focused())
|
|
|
|
return false;
|
|
|
|
|
2020-05-25 07:35:22 +08:00
|
|
|
if (ActiveChild() && ActiveChild()->OnEvent(event))
|
2019-01-13 01:24:46 +08:00
|
|
|
return true;
|
|
|
|
|
2019-01-13 05:25:49 +08:00
|
|
|
return (this->*event_handler_)(event);
|
2019-01-13 01:24:46 +08:00
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +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
|
|
|
}
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
void Container::SetActiveChild(ComponentBase* child) {
|
2020-09-06 19:46:56 +08:00
|
|
|
for (size_t i = 0; i < children_.size(); ++i) {
|
2021-05-10 02:32:27 +08:00
|
|
|
if (children_[i].get() == child) {
|
2020-08-26 20:57:42 +08:00
|
|
|
(selector_ ? *selector_ : selected_) = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-05-10 02:32:27 +08:00
|
|
|
Component active_child = ActiveChild();
|
2019-02-03 01:41:07 +08:00
|
|
|
if (active_child)
|
|
|
|
return active_child->Render();
|
|
|
|
return text(L"Empty container");
|
2019-01-13 05:25:49 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 04:33:41 +08:00
|
|
|
bool Container::OnMouseEvent(Event event) {
|
|
|
|
if (selector_)
|
|
|
|
return ActiveChild()->OnEvent(event);
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
for (Component& child : children_) {
|
2021-04-19 04:33:41 +08:00
|
|
|
if (child->OnEvent(event))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +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.
|