Add CheckBox.

This commit is contained in:
Arthur Sonzogni 2019-01-12 22:25:49 +01:00
parent dba019139b
commit a6040bc360
21 changed files with 371 additions and 108 deletions

View File

@ -4,9 +4,12 @@ function(example name)
target_link_libraries(${name} PUBLIC component) target_link_libraries(${name} PUBLIC component)
endfunction(example) endfunction(example)
example(gallery)
example(input) example(input)
example(menu) example(menu)
example(menu2) example(menu2)
example(menu_style) example(menu_style)
example(toggle) example(toggle)
example(tab) example(tab_horizontal)
example(tab_vertical)
example(checkbox)

View File

@ -0,0 +1,31 @@
#include "ftxui/component/checkbox.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:
CheckBox box_1_;
CheckBox box_2_;
CheckBox box_3_;
Container container_ = Container::Vertical();
public:
MyComponent() {
Add(&container_);
container_.Add(&box_1_);
container_.Add(&box_2_);
container_.Add(&box_3_);
box_1_.label = L"Build examples";
box_2_.label = L"Build tests";
box_3_.label = L"Use WebAssembly";
}
};
int main(int argc, const char *argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
screen.Loop(&component);
return 0;
}

View File

@ -0,0 +1,56 @@
#include "ftxui/component/container.hpp"
#include "ftxui/component/input.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
using namespace ftxui;
class MyComponent : public Component {
Container container = Container::Vertical();
Menu menu;
Input input;
Toggle toggle;
public:
MyComponent() {
Add(&container);
menu.entries = {
L"Browse the web",
L"Meditate",
L"Sleep",
L"Eat",
};
container.Add(&menu);
toggle.entries = {
L"Browse the web",
L"Meditate",
L"Sleep",
L"Eat",
};
container.Add(&toggle);
input.placeholder = L"Input placeholder";
container.Add(&input);
}
Element Render() override {
return
vbox(
hbox(text(L"menu") | size(10,1), separator(), menu.Render()),
separator(),
hbox(text(L"toggle") | size(10,1), separator(), toggle.Render()),
separator(),
hbox(text(L"input") | size(10,1), separator(), input.Render())
) | frame;
}
};
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
screen.Loop(&component);
return 0;
}

View File

@ -1,44 +0,0 @@
#include <iostream>
#include <thread>
#include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
using namespace ftxui;
class MyComponent : public Component {
public:
MyComponent() {
Add(&container);
container.Add(&toggle);
container.Add(&menu);
toggle.options = {L" left ", L" middle ", L" end "};
menu.entries = {L" top ", L" middle ", L" bottom "};
}
std::function<void()> on_enter = [](){};
private:
Container container = Container::Vertical();
Toggle toggle;
Menu menu;
Element Render() override {
return
vbox(
hbox(frame(toggle.Render()), filler()),
frame(menu.Render()));
}
};
int main(int argc, const char *argv[])
{
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
component.on_enter = screen.ExitLoopClosure();
screen.Loop(&component);
}

View File

@ -0,0 +1,66 @@
#include <iostream>
#include <thread>
#include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
using namespace ftxui;
class MyComponent : public Component {
public:
MyComponent() {
Add(&container_);
container_.Add(&toggle_);
toggle_.entries = {
L"menu_1",
L"menu_2",
L"menu_3",
};
container_.Add(&tab_container_);
menu_1_.entries = {
L"Forest",
L"Water",
L"I don't know"
};
tab_container_.Add(&menu_1_);
menu_2_.entries = {
L"Hello",
L"Hi",
L"Hay",
};
tab_container_.Add(&menu_2_);
menu_3_.entries = {
L"Table",
L"Nothing",
L"Is",
L"Empty",
};
tab_container_.Add(&menu_3_);
}
std::function<void()> on_enter = [](){};
private:
Toggle toggle_;
Container container_ = Container::Vertical();
Container tab_container_ = Container::Tab(&(toggle_.selected));
Menu menu_1_;
Menu menu_2_;
Menu menu_3_;
};
int main(int argc, const char *argv[])
{
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
component.on_enter = screen.ExitLoopClosure();
screen.Loop(&component);
}

View File

