FTXUI 6.1.9 C++ functional terminal UI.

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