FTXUI/src/ftxui/dom/dbox.cpp

113 lines
3.5 KiB
C++
Raw Normal View History

2023-08-19 19:56:36 +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.
2021-05-02 02:40:35 +08:00
#include <algorithm> // for max
2024-08-16 17:19:51 +08:00
#include <cstddef> // for size_t
2021-05-02 02:40:35 +08:00
#include <memory> // for __shared_ptr_access, shared_ptr, make_shared
#include <utility> // for move
2024-08-16 17:19:51 +08:00
#include <vector>
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/elements.hpp" // for Element, Elements, dbox
2022-04-28 16:43:31 +08:00
#include "ftxui/dom/node.hpp" // for Node, Elements
2021-05-02 02:40:35 +08:00
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
2024-08-16 17:19:51 +08:00
#include "ftxui/screen/pixel.hpp" // for Pixel
2019-01-03 07:35:59 +08:00
namespace ftxui {
2019-01-03 07:35:59 +08:00
namespace {
2019-01-03 07:35:59 +08:00
class DBox : public Node {
public:
2022-03-31 08:17:43 +08:00
explicit DBox(Elements children) : Node(std::move(children)) {}
2019-01-03 07:35:59 +08:00
void ComputeRequirement() override {
requirement_ = Requirement{};
for (auto& child : children_) {
2019-01-03 07:35:59 +08:00
child->ComputeRequirement();
// Propagate the focused requirement.
2025-03-21 02:59:10 +08:00
if (requirement_.focused.Prefer(child->requirement().focused)) {
requirement_.focused = child->requirement().focused;
}
// Extend the min_x and min_y to contain all the children
2020-06-01 22:13:29 +08:00
requirement_.min_x =
std::max(requirement_.min_x, child->requirement().min_x);
requirement_.min_y =
std::max(requirement_.min_y, child->requirement().min_y);
2019-01-03 07:35:59 +08:00
}
}
void SetBox(Box box) override {
Node::SetBox(box);
2022-03-31 08:17:43 +08:00
for (auto& child : children_) {
2019-01-03 07:35:59 +08:00
child->SetBox(box);
2022-03-31 08:17:43 +08:00
}
2019-01-03 07:35:59 +08:00
}
2024-06-14 00:43:14 +08:00
void Render(Screen& screen) override {
if (children_.size() <= 1) {
2024-08-16 17:19:51 +08:00
Node::Render(screen);
return;
2024-06-14 00:43:14 +08:00
}
const int width = box_.x_max - box_.x_min + 1;
const int height = box_.y_max - box_.y_min + 1;
2024-08-16 17:19:51 +08:00
std::vector<Pixel> pixels(std::size_t(width * height));
2024-06-14 00:43:14 +08:00
for (auto& child : children_) {
child->Render(screen);
// Accumulate the pixels
Pixel* acc = pixels.data();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
auto& pixel = screen.PixelAt(x + box_.x_min, y + box_.y_min);
acc->background_color =
Color::Blend(acc->background_color, pixel.background_color);
acc->automerge = pixel.automerge || acc->automerge;
2024-08-16 17:19:51 +08:00
if (pixel.character.empty()) {
2024-06-14 00:43:14 +08:00
acc->foreground_color =
Color::Blend(acc->foreground_color, pixel.background_color);
} else {
acc->blink = pixel.blink;
acc->bold = pixel.bold;
acc->dim = pixel.dim;
acc->inverted = pixel.inverted;
acc->italic = pixel.italic;
2024-06-14 00:43:14 +08:00
acc->underlined = pixel.underlined;
acc->underlined_double = pixel.underlined_double;
acc->strikethrough = pixel.strikethrough;
acc->hyperlink = pixel.hyperlink;
acc->character = pixel.character;
acc->foreground_color = pixel.foreground_color;
}
2024-08-16 17:19:51 +08:00
++acc; // NOLINT
2024-06-14 00:43:14 +08:00
pixel = Pixel();
}
}
}
// Render the accumulated pixels:
Pixel* acc = pixels.data();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
2024-08-16 17:19:51 +08:00
screen.PixelAt(x + box_.x_min, y + box_.y_min) = *acc++; // NOLINT
2024-06-14 00:43:14 +08:00
}
}
}
2019-01-03 07:35:59 +08:00
};
} // namespace
2019-01-03 07:35:59 +08:00
2020-05-25 07:34:13 +08:00
/// @brief Stack several element on top of each other.
/// @param children_ The input element.
2020-05-25 07:34:13 +08:00
/// @return The right aligned element.
/// @ingroup dom
Element dbox(Elements children_) {
return std::make_shared<DBox>(std::move(children_));
2019-01-03 07:35:59 +08:00
}
2020-02-12 04:44:55 +08:00
} // namespace ftxui