FTXUI/src/ftxui/component/radiobox.cpp

136 lines
3.9 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 <utility> // for move
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#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
2019-01-19 05:41:33 +08:00
#include "ftxui/component/radiobox.hpp"
2021-05-10 02:32:27 +08:00
#include "ftxui/component/screen_interactive.hpp" // for Component
2021-05-10 02:32:27 +08:00
namespace ftxui {
2021-05-02 02:40:35 +08:00
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.
/// @ingroup component
/// @see RadioboxBase
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// std::vector<std::wstring> entries = {
/// L"entry 1",
/// L"entry 2",
/// L"entry 3",
/// };
/// int selected = 0;
/// auto menu = Radiobox(&entries, &selected);
/// screen.Loop(menu);
/// ```
///
/// ### Output
///
/// ```bash
/// ◉ entry 1
/// ○ entry 2
/// ○ entry 3
/// ```
Component Radiobox(const std::vector<std::wstring>* entries, int* selected) {
return Make<RadioboxBase>(entries, selected);
}
2019-01-19 05:41:33 +08:00
2021-05-10 02:32:27 +08:00
// static
RadioboxBase* RadioboxBase::From(Component component) {
return static_cast<RadioboxBase*>(component.get());
}
RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries,
int* selected)
: entries_(entries), selected_(selected) {}
2019-01-19 05:41:33 +08:00
2021-05-10 02:32:27 +08:00
Element RadioboxBase::Render() {
2019-01-19 05:41:33 +08:00
std::vector<Element> elements;
bool is_focused = Focused();
2021-05-10 02:32:27 +08:00
boxes_.resize(entries_->size());
for (size_t i = 0; i < entries_->size(); ++i) {
2019-01-19 05:41:33 +08:00
auto style =
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
2021-05-02 02:40:35 +08:00
auto focus_management = (focused != int(i)) ? nothing
: is_focused ? focus
: select;
2019-01-20 05:06:05 +08:00
2021-05-10 02:32:27 +08:00
const std::wstring& symbol = *selected_ == int(i) ? checked : unchecked;
elements.push_back(hbox(text(symbol), text(entries_->at(i)) | style) |
focus_management | reflect(boxes_[i]));
2019-01-19 05:41:33 +08:00
}
return vbox(std::move(elements));
}
2021-05-10 02:32:27 +08:00
bool RadioboxBase::OnEvent(Event event) {
2021-05-02 02:40:35 +08:00
if (!CaptureMouse(event))
return false;
if (event.is_mouse())
return OnMouseEvent(event);
2019-01-19 05:41:33 +08:00
if (!Focused())
return false;
int new_focused = focused;
if (event == Event::ArrowUp || event == Event::Character('k'))
new_focused--;
if (event == Event::ArrowDown || event == Event::Character('j'))
new_focused++;
2021-05-10 02:32:27 +08:00
if (event == Event::Tab && entries_->size())
new_focused = (new_focused + 1) % entries_->size();
if (event == Event::TabReverse && entries_->size())
new_focused = (new_focused + entries_->size() - 1) % entries_->size();
2021-05-10 02:32:27 +08:00
new_focused = std::max(0, std::min(int(entries_->size()) - 1, new_focused));
2019-01-19 05:41:33 +08:00
if (focused != new_focused) {
focused = new_focused;
return true;
}
if (event == Event::Character(' ') || event == Event::Return) {
2021-05-10 02:32:27 +08:00
*selected_ = focused;
2019-01-19 05:41:33 +08:00
on_change();
}
return false;
}
2021-05-10 02:32:27 +08:00
bool RadioboxBase::OnMouseEvent(Event event) {
2021-05-02 02:40:35 +08:00
if (!CaptureMouse(event))
return false;
2021-05-16 15:38:24 +08:00
for (int i = 0; i < int(boxes_.size()); ++i) {
2021-04-25 21:22:38 +08:00
if (!boxes_[i].Contain(event.mouse().x, event.mouse().y))
continue;
2021-04-25 21:22:38 +08:00
focused = i;
TakeFocus();
2021-04-25 21:22:38 +08:00
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
cursor_position = i;
TakeFocus();
2021-05-10 02:32:27 +08:00
if (*selected_ != i) {
*selected_ = i;
on_change();
}
return true;
}
}
return false;
}
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.