mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00
Make component more functionnal
This commit is contained in:
@@ -1,36 +1,76 @@
|
||||
#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
|
||||
#include "ftxui/component/radiobox.hpp"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp"
|
||||
#include "ftxui/component/mouse.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/component/screen_interactive.hpp" // for Component
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Element RadioBox::Render() {
|
||||
/// @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);
|
||||
}
|
||||
|
||||
// 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_) {}
|
||||
|
||||
Element RadioboxBase::Render() {
|
||||
std::vector<Element> elements;
|
||||
bool is_focused = Focused();
|
||||
boxes_.resize(entries.size());
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
boxes_.resize(entries_->size());
|
||||
for (size_t i = 0; i < entries_->size(); ++i) {
|
||||
auto style =
|
||||
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
|
||||
auto focus_management = (focused != int(i)) ? nothing
|
||||
: is_focused ? focus
|
||||
: select;
|
||||
|
||||
const std::wstring& symbol = selected == int(i) ? checked : unchecked;
|
||||
elements.push_back(hbox(text(symbol), text(entries[i]) | style) |
|
||||
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]));
|
||||
}
|
||||
return vbox(std::move(elements));
|
||||
}
|
||||
|
||||
bool RadioBox::OnEvent(Event event) {
|
||||
bool RadioboxBase::OnEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
return false;
|
||||
if (event.is_mouse())
|
||||
@@ -44,12 +84,12 @@ bool RadioBox::OnEvent(Event event) {
|
||||
new_focused--;
|
||||
if (event == Event::ArrowDown || event == Event::Character('j'))
|
||||
new_focused++;
|
||||
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();
|
||||
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();
|
||||
|
||||
new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused));
|
||||
new_focused = std::max(0, std::min(int(entries_->size()) - 1, new_focused));
|
||||
|
||||
if (focused != new_focused) {
|
||||
focused = new_focused;
|
||||
@@ -57,14 +97,14 @@ bool RadioBox::OnEvent(Event event) {
|
||||
}
|
||||
|
||||
if (event == Event::Character(' ') || event == Event::Return) {
|
||||
selected = focused;
|
||||
*selected_ = focused;
|
||||
on_change();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RadioBox::OnMouseEvent(Event event) {
|
||||
bool RadioboxBase::OnMouseEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
return false;
|
||||
for (int i = 0; i < boxes_.size(); ++i) {
|
||||
@@ -78,8 +118,8 @@ bool RadioBox::OnMouseEvent(Event event) {
|
||||
event.mouse().motion == Mouse::Pressed) {
|
||||
cursor_position = i;
|
||||
TakeFocus();
|
||||
if (selected != i) {
|
||||
selected = i;
|
||||
if (*selected_ != i) {
|
||||
*selected_ = i;
|
||||
on_change();
|
||||
}
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user