FTXUI  5.0.0
C++ functional terminal UI.
checkbox.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 <functional> // for function
5 #include <utility> // for move
6 
7 #include "ftxui/component/component.hpp" // for Make, Checkbox
8 #include "ftxui/component/component_base.hpp" // for Component, ComponentBase
9 #include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
10 #include "ftxui/component/event.hpp" // for Event, Event::Return
11 #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
12 #include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, focus, nothing, select
13 #include "ftxui/screen/box.hpp" // for Box
14 #include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
15 
16 namespace ftxui {
17 
18 namespace {
19 class CheckboxBase : public ComponentBase, public CheckboxOption {
20  public:
21  explicit CheckboxBase(CheckboxOption option)
22  : CheckboxOption(std::move(option)) {}
23 
24  private:
25  // Component implementation.
26  Element Render() override {
27  const bool is_focused = Focused();
28  const bool is_active = Active();
29  auto focus_management = is_focused ? focus : is_active ? select : nothing;
30  auto entry_state = EntryState{
31  *label, *checked, is_active, is_focused || hovered_, -1,
32  };
33  auto element = (transform ? transform : CheckboxOption::Simple().transform)(
34  entry_state);
35  return element | focus_management | reflect(box_);
36  }
37 
38  bool OnEvent(Event event) override {
39  if (!CaptureMouse(event)) {
40  return false;
41  }
42 
43  if (event.is_mouse()) {
44  return OnMouseEvent(event);
45  }
46 
47  hovered_ = false;
48  if (event == Event::Character(' ') || event == Event::Return) {
49  *checked = !*checked;
50  on_change();
51  TakeFocus();
52  return true;
53  }
54  return false;
55  }
56 
57  bool OnMouseEvent(Event event) {
58  hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
59 
60  if (!CaptureMouse(event)) {
61  return false;
62  }
63 
64  if (!hovered_) {
65  return false;
66  }
67 
68  if (event.mouse().button == Mouse::Left &&
69  event.mouse().motion == Mouse::Pressed) {
70  *checked = !*checked;
71  on_change();
72  TakeFocus();
73  return true;
74  }
75 
76  return false;
77  }
78 
79  bool Focusable() const final { return true; }
80 
81  bool hovered_ = false;
82  Box box_;
83 };
84 } // namespace
85 
86 /// @brief Draw checkable element.
87 /// @param option Additional optional parameters.
88 /// @ingroup component
89 /// @see CheckboxBase
90 ///
91 /// ### Example
92 ///
93 /// ```cpp
94 /// auto screen = ScreenInteractive::FitComponent();
95 /// CheckboxOption option;
96 /// option.label = "Make a sandwidth";
97 /// option.checked = false;
98 /// Component checkbox = Checkbox(option);
99 /// screen.Loop(checkbox)
100 /// ```
101 ///
102 /// ### Output
103 ///
104 /// ```bash
105 /// ☐ Make a sandwitch
106 /// ```
107 // NOLINTNEXTLINE
109  return Make<CheckboxBase>(std::move(option));
110 }
111 
112 /// @brief Draw checkable element.
113 /// @param label The label of the checkbox.
114 /// @param checked Whether the checkbox is checked or not.
115 /// @param option Additional optional parameters.
116 /// @ingroup component
117 /// @see CheckboxBase
118 ///
119 /// ### Example
120 ///
121 /// ```cpp
122 /// auto screen = ScreenInteractive::FitComponent();
123 /// std::string label = "Make a sandwidth";
124 /// bool checked = false;
125 /// Component checkbox = Checkbox(&label, &checked);
126 /// screen.Loop(checkbox)
127 /// ```
128 ///
129 /// ### Output
130 ///
131 /// ```bash
132 /// ☐ Make a sandwitch
133 /// ```
134 // NOLINTNEXTLINE
135 Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
136  option.label = std::move(label);
137  option.checked = checked;
138  return Make<CheckboxBase>(std::move(option));
139 }
140 
141 } // namespace ftxui
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition: ref.hpp:94
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:28
std::shared_ptr< Node > Element
Definition: elements.hpp:22
std::shared_ptr< ComponentBase > Component
Element select(Element)
Set the child to be the one selected among its siblings.
Definition: frame.cpp:149
Element focus(Element)
Set the child to be the one in focus globally.
Definition: frame.cpp:156
Decorator reflect(Box &box)
Definition: reflect.cpp:43
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition: node.cpp:47
Component Checkbox(CheckboxOption options)
Draw checkable element.
Definition: checkbox.cpp:108
Option for the Checkbox component.
static CheckboxOption Simple()
Option for standard Checkbox.
std::function< Element(const EntryState &)> transform
static Event Character(std::string)
An event corresponding to a given typed character.
Definition: event.cpp:29
static const Event Return
Definition: event.hpp:51