@ -0,0 +1,66 @@
#include <iostream>
#include <thread>
#include "ftxui/component/container.hpp"
#include "ftxui/component/menu.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/component/toggle.hpp"
#include "ftxui/screen/string.hpp"
using namespace ftxui;
class MyComponent : public Component {
public:
MyComponent() {
Add(&container_);
container_.Add(&menu_);
menu_.entries = {
L"menu_1",
L"menu_2",
L"menu_3",
};
container_.Add(&tab_container_);
menu_1_.entries = {
L"Forest",
L"Water",
L"I don't know"
};
tab_container_.Add(&menu_1_);
menu_2_.entries = {
L"Hello",
L"Hi",
L"Hay",
};
tab_container_.Add(&menu_2_);
menu_3_.entries = {
L"Table",
L"Nothing",
L"Is",
L"Empty",
};
tab_container_.Add(&menu_3_);
}
std::function<void()> on_enter = [](){};
private:
Menu menu_;
Container container_ = Container::Horizontal();
Container tab_container_ = Container::Tab(&(menu_.selected));
Menu menu_1_;
Menu menu_2_;
Menu menu_3_;
};
int main(int argc, const char *argv[])
{
auto screen = ScreenInteractive::TerminalOutput();
MyComponent component;
component.on_enter = screen.ExitLoopClosure();
screen.Loop(&component);
}

View File

@ -12,36 +12,36 @@ using namespace ftxui;
class MyComponent : public Component { class MyComponent : public Component {
public: public:
MyComponent() { MyComponent() {
Add(&container); Add(&container_);
container.Add(&toggle_1); container_.Add(&toggle_1_);
container.Add(&toggle_2); container_.Add(&toggle_2_);
container.Add(&toggle_3); container_.Add(&toggle_3_);
container.Add(&toggle_4); container_.Add(&toggle_4_);
toggle_1.options = {L"On", L"Off"}; toggle_1_.entries = {L"On", L"Off"};
toggle_2.options = {L"Enabled", L"Disabled"}; toggle_2_.entries = {L"Enabled", L"Disabled"};
toggle_3.options = {L"10€", L"0€"}; toggle_3_.entries = {L"10€", L"0€"};
toggle_4.options = {L"Nothing", L"One element", L"Several elements"}; toggle_4_.entries = {L"Nothing", L"One element", L"Several elements"};
} }
std::function<void()> on_enter = []() {}; std::function<void()> on_enter = []() {};
private: private:
Container container = Container::Vertical(); Container container_ = Container::Vertical();
Toggle toggle_1; Toggle toggle_1_;
Toggle toggle_2; Toggle toggle_2_;
Toggle toggle_3; Toggle toggle_3_;
Toggle toggle_4; Toggle toggle_4_;
Element Render() override { Element Render() override {
return return
vbox( vbox(
text(L"Choose your options:"), text(L"Choose your options:"),
text(L""), text(L""),
hbox(text(L" * Poweroff on startup : "), toggle_1.Render()), hbox(text(L" * Poweroff on startup : "), toggle_1_.Render()),
hbox(text(L" * Out of process : "), toggle_2.Render()), hbox(text(L" * Out of process : "), toggle_2_.Render()),
hbox(text(L" * Price of the information : "), toggle_3.Render()), hbox(text(L" * Price of the information : "), toggle_3_.Render()),
hbox(text(L" * Number of elements : "), toggle_4.Render()) hbox(text(L" * Number of elements : "), toggle_4_.Render())
); );
} }
}; };

View File

