FTXUI  5.0.0
C++ functional terminal UI.
dbox.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for max
5#include <memory> // for __shared_ptr_access, shared_ptr, make_shared
6#include <utility> // for move
7#include <vector> // for vector
8
9#include "ftxui/dom/elements.hpp" // for Element, Elements, dbox
10#include "ftxui/dom/node.hpp" // for Node, Elements
11#include "ftxui/dom/requirement.hpp" // for Requirement
12#include "ftxui/screen/box.hpp" // for Box
13
14namespace ftxui {
15
16namespace {
17class DBox : public Node {
18 public:
19 explicit DBox(Elements children) : Node(std::move(children)) {}
20
21 void ComputeRequirement() override {
22 requirement_.min_x = 0;
23 requirement_.min_y = 0;
24 requirement_.flex_grow_x = 0;
25 requirement_.flex_grow_y = 0;
26 requirement_.flex_shrink_x = 0;
27 requirement_.flex_shrink_y = 0;
28 requirement_.selection = Requirement::NORMAL;
29 for (auto& child : children_) {
30 child->ComputeRequirement();
31 requirement_.min_x =
32 std::max(requirement_.min_x, child->requirement().min_x);
33 requirement_.min_y =
34 std::max(requirement_.min_y, child->requirement().min_y);
35
36 if (requirement_.selection < child->requirement().selection) {
37 requirement_.selection = child->requirement().selection;
38 requirement_.selected_box = child->requirement().selected_box;
39 }
40 }
41 }
42
43 void SetBox(Box box) override {
44 Node::SetBox(box);
45
46 for (auto& child : children_) {
47 child->SetBox(box);
48 }
49 }
50
51 void Render(Screen& screen) override {
52 if (children_.size() <= 1) {
53 return Node::Render(screen);
54 }
55
56 const int width = box_.x_max - box_.x_min + 1;
57 const int height = box_.y_max - box_.y_min + 1;
58 std::vector<Pixel> pixels(width * height);
59
60 for (auto& child : children_) {
61 child->Render(screen);
62
63 // Accumulate the pixels
64 Pixel* acc = pixels.data();
65 for (int x = 0; x < width; ++x) {
66 for (int y = 0; y < height; ++y) {
67 auto& pixel = screen.PixelAt(x + box_.x_min, y + box_.y_min);
68 acc->background_color =
69 Color::Blend(acc->background_color, pixel.background_color);
70 acc->automerge = pixel.automerge;
71 if (pixel.character == " ") {
72 acc->foreground_color =
73 Color::Blend(acc->foreground_color, pixel.background_color);
74 } else {
75 acc->blink = pixel.blink;
76 acc->bold = pixel.bold;
77 acc->dim = pixel.dim;
78 acc->inverted = pixel.inverted;
79 acc->underlined = pixel.underlined;
80 acc->underlined_double = pixel.underlined_double;
81 acc->strikethrough = pixel.strikethrough;
82 acc->hyperlink = pixel.hyperlink;
83 acc->character = pixel.character;
84 acc->foreground_color = pixel.foreground_color;
85 }
86 ++acc;
87
88 pixel = Pixel();
89 }
90 }
91 }
92
93 // Render the accumulated pixels:
94 Pixel* acc = pixels.data();
95 for (int x = 0; x < width; ++x) {
96 for (int y = 0; y < height; ++y) {
97 screen.PixelAt(x + box_.x_min, y + box_.y_min) = *acc++;
98 }
99 }
100 }
101};
102} // namespace
103
104/// @brief Stack several element on top of each other.
105/// @param children_ The input element.
106/// @return The right aligned element.
107/// @ingroup dom
109 return std::make_shared<DBox>(std::move(children_));
110}
111
112} // namespace ftxui
static Color Blend(const Color &lhs, const Color &rhs)
Blend two colors together using the alpha channel.
Definition: color.cpp:287
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:26
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:32
std::shared_ptr< Node > Element
Definition: elements.hpp:23
std::vector< Element > Elements
Definition: elements.hpp:24
Element dbox(Elements)
Stack several element on top of each other.
Definition: dbox.cpp:108
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47