FTXUI  0.11.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
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 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 (event.mouse().button == Mouse::WheelDown ||
107 event.mouse().button == Mouse::WheelUp) {
108 return OnMouseWheel(event);
109 }
110
111 for (int i = 0; i < int(boxes_.size()); ++i) {
112 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
113 continue;
114
115 TakeFocus();
116 focused_entry() = i;
117 if (event.mouse().button == Mouse::Left &&
118 event.mouse().motion == Mouse::Released) {
119 if (*selected_ != i) {
120 *selected_ = i;
121 option_->on_change();
122 }
123
124 return true;
125 }
126 }
127 return false;
128 }
129
130 bool OnMouseWheel(Event event) {
131 if (!box_.Contain(event.mouse().x, event.mouse().y))
132 return false;
133
134 int old_hovered = hovered_;
135
136 if (event.mouse().button == Mouse::WheelUp)
137 (hovered_)--;
138 if (event.mouse().button == Mouse::WheelDown)
139 (hovered_)++;
140
141 hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
142
143 if (hovered_ != old_hovered)
144 option_->on_change();
145
146 return true;
147 }
148
149 bool Focusable() const final { return entries_.size(); }
150 int& focused_entry() { return option_->focused_entry(); }
151
152 ConstStringListRef entries_;
153 int* selected_;
154 int hovered_;
155 std::vector<Box> boxes_;
156 Box box_;
157 Ref<RadioboxOption> option_;
158};
159
160} // namespace
161
162/// @brief A list of element, where only one can be selected.
163/// @param entries The list of entries in the list.
164/// @param selected The index of the currently selected element.
165/// @param option Additional optional parameters.
166/// @ingroup component
167/// @see RadioboxBase
168///
169/// ### Example
170///
171/// ```cpp
172/// auto screen = ScreenInteractive::TerminalOutput();
173/// std::vector<std::string> entries = {
174/// "entry 1",
175/// "entry 2",
176/// "entry 3",
177/// };
178/// int selected = 0;
179/// auto menu = Radiobox(&entries, &selected);
180/// screen.Loop(menu);
181/// ```
182///
183/// ### Output
184///
185/// ```bash
186/// ◉ entry 1
187/// ○ entry 2
188/// ○ entry 3
189/// ```
191 int* selected,
192 Ref<RadioboxOption> option) {
193 return Make<RadioboxBase>(entries, selected, std::move(option));
194}
195
196} // namespace ftxui
197
198// Copyright 2020 Arthur Sonzogni. All rights reserved.
199// Use of this source code is governed by the MIT license that can be found in
200// 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:190
Element nothing(Element element)
A decoration doing absolutely nothing.
Definition util.cpp:25
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
std::shared_ptr< Node > Element
Definition elements.hpp:15
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:16
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:34
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 ArrowUp
Definition event.hpp:37
static const Event Tab
Definition event.hpp:45
static const Event ArrowDown
Definition event.hpp:38
static const Event Return
Definition event.hpp:43