mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-15 15:38:21 +08:00
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:
@@ -40,37 +40,42 @@ Component Vertical(Components children, int* selector);
|
||||
Component Horizontal(Components children);
|
||||
Component Horizontal(Components children, int* selector);
|
||||
Component Tab(Components children, int* selector);
|
||||
|
||||
} // namespace Container
|
||||
|
||||
Component Button(ButtonOption options);
|
||||
Component Button(ConstStringRef label,
|
||||
std::function<void()> on_click,
|
||||
Ref<ButtonOption> = ButtonOption::Simple());
|
||||
ButtonOption options = ButtonOption::Simple());
|
||||
|
||||
Component Checkbox(CheckboxOption options);
|
||||
Component Checkbox(ConstStringRef label,
|
||||
bool* checked,
|
||||
Ref<CheckboxOption> option = CheckboxOption::Simple());
|
||||
CheckboxOption options = CheckboxOption::Simple());
|
||||
|
||||
Component Input(StringRef content, Ref<InputOption> option = {});
|
||||
Component Input(InputOption options = {});
|
||||
Component Input(StringRef content, InputOption options = {});
|
||||
Component Input(StringRef content,
|
||||
StringRef placeholder,
|
||||
Ref<InputOption> option = {});
|
||||
InputOption options = {});
|
||||
|
||||
Component Menu(MenuOption options);
|
||||
Component Menu(ConstStringListRef entries,
|
||||
int* selected_,
|
||||
Ref<MenuOption> = MenuOption::Vertical());
|
||||
Component MenuEntry(ConstStringRef label, Ref<MenuEntryOption> = {});
|
||||
|
||||
Component Dropdown(ConstStringListRef entries, int* selected);
|
||||
MenuOption options = MenuOption::Vertical());
|
||||
Component MenuEntry(MenuEntryOption options);
|
||||
Component MenuEntry(ConstStringRef label, MenuEntryOption options = {});
|
||||
|
||||
Component Radiobox(RadioboxOption options);
|
||||
Component Radiobox(ConstStringListRef entries,
|
||||
int* selected_,
|
||||
Ref<RadioboxOption> option = {});
|
||||
RadioboxOption options = {});
|
||||
|
||||
Component Dropdown(ConstStringListRef entries, int* selected);
|
||||
Component Toggle(ConstStringListRef entries, int* selected);
|
||||
|
||||
// General slider constructor:
|
||||
template <typename T>
|
||||
Component Slider(SliderOption<T> options = {});
|
||||
Component Slider(SliderOption<T> options);
|
||||
|
||||
// Shorthand without the `SliderOption` constructor:
|
||||
Component Slider(ConstStringRef label,
|
||||
@@ -89,11 +94,11 @@ Component Slider(ConstStringRef label,
|
||||
ConstRef<long> max = 100l,
|
||||
ConstRef<long> increment = 5l);
|
||||
|
||||
Component ResizableSplit(ResizableSplitOption options);
|
||||
Component ResizableSplitLeft(Component main, Component back, int* main_size);
|
||||
Component ResizableSplitRight(Component main, Component back, int* main_size);
|
||||
Component ResizableSplitTop(Component main, Component back, int* main_size);
|
||||
Component ResizableSplitBottom(Component main, Component back, int* main_size);
|
||||
Component ResizableSplit(ResizableSplitOption options);
|
||||
|
||||
Component Renderer(Component child, std::function<Element()>);
|
||||
Component Renderer(std::function<Element()>);
|
||||
|
@@ -72,6 +72,7 @@ struct AnimatedColorsOption {
|
||||
/// @brief Option for the MenuEntry component.
|
||||
/// @ingroup component
|
||||
struct MenuEntryOption {
|
||||
ConstStringRef label = "MenuEntry";
|
||||
std::function<Element(const EntryState& state)> transform;
|
||||
AnimatedColorsOption animated_colors;
|
||||
};
|
||||
@@ -86,9 +87,12 @@ struct MenuOption {
|
||||
static MenuOption VerticalAnimated();
|
||||
static MenuOption Toggle();
|
||||
|
||||
ConstStringListRef entries; ///> The list of entries.
|
||||
Ref<int> selected = 0; ///> The index of the selected entry.
|
||||
|
||||
// Style:
|
||||
UnderlineOption underline;
|
||||
MenuEntryOption entries;
|
||||
MenuEntryOption entries_option;
|
||||
Direction direction = Direction::Down;
|
||||
std::function<Element()> elements_prefix;
|
||||
std::function<Element()> elements_infix;
|
||||
@@ -115,6 +119,9 @@ struct ButtonOption {
|
||||
Color background_active,
|
||||
Color foreground_active);
|
||||
|
||||
ConstStringRef label = "Button";
|
||||
std::function<void()> on_click = [] {};
|
||||
|
||||
// Style:
|
||||
std::function<Element(const EntryState&)> transform;
|
||||
AnimatedColorsOption animated_colors;
|
||||
@@ -126,6 +133,10 @@ struct CheckboxOption {
|
||||
// Standard constructors:
|
||||
static CheckboxOption Simple();
|
||||
|
||||
ConstStringRef label = "Checkbox";
|
||||
|
||||
Ref<bool> checked = false;
|
||||
|
||||
// Style:
|
||||
std::function<Element(const EntryState&)> transform;
|
||||
|
||||
@@ -153,6 +164,9 @@ struct InputOption {
|
||||
/// @brief A white on black style with high margins:
|
||||
static InputOption Spacious();
|
||||
|
||||
/// The content of the input.
|
||||
StringRef content = "";
|
||||
|
||||
/// The content of the input when it's empty.
|
||||
StringRef placeholder = "";
|
||||
|
||||
@@ -176,6 +190,10 @@ struct RadioboxOption {
|
||||
// Standard constructors:
|
||||
static RadioboxOption Simple();
|
||||
|
||||
// Content:
|
||||
ConstStringListRef entries;
|
||||
Ref<int> selected = 0;
|
||||
|
||||
// Style:
|
||||
std::function<Element(const EntryState&)> transform;
|
||||
|
||||
|
@@ -106,7 +106,7 @@ class Screen {
|
||||
|
||||
// Store an hyperlink in the screen. Return the id of the hyperlink. The id is
|
||||
// used to identify the hyperlink when the user click on it.
|
||||
uint8_t RegisterHyperlink(std::string link);
|
||||
uint8_t RegisterHyperlink(const std::string& link);
|
||||
const std::string& Hyperlink(uint8_t id) const;
|
||||
|
||||
Box stencil;
|
||||
|
@@ -13,6 +13,12 @@ class ConstRef {
|
||||
ConstRef() {}
|
||||
ConstRef(T t) : owned_(t) {}
|
||||
ConstRef(const T* t) : address_(t) {}
|
||||
ConstRef(const ConstRef& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
ConstRef& operator=(const ConstRef& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
const T& operator*() const { return address_ ? *address_ : owned_; }
|
||||
const T& operator()() const { return address_ ? *address_ : owned_; }
|
||||
const T* operator->() const { return address_ ? address_ : &owned_; }
|
||||
@@ -30,6 +36,12 @@ class Ref {
|
||||
Ref(const T& t) : owned_(t) {}
|
||||
Ref(T&& t) : owned_(std::forward<T>(t)) {}
|
||||
Ref(T* t) : owned_(), address_(t) {}
|
||||
Ref(const Ref& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
Ref& operator=(const Ref& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
T& operator*() { return address_ ? *address_ : owned_; }
|
||||
T& operator()() { return address_ ? *address_ : owned_; }
|
||||
T* operator->() { return address_ ? address_ : &owned_; }
|
||||
@@ -47,6 +59,12 @@ class StringRef {
|
||||
StringRef(std::string ref) : owned_(std::move(ref)) {}
|
||||
StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
|
||||
StringRef(const char* ref) : StringRef(std::string(ref)) {}
|
||||
StringRef(const StringRef& t) : owned_(t.owned_), address_(t.address_) {}
|
||||
StringRef& operator=(const StringRef& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
std::string& operator*() { return address_ ? *address_ : owned_; }
|
||||
std::string& operator()() { return address_ ? *address_ : owned_; }
|
||||
std::string* operator->() { return address_ ? address_ : &owned_; }
|
||||
@@ -67,6 +85,18 @@ class ConstStringRef {
|
||||
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
|
||||
ConstStringRef(const char* ref)
|
||||
: ConstStringRef(to_wstring(std::string(ref))) {}
|
||||
ConstStringRef(const ConstStringRef& t)
|
||||
: owned_(t.owned_), address_(t.address_) {}
|
||||
ConstStringRef& operator=(const ConstStringRef& t) {
|
||||
owned_ = t.owned_;
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
ConstStringRef& operator=(ConstStringRef&& t) {
|
||||
owned_ = std::move(t.owned_);
|
||||
address_ = t.address_;
|
||||
return *this;
|
||||
}
|
||||
const std::string& operator()() const {
|
||||
return address_ ? *address_ : owned_;
|
||||
}
|
||||
@@ -76,19 +106,35 @@ class ConstStringRef {
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string owned_;
|
||||
std::string owned_;
|
||||
const std::string* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief An adapter. Reference a list of strings.
|
||||
class ConstStringListRef {
|
||||
public:
|
||||
ConstStringListRef() = default;
|
||||
ConstStringListRef(const std::vector<std::string>* ref) : ref_(ref) {}
|
||||
ConstStringListRef(const std::vector<std::wstring>* ref) : ref_wide_(ref) {}
|
||||
|
||||
size_t size() const { return ref_ ? ref_->size() : ref_wide_->size(); }
|
||||
size_t size() const {
|
||||
if (ref_) {
|
||||
return ref_->size();
|
||||
}
|
||||
if (ref_wide_) {
|
||||
return ref_wide_->size();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string operator[](size_t i) const {
|
||||
return ref_ ? (*ref_)[i] : to_string((*ref_wide_)[i]);
|
||||
if (ref_) {
|
||||
return (*ref_)[i];
|
||||
}
|
||||
if (ref_wide_) {
|
||||
return to_string((*ref_wide_)[i]);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private:
|
||||
|
Reference in New Issue
Block a user