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:
@@ -9,29 +9,62 @@
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Element CheckBox::Render() {
|
||||
/// @brief Draw checkable element.
|
||||
/// @param label The label of the checkbox.
|
||||
/// @param checked Whether the checkbox is checked or not.
|
||||
/// @ingroup component
|
||||
/// @see CheckboxBase
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto screen = ScreenInteractive::FitComponent();
|
||||
/// std::wstring label = L"Make a sandwidth";
|
||||
/// bool checked = false;
|
||||
/// Component checkbox = Checkbox(&label, &checked);
|
||||
/// screen.Loop(checkbox)
|
||||
/// ```
|
||||
///
|
||||
/// ### Output
|
||||
///
|
||||
/// ```bash
|
||||
/// ☐ Make a sandwitch
|
||||
/// ```
|
||||
Component Checkbox(const std::wstring* label, bool* checked) {
|
||||
return Make<CheckboxBase>(label, checked);
|
||||
}
|
||||
|
||||
// static
|
||||
CheckboxBase* From(Component component) {
|
||||
return static_cast<CheckboxBase*>(component.get());
|
||||
}
|
||||
|
||||
CheckboxBase::CheckboxBase(const std::wstring* label, bool* state)
|
||||
: label_(label), state_(state) {}
|
||||
|
||||
Element CheckboxBase::Render() {
|
||||
bool is_focused = Focused();
|
||||
auto style = is_focused ? focused_style : unfocused_style;
|
||||
auto focus_management = is_focused ? focus : state ? select : nothing;
|
||||
return hbox(text(state ? checked : unchecked),
|
||||
text(label) | style | focus_management) |
|
||||
auto focus_management = is_focused ? focus : *state_ ? select : nothing;
|
||||
return hbox(text(*state_ ? checked : unchecked),
|
||||
text(*label_) | style | focus_management) |
|
||||
reflect(box_);
|
||||
}
|
||||
|
||||
bool CheckBox::OnEvent(Event event) {
|
||||
bool CheckboxBase::OnEvent(Event event) {
|
||||
if (event.is_mouse())
|
||||
return OnMouseEvent(event);
|
||||
|
||||
if (event == Event::Character(' ') || event == Event::Return) {
|
||||
state = !state;
|
||||
*state_ = !*state_;
|
||||
on_change();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckBox::OnMouseEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
bool CheckboxBase::OnMouseEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
return false;
|
||||
if (!box_.Contain(event.mouse().x, event.mouse().y))
|
||||
return false;
|
||||
@@ -40,7 +73,7 @@ bool CheckBox::OnMouseEvent(Event event) {
|
||||
|
||||
if (event.mouse().button == Mouse::Left &&
|
||||
event.mouse().motion == Mouse::Pressed) {
|
||||
state = !state;
|
||||
*state_ = !*state_;
|
||||
on_change();
|
||||
return true;
|
||||
}
|
||||
|
Reference in New Issue
Block a user