Files
FTXUI/src/ftxui/dom/size.cpp

96 lines
2.6 KiB
C++
Raw Normal View History

2023-08-19 13:56:36 +02:00
// 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.
2021-05-14 22:00:49 +02:00
#include <algorithm> // for min, max
#include <memory> // for make_shared, __shared_ptr_access
#include <utility> // for move
2020-03-22 22:32:44 +01:00
#include "ftxui/dom/elements.hpp" // for Constraint, WidthOrHeight, EQUAL, GREATER_THAN, LESS_THAN, WIDTH, unpack, Decorator, Element, size
#include "ftxui/dom/node.hpp" // for Node, Elements
2021-05-01 20:40:35 +02:00
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
2019-01-06 19:17:27 +01:00
namespace ftxui {
2019-01-06 19:17:27 +01:00
namespace {
2019-01-06 19:17:27 +01:00
class Size : public Node {
public:
Size(Element child, WidthOrHeight direction, Constraint constraint, int value)
: Node(unpack(std::move(child))),
direction_(direction),
constraint_(constraint),
value_(std::max(0, value)) {}
2019-01-06 19:17:27 +01:00
void ComputeRequirement() override {
Node::ComputeRequirement();
requirement_ = children_[0]->requirement();
2020-06-01 16:13:29 +02:00
auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
switch (constraint_) {
case LESS_THAN:
value = std::min(value, value_);
break;
case EQUAL:
value = value_;
break;
case GREATER_THAN:
value = std::max(value, value_);
break;
}
if (direction_ == WIDTH) {
requirement_.flex_grow_x = 0;
requirement_.flex_shrink_x = 0;
} else {
requirement_.flex_grow_y = 0;
requirement_.flex_shrink_y = 0;
}
2019-01-06 19:17:27 +01:00
}
void SetBox(Box box) override {
Node::SetBox(box);
if (direction_ == WIDTH) {
2020-03-22 22:32:44 +01:00
switch (constraint_) {
case LESS_THAN:
case EQUAL:
box.x_max = std::min(box.x_min + value_ + 1, box.x_max);
break;
case GREATER_THAN:
break;
}
} else {
2020-03-22 22:32:44 +01:00
switch (constraint_) {
case LESS_THAN:
case EQUAL:
box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
break;
case GREATER_THAN:
break;
}
}
children_[0]->SetBox(box);
2019-01-06 19:17:27 +01:00
}
private:
WidthOrHeight direction_;
Constraint constraint_;
int value_;
2019-01-06 19:17:27 +01:00
};
} // namespace
2019-01-06 19:17:27 +01:00
2020-08-16 02:24:50 +02:00
/// @brief Apply a constraint on the size of an element.
2025-09-07 07:19:17 +00:00
/// @param direction Whether the WIDTH or the HEIGHT of the element must be
2020-08-16 02:24:50 +02:00
/// constrained.
2021-07-10 14:23:46 +02:00
/// @param constraint The type of constaint.
/// @param value The value.
2020-08-16 02:24:50 +02:00
/// @ingroup dom
Decorator size(WidthOrHeight direction, Constraint constraint, int value) {
2019-01-06 19:17:27 +01:00
return [=](Element e) {
return std::make_shared<Size>(std::move(e), direction, constraint, value);
2019-01-06 19:17:27 +01:00
};
}
2020-02-11 21:44:55 +01:00
} // namespace ftxui