mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 08:04:21 +08:00
Add {Const,}StringRef to simplify components.
This commit is contained in:
@@ -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_;
|
||||
};
|
||||
|
@@ -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_;
|
||||
|
@@ -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);
|
||||
|
@@ -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_;
|
||||
|
@@ -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 */
|
||||
|
Reference in New Issue
Block a user