Add {Const,}StringRef to simplify components.

This commit is contained in:
ArthurSonzogni 2021-05-14 21:43:35 +02:00
parent 9fdf235836
commit 048efb6912
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
29 changed files with 201 additions and 164 deletions

View File

@ -1,20 +1,21 @@
#include <string> // for operator+, to_wstring, allocator, wstring
#include <memory> // for __shared_ptr_access, shared_ptr
#include <string> // for operator+, to_wstring
#include "ftxui/component/component.hpp" // for Button, Make
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, Element, gauge, text, operator|, vbox, border
using namespace ftxui;
int main(int argc, const char* argv[]) {
int value = 50;
std::wstring label_dec = L"decrease";
std::wstring label_inc = L"increase";
// The tree of components. This defines how to navigate using the keyboard.
auto buttons = Container::Horizontal({
Button(&label_dec, [&] { value--; }),
Button(&label_inc, [&] { value++; }),
Button("Decrease", [&] { value--; }),
Button("Increase", [&] { value++; }),
});
// Modify the way to render them on screen:

View File

@ -1,24 +1,19 @@
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Make
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Vertical
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
using namespace ftxui;
int main(int argc, const char* argv[]) {
std::wstring build_examples_label = L"Build examples";
std::wstring build_tests_label = L"Build tests";
std::wstring use_webassembly_label = L"Use WebAssembly";
bool build_examples_state = false;
bool build_tests_state = false;
bool use_webassembly_state = true;
auto component = Container::Vertical({
Checkbox(&build_examples_label, &build_examples_state),
Checkbox(&build_tests_label, &build_tests_state),
Checkbox(&use_webassembly_label, &use_webassembly_state),
Checkbox("Build examples", &build_examples_state),
Checkbox("Build tests", &build_tests_state),
Checkbox("Use WebAssembly", &use_webassembly_state),
});
auto screen = ScreenInteractive::TerminalOutput();

View File

@ -1,18 +1,17 @@
#include <memory> // for unique_ptr, make_unique, __shared_ptr_access
#include <string> // for operator+, wstring
#include <memory> // for __shared_ptr_access, allocator_traits<>::value_type, shared_ptr
#include <string> // for operator+
#include <vector> // for vector
#include "ftxui/component/component.hpp" // for Checkbox, Make
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, size, vbox, border, frame, Elements, HEIGHT, LESS_THAN
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN
#include "ftxui/screen/string.hpp" // for to_wstring
using namespace ftxui;
struct CheckboxState {
std::wstring label;
bool checked;
};
@ -20,10 +19,9 @@ int main(int argc, const char* argv[]) {
int size = 30;
std::vector<CheckboxState> states(size);
auto container = Container::Vertical({});
for(int i = 0; i<size; ++i) {
for (int i = 0; i < size; ++i) {
states[i].checked = false;
states[i].label = L"Checkbox " + to_wstring(i);
container->Add(Checkbox(&states[i].label, &states[i].checked));
container->Add(Checkbox(L"Checkbox" + to_wstring(i), &states[i].checked));
}
auto component = Renderer(container, [&] {

View File

@ -1,14 +1,13 @@
#include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Button, Input, Make, Menu, Radiobox, Toggle
#include "ftxui/component/component.hpp" // for Slider, Checkbox, Vertical, Renderer, Button, Input, Menu, Radiobox, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, xflex, text, WIDTH, hbox, vbox, EQUAL, LESS_THAN, border, GREATER_THAN
#include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, xflex, text, WIDTH, hbox, vbox, EQUAL, border, GREATER_THAN
using namespace ftxui;
@ -27,7 +26,8 @@ Component Wrap(std::wstring name, Component component) {
int main(int argc, const char* argv[]) {
auto screen = ScreenInteractive::FitComponent();
// -- Menu ----------------------------------------------------------------------
// -- Menu
// ----------------------------------------------------------------------
const std::vector<std::wstring> menu_entries = {
L"Menu 1",
L"Menu 2",
@ -48,14 +48,12 @@ int main(int argc, const char* argv[]) {
toggle = Wrap(L"Toggle", toggle);
// -- Checkbox ---------------------------------------------------------------
std::wstring checkbox_1_label = L"checkbox1";
std::wstring checkbox_2_label = L"checkbox2";
bool checkbox_1_selected = false;
bool checkbox_2_selected = false;
auto checkboxes = Container::Vertical({
Checkbox(&checkbox_1_label, &checkbox_1_selected),
Checkbox(&checkbox_2_label, &checkbox_2_selected),
Checkbox("checkbox1", &checkbox_1_selected),
Checkbox("checkbox2", &checkbox_2_selected),
});
checkboxes = Wrap(L"Checkbox", checkboxes);
@ -72,8 +70,7 @@ int main(int argc, const char* argv[]) {
// -- Input ------------------------------------------------------------------
std::wstring input_label;
std::wstring input_placeholder = L"input";
auto input = Input(&input_label, &input_placeholder);
auto input = Input(&input_label, L"placeholder");
input = Wrap(L"Input", input);
// -- Button -----------------------------------------------------------------

View File

@ -2,20 +2,19 @@
#include <chrono> // for operator""s, chrono_literals
#include <cmath> // for sin
#include <functional> // for ref, reference_wrapper, function
#include <memory> // for make_shared, __shared_ptr_access
#include <string> // for allocator, wstring, basic_string, operator+, to_wstring
#include <thread> // for sleep_for, thread
#include <utility> // for move
#include <vector> // for vector
#include <memory> // for allocator, shared_ptr, __shared_ptr_access
#include <string> // for wstring, basic_string, operator+, to_wstring
#include <thread> // for sleep_for, thread
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Checkbox, Input, Menu, Radiobox, Toggle
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Horizontal, Vertical, Input, Menu, Radiobox, Tab, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Custom
#include "ftxui/component/input.hpp" // for InputBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, operator|, color, bgcolor, Element, filler, size, vbox, flex, hbox, graph, separator, EQUAL, WIDTH, hcenter, bold, border, window, Elements, HEIGHT, hflow, flex_grow, frame, gauge, LESS_THAN, spinner, dim, GREATER_THAN
#include "ftxui/dom/elements.hpp" // for text, operator|, color, bgcolor, Element, filler, size, vbox, flex, hbox, graph, separator, EQUAL, WIDTH, hcenter, bold, border, window, HEIGHT, Elements, hflow, flex_grow, frame, gauge, LESS_THAN, spinner, dim, GREATER_THAN
#include "ftxui/screen/color.hpp" // for Color, Color::BlueLight, Color::RedLight, Color::Black, Color::Blue, Color::Cyan, Color::CyanLight, Color::GrayDark, Color::GrayLight, Color::Green, Color::GreenLight, Color::Magenta, Color::MagentaLight, Color::Red, Color::White, Color::Yellow, Color::YellowLight, Color::Default
using namespace ftxui;
@ -157,17 +156,15 @@ int main(int argc, const char* argv[]) {
false,
false,
};
std::wstring input_add_content = L"";
std::wstring input_add_placeholder = L"input_files";
Component input_add = Input(&input_add_content, &input_add_placeholder);
std::wstring input_add_content;
Component input_add = Input(&input_add_content, "input files");
std::vector<std::wstring> input_entries;
int input_selected = 0;
Component input = Menu(&input_entries, &input_selected);
std::wstring executable_content_ = L"";
std::wstring executable_placeholder_ = L"executable";
Component executable_ = Input(&executable_content_, &executable_placeholder_);
Component executable_ = Input(&executable_content_, "executable");
Component flags = Container::Vertical({
Checkbox(&options_label[0], &options_state[0]),

View File

@ -1,23 +1,20 @@
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for operator+, wstring, char_traits
#include <string> // for operator+, char_traits, wstring
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Input, Make
#include "ftxui/component/component.hpp" // for Input, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, separator, border, vbox, Element
#include "ftxui/dom/elements.hpp" // for text, hbox, Element, separator, operator|, vbox, border
int main(int argc, const char* argv[]) {
using namespace ftxui;
std::wstring first_name_;
std::wstring last_name_;
std::wstring first_name_placeholder_ = L"first_name";
std::wstring last_name_placeholder_ = L"last_name";
Component input_first_name_ = Input(&first_name_, &first_name_placeholder_);
Component input_last_name_ = Input(&last_name_, &last_name_placeholder_);
Component input_first_name_ = Input(&first_name_, "first name");
Component input_last_name_ = Input(&last_name_, "last name");
auto component = Container::Vertical({
input_first_name_,

View File

@ -1,12 +1,11 @@
#include <functional> // for function
#include <memory> // for __shared_ptr_access, shared_ptr
#include <string> // for wstring, allocator, operator+, to_string, basic_string
#include <vector> // for vector
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, operator+, to_string, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu, Make
#include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for MenuBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, separator, bold, hcenter, vbox, hbox, gauge, Element, operator|, border

View File

@ -1,15 +1,14 @@
#include <functional> // for function
#include <initializer_list> // for initializer_list
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator, basic_string
#include <memory> // for __shared_ptr_access, shared_ptr, allocator
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Menu, Make
#include "ftxui/component/component.hpp" // for Menu, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/menu.hpp" // for MenuBase
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive, Component
#include "ftxui/dom/elements.hpp" // for operator|, Element, separator, bgcolor, color, flex, Decorator, bold, hbox, border, dim
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::BlueLight, Color::Red, Color::Yellow

View File

@ -1,13 +1,11 @@
#include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access, shared_ptr, make_shared
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, operator+, basic_string, char_traits
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Make
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/component/component.hpp" // for Button, Renderer, Horizontal, Tab
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, filler, text, hbox, separator, center, vbox, bold, border, clear_under, dbox, size, GREATER_THAN, HEIGHT
int main(int argc, const char* argv[]) {
@ -21,10 +19,8 @@ int main(int argc, const char* argv[]) {
std::wstring rating = L"3/5 stars";
// At depth=0, two buttons. One for rating FTXUI and one for quitting.
std::wstring label_rate_ftxui = L"Rate FTXUI";
std::wstring label_quit = L"Quit";
auto button_rate_ftxui = Button(&label_rate_ftxui, [&] { depth = 1; });
auto button_quit = Button(&label_quit, screen.ExitLoopClosure());
auto button_rate_ftxui = Button("Rate FTXUI", [&] { depth = 1; });
auto button_quit = Button("Quit", screen.ExitLoopClosure());
auto depth_0_container = Container::Horizontal({
button_rate_ftxui,

View File

@ -1,8 +1,9 @@
#include <memory> // for __shared_ptr_access, shared_ptr
#include <string> // for wstring, operator+
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Make, Radiobox
#include "ftxui/component/component.hpp" // for Radiobox, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, size, border, frame, HEIGHT, LESS_THAN

View File

@ -1,5 +1,3 @@
#include <memory> // for allocator
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive

View File

@ -1,14 +1,11 @@
#include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for operator+, to_wstring, char_traits
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for operator+, to_wstring, char_traits
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Slider, Make
#include "ftxui/component/component.hpp" // for Slider, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Escape, Event::Return
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, operator|, Element, size, text, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, Element, operator|, size, text, vbox, xflex, bgcolor, hbox, GREATER_THAN, WIDTH, border, HEIGHT, LESS_THAN
#include "ftxui/screen/color.hpp" // for Color
using namespace ftxui;

View File

@ -1,12 +1,11 @@
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator, basic_string
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/component/component.hpp" // for Radiobox, Renderer, Tab, Toggle, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border
using namespace ftxui;

View File

@ -1,13 +1,12 @@
#include <memory> // for __shared_ptr_access
#include <string> // for wstring, allocator, basic_string
#include <memory> // for allocator, __shared_ptr_access, shared_ptr
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Radiobox, Make, Toggle
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, separator, operator|, vbox, border
#include "ftxui/component/component.hpp" // for Radiobox, Horizontal, Menu, Renderer, Tab
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, separator, hbox, operator|, border
using namespace ftxui;

View File

@ -1,13 +1,10 @@
#include <functional> // for function
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include <memory> // for allocator, __shared_ptr_access
#include <string> // for wstring, basic_string
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Toggle, Make
#include "ftxui/component/component.hpp" // for Toggle, Renderer, Vertical
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/container.hpp" // for Container
#include "ftxui/component/event.hpp" // for Event, Event::Return
#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, hbox, vbox, Element

View File

@ -8,6 +8,7 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef
namespace ftxui {
struct Event;
@ -20,7 +21,7 @@ class ButtonBase : public ComponentBase {
static ButtonBase* From(Component);
// Constructor.
ButtonBase(const std::wstring* label, std::function<void()> on_click);
ButtonBase(ConstStringRef label, std::function<void()> on_click);
~ButtonBase() override = default;
// Component implementation.
@ -28,7 +29,7 @@ class ButtonBase : public ComponentBase {
bool OnEvent(Event) override;
private:
const std::wstring* label_;
ConstStringRef label_;
std::function<void()> on_click_;
Box box_;
};

View File

@ -2,12 +2,13 @@
#define FTXUI_COMPONENT_CHECKBOX_HPP
#include <functional> // for function
#include <string> // for wstring, allocator
#include <string> // for allocator, wstring
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef
namespace ftxui {
struct Event;
@ -21,7 +22,7 @@ class CheckboxBase : public ComponentBase {
static CheckboxBase* From(Component component);
// Constructor.
CheckboxBase(const std::wstring* label, bool* state);
CheckboxBase(ConstStringRef label, bool* state);
~CheckboxBase() override = default;
#if defined(_WIN32)
@ -45,7 +46,7 @@ class CheckboxBase : public ComponentBase {
private:
bool OnMouseEvent(Event event);
const std::wstring* const label_;
ConstStringRef label_;
bool* const state_;
int cursor_position = 0;
Box box_;

View File

@ -7,6 +7,8 @@
#include <vector> // for vector
#include "ftxui/component/component_base.hpp"
#include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef
namespace ftxui {
@ -20,9 +22,9 @@ std::shared_ptr<T> Make(Args&&... args) {
return std::make_shared<T>(args...);
}
Component Button(const std::wstring* label, std::function<void()> on_click);
Component Checkbox(const std::wstring* label, bool* checked);
Component Input(std::wstring* content, const std::wstring* placeholder);
Component Button(ConstStringRef label, std::function<void()> on_click);
Component Checkbox(ConstStringRef label, bool* checked);
Component Input(StringRef content, ConstStringRef placeholder);
Component Menu(const std::vector<std::wstring>* entries, int* selected_);
Component Radiobox(const std::vector<std::wstring>* entries, int* selected_);
Component Toggle(const std::vector<std::wstring>* entries, int* selected);
@ -30,7 +32,7 @@ Component Renderer(Component child, std::function<Element()>);
Component Renderer(std::function<Element()>);
template <class T> // T = {int, float}
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);
namespace Container {
Component Vertical(Components children);

View File

@ -8,6 +8,7 @@
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/dom/elements.hpp" // for Element
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef
namespace ftxui {
struct Event;
@ -20,7 +21,7 @@ class InputBase : public ComponentBase {
static InputBase* From(Component component);
// Constructor.
InputBase(std::wstring* content, const std::wstring* placeholder);
InputBase(StringRef content, ConstStringRef placeholder);
~InputBase() override = default;
// State.
@ -35,8 +36,8 @@ class InputBase : public ComponentBase {
bool OnEvent(Event) override;
private:
std::wstring* const content_;
const std::wstring* const placeholder_;
StringRef content_;
ConstStringRef placeholder_;
bool OnMouseEvent(Event);
Box input_box_;

View File

@ -16,6 +16,41 @@ int wchar_width(wchar_t);
int wchar_width_cjk(wchar_t);
int wstring_width(const std::wstring&);
int wstring_width_cjk(const std::wstring&);
/// @brief For convenience, this class convert multiple mutable string
/// references toward a shared representation.
class StringRef {
public:
StringRef(std::wstring& ref);
StringRef(std::wstring* ref);
StringRef(const wchar_t* ref);
StringRef(const char* ref);
std::wstring& operator*();
std::wstring* operator->();
private:
std::wstring* const borrowed_ = nullptr;
std::wstring owned_;
};
/// @brief For convenience, this class convert multiple immutable string
/// references toward shared representation.
class ConstStringRef {
public:
ConstStringRef(const std::wstring& ref);
ConstStringRef(const std::wstring* ref);
ConstStringRef(const wchar_t* ref);
ConstStringRef(const char* ref);
const std::wstring& operator*();
const std::wstring* operator->();
private:
const std::wstring* const borrowed_ = nullptr;
const std::wstring owned_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_SCREEN_STRING_HPP */

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,

View File

@ -25,6 +25,30 @@ std::wstring to_wstring(const std::string& s) {
#pragma warning(pop)
#endif
StringRef::StringRef(std::wstring& ref) : borrowed_(&ref) {}
StringRef::StringRef(std::wstring* ref) : borrowed_(ref) {}
StringRef::StringRef(const wchar_t* ref) : owned_(ref) {}
StringRef::StringRef(const char* ref) : owned_(to_wstring(std::string(ref))) {}
std::wstring& StringRef::operator*() {
return borrowed_ ? *borrowed_ : owned_;
}
std::wstring* StringRef::operator->() {
return borrowed_ ? borrowed_ : &owned_;
}
ConstStringRef::ConstStringRef(const std::wstring& ref) : borrowed_(&ref) {}
ConstStringRef::ConstStringRef(const std::wstring* ref) : borrowed_(ref) {}
ConstStringRef::ConstStringRef(const wchar_t* ref) : owned_(ref) {}
ConstStringRef::ConstStringRef(const char* ref)
: owned_(to_wstring(std::string(ref))) {}
const std::wstring& ConstStringRef::operator*() {
return borrowed_ ? *borrowed_ : owned_;
}
const std::wstring* ConstStringRef::operator->() {
return borrowed_ ? borrowed_ : &owned_;
}
} // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved.