Add option for Button.

This commit is contained in:
ArthurSonzogni 2021-07-07 22:23:07 +02:00 committed by Arthur Sonzogni
parent cd84b187b3
commit 359100ca73
5 changed files with 19 additions and 11 deletions

View File

@ -13,11 +13,13 @@ int main(int argc, const char* argv[]) {
int value = 50; int value = 50;
// The tree of components. This defines how to navigate using the keyboard. // The tree of components. This defines how to navigate using the keyboard.
auto button_option = ButtonOption();
button_option.border = false;
auto buttons = Container::Horizontal({ auto buttons = Container::Horizontal({
Button( Button(
"[Decrease]", [&] { value--; }, false), "[Decrease]", [&] { value--; }, &button_option),
Button( Button(
"[Increase]", [&] { value++; }, false), "[Increase]", [&] { value++; }, &button_option),
}); });
// Modify the way to render them on screen: // Modify the way to render them on screen:

View File

@ -21,7 +21,9 @@ class ButtonBase : public ComponentBase {
static ButtonBase* From(Component); static ButtonBase* From(Component);
// Constructor. // Constructor.
ButtonBase(ConstStringRef label, std::function<void()> on_click, bool border); ButtonBase(ConstStringRef label,
std::function<void()> on_click,
ConstRef<ButtonOption> option);
~ButtonBase() override = default; ~ButtonBase() override = default;
// Component implementation. // Component implementation.
@ -31,8 +33,8 @@ class ButtonBase : public ComponentBase {
private: private:
ConstStringRef label_; ConstStringRef label_;
std::function<void()> on_click_; std::function<void()> on_click_;
bool border_;
Box box_; Box box_;
ConstRef<ButtonOption> option_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -26,7 +26,7 @@ std::shared_ptr<T> Make(Args&&... args) {
Component Button(ConstStringRef label, Component Button(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
bool border = true); ConstRef<ButtonOption> = {});
Component Checkbox(ConstStringRef label, bool* checked); Component Checkbox(ConstStringRef label, bool* checked);
Component Input(StringRef content, ConstStringRef placeholder); Component Input(StringRef content, ConstStringRef placeholder);
Component Menu(const std::vector<std::wstring>* entries, Component Menu(const std::vector<std::wstring>* entries,

View File

@ -14,6 +14,10 @@ struct MenuOption {
std::function<void()> on_enter = []() {}; std::function<void()> on_enter = []() {};
}; };
}; // namespace ftxui struct ButtonOption {
bool border = true;
};
}; // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */ #endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */

View File

@ -33,8 +33,8 @@ namespace ftxui {
/// ``` /// ```
Component Button(ConstStringRef label, Component Button(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
bool border) { ConstRef<ButtonOption> option) {
return Make<ButtonBase>(label, on_click, border); return Make<ButtonBase>(label, std::move(on_click), std::move(option));
} }
// static // static
@ -44,12 +44,12 @@ ButtonBase* ButtonBase::From(Component component) {
ButtonBase::ButtonBase(ConstStringRef label, ButtonBase::ButtonBase(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
bool border) ConstRef<ButtonOption> option)
: label_(label), on_click_(on_click), border_(border) {} : label_(label), on_click_(on_click), option_(std::move(option)) {}
Element ButtonBase::Render() { Element ButtonBase::Render() {
auto style = Focused() ? inverted : nothing; auto style = Focused() ? inverted : nothing;
auto my_border = border_ ? border : nothing; auto my_border = option_->border ? border : nothing;
return text(*label_) | my_border | style | reflect(box_); return text(*label_) | my_border | style | reflect(box_);
} }