FTXUI  5.0.0
C++ functional terminal UI.
reflect.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 <memory> // for make_shared, __shared_ptr_access
5 #include <utility> // for move
6 #include <vector> // for __alloc_traits<>::value_type
7 
8 #include "ftxui/dom/elements.hpp" // for Element, unpack, Decorator, reflect
9 #include "ftxui/dom/node.hpp" // for Node, Elements
10 #include "ftxui/dom/requirement.hpp" // for Requirement
11 #include "ftxui/screen/box.hpp" // for Box
12 #include "ftxui/screen/screen.hpp" // for Screen
13 
14 namespace ftxui {
15 namespace {
16 
17 // Helper class.
18 class Reflect : public Node {
19  public:
20  Reflect(Element child, Box& box)
21  : Node(unpack(std::move(child))), reflected_box_(box) {}
22 
23  void ComputeRequirement() final {
25  requirement_ = children_[0]->requirement();
26  }
27 
28  void SetBox(Box box) final {
29  reflected_box_ = box;
30  Node::SetBox(box);
31  children_[0]->SetBox(box);
32  }
33 
34  void Render(Screen& screen) final {
35  reflected_box_ = Box::Intersection(screen.stencil, reflected_box_);
36  return Node::Render(screen);
37  }
38 
39  private:
40  Box& reflected_box_;
41 };
42 } // namespace
43 
45  return [&](Element child) -> Element {
46  return std::make_shared<Reflect>(std::move(child), box);
47  };
48 }
49 
50 } // namespace ftxui
virtual void SetBox(Box box)
Assign a position and a dimension to an element for drawing.
Definition: node.cpp:26
virtual void ComputeRequirement()
Compute how much space an elements needs.
Definition: node.cpp:18
virtual void Render(Screen &screen)
Display an element on a ftxui::Screen.
Definition: node.cpp:32
std::function< Element(Element)> Decorator
Definition: elements.hpp:25
std::shared_ptr< Node > Element
Definition: elements.hpp:23
Decorator reflect(Box &box)
Definition: reflect.cpp:44
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
static auto Intersection(Box a, Box b) -> Box
Definition: box.cpp:12