FTXUI  0.11.1
C++ functional terminal UI.
checkbox.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, Component, Checkbox
7 #include "ftxui/component/component_base.hpp" // for ComponentBase
8 #include "ftxui/component/component_options.hpp" // for CheckboxOption
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/dom/elements.hpp" // for operator|, text, Element, hbox, reflect, focus, nothing, select
12 #include "ftxui/screen/box.hpp" // for Box
13 #include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
14 
15 namespace ftxui {
16 
17 namespace {
18 class CheckboxBase : public ComponentBase {
19  public:
20  CheckboxBase(ConstStringRef label, bool* state, Ref<CheckboxOption> option)
21  : label_(label), state_(state), option_(std::move(option)) {
22 #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
23  // Microsoft terminal do not use fonts able to render properly the default
24  // radiobox glyph.
25  if (option_->style_checked == "▣ ")
26  option_->style_checked = "[X]";
27  if (option_->style_unchecked == "☐ ")
28  option_->style_unchecked = "[ ]";
29 #endif
30  }
31 
32  private:
33  // Component implementation.
34  Element Render() override {
35  bool is_focused = Focused();
36  bool is_active = Active();
37  auto style = (is_focused || hovered_) ? option_->style_selected_focused
38  : is_active ? option_->style_selected
39  : option_->style_normal;
40  auto focus_management = is_focused ? focus : is_active ? select : nothing;
41  return hbox({
42  text(*state_ ? option_->style_checked
43  : option_->style_unchecked),
44  text(*label_) | style | focus_management,
45  }) |
46  reflect(box_);
47  }
48 
49  bool OnEvent(Event event) override {
50  if (!CaptureMouse(event))
51  return false;
52 
53  if (event.is_mouse())
54  return OnMouseEvent(event);
55 
56  hovered_ = false;
57  if (event == Event::Character(' ') || event == Event::Return) {
58  *state_ = !*state_;
59  option_->on_change();
60  TakeFocus();
61  return true;
62  }
63  return false;
64  }
65 
66  bool OnMouseEvent(Event event) {
67  hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
68 
69  if (!CaptureMouse(event))
70  return false;
71 
72  if (!hovered_)
73  return false;
74 
75  if (event.mouse().button == Mouse::Left &&
76  event.mouse().motion == Mouse::Pressed) {
77  *state_ = !*state_;
78  option_->on_change();
79  return true;
80  }
81 
82  return false;
83  }
84 
85  bool Focusable() const final { return true; }
86 
87  ConstStringRef label_;
88  bool* const state_;
89  bool hovered_ = false;
90  Ref<CheckboxOption> option_;
91  Box box_;
92 };
93 } // namespace
94 
95 /// @brief Draw checkable element.
96 /// @param label The label of the checkbox.
97 /// @param checked Whether the checkbox is checked or not.
98 /// @param option Additional optional parameters.
99 /// @ingroup component
100 /// @see CheckboxBase
101 ///
102 /// ### Example
103 ///
104 /// ```cpp
105 /// auto screen = ScreenInteractive::FitComponent();
106 /// std::string label = "Make a sandwidth";
107 /// bool checked = false;
108 /// Component checkbox = Checkbox(&label, &checked);
109 /// screen.Loop(checkbox)
110 /// ```
111 ///
112 /// ### Output
113 ///
114 /// ```bash
115 /// ☐ Make a sandwitch
116 /// ```
118  bool* checked,
119  Ref<CheckboxOption> option) {
120  return Make<CheckboxBase>(label, checked, std::move(option));
121 }
122 
123 } // namespace ftxui
124 
125 // Copyright 2020 Arthur Sonzogni. All rights reserved.
126 // Use of this source code is governed by the MIT license that can be found in
127 // 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
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option={})
Draw checkable element.
Definition: checkbox.cpp:117
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 focus(Element)
Definition: frame.cpp:79
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition: hbox.cpp:76
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 select(Element)
Definition: frame.cpp:38
static Event Character(std::string)
Definition: event.cpp:10
static const Event Return
Definition: event.hpp:43