2021-05-10 02:32:27 +08:00
|
|
|
#include <functional> // for function
|
2022-08-31 00:52:33 +08:00
|
|
|
#include <memory> // for allocator_traits<>::value_type
|
2021-05-10 02:32:27 +08:00
|
|
|
#include <utility> // for move
|
2021-07-10 19:20:43 +08:00
|
|
|
#include <vector> // for vector
|
2021-05-10 02:32:27 +08:00
|
|
|
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
|
|
|
#include "ftxui/component/component.hpp" // for Make, Radiobox
|
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
2022-03-31 08:17:43 +08:00
|
|
|
#include "ftxui/component/component_options.hpp" // for RadioboxOption, EntryState
|
2022-01-07 05:38:32 +08:00
|
|
|
#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
|
2021-05-10 02:32:27 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2022-03-31 08:17:43 +08:00
|
|
|
#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
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
namespace ftxui {
|
2021-05-02 02:40:35 +08:00
|
|
|
|
2021-07-10 19:07:01 +08:00
|
|
|
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:
|
2021-08-09 05:25:20 +08:00
|
|
|
RadioboxBase(ConstStringListRef entries,
|
2021-07-10 19:07:01 +08:00
|
|
|
int* selected,
|
|
|
|
Ref<RadioboxOption> option)
|
2022-09-03 18:12:59 +08:00
|
|
|
: entries_(entries), selected_(selected), option_(std::move(option)) {}
|
2021-07-10 19:07:01 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Element Render() override {
|
2022-01-07 05:38:32 +08:00
|
|
|
Clamp();
|
2021-08-09 05:25:20 +08:00
|
|
|
Elements elements;
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_menu_focused = Focused();
|
2023-06-01 01:24:08 +08:00
|
|
|
elements.reserve(size());
|
2022-01-07 05:38:32 +08:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2022-12-20 01:51:25 +08:00
|
|
|
const bool is_focused = (focused_entry() == i) && is_menu_focused;
|
|
|
|
const bool is_selected = (hovered_ == i);
|
2022-05-08 14:44:38 +08:00
|
|
|
auto focus_management = !is_selected ? nothing
|
|
|
|
: is_menu_focused ? focus
|
|
|
|
: select;
|
2022-03-14 01:51:46 +08:00
|
|
|
auto state = EntryState{
|
|
|
|
entries_[i],
|
|
|
|
*selected_ == i,
|
|
|
|
is_selected,
|
|
|
|
is_focused,
|
|
|
|
};
|
|
|
|
auto element =
|
2022-03-31 08:17:43 +08:00
|
|
|
(option_->transform ? option_->transform
|
|
|
|
: RadioboxOption::Simple().transform)(state);
|
2021-07-10 19:07:01 +08:00
|
|
|
|
2022-03-14 01:51:46 +08:00
|
|
|
elements.push_back(element | focus_management | reflect(boxes_[i]));
|
2021-07-10 19:07:01 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
return vbox(std::move(elements)) | reflect(box_);
|
2021-07-10 19:07:01 +08:00
|
|
|
}
|
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
|
2021-07-10 19:07:01 +08:00
|
|
|
bool OnEvent(Event event) override {
|
2022-01-07 05:38:32 +08:00
|
|
|
Clamp();
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!CaptureMouse(event)) {
|
2021-07-10 19:07:01 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.is_mouse()) {
|
2021-07-10 19:07:01 +08:00
|
|
|
return OnMouseEvent(event);
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 19:07:01 +08:00
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
if (Focused()) {
|
2022-12-20 01:51:25 +08:00
|
|
|
const int old_hovered = hovered_;
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event == Event::ArrowUp || event == Event::Character('k')) {
|
2021-09-08 15:36:37 +08:00
|
|
|
(hovered_)--;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::ArrowDown || event == Event::Character('j')) {
|
2021-09-08 15:36:37 +08:00
|
|
|
(hovered_)++;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::PageUp) {
|
2021-10-21 03:15:40 +08:00
|
|
|
(hovered_) -= box_.y_max - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::PageDown) {
|
2021-10-21 03:15:40 +08:00
|
|
|
(hovered_) += box_.y_max - box_.y_min;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::Home) {
|
2021-10-21 03:15:40 +08:00
|
|
|
(hovered_) = 0;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::End) {
|
2022-01-07 05:38:32 +08:00
|
|
|
(hovered_) = size() - 1;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::Tab && size()) {
|
2022-01-07 05:38:32 +08:00
|
|
|
hovered_ = (hovered_ + 1) % size();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event == Event::TabReverse && size()) {
|
2022-01-07 05:38:32 +08:00
|
|
|
hovered_ = (hovered_ + size() - 1) % size();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-01-13 08:46:09 +08:00
|
|
|
hovered_ = util::clamp(hovered_, 0, size() - 1);
|
2021-09-08 15:36:37 +08:00
|
|
|
|
|
|
|
if (hovered_ != old_hovered) {
|
|
|
|
focused_entry() = hovered_;
|
|
|
|
option_->on_change();
|
|
|
|
return true;
|
|
|
|
}
|
2021-07-10 19:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::Character(' ') || event == Event::Return) {
|
2021-09-08 15:36:37 +08:00
|
|
|
*selected_ = hovered_;
|
2021-07-10 19:07:01 +08:00
|
|
|
option_->on_change();
|
2022-10-01 19:34:15 +08:00
|
|
|
return true;
|
2021-07-10 19:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnMouseEvent(Event event) {
|
2021-09-08 15:36:37 +08:00
|
|
|
if (event.mouse().button == Mouse::WheelDown ||
|
|
|
|
event.mouse().button == Mouse::WheelUp) {
|
|
|
|
return OnMouseWheel(event);
|
|
|
|
}
|
|
|
|
|
2022-01-07 05:38:32 +08:00
|
|
|
for (int i = 0; i < size(); ++i) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
|
2021-07-10 19:07:01 +08:00
|
|
|
continue;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-07-10 19:07:01 +08:00
|
|
|
|
|
|
|
TakeFocus();
|
2021-09-08 15:36:37 +08:00
|
|
|
focused_entry() = i;
|
2021-07-10 19:07:01 +08:00
|
|
|
if (event.mouse().button == Mouse::Left &&
|
2021-09-08 15:36:37 +08:00
|
|
|
event.mouse().motion == Mouse::Released) {
|
2021-07-10 19:07:01 +08:00
|
|
|
if (*selected_ != i) {
|
|
|
|
*selected_ = i;
|
|
|
|
option_->on_change();
|
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2021-07-10 19:07:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:36:37 +08:00
|
|
|
bool OnMouseWheel(Event event) {
|
2022-03-31 08:17:43 +08:00
|
|
|
if (!box_.Contain(event.mouse().x, event.mouse().y)) {
|
2021-09-08 15:36:37 +08:00
|
|
|
return false;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-12-20 01:51:25 +08:00
|
|
|
const int old_hovered = hovered_;
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (event.mouse().button == Mouse::WheelUp) {
|
2021-09-08 15:36:37 +08:00
|
|
|
(hovered_)--;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
|
|
|
if (event.mouse().button == Mouse::WheelDown) {
|
2021-09-08 15:36:37 +08:00
|
|
|
(hovered_)++;
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-01-13 08:46:09 +08:00
|
|
|
hovered_ = util::clamp(hovered_, 0, size() - 1);
|
2021-09-08 15:36:37 +08:00
|
|
|
|
2022-03-31 08:17:43 +08:00
|
|
|
if (hovered_ != old_hovered) {
|
2021-09-08 15:36:37 +08:00
|
|
|
option_->on_change();
|
2022-03-31 08:17:43 +08:00
|
|
|
}
|
2021-09-08 15:36:37 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-07 05:38:32 +08:00
|
|
|
void Clamp() {
|
|
|
|
boxes_.resize(size());
|
2022-01-13 08:46:09 +08:00
|
|
|
*selected_ = util::clamp(*selected_, 0, size() - 1);
|
|
|
|
focused_entry() = util::clamp(focused_entry(), 0, size() - 1);
|
|
|
|
hovered_ = util::clamp(hovered_, 0, size() - 1);
|
2022-01-07 05:38:32 +08:00
|
|
|
}
|
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
bool Focusable() const final { return entries_.size(); }
|
2021-07-10 19:07:01 +08:00
|
|
|
int& focused_entry() { return option_->focused_entry(); }
|
2022-03-31 08:17:43 +08:00
|
|
|
int size() const { return int(entries_.size()); }
|
2021-07-10 19:07:01 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
ConstStringListRef entries_;
|
2021-09-08 15:36:37 +08:00
|
|
|
int* selected_;
|
2022-08-29 03:30:01 +08:00
|
|
|
int hovered_ = *selected_;
|
2021-07-10 19:07:01 +08:00
|
|
|
std::vector<Box> boxes_;
|
2021-09-08 15:36:37 +08:00
|
|
|
Box box_;
|
2021-07-10 19:07:01 +08:00
|
|
|
Ref<RadioboxOption> option_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @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.
|
2021-07-10 20:23:46 +08:00
|
|
|
/// @param option Additional optional parameters.
|
2021-05-10 02:32:27 +08:00
|
|
|
/// @ingroup component
|
|
|
|
/// @see RadioboxBase
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
2021-08-09 05:25:20 +08:00
|
|
|
/// std::vector<std::string> entries = {
|
|
|
|
/// "entry 1",
|
|
|
|
/// "entry 2",
|
|
|
|
/// "entry 3",
|
2021-05-10 02:32:27 +08:00
|
|
|
/// };
|
|
|
|
/// int selected = 0;
|
|
|
|
/// auto menu = Radiobox(&entries, &selected);
|
|
|
|
/// screen.Loop(menu);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// ◉ entry 1
|
|
|
|
/// ○ entry 2
|
|
|
|
/// ○ entry 3
|
|
|
|
/// ```
|
2021-08-09 05:25:20 +08:00
|
|
|
Component Radiobox(ConstStringListRef entries,
|
2021-07-10 16:50:25 +08:00
|
|
|
int* selected,
|
2021-07-10 18:29:39 +08:00
|
|
|
Ref<RadioboxOption> option) {
|
2021-07-10 16:50:25 +08:00
|
|
|
return Make<RadioboxBase>(entries, selected, std::move(option));
|
2021-05-10 02:32:27 +08:00
|
|
|
}
|
2019-01-19 05:41:33 +08:00
|
|
|
|
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// 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.
|