Remove Ref<XxxOption> and add new interfaces. (#686)

1. Stop taking Ref<XxxOption> in Component constructors. Instead, use
   the XxxOption directly. Passing by copy avoid problems developers had
   where one was shared in between multiple component, causing issues.

2. Add variants of most component constructors taking a struct only.

This replaces:
https://github.com/ArthurSonzogni/FTXUI/pull/670

This fixes:
https://github.com/ArthurSonzogni/FTXUI/issues/426
This commit is contained in:
Arthur Sonzogni
2023-06-25 17:22:05 +02:00
committed by GitHub
parent e73e7f0d68
commit 455998d759
26 changed files with 918 additions and 693 deletions

View File

@@ -14,10 +14,10 @@
namespace ftxui {
namespace {
class CheckboxBase : public ComponentBase {
class CheckboxBase : public ComponentBase, public CheckboxOption {
public:
CheckboxBase(ConstStringRef label, bool* state, Ref<CheckboxOption> option)
: label_(std::move(label)), state_(state), option_(std::move(option)) {}
explicit CheckboxBase(CheckboxOption option)
: CheckboxOption(std::move(option)) {}
private:
// Component implementation.
@@ -25,15 +25,14 @@ class CheckboxBase : public ComponentBase {
const bool is_focused = Focused();
const bool is_active = Active();
auto focus_management = is_focused ? focus : is_active ? select : nothing;
auto state = EntryState{
*label_,
*state_,
auto entry_state = EntryState{
*label,
*checked,
is_active,
is_focused || hovered_,
};
auto element =
(option_->transform ? option_->transform
: CheckboxOption::Simple().transform)(state);
auto element = (transform ? transform : CheckboxOption::Simple().transform)(
entry_state);
return element | focus_management | reflect(box_);
}
@@ -48,8 +47,8 @@ class CheckboxBase : public ComponentBase {
hovered_ = false;
if (event == Event::Character(' ') || event == Event::Return) {
*state_ = !*state_;
option_->on_change();
*checked = !*checked;
on_change();
TakeFocus();
return true;
}
@@ -69,8 +68,8 @@ class CheckboxBase : public ComponentBase {
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
*state_ = !*state_;
option_->on_change();
*checked = !*checked;
on_change();
return true;
}
@@ -79,10 +78,7 @@ class CheckboxBase : public ComponentBase {
bool Focusable() const final { return true; }
ConstStringRef label_;
bool* const state_;
bool hovered_ = false;
Ref<CheckboxOption> option_;
Box box_;
};
} // namespace
@@ -109,10 +105,11 @@ class CheckboxBase : public ComponentBase {
/// ```bash
/// ☐ Make a sandwitch
/// ```
Component Checkbox(ConstStringRef label,
bool* checked,
Ref<CheckboxOption> option) {
return Make<CheckboxBase>(std::move(label), checked, std::move(option));
// NOLINTNEXTLINE
Component Checkbox(ConstStringRef label, bool* checked, CheckboxOption option) {
option.label = label;
option.checked = checked;
return Make<CheckboxBase>(std::move(option));
}
} // namespace ftxui