FTXUI  6.0.2
C++ functional terminal UI.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
16namespace ftxui {
17
18namespace {
19class 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 OnRender() override {
27 const bool is_focused = Focused();
28 const bool is_active = Active();
29 auto entry_state = EntryState{
30 *label, *checked, is_active, is_focused || hovered_, -1,
31 };
32 auto element = (transform ? transform : CheckboxOption::Simple().transform)(
34 element |= focus;
35 element |= reflect(box_);
36 return element;
37 }
38
39 bool OnEvent(Event event) override {
40 if (!CaptureMouse(event)) {
41 return false;
42 }
43
44 if (event.is_mouse()) {
45 return OnMouseEvent(event);
46 }
47
48 hovered_ = false;
49 if (event == Event::Character(' ') || event == Event::Return) {
50 *checked = !*checked;
51 on_change();
52 TakeFocus();
53 return true;
54 }
55 return false;
56 }
57
58 bool OnMouseEvent(Event event) {
59 hovered_ = box_.Contain(event.mouse().x, event.mouse().y);
60
61 if (!CaptureMouse(event)) {
62 return false;
63 }
64
65 if (!hovered_) {
66 return false;
67 }
68
69 if (event.mouse().button == Mouse::Left &&
70 event.mouse().motion == Mouse::Pressed) {
71 *checked = !*checked;
72 on_change();
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
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
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
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< ComponentBase > Component
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
Decorator reflect(Box &box)
Definition reflect.cpp:43
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 const Event Return
Definition event.hpp:51