FTXUI/src/ftxui/component/container.cpp

353 lines
9.2 KiB
C++
Raw Normal View History

2021-05-15 04:00:49 +08:00
#include <algorithm> // for max, min
2022-03-31 08:17:43 +08:00
#include <cstddef> // for size_t
2021-09-17 02:45:26 +08:00
#include <memory> // for make_shared, __shared_ptr_access, allocator, shared_ptr, allocator_traits<>::value_type
2021-05-10 02:32:27 +08:00
#include <utility> // for move
2021-05-15 04:00:49 +08:00
#include <vector> // for vector, __alloc_traits<>::value_type
2019-01-13 01:24:46 +08:00
2021-09-17 02:45:26 +08:00
#include "ftxui/component/component.hpp" // for Horizontal, Vertical, Tab
#include "ftxui/component/component_base.hpp" // for Components, Component, ComponentBase
#include "ftxui/component/event.hpp" // for Event, Event::Tab, Event::TabReverse, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp
2021-09-17 02:45:26 +08:00
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp
#include "ftxui/dom/elements.hpp" // for text, Elements, operator|, reflect, Element, hbox, vbox
#include "ftxui/screen/box.hpp" // for Box
2020-03-23 05:32:44 +08:00
2019-01-13 01:24:46 +08:00
namespace ftxui {
2021-07-10 18:51:11 +08:00
class ContainerBase : public ComponentBase {
public:
ContainerBase(Components children, int* selector)
: selector_(selector ? selector : &selected_) {
2022-03-31 08:17:43 +08:00
for (Component& child : children) {
Add(std::move(child));
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
}
// Component override.
bool OnEvent(Event event) override {
2022-03-31 08:17:43 +08:00
if (event.is_mouse()) {
2021-07-10 18:51:11 +08:00
return OnMouseEvent(event);
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
2022-03-31 08:17:43 +08:00
if (!Focused()) {
2021-07-10 18:51:11 +08:00
return false;
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
2022-03-31 08:17:43 +08:00
if (ActiveChild() && ActiveChild()->OnEvent(event)) {
2021-07-10 18:51:11 +08:00
return true;
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
return EventHandler(event);
2021-07-10 18:51:11 +08:00
}
Component ActiveChild() override {
2022-03-31 08:17:43 +08:00
if (children_.empty()) {
2021-07-10 18:51:11 +08:00
return nullptr;
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
return children_[*selector_ % children_.size()];
}
void SetActiveChild(ComponentBase* child) override {
for (size_t i = 0; i < children_.size(); ++i) {
if (children_[i].get() == child) {
2022-03-31 08:17:43 +08:00
*selector_ = (int)i;
2021-07-10 18:51:11 +08:00
return;
}
}
}
protected:
2021-07-10 18:51:11 +08:00
// Handlers
2022-03-31 08:17:43 +08:00
virtual bool EventHandler(Event /*unused*/) { return false; } // NOLINT
virtual bool OnMouseEvent(Event event) {
2022-03-31 08:17:43 +08:00
return ComponentBase::OnEvent(std::move(event));
}
int selected_ = 0;
int* selector_ = nullptr;
2021-08-06 04:40:40 +08:00
void MoveSelector(int dir) {
for (int i = *selector_ + dir; i >= 0 && i < (int)children_.size();
i += dir) {
if (children_[i]->Focusable()) {
*selector_ = i;
return;
}
}
}
2022-03-31 08:17:43 +08:00
2021-08-06 04:40:40 +08:00
void MoveSelectorWrap(int dir) {
2022-03-31 08:17:43 +08:00
if (children_.empty()) {
return;
}
2021-08-06 04:40:40 +08:00
for (size_t offset = 1; offset < children_.size(); ++offset) {
2022-12-20 01:51:25 +08:00
const size_t i = ((size_t(*selector_ + offset * dir + children_.size())) %
children_.size());
2021-08-06 04:40:40 +08:00
if (children_[i]->Focusable()) {
2022-03-31 08:17:43 +08:00
*selector_ = (int)i;
2021-08-06 04:40:40 +08:00
return;
}
}
}
};
2021-07-10 18:51:11 +08:00
class VerticalContainer : public ContainerBase {
public:
using ContainerBase::ContainerBase;
Element Render() override {
Elements elements;
2022-03-31 08:17:43 +08:00
for (auto& it : children_) {
elements.push_back(it->Render());
2022-03-31 08:17:43 +08:00
}
if (elements.empty()) {
2021-09-16 06:47:31 +08:00
return text("Empty container") | reflect(box_);
2022-03-31 08:17:43 +08:00
}
2021-09-16 06:47:31 +08:00
return vbox(std::move(elements)) | reflect(box_);
}
bool EventHandler(Event event) override {
2022-12-20 01:51:25 +08:00
const int old_selected = *selector_;
2022-03-31 08:17:43 +08:00
if (event == Event::ArrowUp || event == Event::Character('k')) {
2021-08-06 04:40:40 +08:00
MoveSelector(-1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::ArrowDown || event == Event::Character('j')) {
2021-08-06 04:40:40 +08:00
MoveSelector(+1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::PageUp) {
2022-03-31 08:17:43 +08:00
for (int i = 0; i < box_.y_max - box_.y_min; ++i) {
MoveSelector(-1);
2022-03-31 08:17:43 +08:00
}
}
if (event == Event::PageDown) {
2022-03-31 08:17:43 +08:00
for (int i = 0; i < box_.y_max - box_.y_min; ++i) {
MoveSelector(1);
2022-03-31 08:17:43 +08:00
}
}
if (event == Event::Home) {
2022-03-31 08:17:43 +08:00
for (size_t i = 0; i < children_.size(); ++i) {
MoveSelector(-1);
2022-03-31 08:17:43 +08:00
}
}
if (event == Event::End) {
2022-03-31 08:17:43 +08:00
for (size_t i = 0; i < children_.size(); ++i) {
MoveSelector(1);
2022-03-31 08:17:43 +08:00
}
}
2022-03-31 08:17:43 +08:00
if (event == Event::Tab) {
2021-08-06 04:40:40 +08:00
MoveSelectorWrap(+1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::TabReverse) {
2021-08-06 04:40:40 +08:00
MoveSelectorWrap(-1);
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
*selector_ = std::max(0, std::min(int(children_.size()) - 1, *selector_));
return old_selected != *selector_;
}
2021-09-08 15:36:37 +08:00
bool OnMouseEvent(Event event) override {
2022-03-31 08:17:43 +08:00
if (ContainerBase::OnMouseEvent(event)) {
2021-09-08 15:36:37 +08:00
return true;
2022-03-31 08:17:43 +08:00
}
2021-09-08 15:36:37 +08:00
if (event.mouse().button != Mouse::WheelUp &&
event.mouse().button != Mouse::WheelDown) {
return false;
}
2022-03-31 08:17:43 +08:00
if (!box_.Contain(event.mouse().x, event.mouse().y)) {
2021-09-08 15:36:37 +08:00
return false;
2022-03-31 08:17:43 +08:00
}
2021-09-08 15:36:37 +08:00
2022-03-31 08:17:43 +08:00
if (event.mouse().button == Mouse::WheelUp) {
2021-09-08 15:36:37 +08:00
MoveSelector(-1);
2022-03-31 08:17:43 +08:00
}
if (event.mouse().button == Mouse::WheelDown) {
2021-09-08 15:36:37 +08:00
MoveSelector(+1);
2022-03-31 08:17:43 +08:00
}
2021-09-08 15:36:37 +08:00
*selector_ = std::max(0, std::min(int(children_.size()) - 1, *selector_));
return true;
}
2021-09-16 06:47:31 +08:00
Box box_;
};
2021-07-10 18:51:11 +08:00
class HorizontalContainer : public ContainerBase {
public:
using ContainerBase::ContainerBase;
Element Render() override {
Elements elements;
2022-03-31 08:17:43 +08:00
for (auto& it : children_) {
elements.push_back(it->Render());
2022-03-31 08:17:43 +08:00
}
if (elements.empty()) {
return text("Empty container");
2022-03-31 08:17:43 +08:00
}
return hbox(std::move(elements));
}
bool EventHandler(Event event) override {
2022-12-20 01:51:25 +08:00
const int old_selected = *selector_;
2022-03-31 08:17:43 +08:00
if (event == Event::ArrowLeft || event == Event::Character('h')) {
2021-08-06 04:40:40 +08:00
MoveSelector(-1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::ArrowRight || event == Event::Character('l')) {
2021-08-06 04:40:40 +08:00
MoveSelector(+1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::Tab) {
2021-08-06 04:40:40 +08:00
MoveSelectorWrap(+1);
2022-03-31 08:17:43 +08:00
}
if (event == Event::TabReverse) {
2021-08-06 04:40:40 +08:00
MoveSelectorWrap(-1);
2022-03-31 08:17:43 +08:00
}
2021-07-10 18:51:11 +08:00
*selector_ = std::max(0, std::min(int(children_.size()) - 1, *selector_));
return old_selected != *selector_;
}
};
2021-07-10 18:51:11 +08:00
class TabContainer : public ContainerBase {
public:
using ContainerBase::ContainerBase;
2021-07-10 18:51:11 +08:00
Element Render() override {
2022-12-20 01:51:25 +08:00
const Component active_child = ActiveChild();
2022-03-31 08:17:43 +08:00
if (active_child) {
2021-07-10 18:51:11 +08:00
return active_child->Render();
2022-03-31 08:17:43 +08:00
}
return text("Empty container");
2021-07-10 18:51:11 +08:00
}
bool Focusable() const override {
2022-03-31 08:17:43 +08:00
if (children_.empty()) {
return false;
2022-03-31 08:17:43 +08:00
}
return children_[*selector_ % children_.size()]->Focusable();
}
bool OnMouseEvent(Event event) override {
return ActiveChild()->OnEvent(event);
}
2021-07-10 18:51:11 +08:00
};
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
2021-05-15 02:56:37 +08:00
///
/// ### Example
///
/// ```cpp
/// auto container = Container::Vertical({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// });
/// ```
Component Vertical(Components children) {
return Vertical(std::move(children), nullptr);
2021-05-15 02:56:37 +08:00
}
/// @brief A list of components, drawn one by one vertically and navigated
/// vertically using up/down arrow key or 'j'/'k' keys.
/// This is useful for implementing a Menu for instance.
/// @param children the list of components.
2021-07-10 20:23:46 +08:00
/// @param selector A reference to the index of the selected children.
/// @ingroup component
/// @see ContainerBase
///
/// ### Example
///
/// ```cpp
/// auto container = Container::Vertical({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// });
/// ```
Component Vertical(Components children, int* selector) {
return std::make_shared<VerticalContainer>(std::move(children), selector);
}
2021-05-15 02:56:37 +08:00
/// @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
2021-05-15 02:56:37 +08:00
///
/// ### Example
///
/// ```cpp
/// int selected_children = 2;
2021-05-15 02:56:37 +08:00
/// auto container = Container::Horizontal({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// }, &selected_children);
2021-05-15 02:56:37 +08:00
/// ```
Component Horizontal(Components children) {
return Horizontal(std::move(children), nullptr);
2021-05-15 02:56:37 +08:00
}
/// @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.
2021-07-10 20:23:46 +08:00
/// @param selector A reference to the index of the selected children.
/// @ingroup component
/// @see ContainerBase
///
/// ### Example
///
/// ```cpp
/// int selected_children = 2;
/// auto container = Container::Horizontal({
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// }, selected_children);
/// ```
Component Horizontal(Components children, int* selector) {
return std::make_shared<HorizontalContainer>(std::move(children), selector);
}
2021-05-15 02:56:37 +08:00
/// @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.
2021-07-10 20:23:46 +08:00
/// @param children The list of components.
/// @param selector The index of the drawn children.
2021-05-15 02:56:37 +08:00
/// @ingroup component
/// @see ContainerBase
2021-05-15 02:56:37 +08:00
///
/// ### Example
///
/// ```cpp
/// int tab_drawn = 0;
/// auto container = Container::Tab({
2021-05-15 02:56:37 +08:00
/// children_1,
/// children_2,
/// children_3,
/// children_4,
/// }, &tab_drawn);
2021-05-15 02:56:37 +08:00
/// ```
Component Tab(Components children, int* selector) {
return std::make_shared<TabContainer>(std::move(children), selector);
2021-05-15 02:56:37 +08:00
}
} // namespace Container
2021-05-10 02:32:27 +08:00
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.