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