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