Remove input.hpp

This commit is contained in:
ArthurSonzogni 2021-07-10 12:29:39 +02:00 committed by Arthur Sonzogni
parent 7ee6edfd1f
commit 26db8228f9
16 changed files with 240 additions and 252 deletions

View File

@ -78,7 +78,6 @@ add_library(component STATIC
include/ftxui/component/component_base.hpp include/ftxui/component/component_base.hpp
include/ftxui/component/container.hpp include/ftxui/component/container.hpp
include/ftxui/component/event.hpp include/ftxui/component/event.hpp
include/ftxui/component/input.hpp
include/ftxui/component/menu.hpp include/ftxui/component/menu.hpp
include/ftxui/component/mouse.hpp include/ftxui/component/mouse.hpp
include/ftxui/component/radiobox.hpp include/ftxui/component/radiobox.hpp

View File

@ -12,7 +12,6 @@
#include "ftxui/component/component.hpp" // for Checkbox, Renderer, Horizontal, Vertical, Input, Menu, Radiobox, Tab, 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/component_base.hpp" // for ComponentBase
#include "ftxui/component/event.hpp" // for Event, Event::Custom #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/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, HEIGHT, Elements, 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 #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

View File

@ -26,22 +26,22 @@ std::shared_ptr<T> Make(Args&&... args) {
Component Button(ConstStringRef label, Component Button(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
ConstRef<ButtonOption> = {}); Ref<ButtonOption> = {});
Component Checkbox(ConstStringRef label, Component Checkbox(ConstStringRef label,
bool* checked, bool* checked,
ConstRef<CheckboxOption> option = {}); Ref<CheckboxOption> option = {});
Component Input(StringRef content, Component Input(StringRef content,
ConstStringRef placeholder, ConstStringRef placeholder,
ConstRef<InputOption> option = {}); Ref<InputOption> option = {});
Component Menu(const std::vector<std::wstring>* entries, Component Menu(const std::vector<std::wstring>* entries,
int* selected_, int* selected_,
ConstRef<MenuOption> = {}); Ref<MenuOption> = {});
Component Radiobox(const std::vector<std::wstring>* entries, Component Radiobox(const std::vector<std::wstring>* entries,
int* selected_, int* selected_,
ConstRef<RadioboxOption> option = {}); Ref<RadioboxOption> option = {});
Component Toggle(const std::vector<std::wstring>* entries, Component Toggle(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<ToggleOption> option = {}); Ref<ToggleOption> option = {});
template <class T> // T = {int, float, long} template <class T> // T = {int, float, long}
Component Slider(StringRef label, T* value, T min, T max, T increment); Component Slider(StringRef label, T* value, T min, T max, T increment);
Component Renderer(Component child, std::function<Element()>); Component Renderer(Component child, std::function<Element()>);

View File

@ -2,6 +2,7 @@
#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP #define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
#include <ftxui/dom/elements.hpp> #include <ftxui/dom/elements.hpp>
#include <ftxui/util/ref.hpp>
namespace ftxui { namespace ftxui {
@ -42,6 +43,8 @@ struct InputOption {
std::function<void()> on_change = [] {}; std::function<void()> on_change = [] {};
/// Called when the user presses enter. /// Called when the user presses enter.
std::function<void()> on_enter = [] {}; std::function<void()> on_enter = [] {};
Ref<int> cursor_position = 0;
}; };
/// @brief Option for the Radiobox component. /// @brief Option for the Radiobox component.

View File

@ -1,52 +0,0 @@
#ifndef FTXUI_COMPONENT_INPUT_H_
#define FTXUI_COMPONENT_INPUT_H_
#include <functional> // for function
#include <string> // for wstring
#include "ftxui/component/component.hpp" // for Component
#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;
/// @brief An input box. The user can type text into it.
/// @ingroup component.
class InputBase : public ComponentBase {
public:
// Access this interface from a Component
static InputBase* From(Component component);
// Constructor.
InputBase(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option = {});
~InputBase() override = default;
// State.
int cursor_position = 0;
// Component implementation.
Element Render() override;
bool OnEvent(Event) override;
private:
StringRef content_;
ConstStringRef placeholder_;
bool OnMouseEvent(Event);
Box input_box_;
Box cursor_box_;
ConstRef<InputOption> option_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_INPUT_H_ */
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.

View File

@ -24,7 +24,7 @@ class MenuBase : public ComponentBase {
// Constructor. // Constructor.
MenuBase(const std::vector<std::wstring>* entries, MenuBase(const std::vector<std::wstring>* entries,
int* selected_, int* selected_,
ConstRef<MenuOption> option = {}); Ref<MenuOption> option = {});
~MenuBase() override = default; ~MenuBase() override = default;
// State. // State.
@ -37,7 +37,7 @@ class MenuBase : public ComponentBase {
protected: protected:
const std::vector<std::wstring>* const entries_; const std::vector<std::wstring>* const entries_;
int* selected_ = 0; int* selected_ = 0;
ConstRef<MenuOption> option_; Ref<MenuOption> option_;
bool OnMouseEvent(Event); bool OnMouseEvent(Event);

View File

@ -24,7 +24,7 @@ class RadioboxBase : public ComponentBase {
// Constructor. // Constructor.
RadioboxBase(const std::vector<std::wstring>* entries, RadioboxBase(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<RadioboxOption> option = {}); Ref<RadioboxOption> option = {});
~RadioboxBase() override = default; ~RadioboxBase() override = default;
int focused = 0; int focused = 0;
@ -40,7 +40,7 @@ class RadioboxBase : public ComponentBase {
bool OnMouseEvent(Event event); bool OnMouseEvent(Event event);
int cursor_position = 0; int cursor_position = 0;
std::vector<Box> boxes_; std::vector<Box> boxes_;
ConstRef<RadioboxOption> option_; Ref<RadioboxOption> option_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -23,7 +23,7 @@ class ToggleBase : public ComponentBase {
// Constructor. // Constructor.
ToggleBase(const std::vector<std::wstring>* entries, ToggleBase(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<ToggleOption> option = {}); Ref<ToggleOption> option = {});
~ToggleBase() override = default; ~ToggleBase() override = default;
// State. // State.
@ -39,7 +39,7 @@ class ToggleBase : public ComponentBase {
bool OnMouseEvent(Event event); bool OnMouseEvent(Event event);
std::vector<Box> boxes_; std::vector<Box> boxes_;
ConstRef<ToggleOption> option_; Ref<ToggleOption> option_;
}; };
} // namespace ftxui } // namespace ftxui

View File

@ -6,7 +6,7 @@
namespace ftxui { namespace ftxui {
/// @brief An adapter. Own or reference a constant object. /// @brief An adapter. Own or reference an immutable object.
template <typename T> template <typename T>
class ConstRef { class ConstRef {
public: public:
@ -14,6 +14,7 @@ class ConstRef {
ConstRef(T t) : owned_(t) {} ConstRef(T t) : owned_(t) {}
ConstRef(const T* t) : address_(t) {} ConstRef(const T* t) : address_(t) {}
const T& operator*() { return address_ ? *address_ : owned_; } const T& operator*() { return address_ ? *address_ : owned_; }
const T& operator()() { return address_ ? *address_ : owned_; }
const T* operator->() { return address_ ? address_ : &owned_; } const T* operator->() { return address_ ? address_ : &owned_; }
private: private:
@ -21,6 +22,22 @@ class ConstRef {
const T* address_ = nullptr; const T* address_ = nullptr;
}; };
/// @brief An adapter. Own or reference an mutable object.
template <typename T>
class Ref{
public:
Ref() {}
Ref(T t) : owned_(t) {}
Ref(T* t) : address_(t) {}
T& operator*() { return address_ ? *address_ : owned_; }
T& operator()() { return address_ ? *address_ : owned_; }
T* operator->() { return address_ ? address_ : &owned_; }
private:
T owned_;
T* address_ = nullptr;
};
/// @brief An adapter. Own or reference a constant string. For convenience, this /// @brief An adapter. Own or reference a constant string. For convenience, this
/// class convert multiple mutable string toward a shared representation. /// class convert multiple mutable string toward a shared representation.
class StringRef { class StringRef {

View File

@ -16,7 +16,7 @@ class ButtonBase : public ComponentBase {
public: public:
ButtonBase(ConstStringRef label, ButtonBase(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
ConstRef<ButtonOption> option) Ref<ButtonOption> option)
: label_(label), on_click_(on_click), option_(std::move(option)) {} : label_(label), on_click_(on_click), option_(std::move(option)) {}
~ButtonBase() override = default; ~ButtonBase() override = default;
@ -55,7 +55,7 @@ class ButtonBase : public ComponentBase {
ConstStringRef label_; ConstStringRef label_;
std::function<void()> on_click_; std::function<void()> on_click_;
Box box_; Box box_;
ConstRef<ButtonOption> option_; Ref<ButtonOption> option_;
}; };
} // namespace } // namespace
@ -84,7 +84,7 @@ class ButtonBase : public ComponentBase {
/// ``` /// ```
Component Button(ConstStringRef label, Component Button(ConstStringRef label,
std::function<void()> on_click, std::function<void()> on_click,
ConstRef<ButtonOption> option) { Ref<ButtonOption> option) {
return Make<ButtonBase>(label, std::move(on_click), std::move(option)); return Make<ButtonBase>(label, std::move(on_click), std::move(option));
} }

View File

@ -25,7 +25,7 @@ class CheckboxBase : public ComponentBase {
public: public:
CheckboxBase(ConstStringRef label, CheckboxBase(ConstStringRef label,
bool* state, bool* state,
ConstRef<CheckboxOption> option) Ref<CheckboxOption> option)
: label_(label), state_(state), option_(std::move(option)) { : label_(label), state_(state), option_(std::move(option)) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default // Microsoft terminal do not use fonts able to render properly the default
@ -84,7 +84,7 @@ class CheckboxBase : public ComponentBase {
ConstStringRef label_; ConstStringRef label_;
bool* const state_; bool* const state_;
Box box_; Box box_;
ConstRef<CheckboxOption> option_; Ref<CheckboxOption> option_;
}; };
} // namespace } // namespace
@ -111,7 +111,7 @@ class CheckboxBase : public ComponentBase {
/// ``` /// ```
Component Checkbox(ConstStringRef label, Component Checkbox(ConstStringRef label,
bool* checked, bool* checked,
ConstRef<CheckboxOption> option) { Ref<CheckboxOption> option) {
return Make<CheckboxBase>(label, checked, std::move(option)); return Make<CheckboxBase>(label, checked, std::move(option));
} }

View File

@ -3,13 +3,166 @@
#include <string> // for wstring, allocator, basic_string #include <string> // for wstring, allocator, basic_string
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/component.hpp" // for Component
#include "ftxui/component/component_base.hpp" // for Component
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return #include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return
#include "ftxui/component/input.hpp"
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed #include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for Component #include "ftxui/component/screen_interactive.hpp" // for Component
#include "ftxui/util/ref.hpp" // for Component
namespace ftxui { namespace ftxui {
/// @brief An input box. The user can type text into it.
/// @ingroup component.
class InputBase : public ComponentBase {
public:
InputBase(StringRef content,
ConstStringRef placeholder,
Ref<InputOption> option)
: content_(content), placeholder_(placeholder), option_(option) {}
~InputBase() override = default;
int& cursor_position() { return *(option_->cursor_position); }
// Component implementation:
Element Render() override {
cursor_position() =
std::max(0, std::min<int>(content_->size(), cursor_position()));
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
bool is_focused = Focused();
// placeholder.
if (content_->size() == 0) {
if (is_focused)
return text(*placeholder_) | focus | dim | inverted | main_decorator |
reflect(input_box_);
else
return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
}
// Not focused.
if (!is_focused)
return text(*content_) | main_decorator | reflect(input_box_);
std::wstring part_before_cursor = content_->substr(0, cursor_position());
std::wstring part_at_cursor = cursor_position() < (int)content_->size()
? content_->substr(cursor_position(), 1)
: L" ";
std::wstring part_after_cursor =
cursor_position() < (int)content_->size() - 1
? content_->substr(cursor_position() + 1)
: L"";
auto focused = is_focused ? focus : select;
// clang-format off
return
hbox(
text(part_before_cursor),
text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
text(part_after_cursor)
) | flex | inverted | frame | main_decorator | reflect(input_box_);
// clang-format on
}
bool OnEvent(Event event) override {
cursor_position() =
std::max(0, std::min<int>(content_->size(), cursor_position()));
if (event.is_mouse())
return OnMouseEvent(event);
std::wstring c;
// Backspace.
if (event == Event::Backspace) {
if (cursor_position() == 0)
return false;
content_->erase(cursor_position() - 1, 1);
cursor_position()--;
option_->on_change();
return true;
}
// Delete
if (event == Event::Delete) {
if (cursor_position() == int(content_->size()))
return false;
content_->erase(cursor_position(), 1);
option_->on_change();
return true;
}
// Enter.
if (event == Event::Return) {
option_->on_enter();
return true;
}
if (event == Event::Custom) {
return false;
}
if (event == Event::ArrowLeft && cursor_position() > 0) {
cursor_position()--;
return true;
}
if (event == Event::ArrowRight &&
cursor_position() < (int)content_->size()) {
cursor_position()++;
return true;
}
if (event == Event::Home) {
cursor_position() = 0;
return true;
}
if (event == Event::End) {
cursor_position() = (int)content_->size();
return true;
}
// Content
if (event.is_character()) {
content_->insert(cursor_position(), 1, event.character());
cursor_position()++;
option_->on_change();
return true;
}
return false;
}
private:
bool OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (!input_box_.Contain(event.mouse().x, event.mouse().y))
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
int new_cursor_position =
cursor_position() + event.mouse().x - cursor_box_.x_min;
new_cursor_position =
std::max(0, std::min<int>(content_->size(), new_cursor_position));
if (cursor_position() != new_cursor_position) {
cursor_position() = new_cursor_position;
option_->on_change();
}
}
return true;
}
StringRef content_;
ConstStringRef placeholder_;
Box input_box_;
Box cursor_box_;
Ref<InputOption> option_;
};
/// @brief An input box for editing text. /// @brief An input box for editing text.
/// @param content The editable content. /// @param content The editable content.
/// @param placeholder The text displayed when content is still empty. /// @param placeholder The text displayed when content is still empty.
@ -33,149 +186,10 @@ namespace ftxui {
/// ``` /// ```
Component Input(StringRef content, Component Input(StringRef content,
ConstStringRef placeholder, ConstStringRef placeholder,
ConstRef<InputOption> option) { Ref<InputOption> option) {
return Make<InputBase>(content, placeholder, std::move(option)); return Make<InputBase>(content, placeholder, std::move(option));
} }
// static
InputBase* InputBase::From(Component component) {
return static_cast<InputBase*>(component.get());
}
InputBase::InputBase(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option)
: content_(content), placeholder_(placeholder), option_(option) {}
// Component implementation.
Element InputBase::Render() {
cursor_position =
std::max(0, std::min<int>(content_->size(), cursor_position));
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
bool is_focused = Focused();
// placeholder.
if (content_->size() == 0) {
if (is_focused)
return text(*placeholder_) | focus | dim | inverted | main_decorator |
reflect(input_box_);
else
return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
}
// Not focused.
if (!is_focused)
return text(*content_) | main_decorator | reflect(input_box_);
std::wstring part_before_cursor = content_->substr(0, cursor_position);
std::wstring part_at_cursor = cursor_position < (int)content_->size()
? content_->substr(cursor_position, 1)
: L" ";
std::wstring part_after_cursor = cursor_position < (int)content_->size() - 1
? content_->substr(cursor_position + 1)
: L"";
auto focused = is_focused ? focus : select;
// clang-format off
return
hbox(
text(part_before_cursor),
text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
text(part_after_cursor)
) | flex | inverted | frame | main_decorator | reflect(input_box_);
// clang-format on
}
bool InputBase::OnEvent(Event event) {
cursor_position =
std::max(0, std::min<int>(content_->size(), cursor_position));
if (event.is_mouse())
return OnMouseEvent(event);
std::wstring c;
// Backspace.
if (event == Event::Backspace) {
if (cursor_position == 0)
return false;
content_->erase(cursor_position - 1, 1);
cursor_position--;
option_->on_change();
return true;
}
// Delete
if (event == Event::Delete) {
if (cursor_position == int(content_->size()))
return false;
content_->erase(cursor_position, 1);
option_->on_change();
return true;
}
// Enter.
if (event == Event::Return) {
option_->on_enter();
return true;
}
if (event == Event::Custom) {
return false;
}
if (event == Event::ArrowLeft && cursor_position > 0) {
cursor_position--;
return true;
}
if (event == Event::ArrowRight && cursor_position < (int)content_->size()) {
cursor_position++;
return true;
}
if (event == Event::Home) {
cursor_position = 0;
return true;
}
if (event == Event::End) {
cursor_position = (int)content_->size();
return true;
}
// Content
if (event.is_character()) {
content_->insert(cursor_position, 1, event.character());
cursor_position++;
option_->on_change();
return true;
}
return false;
}
bool InputBase::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
if (!input_box_.Contain(event.mouse().x, event.mouse().y))
return false;
TakeFocus();
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
int new_cursor_position =
cursor_position + event.mouse().x - cursor_box_.x_min;
new_cursor_position =
std::max(0, std::min<int>(content_->size(), new_cursor_position));
if (cursor_position != new_cursor_position) {
cursor_position = new_cursor_position;
option_->on_change();
}
}
return true;
}
} // namespace ftxui } // namespace ftxui
// Copyright 2020 Arthur Sonzogni. All rights reserved. // Copyright 2020 Arthur Sonzogni. All rights reserved.

View File

@ -4,68 +4,72 @@
#include <string> // for wstring, allocator #include <string> // for wstring, allocator
#include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp"
#include "ftxui/component/component_options.hpp"
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home #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, TEST
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
using namespace ftxui; using namespace ftxui;
TEST(InputTest, Init) { TEST(InputTest, Init) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
Component input = Input(&content, &placeholder, &option);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0); EXPECT_EQ(option.cursor_position(), 0);
} }
TEST(InputTest, Type) { TEST(InputTest, Type) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
Component input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
EXPECT_EQ(content, L"a"); EXPECT_EQ(content, L"a");
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u); EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
EXPECT_EQ(content, L"ab"); EXPECT_EQ(content, L"ab");
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
} }
TEST(InputTest, Arrow) { TEST(InputTest, Arrow) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
auto input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c')); input->OnEvent(Event::Character('c'));
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u); EXPECT_EQ(option.cursor_position(), 3u);
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u); EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u); EXPECT_EQ(option.cursor_position(), 0u);
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u); EXPECT_EQ(option.cursor_position(), 0u);
input->OnEvent(Event::ArrowRight); input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u); EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::ArrowRight); input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
input->OnEvent(Event::ArrowRight); input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u); EXPECT_EQ(option.cursor_position(), 3u);
input->OnEvent(Event::ArrowRight); input->OnEvent(Event::ArrowRight);
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u); EXPECT_EQ(option.cursor_position(), 3u);
} }
TEST(InputTest, Insert) { TEST(InputTest, Insert) {
@ -97,16 +101,17 @@ TEST(InputTest, Insert) {
TEST(InputTest, Home) { TEST(InputTest, Home) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
auto input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
input->OnEvent(Event::Character('c')); input->OnEvent(Event::Character('c'));
EXPECT_EQ(content, L"abc"); EXPECT_EQ(content, L"abc");
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u); EXPECT_EQ(option.cursor_position(), 3u);
input->OnEvent(Event::Home); input->OnEvent(Event::Home);
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u); EXPECT_EQ(option.cursor_position(), 0u);
input->OnEvent(Event::Character('-')); input->OnEvent(Event::Character('-'));
EXPECT_EQ(content, L"-abc"); EXPECT_EQ(content, L"-abc");
@ -115,7 +120,8 @@ TEST(InputTest, Home) {
TEST(InputTest, End) { TEST(InputTest, End) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
auto input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
@ -124,15 +130,16 @@ TEST(InputTest, End) {
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u); EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::End); input->OnEvent(Event::End);
EXPECT_EQ(InputBase::From(input)->cursor_position, 3u); EXPECT_EQ(option.cursor_position(), 3u);
} }
TEST(InputTest, Delete) { TEST(InputTest, Delete) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
auto input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
@ -140,21 +147,22 @@ TEST(InputTest, Delete) {
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(content, L"abc"); EXPECT_EQ(content, L"abc");
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
input->OnEvent(Event::Delete); input->OnEvent(Event::Delete);
EXPECT_EQ(content, L"ab"); EXPECT_EQ(content, L"ab");
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
input->OnEvent(Event::Delete); input->OnEvent(Event::Delete);
EXPECT_EQ(content, L"ab"); EXPECT_EQ(content, L"ab");
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
} }
TEST(InputTest, Backspace) { TEST(InputTest, Backspace) {
std::wstring content; std::wstring content;
std::wstring placeholder; std::wstring placeholder;
Component input = Input(&content, &placeholder); auto option = InputOption();
auto input = Input(&content, &placeholder, &option);
input->OnEvent(Event::Character('a')); input->OnEvent(Event::Character('a'));
input->OnEvent(Event::Character('b')); input->OnEvent(Event::Character('b'));
@ -162,19 +170,19 @@ TEST(InputTest, Backspace) {
input->OnEvent(Event::ArrowLeft); input->OnEvent(Event::ArrowLeft);
EXPECT_EQ(content, L"abc"); EXPECT_EQ(content, L"abc");
EXPECT_EQ(InputBase::From(input)->cursor_position, 2u); EXPECT_EQ(option.cursor_position(), 2u);
input->OnEvent(Event::Backspace); input->OnEvent(Event::Backspace);
EXPECT_EQ(content, L"ac"); EXPECT_EQ(content, L"ac");
EXPECT_EQ(InputBase::From(input)->cursor_position, 1u); EXPECT_EQ(option.cursor_position(), 1u);
input->OnEvent(Event::Backspace); input->OnEvent(Event::Backspace);
EXPECT_EQ(content, L"c"); EXPECT_EQ(content, L"c");
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u); EXPECT_EQ(option.cursor_position(), 0u);
input->OnEvent(Event::Backspace); input->OnEvent(Event::Backspace);
EXPECT_EQ(content, L"c"); EXPECT_EQ(content, L"c");
EXPECT_EQ(InputBase::From(input)->cursor_position, 0u); EXPECT_EQ(option.cursor_position(), 0u);
} }
// Copyright 2021 Arthur Sonzogni. All rights reserved. // Copyright 2021 Arthur Sonzogni. All rights reserved.

View File

@ -40,7 +40,7 @@ namespace ftxui {
/// ``` /// ```
Component Menu(const std::vector<std::wstring>* entries, Component Menu(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<MenuOption> option) { Ref<MenuOption> option) {
return Make<MenuBase>(entries, selected, std::move(option)); return Make<MenuBase>(entries, selected, std::move(option));
} }
@ -51,7 +51,7 @@ MenuBase* MenuBase::From(Component component) {
MenuBase::MenuBase(const std::vector<std::wstring>* entries, MenuBase::MenuBase(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<MenuOption> option) Ref<MenuOption> option)
: entries_(entries), selected_(selected), option_(option) {} : entries_(entries), selected_(selected), option_(option) {}
Element MenuBase::Render() { Element MenuBase::Render() {

View File

@ -41,7 +41,7 @@ namespace ftxui {
/// ``` /// ```
Component Radiobox(const std::vector<std::wstring>* entries, Component Radiobox(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<RadioboxOption> option) { Ref<RadioboxOption> option) {
return Make<RadioboxBase>(entries, selected, std::move(option)); return Make<RadioboxBase>(entries, selected, std::move(option));
} }
@ -52,7 +52,7 @@ RadioboxBase* RadioboxBase::From(Component component) {
RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries, RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<RadioboxOption> option) Ref<RadioboxOption> option)
: entries_(entries), selected_(selected), option_(std::move(option)) { : entries_(entries), selected_(selected), option_(std::move(option)) {
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) #if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default // Microsoft terminal do not use fonts able to render properly the default

View File

@ -12,7 +12,7 @@ namespace ftxui {
Component Toggle(const std::vector<std::wstring>* entries, Component Toggle(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<ToggleOption> option) { Ref<ToggleOption> option) {
return Make<ToggleBase>(entries, selected, std::move(option)); return Make<ToggleBase>(entries, selected, std::move(option));
} }
@ -23,7 +23,7 @@ ToggleBase* ToggleBase::From(Component component) {
ToggleBase::ToggleBase(const std::vector<std::wstring>* entries, ToggleBase::ToggleBase(const std::vector<std::wstring>* entries,
int* selected, int* selected,
ConstRef<ToggleOption> option) Ref<ToggleOption> option)
: entries_(entries), selected_(selected), option_(std::move(option)) {} : entries_(entries), selected_(selected), option_(std::move(option)) {}
Element ToggleBase::Render() { Element ToggleBase::Render() {