FTXUI  4.1.1
C++ functional terminal UI.
Loading...
Searching...
No Matches
checkbox.cpp
Go to the documentation of this file.
1#include <functional> // for function
2#include <utility> // for move
3
4#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
5#include "ftxui/component/component.hpp" // for Make, Checkbox
6#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
7#include "ftxui/component/component_options.hpp" // for CheckboxOption, EntryState
8#include "ftxui/component/event.hpp" // for Event, Event::Return
9#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
10#include "ftxui/dom/elements.hpp" // for operator|, Element, reflect, focus, nothing, select
11#include "ftxui/screen/box.hpp" // for Box
12#include "ftxui/util/ref.hpp" // for Ref, ConstStringRef
13
14namespace ftxui {
15
16namespace {
17class CheckboxBase : public ComponentBase {
18 public:
19 CheckboxBase(ConstStringRef label, bool* state, Ref<CheckboxOption> option)
20 : label_(std::move(label)), state_(state), option_(std::move(option)) {}
21
22 private:
23 // Component implementation.
24 Element Render() override {
25 const bool is_focused = Focused();
26 const bool is_active = Active();
27 auto focus_management = is_focused ? focus : is_active ? select : nothing;
28 auto state = EntryState{
29 *label_,
30 *state_,
31 is_active,
32 is_focused || hovered_,
33 };
34 auto element =
35 (option_->transform ? option_->transform
37 return element | focus_management | reflect(box_);
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 *state_ = !*state_;
52 option_->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 *state_ = !*state_;
73 option_->on_change();
74 return true;
75 }
76
77 return false;
78 }
79
80 bool Focusable() const final { return true; }
81
82 ConstStringRef label_;
83 bool* const state_;
84 bool hovered_ = false;
85 Ref<CheckboxOption> option_;
86 Box box_;
87};
88} // namespace
89
90/// @brief Draw checkable element.
91/// @param label The label of the checkbox.
92/// @param checked Whether the checkbox is checked or not.
93/// @param option Additional optional parameters.
94/// @ingroup component
95/// @see CheckboxBase
96///
97/// ### Example
98///
99/// ```cpp
100/// auto screen = ScreenInteractive::FitComponent();
101/// std::string label = "Make a sandwidth";
102/// bool checked = false;
103/// Component checkbox = Checkbox(&label, &checked);
104/// screen.Loop(checkbox)
105/// ```
106///
107/// ### Output
108///
109/// ```bash
110/// ☐ Make a sandwitch
111/// ```
113 bool* checked,
114 Ref<CheckboxOption> option) {
115 return Make<CheckboxBase>(std::move(label), checked, std::move(option));
116}
117
118} // namespace ftxui
119
120// Copyright 2020 Arthur Sonzogni. All rights reserved.
121// Use of this source code is governed by the MIT license that can be found in
122// 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:112
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:27
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:19
Element focus(Element)
Definition frame.cpp:83
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:44
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:49