24class RadioboxBase :
public ComponentBase,
public RadioboxOption {
26 explicit RadioboxBase(
const RadioboxOption& option)
27 : RadioboxOption(option) {}
33 const bool is_menu_focused = Focused();
34 elements.reserve(
size());
35 for (
int i = 0; i <
size(); ++i) {
36 const bool is_focused = (focused_entry() == i) && is_menu_focused;
37 const bool is_selected = (hovered_ == i);
38 auto state = EntryState{
39 entries[i], selected() == i, is_selected, is_focused, i,
46 elements.push_back(element |
reflect(boxes_[i]));
52 bool OnEvent(Event event)
override {
54 if (!CaptureMouse(event)) {
58 if (event.is_mouse()) {
59 return OnMouseEvent(event);
63 const int old_hovered = hovered_;
71 (hovered_) -= box_.y_max - box_.y_min;
74 (hovered_) += box_.y_max - box_.y_min;
80 (hovered_) =
size() - 1;
83 hovered_ = (hovered_ + 1) %
size();
86 hovered_ = (hovered_ +
size() - 1) %
size();
91 if (hovered_ != old_hovered) {
92 focused_entry() = hovered_;
98 if (event == Event::Character(
' ') || event ==
Event::Return) {
99 selected() = hovered_;
107 bool OnMouseEvent(Event event) {
110 return OnMouseWheel(event);
113 for (
int i = 0; i <
size(); ++i) {
114 if (!boxes_[i].Contain(event.mouse().x, event.mouse().y)) {
122 if (selected() != i) {
133 bool OnMouseWheel(Event event) {
134 if (!box_.Contain(event.mouse().x, event.mouse().y)) {
138 const int old_hovered = hovered_;
149 if (hovered_ != old_hovered) {
157 boxes_.resize(
size());
163 bool Focusable() const final {
return entries.size(); }
164 int size()
const {
return int(entries.size()); }
166 int hovered_ = selected();
167 std::vector<Box> boxes_;
238 option.
entries = std::move(entries);
static const Event TabReverse
static const Event PageUp
ConstStringListRef entries
static const Event ArrowUp
static const Event ArrowDown
static const Event PageDown
static const Event Return
static RadioboxOption Simple()
标准Radiobox的选项
std::function< Element(const EntryState &)> transform
Component Radiobox(RadioboxOption options)
元素列表,其中只能选择一个。
Decorator size(WidthOrHeight, Constraint, int value)
对元素大小应用约束。
Element focus(Element)
将 child 设置为其同级元素中获得焦点的元素。
Element vbox(Elements)
垂直一个接一个显示元素的容器。
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
#include "ftxui/component/component_base.hpp" // 用于 ComponentBase
std::shared_ptr< T > Make(Args &&... args)
std::shared_ptr< Node > Element
std::vector< Element > Elements
Decorator reflect(Box &box)
std::shared_ptr< ComponentBase > Component