FTXUI  6.0.2
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for max
5#include <cstddef> // for size_t
6#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
7#include <utility> // for move
8#include <vector> // for vector, __alloc_traits<>::value_type
9
10#include "ftxui/dom/box_helper.hpp" // for Element, Compute
11#include "ftxui/dom/elements.hpp" // for Element, Elements, hbox
12#include "ftxui/dom/node.hpp" // for Node, Elements
13#include "ftxui/dom/requirement.hpp" // for Requirement
14#include "ftxui/dom/selection.hpp" // for Selection
15#include "ftxui/screen/box.hpp" // for Box
16namespace ftxui {
17
18namespace {
19class HBox : public Node {
20 public:
21 explicit HBox(Elements children) : Node(std::move(children)) {}
22
23 private:
24 void ComputeRequirement() override {
25 requirement_ = Requirement{};
26
27 for (auto& child : children_) {
28 child->ComputeRequirement();
29
30 // Propagate the focused requirement.
31 if (requirement_.focused.Prefer(child->requirement().focused)) {
32 requirement_.focused = child->requirement().focused;
33 requirement_.focused.box.Shift(requirement_.min_x, 0);
34 }
35
36 // Extend the min_x and min_y to contain all the children
37 requirement_.min_x += child->requirement().min_x;
38 requirement_.min_y =
39 std::max(requirement_.min_y, child->requirement().min_y);
40 }
41 }
42
43 void SetBox(Box box) override {
44 Node::SetBox(box);
45
46 std::vector<box_helper::Element> elements(children_.size());
47 for (size_t i = 0; i < children_.size(); ++i) {
48 auto& element = elements[i];
49 const auto& requirement = children_[i]->requirement();
50 element.min_size = requirement.min_x;
51 element.flex_grow = requirement.flex_grow_x;
52 element.flex_shrink = requirement.flex_shrink_x;
53 }
54 const int target_size = box.x_max - box.x_min + 1;
56
57 int x = box.x_min;
58 for (size_t i = 0; i < children_.size(); ++i) {
59 box.x_min = x;
60 box.x_max = x + elements[i].size - 1;
61 children_[i]->SetBox(box);
62 x = box.x_max + 1;
63 }
64 }
65
66 void Select(Selection& selection) override {
67 // If this Node box_ doesn't intersect with the selection, then no
68 // selection.
69 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
70 return;
71 }
72
73 Selection selection_saturated = selection.SaturateHorizontal(box_);
74 for (auto& child : children_) {
76 }
77 }
78};
79
80} // namespace
81
82/// @brief A container displaying elements horizontally one by one.
83/// @param children The elements in the container
84/// @return The container.
85///
86/// #### Example
87///
88/// ```cpp
89/// hbox({
90/// text("Left"),
91/// text("Right"),
92/// });
93/// ```
95 return std::make_shared<HBox>(std::move(children));
96}
97
98} // namespace ftxui
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition node.cpp:43
void Compute(std::vector< Element > *elements, int target_size)
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:94
std::vector< Element > Elements
Definition elements.hpp:23
static auto Intersection(Box a, Box b) -> Box
Definition box.cpp:12