feat: Dropdown select menu. (#214)

Dom
 - `vscroll_indicator`. Show a scrollback indicator on the right.

Component
 - `Maybe`: Display an component conditionnally based on a boolean.
 - `Dropdown`: A dropdown select list.

This address:
https://github.com/ArthurSonzogni/FTXUI/issues/204
This commit is contained in:
Arthur Sonzogni
2021-09-26 15:19:17 +02:00
parent 0d1a7ffe6d
commit c5ef0c7fb5
12 changed files with 282 additions and 6 deletions

View File

@@ -0,0 +1,54 @@
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/event.hpp"
namespace ftxui {
Component Dropdown(ConstStringListRef entries, int* selected) {
class Impl : public ComponentBase {
public:
Impl(ConstStringListRef entries, int* selected)
: entries_(std::move(entries)), selected_(selected) {
CheckboxOption option;
option.style_checked = "↓│";
option.style_unchecked = "→│";
checkbox_ = Checkbox(&title_, &show_, option),
radiobox_ = Radiobox(entries_, selected_);
Add(Container::Vertical({
checkbox_,
Maybe(radiobox_, &show_),
}));
}
Element Render() override {
title_ = entries_[*selected_];
if (show_) {
return vbox({
checkbox_->Render(),
separator(),
radiobox_->Render() | vscroll_indicator | frame |
size(HEIGHT, LESS_THAN, 12),
}) |
border;
}
return vbox({
checkbox_->Render() | border,
filler(),
});
}
private:
ConstStringListRef entries_;
bool show_ = false;
int* selected_;
std::string title_;
Component checkbox_;
Component radiobox_;
};
return Make<Impl>(std::move(entries), selected);
}
} // namespace ftxui

View File

@@ -0,0 +1,31 @@
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_base.hpp"
#include "ftxui/component/event.hpp"
namespace ftxui {
Component Maybe(Component child, bool* show) {
class Impl : public ComponentBase {
public:
Impl(bool* show): show_(show) {}
private:
Element Render() override {
return *show_ ? ComponentBase::Render() : std::make_unique<Node>();
}
bool Focusable() const override {
return *show_ && ComponentBase::Focusable();
}
bool OnEvent(Event event) override {
return *show_ && ComponentBase::OnEvent(event);
}
bool* show_;
};
auto maybe = Make<Impl>(show);
maybe->Add(std::move(child));
return maybe;
}
} // namespace ftxui

View File

@@ -0,0 +1,26 @@
#include "ftxui/component/component_base.hpp"
Component Maybe(Component child, bool* show) {
class Impl : public ComponentBase {
public:
Impl(Component child, bool* show) : ComponentBase(child), show_(show) {}
private:
Element Render() override {
if (*show_)
return ComponentBase::Render();
else
return text("");
}
bool Focusable() const override {
return *show_ && ComponentBase::Focusable();
}
bool OnEvent(Event event) override {
if (*show_)
return false return ComponentBase::OnEvent(event);
}
bool* show_;
};
return Make<Impl>(std::move(child), show);
}

View File

@@ -0,0 +1,53 @@
#include "ftxui/dom/elements.hpp"
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/box.hpp"
#include "ftxui/screen/screen.hpp"
namespace ftxui {
/// @brief Add a filter that will invert the foreground and the background
/// colors.
/// @ingroup dom
Element vscroll_indicator(Element child) {
class Impl : public NodeDecorator {
using NodeDecorator::NodeDecorator;
void Render(Screen& screen) final {
Node::Render(screen);
const Box& stencil = screen.stencil;
float size_inner = box_.y_max - box_.y_min;
float size_outter = stencil.y_max - stencil.y_min;
float start_y = stencil.y_min +
(stencil.y_min - box_.y_min) * size_outter / size_inner;
float end_y = stencil.y_min +
(stencil.y_max - box_.y_min) * size_outter / size_inner;
const int x = stencil.x_max;
for (int y = stencil.y_min; y <= stencil.y_max; ++y) {
bool up = (2 * y + -1 >= 2 * start_y) && (2 * y + -1 <= 2 * end_y);
bool down = (2 * y + 0 >= 2 * start_y) && (2 * y + 0 <= 2 * end_y);
if (up) {
if (down) {
screen.PixelAt(x, y).character = "";
} else {
screen.PixelAt(x, y).character = "";
}
} else {
if (down) {
screen.PixelAt(x, y).character = "";
} else {
screen.PixelAt(x, y).character = " ";
}
}
screen.PixelAt(x,y).inverted = true;
}
};
};
return std::make_shared<Impl>(std::move(child));
}
} // namespace ftxui