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