FTXUI/src/ftxui/component/radiobox.cpp

201 lines
6.2 KiB
C++
Raw Normal View History

2021-05-10 02:32:27 +08:00
#include <stddef.h> // for size_t
#include <algorithm> // for max, min
#include <functional> // for function
#include <memory> // for shared_ptr, allocator_traits<>::value_type
#include <string> // for string
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
#include "ftxui/component/component_options.hpp" // for RadioboxOption
2021-05-10 02:32:27 +08:00
#include "ftxui/component/event.hpp" // for Event, Event::ArrowDown, Event::ArrowUp, Event::Return, Event::Tab, Event::TabReverse
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for Component
2021-07-10 19:20:43 +08:00
#include "ftxui/dom/elements.hpp" // for Element, operator|, text, hbox, reflect, vbox, focus, nothing, select
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/util/ref.hpp" // for Ref
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:
RadioboxBase(ConstStringListRef entries,
2021-07-10 19:07:01 +08:00
int* selected,
Ref<RadioboxOption> option)
: entries_(entries), selected_(selected), option_(std::move(option)) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default
// radiobox glyph.
if (option_->style_checked == "")
option_->style_checked = "(*)";
if (option_->style_unchecked == "")
option_->style_unchecked = "( )";
2021-07-10 19:07:01 +08:00
#endif
2021-09-08 15:36:37 +08:00
hovered_ = *selected_;
2021-07-10 19:07:01 +08:00
}
private:
Element Render() override {
Elements elements;
2021-09-08 15:36:37 +08:00
bool is_menu_focused = Focused();
boxes_.resize(entries_.size());
for (size_t i = 0; i < entries_.size(); ++i) {
2021-09-08 15:36:37 +08:00
bool is_focused = (focused_entry() == int(i)) && is_menu_focused;
bool is_selected = (hovered_ == int(i));
auto style = is_selected ? (is_focused ? option_->style_selected_focused
: option_->style_selected)
: (is_focused ? option_->style_focused
: option_->style_normal);
auto focus_management = !is_selected ? nothing
: is_menu_focused ? focus
: select;
2021-07-10 19:07:01 +08:00
const std::string& symbol = *selected_ == int(i)
? option_->style_checked
: option_->style_unchecked;
elements.push_back(hbox(text(symbol), text(entries_[i]) | style) |
2021-07-10 19:07:01 +08:00
focus_management | reflect(boxes_[i]));
}
2021-09-08 15:36:37 +08:00
return vbox(std::move(elements)) | reflect(box_);
2021-07-10 19:07:01 +08:00
}
bool OnEvent(Event event) override {
if (!CaptureMouse(event))
return false;
2021-09-08 15:36:37 +08:00
2021-07-10 19:07:01 +08:00
if (event.is_mouse())
return OnMouseEvent(event);
2021-09-08 15:36:37 +08:00
if (Focused()) {
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::Tab && entries_.size())
hovered_ = (hovered_ + 1) % entries_.size();
if (event == Event::TabReverse && entries_.size())
hovered_ = (hovered_ + entries_.size() - 1) % entries_.size();
hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
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_;
//*selected_ = focused_entry();
2021-07-10 19:07:01 +08:00
option_->on_change();
}
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);
}
2021-07-10 19:07:01 +08:00
for (int i = 0; i < int(boxes_.size()); ++i) {
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
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) {
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;
int old_hovered = hovered_;
if (event.mouse().button == Mouse::WheelUp)
(hovered_)--;
if (event.mouse().button == Mouse::WheelDown)
(hovered_)++;
hovered_ = std::max(0, std::min(int(entries_.size()) - 1, hovered_));
if (hovered_ != old_hovered)
option_->on_change();
return true;
}
bool Focusable() const final { return entries_.size(); }
2021-07-10 19:07:01 +08:00
int& focused_entry() { return option_->focused_entry(); }
ConstStringListRef entries_;
2021-09-08 15:36:37 +08:00
int* selected_;
int hovered_;
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();
/// 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
/// ```
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
// 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.