mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-18 00:48:09 +08:00
Add RadioBox.
This commit is contained in:
@@ -36,6 +36,7 @@ add_library(component
|
||||
src/ftxui/component/event.cpp
|
||||
src/ftxui/component/input.cpp
|
||||
src/ftxui/component/menu.cpp
|
||||
src/ftxui/component/radiobox.cpp
|
||||
src/ftxui/component/screen_interactive.cpp
|
||||
src/ftxui/component/toggle.cpp
|
||||
)
|
||||
|
@@ -15,8 +15,10 @@ class CheckBox : public Component {
|
||||
bool state = false;
|
||||
std::wstring label = L"label";
|
||||
|
||||
std::wstring checked = L"[X] ";
|
||||
std::wstring unchecked = L"[ ] ";
|
||||
//std::wstring checked = L"[X] ";
|
||||
//std::wstring unchecked = L"[ ] ";
|
||||
std::wstring checked = L"☑ ";
|
||||
std::wstring unchecked = L"☐ ";
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = [](){};
|
||||
|
38
ftxui/include/ftxui/component/radiobox.hpp
Normal file
38
ftxui/include/ftxui/component/radiobox.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef FTXUI_COMPONENT_RADIOBOX_HPP
|
||||
#define FTXUI_COMPONENT_RADIOBOX_HPP
|
||||
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
class RadioBox : public Component {
|
||||
public:
|
||||
// Constructor.
|
||||
RadioBox() = default;
|
||||
~RadioBox() override = default;
|
||||
|
||||
int selected = 0;
|
||||
int focused = 0;
|
||||
std::vector<std::wstring> entries;
|
||||
|
||||
std::wstring checked = L"◉ ";
|
||||
std::wstring unchecked = L"○ ";
|
||||
|
||||
Decorator focused_style = inverted;
|
||||
Decorator unfocused_style = nothing;
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = [](){};
|
||||
|
||||
// Component implementation.
|
||||
Element Render() override;
|
||||
bool OnEvent(Event) override;
|
||||
|
||||
private:
|
||||
int cursor_position = 0;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_RADIOBOX_HPP */
|
41
ftxui/src/ftxui/component/radiobox.cpp
Normal file
41
ftxui/src/ftxui/component/radiobox.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "ftxui/component/radiobox.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Element RadioBox::Render() {
|
||||
std::vector<Element> elements;
|
||||
bool is_focused = Focused();
|
||||
for (size_t i = 0; i < entries.size(); ++i) {
|
||||
auto style =
|
||||
(focused == int(i) && is_focused) ? focused_style : unfocused_style;
|
||||
const std::wstring& symbol = selected == int(i) ? checked : unchecked;
|
||||
elements.push_back(hbox(text(symbol), text(entries[i]) | style));
|
||||
}
|
||||
return vbox(std::move(elements));
|
||||
}
|
||||
|
||||
bool RadioBox::OnEvent(Event event) {
|
||||
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++;
|
||||
new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused));
|
||||
if (focused != new_focused) {
|
||||
focused = new_focused;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (event == Event::Character(' ')) {
|
||||
selected = focused;
|
||||
on_change();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
Reference in New Issue
Block a user