FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
radiobox.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 <utility> // for move
6#include <vector> // for vector
7
8#include "ftxui/component/component.hpp" // for Make, Radiobox
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/component/component_options.hpp" // for RadioboxOption, EntryState
11#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp, Event::Return, Event::Tab, Event::TabReverse
12#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp, Mouse::Left, Mouse::Released
13#include "ftxui/component/screen_interactive.hpp" // for Component
14#include "ftxui/dom/elements.hpp" // for operator|, reflect, Element, vbox, Elements, focus, nothing, select
15#include "ftxui/screen/box.hpp" // for Box
16#include "ftxui/screen/util.hpp" // for clamp
17#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef
18
19namespace ftxui {
20
21namespace {
22/// @brief A list of selectable element. One and only one can be selected at
23/// the same time.
24/// @ingroup component
25class RadioboxBase : public ComponentBase, public RadioboxOption {
26 public:
27 explicit RadioboxBase(const RadioboxOption& option)
28 : RadioboxOption(option) {}
29
30 private:
31 Element Render() override {
32 Clamp();
34 const bool is_menu_focused = Focused();
35 elements.reserve(size());
36 for (int i = 0; i < size(); ++i) {
37 const bool is_focused = (focused_entry() == i) && is_menu_focused;
38 const bool is_selected = (hovered_ == i);
41 : select;
42 auto state = EntryState{
43 entries[i], selected() == i, is_selected, is_focused, i,
44 };
45 auto element =
46 (transform ? transform : RadioboxOption::Simple().transform)(state);
47
48 elements.push_back(element | focus_management | reflect(boxes_[i]));
49 }
50 return vbox(std::move(elements)) | reflect(box_);
51 }
52
53 // NOLINTNEXTLINE(readability-function-cognitive-complexity)
54 bool OnEvent(Event event) override {
55 Clamp();
56 if (!CaptureMouse(event)) {
57 return false;
58 }
59
60 if (event.is_mouse()) {
61 return OnMouseEvent(event);
62 }
63
64 if (Focused()) {
65 const int old_hovered = hovered_;
66 if (event == Event::ArrowUp || event == Event::Character('k')) {
67 (hovered_)--;
68 }
69 if (event == Event::ArrowDown || event == Event::Character('j')) {
70 (hovered_)++;
71 }
72 if (event == Event::PageUp) {
73 (hovered_) -= box_.y_max - box_.y_min;
74 }
75 if (event == Event::PageDown) {
76 (hovered_) += box_.y_max - box_.y_min;
77 }
78 if (event == Event::Home) {
79 (hovered_) = 0;
80 }
81 if (event == Event::End) {
82 (hovered_) = size() - 1;
83 }
84 if (event == Event::Tab && size()) {
85 hovered_ = (hovered_ + 1) % size();
86 }
87 if (event == Event::TabReverse && size()) {
88 hovered_ = (hovered_ + size() - 1) % size();
89 }
90
91 hovered_ = util::clamp(hovered_, 0, size() - 1);
92
93 if (hovered_ != old_hovered) {
94 focused_entry() = hovered_;
95 on_change();
96 return true;
97 }
98 }
99
100 if (event == Event::Character(' ') || event == Event::Return) {
101 selected() = hovered_;
102 on_change();
103 return true;
104 }
105
106 return false;
107 }
108
109 bool OnMouseEvent(Event event) {
110 if (event.mouse().button == Mouse::WheelDown ||
111 event.mouse().button == Mouse::WheelUp) {
112 return OnMouseWheel(event);
113 }
114
115 for (int i = 0; i < size(); ++i) {
116 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
117 continue;
118 }
119
120 TakeFocus();
121 focused_entry() = i;
122 if (event.mouse().button == Mouse::Left &&
123 event.mouse().motion == Mouse::Pressed) {
124 if (selected() != i) {
125 selected() = i;
126 on_change();
127 }
128
129 return true;
130 }
131 }
132 return false;
133 }
134
135 bool OnMouseWheel(Event event) {
136 if (!box_.Contain(event.mouse().x, event.mouse().y)) {
137 return false;
138 }
139
140 const int old_hovered = hovered_;
141
142 if (event.mouse().button == Mouse::WheelUp) {
143 (hovered_)--;
144 }
145 if (event.mouse().button == Mouse::WheelDown) {
146 (hovered_)++;
147 }
148
149 hovered_ = util::clamp(hovered_, 0, size() - 1);
150
151 if (hovered_ != old_hovered) {
152 on_change();
153 }
154
155 return true;
156 }
157
158 void Clamp() {
159 boxes_.resize(size());
160 selected() = util::clamp(selected(), 0, size() - 1);
161 focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
162 hovered_ = util::clamp(hovered_, 0, size() - 1);
163 }
164
165 bool Focusable() const final { return entries.size(); }
166 int size() const { return int(entries.size()); }
167
168 int hovered_ = selected();
169 std::vector<Box> boxes_;
170 Box box_;
171};
172
173} // namespace
174
175/// @brief A list of element, where only one can be selected.
176/// @param option The parameters
177/// @ingroup component
178/// @see RadioboxBase
179///
180/// ### Example
181///
182/// ```cpp
183/// auto screen = ScreenInteractive::TerminalOutput();
184/// std::vector<std::string> entries = {
185/// "entry 1",
186/// "entry 2",
187/// "entry 3",
188/// };
189/// int selected = 0;
190/// auto menu = Radiobox({
191/// .entries = entries,
192/// .selected = &selected,
193/// });
194/// screen.Loop(menu);
195/// ```
196///
197/// ### Output
198///
199/// ```bash
200/// ◉ entry 1
201/// ○ entry 2
202/// ○ entry 3
203/// ```
204/// NOLINTNEXTLINE
208
209/// @brief A list of element, where only one can be selected.
210/// @param entries The list of entries in the list.
211/// @param selected The index of the currently selected element.
212/// @param option Additional optional parameters.
213/// @ingroup component
214/// @see RadioboxBase
215///
216/// ### Example
217///
218/// ```cpp
219/// auto screen = ScreenInteractive::TerminalOutput();
220/// std::vector<std::string> entries = {
221/// "entry 1",
222/// "entry 2",
223/// "entry 3",
224/// };
225/// int selected = 0;
226/// auto menu = Radiobox(&entries, &selected);
227/// screen.Loop(menu);
228/// ```
229///
230/// ### Output
231///
232/// ```bash
233/// ◉ entry 1
234/// ○ entry 2
235/// ○ entry 3
236/// ```
238 int* selected,
240 option.entries = std::move(entries);
241 option.selected = selected;
242 return Make<RadioboxBase>(std::move(option));
243}
244
245} // namespace ftxui
An adapter. Reference a list of strings.
Definition ref.hpp:116
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition util.hpp:11
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:28
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
std::shared_ptr< Node > Element
Definition elements.hpp:22
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< ComponentBase > Component
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition radiobox.cpp:205
std::vector< Element > Elements
Definition elements.hpp:23
Element select(Element)
Set the child to be the one selected among its siblings.
Definition frame.cpp:149
Element focus(Element)
Set the child to be the one in focus globally.
Definition frame.cpp:156
Decorator reflect(Box &box)
Definition reflect.cpp:43
void Render(Screen &screen, const Element &element)
Display an element on a ftxui::Screen.
Definition node.cpp:47
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:83
static const Event TabReverse
Definition event.hpp:54
static const Event PageUp
Definition event.hpp:60
static const Event ArrowUp
Definition event.hpp:40
static const Event Tab
Definition event.hpp:53
static const Event ArrowDown
Definition event.hpp:41
static const Event End
Definition event.hpp:59
static const Event Home
Definition event.hpp:58
static const Event PageDown
Definition event.hpp:61
static const Event Return
Definition event.hpp:51
Option for the Radiobox component.
static RadioboxOption Simple()
Option for standard Radiobox.
std::function< Element(const EntryState &)> transform