FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
src/ftxui/dom/size.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// このソースコードの使用は、LICENSEファイルにあるMITライセンスによって管理されています。
3// the LICENSE file.
4#include <algorithm> // for min, max
5#include <memory> // for make_shared, __shared_ptr_access
6#include <utility> // for move
7
8#include "ftxui/dom/elements.hpp" // for Constraint, WidthOrHeight, EQUAL, GREATER_THAN, LESS_THAN, WIDTH, unpack, Decorator, Element, size
9#include "ftxui/dom/node.hpp" // for Node, Elements
10#include "ftxui/dom/requirement.hpp" // for Requirement
11#include "ftxui/screen/box.hpp" // for Box
12
13namespace ftxui {
14
15namespace {
16class Size : public Node {
17 public:
18 Size(Element child, WidthOrHeight direction, Constraint constraint, int value)
19 : Node(unpack(std::move(child))),
20 direction_(direction),
21 constraint_(constraint),
22 value_(std::max(0, value)) {}
23
24 void ComputeRequirement() override {
26 requirement_ = children_[0]->requirement();
27
28 auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
29
30 switch (constraint_) {
31 case LESS_THAN:
32 value = std::min(value, value_);
33 break;
34 case EQUAL:
35 value = value_;
36 break;
37 case GREATER_THAN:
38 value = std::max(value, value_);
39 break;
40 }
41
42 if (direction_ == WIDTH) {
43 requirement_.flex_grow_x = 0;
44 requirement_.flex_shrink_x = 0;
45 } else {
46 requirement_.flex_grow_y = 0;
47 requirement_.flex_shrink_y = 0;
48 }
49 }
50
51 void SetBox(Box box) override {
52 Node::SetBox(box);
53
54 if (direction_ == WIDTH) {
55 switch (constraint_) {
56 case LESS_THAN:
57 case EQUAL:
58 box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
59 break;
60 case GREATER_THAN:
61 break;
62 }
63 } else {
64 switch (constraint_) {
65 case LESS_THAN:
66 case EQUAL:
67 box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
68 break;
69 case GREATER_THAN:
70 break;
71 }
72 }
73 children_[0]->SetBox(box);
74 }
75
76 private:
77 WidthOrHeight direction_;
78 Constraint constraint_;
79 int value_;
80};
81} // namespace
82
83/// @brief 要素のサイズに制約を適用します。
84/// @param direction 要素のWIDTHまたはHEIGHTを制約するかどうか。
85/// @param constraint 制約のタイプ。
86/// @param value 値。
87/// @ingroup dom
88Decorator size(WidthOrHeight direction, Constraint constraint, int value) {
89 return [=](Element e) {
90 return std::make_shared<Size>(std::move(e), direction, constraint, value);
91 };
92}
93
94} // namespace ftxui
virtual void SetBox(Box box)
描画のために要素に位置と次元を割り当てます。
Definition node.cpp:41
virtual void ComputeRequirement()
要素が必要とするスペースを計算します。
Definition node.cpp:20
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
WidthOrHeight
Definition elements.hpp:156
std::function< Element(Element)> Decorator
Definition elements.hpp:23
std::shared_ptr< Node > Element
Definition elements.hpp:21
@ LESS_THAN
Definition elements.hpp:157
@ GREATER_THAN
Definition elements.hpp:157
std::string value_
return size
Definition string.cpp:1516