FTXUI  2.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
radiobox.cpp
Go to the documentation of this file.
1#include <algorithm> // for clamp, max
2#include <functional> // for function
3#include <memory> // for shared_ptr, allocator_traits<>::value_type
4#include <string> // for string
5#include <utility> // for move
6#include <vector> // for vector
7
8#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
9#include "ftxui/component/component.hpp" // for Make, Radiobox
10#include "ftxui/component/component_base.hpp" // for ComponentBase
11#include "ftxui/component/component_options.hpp" // for RadioboxOption
12#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
13#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp, Mouse::Left, Mouse::Released
14#include "ftxui/component/screen_interactive.hpp" // for Component
15#include "ftxui/dom/elements.hpp" // for operator|, reflect, text, Element, hbox, vbox, Elements, focus, nothing, select
16#include "ftxui/screen/box.hpp" // for Box
17#include "ftxui/screen/util.hpp"
18#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef
19
20namespace ftxui {
21
22namespace {
23/// @brief A list of selectable element. One and only one can be selected at
24/// the same time.
25/// @ingroup component
26class 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 Clamp();
46 Elements elements;
47 bool is_menu_focused = Focused();
48 for (int i = 0; i < size(); ++i) {
49 bool is_focused = (focused_entry() == i) && is_menu_focused;
50 bool is_selected = (hovered_ == 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 =
61 *selected_ == i ? option_->style_checked : option_->style_unchecked;
62 elements.push_back(hbox(text(symbol), text(entries_[i]) | style) |
63 focus_management | reflect(boxes_[i]));
64 }
65 return vbox(std::move(elements)) | reflect(box_);
66 }
67
68 bool OnEvent(Event event) override {
69 Clamp();
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_) = size() - 1;
90 if (event == Event::Tab && size())
91 hovered_ = (hovered_ + 1) % size();
92 if (event == Event::TabReverse && size())
93 hovered_ = (hovered_ + size() - 1) % size();
94
95 hovered_ = util::clamp(hovered_, 0, size() - 1);
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 < 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_ = util::clamp(hovered_, 0, size() - 1);
150
151 if (hovered_ != old_hovered)
152 option_->on_change();
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& focused_entry() { return option_->focused_entry(); }
166 int size() const { return entries_.size(); }
167
168 ConstStringListRef entries_;
169 int* selected_;
170 int hovered_;
171 std::vector<Box> boxes_;
172 Box box_;
173 Ref<RadioboxOption> option_;
174};
175
176} // namespace
177
178/// @brief A list of element, where only one can be selected.
179/// @param entries The list of entries in the list.
180/// @param selected The index of the currently selected element.
181/// @param option Additional optional parameters.
182/// @ingroup component
183/// @see RadioboxBase
184///
185/// ### Example
186///
187/// ```cpp
188/// auto screen = ScreenInteractive::TerminalOutput();
189/// std::vector<std::string> entries = {
190/// "entry 1",
191/// "entry 2",
192/// "entry 3",
193/// };
194/// int selected = 0;
195/// auto menu = Radiobox(&entries, &selected);
196/// screen.Loop(menu);
197/// ```
198///
199/// ### Output
200///
201/// ```bash
202/// ◉ entry 1
203/// ○ entry 2
204/// ○ entry 3
205/// ```
207 int* selected,
208 Ref<RadioboxOption> option) {
209 return Make<RadioboxBase>(entries, selected, std::move(option));
210}
211
212} // namespace ftxui
213
214// Copyright 2020 Arthur Sonzogni. All rights reserved.
215// Use of this source code is governed by the MIT license that can be found in
216// the LICENSE file.
An adapter. Reference a list of strings.
Definition ref.hpp:96
An adapter. Own or reference an mutable object.
Definition ref.hpp:27
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition util.hpp:6
Component Radiobox(ConstStringListRef entries, int *selected_, Ref< RadioboxOption > option={})
A list of element, where only one can be selected.
Definition radiobox.cpp:206
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:26
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:18
Element focus(Element)
Definition frame.cpp:79
Element hbox(Elements)
A container displaying elements horizontally one by one.
Definition hbox.cpp:76
std::vector< Element > Elements
Definition elements.hpp:19
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:106
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
Decorator size(Direction, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:86
Element select(Element)
Definition frame.cpp:38
std::shared_ptr< ComponentBase > Component
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 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