Add option to have button without border. (#101)

This commit is contained in:
Arthur Sonzogni 2021-05-18 17:49:53 +02:00 committed by GitHub
parent ab9d6feaa5
commit 7b88656e25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 9 deletions

View File

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

View File

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

View File

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

View File

@ -31,8 +31,10 @@ namespace ftxui {
/// │Click to quit│
/// └─────────────┘
/// ```
Component Button(ConstStringRef label, std::function<void()> on_click) {
return Make<ButtonBase>(label, on_click);
Component Button(ConstStringRef label,
std::function<void()> on_click,
bool border) {
return Make<ButtonBase>(label, on_click, border);
}
// static
@ -40,12 +42,15 @@ ButtonBase* ButtonBase::From(Component component) {
return static_cast<ButtonBase*>(component.get());
}
ButtonBase::ButtonBase(ConstStringRef label, std::function<void()> on_click)
: label_(label), on_click_(on_click) {}
ButtonBase::ButtonBase(ConstStringRef label,
std::function<void()> on_click,
bool border)
: label_(label), on_click_(on_click), border_(border) {}
Element ButtonBase::Render() {
auto style = Focused() ? inverted : nothing;
return text(*label_) | border | style | reflect(box_);
auto my_border = border_ ? border : nothing;
return text(*label_) | my_border | style | reflect(box_);
}
bool ButtonBase::OnEvent(Event event) {