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

99 lines
3.0 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-01 20:40:35 +02:00
#include <algorithm> // for max
2022-03-31 02:17:43 +02:00
#include <cstddef> // for size_t
2021-09-16 20:45:26 +02:00
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared, allocator_traits<>::value_type
#include <utility> // for move
#include <vector> // for vector, __alloc_traits<>::value_type
2020-03-22 22:32:44 +01:00
2021-09-16 20:45:26 +02:00
#include "ftxui/dom/box_helper.hpp" // for Element, Compute
2021-05-01 20:40:35 +02:00
#include "ftxui/dom/elements.hpp" // for Element, Elements, hbox
2021-09-16 20:45:26 +02:00
#include "ftxui/dom/node.hpp" // for Node, Elements
2021-05-01 20:40:35 +02:00
#include "ftxui/dom/requirement.hpp" // for Requirement
2025-03-22 17:30:34 +01:00
#include "ftxui/dom/selection.hpp" // for Selection
2021-05-01 20:40:35 +02:00
#include "ftxui/screen/box.hpp" // for Box
namespace ftxui {
2018-09-18 08:48:40 +02:00
namespace {
2018-09-18 08:48:40 +02:00
class HBox : public Node {
public:
2022-03-31 02:17:43 +02:00
explicit HBox(Elements children) : Node(std::move(children)) {}
2018-09-18 08:48:40 +02:00
private:
2018-09-18 08:48:40 +02:00
void ComputeRequirement() override {
requirement_ = Requirement{};
for (auto& child : children_) {
2018-09-18 08:48:40 +02:00
child->ComputeRequirement();
// Propagate the focused requirement.
if (requirement_.focused.Prefer(child->requirement().focused)) {
requirement_.focused = child->requirement().focused;
requirement_.focused.box.Shift(requirement_.min_x, 0);
2019-01-19 22:06:05 +01:00
}
// Extend the min_x and min_y to contain all the children
2020-06-01 16:13:29 +02:00
requirement_.min_x += child->requirement().min_x;
requirement_.min_y =
std::max(requirement_.min_y, child->requirement().min_y);
2018-09-18 08:48:40 +02:00
}
}
void SetBox(Box box) override {
Node::SetBox(box);
std::vector<box_helper::Element> elements(children_.size());
for (size_t i = 0; i < children_.size(); ++i) {
auto& element = elements[i];
const auto& requirement = children_[i]->requirement();
element.min_size = requirement.min_x;
element.flex_grow = requirement.flex_grow_x;
element.flex_shrink = requirement.flex_shrink_x;
}
2022-12-19 18:51:25 +01:00
const int target_size = box.x_max - box.x_min + 1;
box_helper::Compute(&elements, target_size);
int x = box.x_min;
for (size_t i = 0; i < children_.size(); ++i) {
box.x_min = x;
box.x_max = x + elements[i].size - 1;
children_[i]->SetBox(box);
x = box.x_max + 1;
2018-09-18 08:48:40 +02:00
}
}
void Select(Selection& selection) override {
// If this Node box_ doesn't intersect with the selection, then no
// selection.
if (Box::Intersection(selection.GetBox(), box_).IsEmpty()) {
return;
}
Selection selection_saturated = selection.SaturateHorizontal(box_);
for (auto& child : children_) {
child->Select(selection_saturated);
}
}
2018-09-18 08:48:40 +02:00
};
} // namespace
2020-05-25 01:34:13 +02:00
/// @brief A container displaying elements horizontally one by one.
/// @param children The elements in the container
/// @return The container.
///
/// #### Example
///
/// ```cpp
/// hbox({
/// text("Left"),
/// text("Right"),
2020-05-25 01:34:13 +02:00
/// });
/// ```
Element hbox(Elements children) {
return std::make_shared<HBox>(std::move(children));
2018-09-18 08:48:40 +02:00
}
2020-02-11 21:44:55 +01:00
} // namespace ftxui