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

@@ -30,6 +30,7 @@ add_library(dom
)
add_library(component
src/ftxui/component/checkbox.cpp
src/ftxui/component/component.cpp
src/ftxui/component/container.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
#define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/component/delegate.hpp"
#include "ftxui/component/event.hpp"
#include "ftxui/dom/elements.hpp"

View File

@@ -13,22 +13,31 @@ class Container : public Component {
public:
static Container Vertical();
static Container Horizontal();
static Container Tab(size_t* selector);
~Container() override = default;
// Component override.
bool OnEvent(Event event) override;
Element Render() override;
Component* ActiveChild() override;
protected:
// Handlers
using Handler = bool (Container::*)(Event);
Handler handler_;
bool Vertical(Event event);
bool Horizontal(Event event);
using EventHandler = bool (Container::*)(Event);
bool VerticalEvent(Event event);
bool HorizontalEvent(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;
Container(Handler);
size_t* selector_ = &selected_;
};
} // 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.
std::vector<std::wstring> entries = {};
int selected = 0;
size_t selected = 0;
Decorator active_style = inverted;
Decorator selected_style = bold;

View File

@@ -13,8 +13,8 @@ class Toggle : public Component {
~Toggle() override = default;
// State.
size_t activated = 0;
std::vector<std::wstring> options = {L"On", L"Off"};
size_t selected = 0;
std::vector<std::wstring> entries = {L"On", L"Off"};
// Callback.
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() {
if (children_.size() == 1)
return children_.front()->Render();
return text(L"Not implemented component");
}

View File

@@ -4,15 +4,28 @@ namespace ftxui {
// static
Container Container::Horizontal() {
return Container(&Container::Horizontal);
Container container;
container.event_handler_ = &Container::HorizontalEvent;
container.render_handler_ = &Container::HorizontalRender;
return container;
}
// static
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) {
if (!Focused())
@@ -21,14 +34,14 @@ bool Container::OnEvent(Event event) {
if (ActiveChild()->OnEvent(event))
return true;
return (this->*handler_)(event);
return (this->*event_handler_)(event);
}
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();
// Left pressed ?
if (event == Event::ArrowUp || event == Event::Character('k')) {
@@ -49,7 +62,7 @@ bool Container::Vertical(Event event) {
return false;
}
bool Container::Horizontal(Event event) {
bool Container::HorizontalEvent(Event event) {
selected_ %= children_.size();
// Left pressed ?
if (event == Event::ArrowLeft || event == Event::Character('h')) {
@@ -70,4 +83,26 @@ bool Container::Horizontal(Event event) {
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

View File

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

View File

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

View File

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

View File

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