Add the Button component.

This commit is contained in:
ArthurSonzogni 2020-08-26 16:26:09 +02:00 committed by Arthur Sonzogni
parent 81d79d311d
commit 5a8ed208da
6 changed files with 148 additions and 23 deletions

View File

@ -65,6 +65,18 @@ add_library(dom
) )
add_library(component add_library(component
include/ftxui/component/button.hpp
include/ftxui/component/checkbox.hpp
include/ftxui/component/component.hpp
include/ftxui/component/container.hpp
include/ftxui/component/event.hpp
include/ftxui/component/input.hpp
include/ftxui/component/menu.hpp
include/ftxui/component/radiobox.hpp
include/ftxui/component/receiver.hpp
include/ftxui/component/screen_interactive.hpp
include/ftxui/component/toggle.hpp
src/ftxui/component/button.cpp
src/ftxui/component/checkbox.cpp src/ftxui/component/checkbox.cpp
src/ftxui/component/component.cpp src/ftxui/component/component.cpp
src/ftxui/component/container.cpp src/ftxui/component/container.cpp
@ -75,16 +87,6 @@ add_library(component
src/ftxui/component/radiobox.cpp src/ftxui/component/radiobox.cpp
src/ftxui/component/screen_interactive.cpp src/ftxui/component/screen_interactive.cpp
src/ftxui/component/toggle.cpp src/ftxui/component/toggle.cpp
include/ftxui/component/checkbox.hpp
include/ftxui/component/component.hpp
include/ftxui/component/container.hpp
include/ftxui/component/event.hpp
include/ftxui/component/input.hpp
include/ftxui/component/menu.hpp
include/ftxui/component/radiobox.hpp
include/ftxui/component/screen_interactive.hpp
include/ftxui/component/receiver.hpp
include/ftxui/component/toggle.hpp
) )
add_library(ftxui::screen ALIAS screen) add_library(ftxui::screen ALIAS screen)

View File

@ -4,9 +4,11 @@ function(example name)
set_property(TARGET ${name} PROPERTY CXX_STANDARD 17) set_property(TARGET ${name} PROPERTY CXX_STANDARD 17)
endfunction(example) endfunction(example)
example(button)
example(checkbox) example(checkbox)
example(checkbox_in_frame) example(checkbox_in_frame)
example(gallery) example(gallery)
example(homescreen)
example(input) example(input)
example(menu) example(menu)
example(menu2) example(menu2)
@ -16,4 +18,3 @@ example(radiobox_in_frame)
example(tab_horizontal) example(tab_horizontal)
example(tab_vertical) example(tab_vertical)
example(toggle) example(toggle)
example(homescreen)

View File

@ -0,0 +1,48 @@
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include "ftxui/component/button.hpp"
#include "ftxui/component/component.hpp"
#include "ftxui/component/container.hpp"
#include "ftxui/component/screen_interactive.hpp"
using namespace ftxui;
class MyComponent : public Component {
private:
std::vector<std::unique_ptr<Button>> buttons_;
Container container_ = Container::Horizontal();
public:
MyComponent() {
Add(&container_);
auto button_add = std::make_unique<Button>();
auto button_remove = std::make_unique<Button>();
container_.Add(button_add.get());
container_.Add(button_remove.get());
button_add->label = L"Add one button";
button_remove->label = L"Remove last button";
button_add->on_click = [&] {
auto extra_button = std::make_unique<Button>();
extra_button->label = L"extra button";
container_.Add(extra_button.get());
buttons_.push_back(std::move(extra_button));
};
button_remove->on_click = [&] { buttons_.resize(buttons_.size() - 1); };
buttons_.push_back(std::move(button_add));
buttons_.push_back(std::move(button_remove));
}
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
screen.Loop(&component);
return 0;
}

View File

@ -3,6 +3,7 @@
// the LICENSE file. // the LICENSE file.
#include "ftxui/component/checkbox.hpp" #include "ftxui/component/checkbox.hpp"
#include "ftxui/component/button.hpp"
#include "ftxui/component/container.hpp" #include "ftxui/component/container.hpp"
#include "ftxui/component/input.hpp" #include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp" #include "ftxui/component/menu.hpp"
@ -21,6 +22,7 @@ class MyComponent : public Component {
CheckBox checkbox2; CheckBox checkbox2;
RadioBox radiobox; RadioBox radiobox;
Input input; Input input;
Button button;
public: public:
MyComponent() { MyComponent() {
@ -55,6 +57,10 @@ class MyComponent : public Component {
input.placeholder = L"Input placeholder"; input.placeholder = L"Input placeholder";
container.Add(&input); container.Add(&input);
button.label = L"Quit";
button.on_click = [&] { on_quit(); };
container.Add(&button);
} }
Element Render(std::wstring name, Component& component) { Element Render(std::wstring name, Component& component) {
@ -66,7 +72,8 @@ class MyComponent : public Component {
} }
Element Render() override { Element Render() override {
return vbox({ return //
vbox({
Render(L"menu", menu), Render(L"menu", menu),
separator(), separator(),
Render(L"toggle", toggle), Render(L"toggle", toggle),
@ -76,14 +83,19 @@ class MyComponent : public Component {
Render(L"radiobox", radiobox), Render(L"radiobox", radiobox),
separator(), separator(),
Render(L"input", input) | size(WIDTH, LESS_THAN, 30), Render(L"input", input) | size(WIDTH, LESS_THAN, 30),
separator(),
Render(L"button", button),
}) | }) |
border; border;
} }
std::function<void()> on_quit = [] {};
}; };
int main(int argc, const char* argv[]) { int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::FitComponent(); auto screen = ScreenInteractive::FitComponent();
MyComponent component; MyComponent component;
component.on_quit = screen.ExitLoopClosure();
screen.Loop(&component); screen.Loop(&component);
return 0; return 0;

View File

@ -0,0 +1,36 @@
#ifndef FTXUI_COMPONENT_BUTTON_HPP
#define FTXUI_COMPONENT_BUTTON_HPP
#include <functional>
#include "ftxui/component/component.hpp"
namespace ftxui {
/// @brief A button. An action is associated to the click event.
/// @ingroup dom
class Button : public Component {
public:
// Constructor.
Button() = default;
Button(std::wstring label) : label(label) {}
~Button() override = default;
/// The Button label.
std::wstring label = L"button";
/// Called when the user press the "enter" button.
std::function<void()> on_click = [] {};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_BUTTON_HPP */
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

View File

@ -0,0 +1,26 @@
#include "ftxui/component/button.hpp"
#include <functional>
namespace ftxui {
Element Button::Render() {
if (Focused())
return text(label) | border |inverted;
else
return text(label) | border;
}
bool Button::OnEvent(Event event) {
if (event == Event::Return) {
on_click();
return true;
}
return false;
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.