2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-01-07 02:17:27 +08:00
|
|
|
#include "ftxui/dom/elements.hpp"
|
2019-01-21 06:04:10 +08:00
|
|
|
#include "ftxui/dom/node.hpp"
|
2019-01-07 02:17:27 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2019-01-07 02:17:27 +08:00
|
|
|
|
|
|
|
class Size : public Node {
|
|
|
|
public:
|
2019-01-21 06:04:10 +08:00
|
|
|
Size(Element child, Direction direction, Constraint constraint, size_t value)
|
|
|
|
: Node(unpack(std::move(child))),
|
|
|
|
direction_(direction),
|
|
|
|
constraint_(constraint),
|
|
|
|
value_(value) {}
|
|
|
|
|
2019-01-07 02:17:27 +08:00
|
|
|
~Size() override {}
|
2019-01-21 06:04:10 +08:00
|
|
|
|
2019-01-07 02:17:27 +08:00
|
|
|
void ComputeRequirement() override {
|
|
|
|
Node::ComputeRequirement();
|
2019-01-20 05:06:05 +08:00
|
|
|
requirement_ = children[0]->requirement();
|
2019-01-21 06:04:10 +08:00
|
|
|
|
2020-06-01 22:13:29 +08:00
|
|
|
auto& value = direction_ == WIDTH ? requirement_.min_x : requirement_.min_y;
|
2019-01-21 06:04:10 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-06-02 05:40:32 +08:00
|
|
|
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-07 02:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetBox(Box box) override {
|
|
|
|
Node::SetBox(box);
|
2019-01-23 09:16:00 +08:00
|
|
|
|
|
|
|
if (direction_ == WIDTH) {
|
2020-03-23 05:32:44 +08:00
|
|
|
switch (constraint_) {
|
2019-01-23 09:16:00 +08:00
|
|
|
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-23 05:32:44 +08:00
|
|
|
switch (constraint_) {
|
2019-01-23 09:16:00 +08:00
|
|
|
case LESS_THAN:
|
|
|
|
case EQUAL:
|
|
|
|
box.y_max = std::min(box.y_min + value_ + 1, box.y_max);
|
|
|
|
break;
|
|
|
|
case GREATER_THAN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 02:17:27 +08:00
|
|
|
children[0]->SetBox(box);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-01-21 06:04:10 +08:00
|
|
|
Direction direction_;
|
|
|
|
Constraint constraint_;
|
|
|
|
int value_;
|
2019-01-07 02:17:27 +08:00
|
|
|
};
|
|
|
|
|
2019-01-21 06:04:10 +08:00
|
|
|
Decorator size(Direction direction, Constraint constraint, int value) {
|
2019-01-07 02:17:27 +08:00
|
|
|
return [=](Element e) {
|
2020-05-21 02:36:47 +08:00
|
|
|
return std::make_shared<Size>(std::move(e), direction, constraint, value);
|
2019-01-07 02:17:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-12 04:44:55 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08: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.
|