@ -30,6 +30,7 @@ add_library(dom
) )
add_library(component add_library(component
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
src/ftxui/component/event.cpp src/ftxui/component/event.cpp

View File

@ -0,0 +1,34 @@
#ifndef FTXUI_COMPONENT_CHECKBOX_HPP
#define FTXUI_COMPONENT_CHECKBOX_HPP
#include "ftxui/component/component.hpp"
#include <functional>
namespace ftxui {
class CheckBox : public Component {
public:
// Constructor.
CheckBox() = default;
~CheckBox() override = default;
bool state = false;
std::wstring label = L"label";
std::wstring checked = L"[X] ";
std::wstring unchecked = L"[ ] ";
// State update callback.
std::function<void()> on_change = [](){};
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
int cursor_position = 0;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_CHECKBOX_HPP */

View File

@ -1,7 +1,6 @@
#ifndef FTXUI_COMPONENT_COMPONENT_HPP #ifndef FTXUI_COMPONENT_COMPONENT_HPP
#define FTXUI_COMPONENT_COMPONENT_HPP #define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/component/delegate.hpp"
#include "ftxui/component/event.hpp" #include "ftxui/component/event.hpp"
#include "ftxui/dom/elements.hpp" #include "ftxui/dom/elements.hpp"

View File

@ -13,22 +13,31 @@ class Container : public Component {
public: public:
static Container Vertical(); static Container Vertical();
static Container Horizontal(); static Container Horizontal();
static Container Tab(size_t* selector);
~Container() override = default; ~Container() override = default;
// Component override. // Component override.
bool OnEvent(Event event) override; bool OnEvent(Event event) override;
Element Render() override;
Component* ActiveChild() override; Component* ActiveChild() override;
protected: protected:
// Handlers // Handlers
using Handler = bool (Container::*)(Event); using EventHandler = bool (Container::*)(Event);
Handler handler_; bool VerticalEvent(Event event);
bool Vertical(Event event); bool HorizontalEvent(Event event);
bool Horizontal(Event event); bool TabEvent(Event event) { return false; }
EventHandler event_handler_;
using RenderHandler = Element (Container::*)();
Element VerticalRender();
Element HorizontalRender();
Element TabRender();
RenderHandler render_handler_;
size_t selected_ = 0; size_t selected_ = 0;
size_t* selector_ = &selected_;
Container(Handler);
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -1,13 +0,0 @@
#ifndef FTXUI_COMPONENT_DELEGATE_HPP
#define FTXUI_COMPONENT_DELEGATE_HPP
#include "ftxui/dom/elements.hpp"
namespace ftxui {
class Component;
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_DELEGATE_HPP */

View File

@ -15,7 +15,7 @@ class Menu : public Component {
// State. // State.
std::vector<std::wstring> entries = {}; std::vector<std::wstring> entries = {};
int selected = 0; size_t selected = 0;
Decorator active_style = inverted; Decorator active_style = inverted;
Decorator selected_style = bold; Decorator selected_style = bold;

View File

@ -13,8 +13,8 @@ class Toggle : public Component {
~Toggle() override = default; ~Toggle() override = default;
// State. // State.
size_t activated = 0; size_t selected = 0;
std::vector<std::wstring> options = {L"On", L"Off"}; std::vector<std::wstring> entries = {L"On", L"Off"};
// Callback. // Callback.
std::function<void()> on_change = [](){}; std::function<void()> on_change = [](){};

View File

@ -0,0 +1,20 @@
#include "ftxui/component/checkbox.hpp"
#include <functional>
namespace ftxui {
Element CheckBox::Render() {
auto style = Focused() ? bold : nothing;
return text((state ? checked : unchecked) + label) | style;
}
bool CheckBox::OnEvent(Event event) {
if (event == Event::Character(' ') || event == Event::Return) {
state = !state;
on_change();
return true;
}
return false;
}
} // namespace ftxui

View File

@ -35,6 +35,9 @@ Component* Component::ActiveChild() {
} }
Element Component::Render() { Element Component::Render() {
if (children_.size() == 1)
return children_.front()->Render();
return text(L"Not implemented component"); return text(L"Not implemented component");
} }

View File

@ -4,15 +4,28 @@ namespace ftxui {
// static // static
Container Container::Horizontal() { Container Container::Horizontal() {
return Container(&Container::Horizontal); Container container;
container.event_handler_ = &Container::HorizontalEvent;
container.render_handler_ = &Container::HorizontalRender;
return container;
} }
// static // static
Container Container::Vertical() { Container Container::Vertical() {
return Container(&Container::Vertical); Container container;
container.event_handler_ = &Container::VerticalEvent;
container.render_handler_ = &Container::VerticalRender;
return container;
} }
Container::Container(Container::Handler handler) : handler_(handler) {} // static
Container Container::Tab(size_t* selector) {
Container container;
container.event_handler_ = &Container::TabEvent;
container.render_handler_ = &Container::TabRender;
container.selector_ = selector;
return container;
}
bool Container::OnEvent(Event event) { bool Container::OnEvent(Event event) {
if (!Focused()) if (!Focused())
@ -21,14 +34,14 @@ bool Container::OnEvent(Event event) {
if (ActiveChild()->OnEvent(event)) if (ActiveChild()->OnEvent(event))
return true; return true;
return (this->*handler_)(event); return (this->*event_handler_)(event);
} }
Component* Container::ActiveChild() { Component* Container::ActiveChild() {
return children_[selected_ % children_.size()]; return children_[*selector_ % children_.size()];
} }
bool Container::Vertical(Event event) { bool Container::VerticalEvent(Event event) {
selected_ %= children_.size(); selected_ %= children_.size();
// Left pressed ? // Left pressed ?
if (event == Event::ArrowUp || event == Event::Character('k')) { if (event == Event::ArrowUp || event == Event::Character('k')) {
@ -49,7 +62,7 @@ bool Container::Vertical(Event event) {
return false; return false;
} }
bool Container::Horizontal(Event event) { bool Container::HorizontalEvent(Event event) {
selected_ %= children_.size(); selected_ %= children_.size();
// Left pressed ? // Left pressed ?
if (event == Event::ArrowLeft || event == Event::Character('h')) { if (event == Event::ArrowLeft || event == Event::Character('h')) {
@ -70,4 +83,26 @@ bool Container::Horizontal(Event event) {
return false; return false;
} }
Element Container::Render() {
return (this->*render_handler_)();
}
Element Container::VerticalRender() {
Elements elements;
for(auto& it : children_)
elements.push_back(it->Render());
return vbox(std::move(elements));
}
Element Container::HorizontalRender() {
Elements elements;
for(auto& it : children_)
elements.push_back(it->Render());
return hbox(std::move(elements));
}
Element Container::TabRender() {
return ActiveChild()->Render();
}
} // namespace ftxui } // namespace ftxui

View File

@ -8,7 +8,7 @@ Element Menu::Render() {
std::vector<Element> elements; std::vector<Element> elements;
bool focused = Focused(); bool focused = Focused();
for (size_t i = 0; i < entries.size(); ++i) { for (size_t i = 0; i < entries.size(); ++i) {
if (size_t(selected) == i) { if (selected == i) {
if (focused) if (focused)
elements.push_back(active_style(text(L"> " + entries[i]))); elements.push_back(active_style(text(L"> " + entries[i])));
else else
@ -25,12 +25,12 @@ bool Menu::OnEvent(Event event) {
if (!Focused()) if (!Focused())
return false; return false;
int new_selected = selected; size_t new_selected = selected;
if (event == Event::ArrowUp || event == Event::Character('k')) if (event == Event::ArrowUp || event == Event::Character('k'))
new_selected--; new_selected--;
if (event == Event::ArrowDown || event == Event::Character('j')) if (event == Event::ArrowDown || event == Event::Character('j'))
new_selected++; new_selected++;
new_selected = std::max(0, std::min(int(entries.size())-1, new_selected)); new_selected = std::max(size_t(0), std::min(entries.size()-size_t(1), new_selected));
if (selected != new_selected) { if (selected != new_selected) {
selected = new_selected; selected = new_selected;

View File

@ -5,7 +5,6 @@
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include "ftxui/component/component.hpp" #include "ftxui/component/component.hpp"
#include "ftxui/component/delegate.hpp"
#include "ftxui/screen/terminal.hpp" #include "ftxui/screen/terminal.hpp"
namespace ftxui { namespace ftxui {
@ -87,9 +86,9 @@ void ScreenInteractive::Loop(Component* component) {
std::string reset_position; std::string reset_position;
while (!quit_) { while (!quit_) {
reset_position = ResetPosition();
Draw(component); Draw(component);
std::cout << reset_position << ToString() << std::flush; std::cout << reset_position << ToString() << std::flush;
reset_position = ResetPosition();
Clear(); Clear();
component->OnEvent(GetEvent()); component->OnEvent(GetEvent());
} }
@ -122,10 +121,8 @@ void ScreenInteractive::Draw(Component* component) {
} }
if (dimx != dimx_ || dimy != dimy_) { if (dimx != dimx_ || dimy != dimy_) {
std::cerr << dimx_ << " " << dimy_ << std::endl;
dimx_ = dimx; dimx_ = dimx;
dimy_ = dimy; dimy_ = dimy;
std::cerr << dimx_ << " " << dimy_ << std::endl;
pixels_ = std::vector<std::vector<Pixel>>( pixels_ = std::vector<std::vector<Pixel>>(
dimy, std::vector<Pixel>(dimx)); dimy, std::vector<Pixel>(dimx));
} }

View File

@ -6,29 +6,29 @@ Element Toggle::Render() {
auto highlight = Focused() ? inverted : bold; auto highlight = Focused() ? inverted : bold;
Elements children; Elements children;
for(size_t i = 0; i<options.size(); ++i) { for(size_t i = 0; i<entries.size(); ++i) {
// Separator. // Separator.
if (i != 0) if (i != 0)
children.push_back(separator()); children.push_back(separator());
// Entry. // Entry.
auto style = i == activated ? highlight : dim; auto style = i == selected ? highlight : dim;
children.push_back(style(text(options[i]))); children.push_back(style(text(entries[i])));
} }
return hbox(std::move(children)); return hbox(std::move(children));
} }
bool Toggle::OnEvent(Event event) { bool Toggle::OnEvent(Event event) {
if (activated > 0 && if (selected > 0 &&
(event == Event::ArrowLeft || event == Event::Character('h'))) { (event == Event::ArrowLeft || event == Event::Character('h'))) {
activated--; selected--;
on_change(); on_change();
return true; return true;
} }
if (activated < options.size() - 1 && if (selected < entries.size() - 1 &&
(event == Event::ArrowRight || event == Event::Character('l'))) { (event == Event::ArrowRight || event == Event::Character('l'))) {
activated++; selected++;
on_change(); on_change();
return true; return true;
} }

View File

@ -94,7 +94,7 @@ Screen Screen::TerminalOutput(std::unique_ptr<Node>& element) {
std::string Screen::ResetPosition() { std::string Screen::ResetPosition() {
std::stringstream ss; std::stringstream ss;
//ss << '\r'; ss << '\r';
for (size_t y = 1; y < dimy_; ++y) { for (size_t y = 1; y < dimy_; ++y) {
ss << "\e[2K\r\e[1A"; ss << "\e[2K\r\e[1A";
} }