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 
21 namespace ftxui {
22 
23 namespace {
24 /// @brief A list of selectable element. One and only one can be selected at
25 /// the same time.
26 /// @ingroup component
27 class 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().button == Mouse::Left &&
127  event.mouse().motion == Mouse::Pressed) {
128  if (selected() != i) {
129  selected() = i;
130  on_change();
131  }
132 
133  return true;
134  }
135  }
136  return false;
137  }
138 
139  bool OnMouseWheel(Event event) {
140  if (!box_.Contain(event.mouse().x, event.mouse().y)) {
141  return false;
142  }
143 
144  const int old_hovered = hovered_;
145 
146  if (event.mouse().button == Mouse::WheelUp) {
147  (hovered_)--;
148  }
149  if (event.mouse().button == Mouse::WheelDown) {
150  (hovered_)++;
151  }
152 
153  hovered_ = util::clamp(hovered_, 0, size() - 1);
154 
155  if (hovered_ != old_hovered) {
156  on_change();
157  }
158 
159  return true;
160  }
161 
162  void Clamp() {
163  boxes_.resize(size());
164  selected() = util::clamp(selected(), 0, size() - 1);
165  focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
166  hovered_ = util::clamp(hovered_, 0, size() - 1);
167  }
168 
169  bool Focusable() const final { return entries.size(); }
170  int size() const { return int(entries.size()); }
171 
172  int hovered_ = selected();
173  std::vector<Box> boxes_;
174  Box box_;
175 };
176 
177 } // namespace
178 
179 /// @brief A list of element, where only one can be selected.
180 /// @param option The parameters
181 /// @ingroup component
182 /// @see RadioboxBase
183 ///
184 /// ### Example
185 ///
186 /// ```cpp
187 /// auto screen = ScreenInteractive::TerminalOutput();
188 /// std::vector<std::string> entries = {
189 /// "entry 1",
190 /// "entry 2",
191 /// "entry 3",
192 /// };
193 /// int selected = 0;
194 /// auto menu = Radiobox({
195 /// .entries = entries,
196 /// .selected = &selected,
197 /// });
198 /// screen.Loop(menu);
199 /// ```
200 ///
201 /// ### Output
202 ///
203 /// ```bash
204 /// ◉ entry 1
205 /// ○ entry 2
206 /// ○ entry 3
207 /// ```
209  return Make<RadioboxBase>(std::move(option));
210 }
211 
212 /// @brief A list of element, where only one can be selected.
213 /// @param entries The list of entries in the list.
214 /// @param selected The index of the currently selected element.
215 /// @param option Additional optional parameters.
216 /// @ingroup component
217 /// @see RadioboxBase
218 ///
219 /// ### Example
220 ///
221 /// ```cpp
222 /// auto screen = ScreenInteractive::TerminalOutput();
223 /// std::vector<std::string> entries = {
224 /// "entry 1",
225 /// "entry 2",
226 /// "entry 3",
227 /// };
228 /// int selected = 0;
229 /// auto menu = Radiobox(&entries, &selected);
230 /// screen.Loop(menu);
231 /// ```
232 ///
233 /// ### Output
234 ///
235 /// ```bash
236 /// ◉ entry 1
237 /// ○ entry 2
238 /// ○ entry 3
239 /// ```
241  int* selected,
242  RadioboxOption option) {
243  option.entries = entries;
244  option.selected = selected;
245  return Make<RadioboxBase>(std::move(option));
246 }
247 
248 } // 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:208
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