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