FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
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 // フォーカス要求を伝播する。
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 // 全ての子要素を内包するようにmin_xとmin_yを拡張する。
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;
55 box_helper::Compute(&elements, target_size);
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 // このNode box_が選択範囲と交差しない場合、選択は行われない。
68 if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
69 return;
70 }
71
72 Selection selection_saturated = selection.SaturateHorizontal(box_);
73 for (auto& child : children_) {
74 child->Select(selection_saturated);
75 }
76 }
77};
78
79} // namespace
80
81/// @brief 要素を水平方向に1つずつ表示するコンテナ。
82/// @param children コンテナ内の要素
83/// @return コンテナ。
84///
85/// #### 例
86///
87/// ```cpp
88/// hbox({
89/// text("Left"),
90/// text("Right"),
91/// });
92/// ```
94 return std::make_shared<HBox>(std::move(children));
95}
96
97} // namespace ftxui
void Compute(std::vector< Element > *elements, int target_size)
FTXUI ftxui::名前空間
Definition animation.hpp:11
std::shared_ptr< Node > Element
Definition elements.hpp:25
Element hbox(Elements children)
要素を水平方向に1つずつ表示するコンテナ。
Definition hbox.cpp:93
std::vector< Element > Elements
Definition elements.hpp:26
const Element & element
Definition node.hpp:94