FTXUI  0.8.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::Tab && entries_.size())
83  hovered_ = (hovered_ + 1) % entries_.size();
84  if (event == Event::TabReverse && entries_.size())
85  hovered_ = (hovered_ + entries_.size() - 1) % entries_.size();
86 
87  hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
88 
89  if (hovered_ != old_hovered) {
90  focused_entry() = hovered_;
91  option_->on_change();
92  return true;
93  }
94  }
95 
96  if (event == Event::Character(' ') || event == Event::Return) {
97  *selected_ = hovered_;
98  //*selected_ = focused_entry();
99  option_->on_change();
100  }
101 
102  return false;
103  }
104 
105  bool OnMouseEvent(Event event) {
106  if (!CaptureMouse(event))
107  return false;
108 
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 < int(boxes_.size()); ++i) {
115  if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
116  continue;
117 
118  TakeFocus();
119  focused_entry() = i;
120  if (event.mouse().button == Mouse::Left &&
121  event.mouse().motion == Mouse::Released) {
122  if (*selected_ != i) {
123  *selected_ = i;
124  option_->on_change();
125  }
126 
127  return true;
128  }
129  }
130  return false;
131  }
132 
133  bool OnMouseWheel(Event event) {
134  if (!box_.Contain(event.mouse().x, event.mouse().y))
135  return false;
136 
137  int old_hovered = hovered_;
138 
139  if (event.mouse().button == Mouse::WheelUp)
140  (hovered_)--;
141  if (event.mouse().button == Mouse::WheelDown)
142  (hovered_)++;
143 
144  hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
145 
146  if (hovered_ != old_hovered)
147  option_->on_change();
148 
149  return true;
150  }
151 
152  bool Focusable() const final { return entries_.size(); }
153  int& focused_entry() { return option_->focused_entry(); }
154 
155  ConstStringListRef entries_;
156  int* selected_;
157  int hovered_;
158  std::vector<Box> boxes_;
159  Box box_;
160  Ref<RadioboxOption> option_;
161 };
162 
163 } // namespace
164 
165 /// @brief A list of element, where only one can be selected.
166 /// @param entries The list of entries in the list.
167 /// @param selected The index of the currently selected element.
168 /// @param option Additional optional parameters.
169 /// @ingroup component
170 /// @see RadioboxBase
171 ///
172 /// ### Example
173 ///
174 /// ```cpp
175 /// auto screen = ScreenInteractive::TerminalOutput();
176 /// std::vector<std::string> entries = {
177 /// "entry 1",
178 /// "entry 2",
179 /// "entry 3",
180 /// };
181 /// int selected = 0;
182 /// auto menu = Radiobox(&entries, &selected);
183 /// screen.Loop(menu);
184 /// ```
185 ///
186 /// ### Output
187 ///
188 /// ```bash
189 /// ◉ entry 1
190 /// ○ entry 2
191 /// ○ entry 3
192 /// ```
194  int* selected,
195  Ref<RadioboxOption> option) {
196  return Make<RadioboxBase>(entries, selected, std::move(option));
197 }
198 
199 } // namespace ftxui
200 
201 // Copyright 2020 Arthur Sonzogni. All rights reserved.
202 // Use of this source code is governed by the MIT license that can be found in
203 // the LICENSE file.
ftxui::Mouse::Released
@ Released
Definition: mouse.hpp:19
screen_interactive.hpp
ftxui::focus
Element focus(Element)
Definition: frame.cpp:79
ftxui
Definition: captured_mouse.hpp:6
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition: component_base.hpp:17
ftxui::Mouse::WheelUp
@ WheelUp
Definition: mouse.hpp:14
ftxui::nothing
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition: util.cpp:25
event.hpp
ftxui::Event::Character
static Event Character(std::string)
Definition: event.cpp:10
ftxui::reflect
Decorator reflect(Box &box)
Definition: reflect.cpp:38
box.hpp
ftxui::hbox
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition: hbox.cpp:75
ref.hpp
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
ftxui::select
Element select(Element)
Definition: frame.cpp:38
elements.hpp
captured_mouse.hpp
component.hpp
ftxui::Event::Return
static const Event Return
Definition: event.hpp:43
component_base.hpp
ftxui::Element
std::shared_ptr< Node > Element
Definition: elements.hpp:15
ftxui::Ref
An adapter. Own or reference an mutable object.
Definition: ref.hpp:27
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition: ref.hpp:94
ftxui::Mouse::WheelDown
@ WheelDown
Definition: mouse.hpp:15
ftxui::Event::TabReverse
static const Event TabReverse
Definition: event.hpp:46
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:76
ftxui::Radiobox
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition: radiobox.cpp:193
mouse.hpp
ftxui::Render
void Render(Screen &screen, const Element &node)
Display an element on a ftxui::Screen.
Definition: node.cpp:34
ftxui::Event::Tab
static const Event Tab
Definition: event.hpp:45
component_options.hpp
ftxui::Event::ArrowDown
static const Event ArrowDown
Definition: event.hpp:38
ftxui::Event::ArrowUp
static const Event ArrowUp
Definition: event.hpp:37
ftxui::Mouse::Left
@ Left
Definition: mouse.hpp:10
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:106