Feature: Add multi-line input. (#630)

This commit is contained in:
Arthur Sonzogni
2023-05-02 13:32:37 +02:00
committed by GitHub
parent 6215240b37
commit 7de4f8683d
33 changed files with 1471 additions and 690 deletions

View File

@@ -51,8 +51,9 @@ Component Checkbox(ConstStringRef label,
bool* checked,
Ref<CheckboxOption> option = CheckboxOption::Simple());
Component Input(StringRef content, Ref<InputOption> option = {});
Component Input(StringRef content,
ConstStringRef placeholder,
StringRef placeholder,
Ref<InputOption> option = {});
Component Menu(ConstStringListRef entries,

View File

@@ -5,7 +5,7 @@
#include <ftxui/component/animation.hpp> // for Duration, QuadraticInOut, Function
#include <ftxui/dom/direction.hpp> // for Direction, Direction::Left, Direction::Right, Direction::Down
#include <ftxui/dom/elements.hpp> // for Element, separator
#include <ftxui/util/ref.hpp> // for Ref, ConstRef
#include <ftxui/util/ref.hpp> // for Ref, ConstRef, StringRef
#include <functional> // for function
#include <optional> // for optional
#include <string> // for string
@@ -134,20 +134,42 @@ struct CheckboxOption {
std::function<void()> on_change = [] {};
};
/// @brief Used to define style for the Input component.
struct InputState {
Element element;
bool hovered; /// < Whether the input is hovered by the mouse.
bool focused; /// < Whether the input is focused by the user.
bool is_placeholder; /// < Whether the input is empty and displaying the
/// < placeholder.
};
/// @brief Option for the Input component.
/// @ingroup component
struct InputOption {
// A set of predefined styles:
/// @brief Create the default input style:
static InputOption Default();
/// @brief A white on black style with high margins:
static InputOption Spacious();
/// @brief A style with a border:
static InputOption Arthur();
/// The content of the input when it's empty.
StringRef placeholder = "";
// Style:
std::function<Element(InputState)> transform;
Ref<bool> password = false; /// < Obscure the input content using '*'.
Ref<bool> multiline = true; /// < Whether the input can be multiline.
/// Called when the content changes.
std::function<void()> on_change = [] {};
/// Called when the user presses enter.
std::function<void()> on_enter = [] {};
/// Obscure the input content using '*'.
Ref<bool> password = false;
/// When set different from -1, this attributes is used to store the cursor
/// position.
Ref<int> cursor_position = -1;
// The char position of the cursor:
Ref<int> cursor_position = 0;
};
/// @brief Option for the Radiobox component.

View File

@@ -1,7 +1,7 @@
#ifndef FTXUI_COMPONENT_RECEIVER_HPP_
#define FTXUI_COMPONENT_RECEIVER_HPP_
#include <algorithm> // for copy
#include <algorithm> // for copy, max
#include <atomic> // for atomic, __atomic_base
#include <condition_variable> // for condition_variable
#include <functional>

View File

@@ -15,44 +15,10 @@ std::wstring to_wstring(T s) {
}
int string_width(const std::string&);
// Split the string into a its glyphs. An empty one is inserted ater fullwidth
// ones.
std::vector<std::string> Utf8ToGlyphs(const std::string& input);
// If |input| was an array of glyphs, this returns the number of char to eat
// before reaching the glyph at index |glyph_index|.
int GlyphPosition(const std::string& input,
size_t glyph_index,
size_t start = 0);
// Returns the number of glyphs in |input|.
int GlyphCount(const std::string& input);
// Properties from:
// https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/WordBreakProperty.txt
enum class WordBreakProperty {
ALetter,
CR,
Double_Quote,
Extend,
ExtendNumLet,
Format,
Hebrew_Letter,
Katakana,
LF,
MidLetter,
MidNum,
MidNumLet,
Newline,
Numeric,
Regional_Indicator,
Single_Quote,
WSegSpace,
ZWJ,
};
WordBreakProperty CodepointToWordBreakProperty(uint32_t codepoint);
std::vector<WordBreakProperty> Utf8ToWordBreakProperty(
const std::string& input);
bool IsWordBreakingCharacter(const std::string& input, size_t glyph_index);
// Map every cells drawn by |input| to their corresponding Glyphs. Half-size
// Glyphs takes one cell, full-size Glyphs take two cells.

View File

@@ -48,6 +48,7 @@ class StringRef {
StringRef(const wchar_t* ref) : StringRef(to_string(std::wstring(ref))) {}
StringRef(const char* ref) : StringRef(std::string(ref)) {}
std::string& operator*() { return address_ ? *address_ : owned_; }
std::string& operator()() { return address_ ? *address_ : owned_; }
std::string* operator->() { return address_ ? address_ : &owned_; }
private: