Loading...
Searching...
No Matches
radiobox.cpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <functional> // for function
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/component.hpp" // for Make, Radiobox
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for RadioboxOption, EntryState
#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
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp, Mouse::Left, Mouse::Released
#include "ftxui/component/screen_interactive.hpp" // for Component
#include "ftxui/dom/elements.hpp" // for operator|, reflect, Element, vbox, Elements, focus, nothing, select
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/util.hpp" // for clamp
#include "ftxui/util/ref.hpp" // for Ref, ConstStringListRef
namespace ftxui {
namespace {
/// @brief A list of selectable element. One and only one can be selected at
/// the same time.
/// @ingroup component
class RadioboxBase : public ComponentBase, public RadioboxOption {
public:
explicit RadioboxBase(const RadioboxOption& option)
: RadioboxOption(option) {}
private:
Element OnRender() override {
Clamp();
Elements elements;
const bool is_menu_focused = Focused();
elements.reserve(size());
for (int i = 0; i < size(); ++i) {
const bool is_focused = (focused_entry() == i) && is_menu_focused;
const bool is_selected = (hovered_ == i);
auto state = EntryState{
entries[i], selected() == i, is_selected, is_focused, i,
};
auto element =
(transform ? transform : RadioboxOption::Simple().transform)(state);
if (is_selected) {
element |= focus;
}
elements.push_back(element | reflect(boxes_[i]));
}
return vbox(std::move(elements), hovered_) | reflect(box_);
}
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
bool OnEvent(Event event) override {
Clamp();
if (!CaptureMouse(event)) {
return false;
}
if (event.is_mouse()) {
return OnMouseEvent(event);
}
if (Focused()) {
const int old_hovered = hovered_;
if (event == Event::ArrowUp || event == Event::Character('k')) {
(hovered_)--;
}
if (event == Event::ArrowDown || event == Event::Character('j')) {
(hovered_)++;
}
if (event == Event::PageUp) {
(hovered_) -= box_.y_max - box_.y_min;
}
if (event == Event::PageDown) {
(hovered_) += box_.y_max - box_.y_min;
}
if (event == Event::Home) {
(hovered_) = 0;
}
if (event == Event::End) {
(hovered_) = size() - 1;
}
if (event == Event::Tab && size()) {
hovered_ = (hovered_ + 1) % size();
}
if (event == Event::TabReverse && size()) {
hovered_ = (hovered_ + size() - 1) % size();
}
hovered_ = util::clamp(hovered_, 0, size() - 1);
if (hovered_ != old_hovered) {
focused_entry() = hovered_;
on_change();
return true;
}
}
if (event == Event::Character(' ') || event == Event::Return) {
selected() = hovered_;
on_change();
return true;
}
return false;
}
bool OnMouseEvent(Event event) {
if (event.mouse().button == Mouse::WheelDown ||
event.mouse().button == Mouse::WheelUp) {
return OnMouseWheel(event);
}
for (int i = 0; i < size(); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
continue;
}
TakeFocus();
focused_entry() = i;
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
if (selected() != i) {
selected() = i;
on_change();
}
return true;
}
}
return false;
}
bool OnMouseWheel(Event event) {
if (!box_.Contain(event.mouse().x, event.mouse().y)) {
return false;
}
const int old_hovered = hovered_;
if (event.mouse().button == Mouse::WheelUp) {
(hovered_)--;
}
if (event.mouse().button == Mouse::WheelDown) {
(hovered_)++;
}
hovered_ = util::clamp(hovered_, 0, size() - 1);
if (hovered_ != old_hovered) {
on_change();
}
return true;
}
void Clamp() {
boxes_.resize(size());
selected() = util::clamp(selected(), 0, size() - 1);
focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
hovered_ = util::clamp(hovered_, 0, size() - 1);
}
bool Focusable() const final { return entries.size(); }
int size() const { return int(entries.size()); }
int hovered_ = selected();
std::vector<Box> boxes_;
Box box_;
};
} // namespace
/// @brief A list of element, where only one can be selected.
/// @param option The parameters
/// @ingroup component
/// @see RadioboxBase
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// std::vector<std::string> entries = {
/// "entry 1",
/// "entry 2",
/// "entry 3",
/// };
/// int selected = 0;
/// auto menu = Radiobox({
/// .entries = entries,
/// .selected = &selected,
/// });
/// screen.Loop(menu);
/// ```
///
/// ### Output
///
/// ```bash
/// ◉ entry 1
/// ○ entry 2
/// ○ entry 3
/// ```
/// NOLINTNEXTLINE
Component Radiobox(RadioboxOption option) {
return Make<RadioboxBase>(std::move(option));
}
/// @brief A list of element, where only one can be selected.
/// @param entries The list of entries in the list.
/// @param selected The index of the currently selected element.
/// @param option Additional optional parameters.
/// @ingroup component
/// @see RadioboxBase
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// std::vector<std::string> entries = {
/// "entry 1",
/// "entry 2",
/// "entry 3",
/// };
/// int selected = 0;
/// auto menu = Radiobox(&entries, &selected);
/// screen.Loop(menu);
/// ```
///
/// ### Output
///
/// ```bash
/// ◉ entry 1
/// ○ entry 2
/// ○ entry 3
/// ```
Component Radiobox(ConstStringListRef entries,
int* selected,
RadioboxOption option) {
option.entries = std::move(entries);
option.selected = selected;
return Make<RadioboxBase>(std::move(option));
}
} // namespace ftxui
box.hpp
ftxui::ConstStringListRef
An adapter. Reference a list of strings.
Definition ref.hpp:116
component.hpp
component_base.hpp
component_options.hpp
elements.hpp
event.hpp
mouse.hpp
ftxui::util::clamp
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition util.hpp:11
ftxui
Definition animation.hpp:10
ftxui::size
Decorator size(WidthOrHeight, Constraint, int value)
Apply a constraint on the size of an element.
Definition size.cpp:89
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Radiobox
Component Radiobox(RadioboxOption options)
A list of element, where only one can be selected.
Definition radiobox.cpp:204
ftxui::Elements
std::vector< Element > Elements
Definition elements.hpp:23
ftxui::focus
Element focus(Element)
Set the child to be the one focused among its siblings.
Definition frame.cpp:101
ftxui::reflect
Decorator reflect(Box &box)
Definition reflect.cpp:43
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:96
ref.hpp
screen_interactive.hpp
ftxui::Event::TabReverse
static const Event TabReverse
Definition event.hpp:54
ftxui::Event::PageUp
static const Event PageUp
Definition event.hpp:60
ftxui::Event::ArrowUp
static const Event ArrowUp
Definition event.hpp:40
ftxui::Event::Tab
static const Event Tab
Definition event.hpp:53
ftxui::Event::ArrowDown
static const Event ArrowDown
Definition event.hpp:41
ftxui::Event::End
static const Event End
Definition event.hpp:59
ftxui::Event::Home
static const Event Home
Definition event.hpp:58
ftxui::Event::PageDown
static const Event PageDown
Definition event.hpp:61
ftxui::Event::Return
static const Event Return
Definition event.hpp:51
ftxui::Mouse::Left
@ Left
Definition mouse.hpp:13
ftxui::Mouse::WheelUp
@ WheelUp
Definition mouse.hpp:17
ftxui::Mouse::WheelDown
@ WheelDown
Definition mouse.hpp:18
ftxui::Mouse::Pressed
@ Pressed
Definition mouse.hpp:25
ftxui::RadioboxOption
Option for the Radiobox component.
Definition component_options.hpp:193
ftxui::RadioboxOption::Simple
static RadioboxOption Simple()
Option for standard Radiobox.
Definition component_options.cpp:287
ftxui::RadioboxOption::transform
std::function< Element(const EntryState &)> transform
Definition component_options.hpp:202
util.hpp