Add {Const,}StringRef to simplify components.

This commit is contained in:
ArthurSonzogni
2021-05-14 21:43:35 +02:00
parent 9fdf235836
commit 048efb6912
29 changed files with 201 additions and 164 deletions

View File

@@ -31,7 +31,7 @@ namespace ftxui {
/// │Click to quit│
/// └─────────────┘
/// ```
Component Button(const std::wstring* label, std::function<void()> on_click) {
Component Button(ConstStringRef label, std::function<void()> on_click) {
return Make<ButtonBase>(label, on_click);
}
@@ -40,8 +40,7 @@ ButtonBase* ButtonBase::From(Component component) {
return static_cast<ButtonBase*>(component.get());
}
ButtonBase::ButtonBase(const std::wstring* label,
std::function<void()> on_click)
ButtonBase::ButtonBase(ConstStringRef label, std::function<void()> on_click)
: label_(label), on_click_(on_click) {}
Element ButtonBase::Render() {

View File

@@ -30,7 +30,7 @@ namespace ftxui {
/// ```bash
/// ☐ Make a sandwitch
/// ```
Component Checkbox(const std::wstring* label, bool* checked) {
Component Checkbox(ConstStringRef label, bool* checked) {
return Make<CheckboxBase>(label, checked);
}
@@ -39,7 +39,7 @@ CheckboxBase* From(Component component) {
return static_cast<CheckboxBase*>(component.get());
}
CheckboxBase::CheckboxBase(const std::wstring* label, bool* state)
CheckboxBase::CheckboxBase(ConstStringRef label, bool* state)
: label_(label), state_(state) {}
Element CheckboxBase::Render() {

View File

@@ -15,7 +15,7 @@ namespace Container {
/// vertically using up/down arrow key or 'j'/'k' keys.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
/// @see ContainerBase
///
/// ### Example
///
@@ -35,7 +35,7 @@ Component Vertical(Components children) {
/// horizontally using left/right arrow key or 'h'/'l' keys.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
/// @see ContainerBase
///
/// ### Example
///
@@ -57,7 +57,7 @@ Component Horizontal(Components children) {
/// @param selector The index of the drawn children.
/// @param children the list of components.
/// @ingroup component
/// @see ContainerBase
/// @see ContainerBase
///
/// ### Example
///
@@ -83,7 +83,7 @@ Component ContainerBase::Vertical() {
// static
Component ContainerBase::Vertical(Components children) {
auto container = std::make_shared<Container>();
auto container = std::make_shared<ContainerBase>();
container->event_handler_ = &ContainerBase::VerticalEvent;
container->render_handler_ = &ContainerBase::VerticalRender;
for (Component& child : children)
@@ -98,7 +98,7 @@ Component ContainerBase::Horizontal() {
// static
Component ContainerBase::Horizontal(Components children) {
auto container = std::make_shared<Container>();
auto container = std::make_shared<ContainerBase>();
container->event_handler_ = &ContainerBase::HorizontalEvent;
container->render_handler_ = &ContainerBase::HorizontalRender;
for (Component& child : children)
@@ -113,7 +113,7 @@ Component ContainerBase::Tab(int* selector) {
// static
Component ContainerBase::Tab(int* selector, Components children) {
auto container = std::make_shared<Container>();
auto container = std::make_shared<ContainerBase>();
container->selector_ = selector;
container->event_handler_ = &ContainerBase::TabEvent;
container->render_handler_ = &ContainerBase::TabRender;

View File

@@ -9,7 +9,7 @@
using namespace ftxui;
TEST(ContainerTest, HorizontalEvent) {
auto container = Container::Horizontal();
auto container = ContainerBase::Horizontal();
Component c0, c1, c2;
container->Add(c0);
container->Add(c1);
@@ -80,7 +80,7 @@ TEST(ContainerTest, HorizontalEvent) {
}
TEST(ContainerTest, VerticalEvent) {
auto container = Container::Vertical();
auto container = ContainerBase::Vertical();
Component c0, c1, c2;
container->Add(c0);
container->Add(c1);
@@ -151,7 +151,7 @@ TEST(ContainerTest, VerticalEvent) {
}
TEST(ContainerTest, SetActiveChild) {
auto container = Container::Horizontal();
auto container = ContainerBase::Horizontal();
Component c0, c1, c2;
container->Add(c0);
container->Add(c1);
@@ -203,16 +203,16 @@ TEST(ContainerTest, SetActiveChild) {
}
TEST(ContainerTest, TakeFocus) {
auto c = Container::Horizontal();
auto c1 = Container::Vertical();
auto c2 = Container::Vertical();
auto c3 = Container::Vertical();
auto c11 = Container::Horizontal();
auto c12 = Container::Horizontal();
auto c13 = Container::Horizontal();
auto c21 = Container::Horizontal();
auto c22 = Container::Horizontal();
auto c23 = Container::Horizontal();
auto c = ContainerBase::Horizontal();
auto c1 = ContainerBase::Vertical();
auto c2 = ContainerBase::Vertical();
auto c3 = ContainerBase::Vertical();
auto c11 = ContainerBase::Horizontal();
auto c12 = ContainerBase::Horizontal();
auto c13 = ContainerBase::Horizontal();
auto c21 = ContainerBase::Horizontal();
auto c22 = ContainerBase::Horizontal();
auto c23 = ContainerBase::Horizontal();
c->Add(c1);
c->Add(c2);

View File

@@ -1,5 +1,6 @@
#include <algorithm> // for max, min
#include <memory> // for shared_ptr
#include <string> // for wstring, allocator, basic_string
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return
@@ -30,7 +31,7 @@ namespace ftxui {
/// ```bash
/// placeholder
/// ```
Component Input(std::wstring* content, const std::wstring* placeholder) {
Component Input(StringRef content, ConstStringRef placeholder) {
return Make<InputBase>(content, placeholder);
}
@@ -39,7 +40,7 @@ InputBase* InputBase::From(Component component) {
return static_cast<InputBase*>(component.get());
}
InputBase::InputBase(std::wstring* content, const std::wstring* placeholder)
InputBase::InputBase(StringRef content, ConstStringRef placeholder)
: content_(content), placeholder_(placeholder) {}
// Component implementation.

View File

@@ -1,11 +1,12 @@
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult
#include <gtest/gtest-message.h> // for Message
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
#include "ftxui/component/input.hpp"
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, SuiteApiResolver, TEST, TestFactoryImpl
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
using namespace ftxui;

View File

@@ -1,7 +1,8 @@
#include <functional> // for function
#include <memory> // for shared_ptr
#include <memory> // for __shared_ptr_access
#include <utility> // for move
#include "ftxui/component/component.hpp" // for Make
#include "ftxui/component/component.hpp" // for Component, Make, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element

View File

@@ -1,4 +1,4 @@
#include <string> // for allocator, wstring
#include <string> // for allocator
#include <utility> // for move
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
@@ -10,13 +10,14 @@
#include "ftxui/dom/elements.hpp" // for Element, text, color, operator|, xflex, gauge, dim, hbox, reflect, underlined, vcenter
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/color.hpp" // for Color, Color::GrayDark, Color::GrayLight
#include "ftxui/screen/string.hpp" // for StringRef
namespace ftxui {
template <class T>
class SliderBase : public ComponentBase {
public:
SliderBase(std::wstring label, T* value, T min, T max, T increment)
SliderBase(StringRef label, T* value, T min, T max, T increment)
: label_(label),
value_(value),
min_(min),
@@ -28,7 +29,7 @@ class SliderBase : public ComponentBase {
Focused() ? color(Color::GrayLight) : color(Color::GrayDark);
float percent = float(*value_ - min_) / float(max_ - min_);
return hbox({
text(label_) | dim | vcenter,
text(*label_) | dim | vcenter,
hbox({
text(L"["),
gauge(percent) | underlined | xflex | reflect(gauge_box_),
@@ -84,7 +85,7 @@ class SliderBase : public ComponentBase {
}
private:
std::wstring label_;
StringRef label_;
T* value_;
T min_;
T max_;
@@ -117,17 +118,17 @@ class SliderBase : public ComponentBase {
/// Value:[██████████████████████████ ]
/// ```
template <class T>
Component Slider(std::wstring label, T* value, T min, T max, T increment) {
Component Slider(StringRef label, T* value, T min, T max, T increment) {
return Make<SliderBase<T>>(std::move(label), value, min, max, increment);
}
template Component Slider(std::wstring label,
template Component Slider(StringRef label,
int* value,
int min,
int max,
int increment);
template Component Slider(std::wstring label,
template Component Slider(StringRef label,
float* value,
float min,
float max,