FTXUI 7.0.3
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 TakeFocus();
76 return true;
77 }
78
79 return false;
80 }
81
82 bool Focusable() const final { return true; }
83
84 bool hovered_ = false;
85 Box box_;
86};
87} // namespace
88
89/// @brief Draw checkable element.
90/// @param option Additional optional parameters.
91/// @ingroup component
92/// @see CheckboxBase
93///
94/// ### Example
95///
96/// ```cpp
97/// auto screen = App::FitComponent();
98/// CheckboxOption option;
99/// option.label = "Make a sandwidth";
100/// option.checked = false;
101/// Component checkbox = Checkbox(option);
102/// screen.Loop(checkbox)
103/// ```
104///
105/// ### Output
106///
107/// ```bash
108/// ☐ Make a sandwich
109/// ```
110// NOLINTNEXTLINE
111Component Checkbox(CheckboxOption option) {
112 return Make<CheckboxBase>(std::move(option));
113}
114
115/// @brief Draw checkable element.
116/// @param label The label of the checkbox.
117/// @param checked Whether the checkbox is checked or not.
118/// @param option Additional optional parameters.
119/// @ingroup component
120/// @see CheckboxBase
121///
122/// ### Example
123///
124/// ```cpp
125/// auto screen = App::FitComponent();
126/// std::string label = "Make a sandwidth";
127/// bool checked = false;
128/// Component checkbox = Checkbox(&label, &checked);
129/// screen.Loop(checkbox)
130/// ```
131///
132/// ### Output
133///
134/// ```bash
135/// ☐ Make a sandwich
136/// ```
137// NOLINTNEXTLINE
138Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
139 option.label = std::move(label);
140 option.checked = checked;
141 return Make<CheckboxBase>(std::move(option));
142}
143
144} // 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:25
const Element & element
Definition node.hpp:95
Decorator reflect(Box &box)
Definition reflect.cpp:43
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23