FTXUI  3.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
15namespace ftxui {
16
17namespace {
18class CheckboxBase : public ComponentBase {
19 public:
20 CheckboxBase(ConstStringRef label, bool* state, Ref<CheckboxOption> option)
21 : label_(std::move(label)), state_(state), option_(std::move(option)) {}
22
23 private:
24 // Component implementation.
25 Element Render() override {
26 bool is_focused = Focused();
27 bool is_active = Active();
28 auto focus_management = is_focused ? focus : is_active ? select : nothing;
29 auto state = EntryState{
30 *label_,
31 *state_,
32 is_active,
33 is_focused || hovered_,
34 };
35 auto element =
36 (option_->transform ? option_->transform
38 return element | focus_management | reflect(box_);
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 *state_ = !*state_;
53 option_->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 *state_ = !*state_;
74 option_->on_change();
75 return true;
76 }
77
78 return false;
79 }
80
81 bool Focusable() const final { return true; }
82
83 ConstStringRef label_;
84 bool* const state_;
85 bool hovered_ = false;
86 Ref<CheckboxOption> option_;
87 Box box_;
88};
89} // namespace
90
91/// @brief Draw checkable element.
92/// @param label The label of the checkbox.
93/// @param checked Whether the checkbox is checked or not.
94/// @param option Additional optional parameters.
95/// @ingroup component
96/// @see CheckboxBase
97///
98/// ### Example
99///
100/// ```cpp
101/// auto screen = ScreenInteractive::FitComponent();
102/// std::string label = "Make a sandwidth";
103/// bool checked = false;
104/// Component checkbox = Checkbox(&label, &checked);
105/// screen.Loop(checkbox)
106/// ```
107///
108/// ### Output
109///
110/// ```bash
111/// ☐ Make a sandwitch
112/// ```
114 bool* checked,
115 Ref<CheckboxOption> option) {
116 return Make<CheckboxBase>(std::move(label), checked, std::move(option));
117}
118
119} // namespace ftxui
120
121// Copyright 2020 Arthur Sonzogni. All rights reserved.
122// Use of this source code is governed by the MIT license that can be found in
123// the LICENSE file.
An adapter. Own or reference a constant string. For convenience, this class convert multiple immutabl...
Definition ref.hpp:60
An adapter. Own or reference an mutable object.
Definition ref.hpp:27
Component Checkbox(ConstStringRef label, bool *checked, Ref< CheckboxOption > option=CheckboxOption::Simple())
Draw checkable element.
Definition checkbox.cpp:113
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:26
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:18
Element focus(Element)
Definition frame.cpp:79
Decorator reflect(Box &box)
Definition reflect.cpp:39
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:43
Element select(Element)
Definition frame.cpp:38
std::shared_ptr< ComponentBase > Component
static CheckboxOption Simple()
Option for standard Checkbox.
std::function< Element(const EntryState &)> transform
static const Event Return
Definition event.hpp:44