FTXUI  0.11.1
C++ functional terminal UI.
button.cpp
Go to the documentation of this file.
1 #include <functional> // for function
2 #include <memory> // for shared_ptr
3 #include <utility> // for move
4 
5 #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
6 #include "ftxui/component/component.hpp" // for Make, Button
7 #include "ftxui/component/component_base.hpp" // for ComponentBase
8 #include "ftxui/component/component_options.hpp" // for ButtonOption
9 #include "ftxui/component/event.hpp" // for Event, Event::Return
10 #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
11 #include "ftxui/component/screen_interactive.hpp" // for Component
12 #include "ftxui/dom/elements.hpp" // for operator|, Element, nothing, reflect, text, border, inverted
13 #include "ftxui/screen/box.hpp" // for Box
14 #include "ftxui/util/ref.hpp" // for ConstStringRef, Ref
15 
16 namespace ftxui {
17 
18 namespace {
19 class ButtonBase : public ComponentBase {
20  public:
21  ButtonBase(ConstStringRef label,
22  std::function<void()> on_click,
23  Ref<ButtonOption> option)
24  : label_(label), on_click_(on_click), option_(std::move(option)) {}
25 
26  // Component implementation:
27  Element Render() override {
28  auto style = Focused() ? inverted : nothing;
29  auto my_border = option_->border ? border : nothing;
30  return text(*label_) | my_border | style | reflect(box_);
31  }
32 
33  bool OnEvent(Event event) override {
34  if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) {
35  if (!CaptureMouse(event))
36  return false;
37 
38  TakeFocus();
39 
40  if (event.mouse().button == Mouse::Left &&
41  event.mouse().motion == Mouse::Pressed) {
42  on_click_();
43  return true;
44  }
45 
46  return false;
47  }
48 
49  if (event == Event::Return) {
50  on_click_();
51  return true;
52  }
53  return false;
54  }
55 
56  bool Focusable() const final { return true; }
57 
58  private:
59  ConstStringRef label_;
60  std::function<void()> on_click_;
61  Box box_;
62  Ref<ButtonOption> option_;
63 };
64 
65 } // namespace
66 
67 /// @brief Draw a button. Execute a function when clicked.
68 /// @param label The label of the button.
69 /// @param on_click The action to execute when clicked.
70 /// @param option Additional optional parameters.
71 /// @ingroup component
72 /// @see ButtonBase
73 ///
74 /// ### Example
75 ///
76 /// ```cpp
77 /// auto screen = ScreenInteractive::FitComponent();
78 /// std::string label = "Click to quit";
79 /// Component button = Button(&label, screen.ExitLoopClosure());
80 /// screen.Loop(button)
81 /// ```
82 ///
83 /// ### Output
84 ///
85 /// ```bash
86 /// ┌─────────────┐
87 /// │Click to quit│
88 /// └─────────────┘
89 /// ```
91  std::function<void()> on_click,
92  Ref<ButtonOption> option) {
93  return Make<ButtonBase>(label, std::move(on_click), std::move(option));
94 }
95 
96 } // namespace ftxui
97 
98 // Copyright 2020 Arthur Sonzogni. All rights reserved.
99 // Use of this source code is governed by the MIT license that can be found in
100 // the LICENSE file.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:76
An adapter. Own or reference an mutable object.
Definition: ref.hpp:27
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:26
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::shared_ptr< ComponentBase > Component
Element inverted(Element)
Add a filter that will invert the foreground and the background colors.
Definition: inverted.cpp:29
Component Button(ConstStringRef label, std::function< void()> on_click, Ref< ButtonOption >={})
Draw a button. Execute a function when clicked.
Definition: button.cpp:90
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:106
Decorator reflect(Box &box)
Definition: reflect.cpp:39
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Element border(Element)
Draw a border around the element.
Definition: border.cpp:150
static const Event Return
Definition: event.hpp:43