FTXUI  0.11.1
C++ functional terminal UI.
radiobox.cpp
Go to the documentation of this file.
1 #include <stddef.h> // for size_t
2 #include <algorithm> // for max, min
3 #include <functional> // for function
4 #include <memory> // for shared_ptr, allocator_traits<>::value_type
5 #include <string> // for string
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
13 #include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Return, Event::Tab, Event::TabReverse
14 #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
15 #include "ftxui/component/screen_interactive.hpp" // for Component
16 #include "ftxui/dom/elements.hpp" // for Element, operator|, text, hbox, reflect, vbox, focus, nothing, select
17 #include "ftxui/screen/box.hpp" // for Box
18 #include "ftxui/util/ref.hpp" // for Ref
19 
20 namespace ftxui {
21 
22 namespace {
23 /// @brief A list of selectable element. One and only one can be selected at
24 /// the same time.
25 /// @ingroup component
26 class RadioboxBase : public ComponentBase {
27  public:
28  RadioboxBase(ConstStringListRef entries,
29  int* selected,
30  Ref<RadioboxOption> option)
31  : entries_(entries), selected_(selected), option_(std::move(option)) {
32 #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
33  // Microsoft terminal do not use fonts able to render properly the default
34  // radiobox glyph.
35  if (option_->style_checked == "◉ ")
36  option_->style_checked = "(*)";
37  if (option_->style_unchecked == "○ ")
38  option_->style_unchecked = "( )";
39 #endif
40  hovered_ = *selected_;
41  }
42 
43  private:
44  Element Render() override {
45  Elements elements;
46  bool is_menu_focused = Focused();
47  boxes_.resize(entries_.size());
48  for (size_t i = 0; i < entries_.size(); ++i) {
49  bool is_focused = (focused_entry() == int(i)) && is_menu_focused;
50  bool is_selected = (hovered_ == int(i));
51 
52  auto style = is_selected ? (is_focused ? option_->style_selected_focused
53  : option_->style_selected)
54  : (is_focused ? option_->style_focused
55  : option_->style_normal);
56  auto focus_management = !is_selected ? nothing
57  : is_menu_focused ? focus
58  : select;
59 
60  const std::string& symbol = *selected_ == int(i)
61  ? option_->style_checked
62  : option_->style_unchecked;
63  elements.push_back(hbox(text(symbol), text(entries_[i]) | style) |
64  focus_management | reflect(boxes_[i]));
65  }
66  return vbox(std::move(elements)) | reflect(box_);
67  }
68 
69  bool OnEvent(Event event) override {
70  if (!CaptureMouse(event))
71  return false;
72 
73  if (event.is_mouse())
74  return OnMouseEvent(event);
75 
76  if (Focused()) {
77  int old_hovered = hovered_;
78  if (event == Event::ArrowUp || event == Event::Character('k'))
79  (hovered_)--;
80  if (event == Event::ArrowDown || event == Event::Character('j'))
81  (hovered_)++;
82  if (event == Event::PageUp)
83  (hovered_) -= box_.y_max - box_.y_min;
84  if (event == Event::PageDown)
85  (hovered_) += box_.y_max - box_.y_min;
86  if (event == Event::Home)
87  (hovered_) = 0;
88  if (event == Event::End)
89  (hovered_) = entries_.size() - 1;
90  if (event == Event::Tab && entries_.size())
91  hovered_ = (hovered_ + 1) % entries_.size();
92  if (event == Event::TabReverse && entries_.size())
93  hovered_ = (hovered_ + entries_.size() - 1) % entries_.size();
94 
95  hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
96 
97  if (hovered_ != old_hovered) {
98  focused_entry() = hovered_;
99  option_->on_change();
100  return true;
101  }
102  }
103 
104  if (event == Event::Character(' ') || event == Event::Return) {
105  *selected_ = hovered_;
106  //*selected_ = focused_entry();
107  option_->on_change();
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 < int(boxes_.size()); ++i) {
120  if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
121  continue;
122 
123  TakeFocus();
124  focused_entry() = i;
125  if (event.mouse().button == Mouse::Left &&
126  event.mouse().motion == Mouse::Released) {
127  if (*selected_ != i) {
128  *selected_ = i;
129  option_->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  int old_hovered = hovered_;
143 
144  if (event.mouse().button == Mouse::WheelUp)
145  (hovered_)--;
146  if (event.mouse().button == Mouse::WheelDown)
147  (hovered_)++;
148 
149  hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
150 
151  if (hovered_ != old_hovered)
152  option_->on_change();
153 
154  return true;
155  }
156 
157  bool Focusable() const final { return entries_.size(); }
158  int& focused_entry() { return option_->focused_entry(); }
159 
160  ConstStringListRef entries_;
161  int* selected_;
162  int hovered_;
163  std::vector<Box> boxes_;
164  Box box_;
165  Ref<RadioboxOption> option_;
166 };
167 
168 } // namespace
169 
170 /// @brief A list of element, where only one can be selected.
171 /// @param entries The list of entries in the list.
172 /// @param selected The index of the currently selected element.
173 /// @param option Additional optional parameters.
174 /// @ingroup component
175 /// @see RadioboxBase
176 ///
177 /// ### Example
178 ///
179 /// ```cpp
180 /// auto screen = ScreenInteractive::TerminalOutput();
181 /// std::vector<std::string> entries = {
182 /// "entry 1",
183 /// "entry 2",
184 /// "entry 3",
185 /// };
186 /// int selected = 0;
187 /// auto menu = Radiobox(&entries, &selected);
188 /// screen.Loop(menu);
189 /// ```
190 ///
191 /// ### Output
192 ///
193 /// ```bash
194 /// ◉ entry 1
195 /// ○ entry 2
196 /// ○ entry 3
197 /// ```
199  int* selected,
200  Ref<RadioboxOption> option) {
201  return Make<RadioboxBase>(entries, selected, std::move(option));
202 }
203 
204 } // namespace ftxui
205 
206 // Copyright 2020 Arthur Sonzogni. All rights reserved.
207 // Use of this source code is governed by the MIT license that can be found in
208 // the LICENSE file.
An adapter. Reference a list of strings.
Definition: ref.hpp:94
An adapter. Own or reference an mutable object.
Definition: ref.hpp:27
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition: radiobox.cpp:198
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:26
std::shared_ptr< Node > Element
Definition: elements.hpp:16
std::shared_ptr< ComponentBase > Component
Element focus(Element)
Definition: frame.cpp:79
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition: hbox.cpp:76
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:106
std::vector< Element > Elements
Definition: elements.hpp:17
Decorator reflect(Box &box)
Definition: reflect.cpp:39
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:40
Element select(Element)
Definition: frame.cpp:38
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:77
static const Event TabReverse
Definition: event.hpp:46
static const Event PageUp
Definition: event.hpp:52
static Event Character(std::string)
Definition: event.cpp:10
static const Event ArrowUp
Definition: event.hpp:37
static const Event Tab
Definition: event.hpp:45
static const Event ArrowDown
Definition: event.hpp:38
static const Event End
Definition: event.hpp:50
static const Event Home
Definition: event.hpp:49
static const Event PageDown
Definition: event.hpp:53
static const Event Return
Definition: event.hpp:43