FTXUI/src/ftxui/component/container.cpp

230 lines
6.2 KiB
C++
Raw Normal View History

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-15 02:56:37 +08:00
namespace Container {
/// @brief A list of components, drawn one by one vertically and navigated
/// vertically using up/down arrow key or 'j'/'k' keys.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
///
/// ### Example
///
/// ```cpp
/// auto container = Container::Vertical({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// });
/// ```
Component Vertical(Components children) {
return ContainerBase::Vertical(std::move(children));
}
/// @brief A list of components, drawn one by one horizontally and navigated
/// horizontally using left/right arrow key or 'h'/'l' keys.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
///
/// ### Example
///
/// ```cpp
/// auto container = Container::Horizontal({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// });
/// ```
Component Horizontal(Components children) {
return ContainerBase::Horizontal(std::move(children));
}
/// @brief A list of components, where only one is drawn and interacted with at
/// a time. The |selector| gives the index of the selected component. This is
/// useful to implement tabs.
/// @param selector The index of the drawn children.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
///
/// ### Example
///
/// ```cpp
/// int tab_drawn = 0;
/// auto container = Container::Tab(&tab_drawn, {
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// });
/// ```
Component Tab(int* selector, Components children) {
return ContainerBase::Tab(selector, std::move(children));
}
} // namespace Container
2021-05-10 02:32:27 +08:00
2019-01-13 01:24:46 +08:00
// static
2021-05-15 02:56:37 +08:00
Component ContainerBase::Vertical() {
2021-05-10 02:32:27 +08:00
return Vertical({});
}
// static
2021-05-15 02:56:37 +08:00
Component ContainerBase::Vertical(Components children) {
2021-05-10 02:32:27 +08:00
auto container = std::make_shared<Container>();
2021-05-15 02:56:37 +08:00
container->event_handler_ = &ContainerBase::VerticalEvent;
container->render_handler_ = &ContainerBase::VerticalRender;
2021-05-10 02:32:27 +08:00
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-15 02:56:37 +08:00
Component ContainerBase::Horizontal() {
2021-05-10 02:32:27 +08:00
return Horizontal({});
}
// static
2021-05-15 02:56:37 +08:00
Component ContainerBase::Horizontal(Components children) {
2021-05-10 02:32:27 +08:00
auto container = std::make_shared<Container>();
2021-05-15 02:56:37 +08:00
container->event_handler_ = &ContainerBase::HorizontalEvent;
container->render_handler_ = &ContainerBase::HorizontalRender;
2021-05-10 02:32:27 +08:00
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-15 02:56:37 +08:00
Component ContainerBase::Tab(int* selector) {
2021-05-10 02:32:27 +08:00
return Tab(selector, {});
}
// static
2021-05-15 02:56:37 +08:00
Component ContainerBase::Tab(int* selector, Components children) {
2021-05-10 02:32:27 +08:00
auto container = std::make_shared<Container>();
container->selector_ = selector;
2021-05-15 02:56:37 +08:00
container->event_handler_ = &ContainerBase::TabEvent;
container->render_handler_ = &ContainerBase::TabRender;
2021-05-10 02:32:27 +08:00
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
2021-05-15 02:56:37 +08:00
bool ContainerBase::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
2019-01-13 01:24:46 +08:00
if (!Focused())
return false;
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-15 02:56:37 +08:00
Component ContainerBase::ActiveChild() {
2019-02-03 01:41:07 +08:00
if (children_.size() == 0)
return nullptr;
int selected = selector_ ? *selector_ : selected_;
return children_[selected % children_.size()];
2019-01-13 01:24:46 +08:00
}
2021-05-15 02:56:37 +08:00
void ContainerBase::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) {
(selector_ ? *selector_ : selected_) = i;
return;
}
}
}
2021-05-15 02:56:37 +08:00
bool ContainerBase::VerticalEvent(Event event) {
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
}
2021-05-15 02:56:37 +08:00
bool ContainerBase::HorizontalEvent(Event event) {
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
}
2021-05-15 02:56:37 +08:00
Element ContainerBase::Render() {
2019-01-13 05:25:49 +08:00
return (this->*render_handler_)();
}
2021-05-15 02:56:37 +08:00
Element ContainerBase::VerticalRender() {
2019-01-13 05:25:49 +08:00
Elements elements;
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));
}
2021-05-15 02:56:37 +08:00
Element ContainerBase::HorizontalRender() {
2019-01-13 05:25:49 +08:00
Elements elements;
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));
}
2021-05-15 02:56:37 +08:00
Element ContainerBase::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-05-15 02:56:37 +08:00
bool ContainerBase::OnMouseEvent(Event event) {
if (selector_)
return ActiveChild()->OnEvent(event);
2021-05-10 02:32:27 +08:00
for (Component& child : children_) {
if (child->OnEvent(event))
return true;
}
return false;
}
2019-01-13 01:24:46 +08:00
} // namespace ftxui
// 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.