mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-15 23:48:15 +08:00
Introduce Options and use them for Menu.
Introduce Options for components. This allows me to add new features, without updating functions signatures.
This commit is contained in:

committed by
Arthur Sonzogni

parent
82adc3b410
commit
cd84b187b3
@@ -7,8 +7,9 @@
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/component_base.hpp"
|
||||
#include "ftxui/component/component_options.hpp"
|
||||
#include "ftxui/dom/elements.hpp" // for Element
|
||||
#include "ftxui/screen/string.hpp" // for ConstStringRef, StringRef
|
||||
#include "ftxui/util/ref.hpp" // for ConstStringRef, StringRef
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
@@ -28,7 +29,9 @@ Component Button(ConstStringRef label,
|
||||
bool border = true);
|
||||
Component Checkbox(ConstStringRef label, bool* checked);
|
||||
Component Input(StringRef content, ConstStringRef placeholder);
|
||||
Component Menu(const std::vector<std::wstring>* entries, int* selected_);
|
||||
Component Menu(const std::vector<std::wstring>* entries,
|
||||
int* selected_,
|
||||
ConstRef<MenuOption> = {});
|
||||
Component Radiobox(const std::vector<std::wstring>* entries, int* selected_);
|
||||
Component Toggle(const std::vector<std::wstring>* entries, int* selected);
|
||||
template <class T> // T = {int, float, long}
|
||||
|
19
include/ftxui/component/component_options.hpp
Normal file
19
include/ftxui/component/component_options.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
|
||||
#define FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
struct MenuOption {
|
||||
Decorator normal_style = nothing;
|
||||
Decorator focused_style = inverted;
|
||||
Decorator selected_style = bold;
|
||||
Decorator selected_focused_style = focused_style | selected_style;
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = []() {};
|
||||
std::function<void()> on_enter = []() {};
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_OPTIONS_HPP */
|
@@ -5,8 +5,9 @@
|
||||
#include <string> // for wstring
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Component
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/component.hpp" // for Component
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/component_options.hpp" // for Component
|
||||
#include "ftxui/dom/elements.hpp" // for Element, Decorator, operator|, bold, inverted, nothing
|
||||
#include "ftxui/screen/box.hpp" // for Box
|
||||
|
||||
@@ -21,21 +22,14 @@ class MenuBase : public ComponentBase {
|
||||
static MenuBase* From(Component component);
|
||||
|
||||
// Constructor.
|
||||
MenuBase(const std::vector<std::wstring>* entries, int* selected_);
|
||||
MenuBase(const std::vector<std::wstring>* entries,
|
||||
int* selected_,
|
||||
ConstRef<MenuOption> option = {});
|
||||
~MenuBase() override = default;
|
||||
|
||||
// State.
|
||||
int focused = 0;
|
||||
|
||||
Decorator normal_style = nothing;
|
||||
Decorator focused_style = inverted;
|
||||
Decorator selected_style = bold;
|
||||
Decorator selected_focused_style = focused_style | selected_style;
|
||||
|
||||
// State update callback.
|
||||
std::function<void()> on_change = []() {};
|
||||
std::function<void()> on_enter = []() {};
|
||||
|
||||
// Component implementation.
|
||||
Element Render() override;
|
||||
bool OnEvent(Event) override;
|
||||
@@ -43,6 +37,7 @@ class MenuBase : public ComponentBase {
|
||||
protected:
|
||||
const std::vector<std::wstring>* const entries_;
|
||||
int* selected_ = 0;
|
||||
ConstRef<MenuOption> option_;
|
||||
|
||||
bool OnMouseEvent(Event);
|
||||
|
||||
|
@@ -19,40 +19,6 @@ 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(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 */
|
||||
|
63
include/ftxui/util/ref.hpp
Normal file
63
include/ftxui/util/ref.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#ifndef FTXUI_UTIL_REF_HPP
|
||||
#define FTXUI_UTIL_REF_HPP
|
||||
|
||||
#include <ftxui/screen/string.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
// An adapter for a const object referenced or owned.
|
||||
template <typename T>
|
||||
class ConstRef {
|
||||
public:
|
||||
ConstRef() {}
|
||||
ConstRef(T t): owned_(t) {}
|
||||
ConstRef(const T* t) : address_(t) {}
|
||||
const T& operator*() { return address_ ? *address_ : owned_; }
|
||||
const T* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
private:
|
||||
T owned_;
|
||||
const T* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief For convenience, this class convert multiple mutable string
|
||||
/// references toward a shared representation.
|
||||
class StringRef {
|
||||
public:
|
||||
StringRef(std::wstring* ref) : address_(ref) {}
|
||||
StringRef(std::wstring ref) : owned_(std::move(ref)) {}
|
||||
StringRef(const wchar_t* ref) : StringRef(std::wstring(ref)) {}
|
||||
StringRef(const char* ref) : StringRef(to_wstring(std::string(ref))) {}
|
||||
std::wstring& operator*() { return address_ ? *address_ : owned_; }
|
||||
std::wstring* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
private:
|
||||
std::wstring owned_;
|
||||
std::wstring* address_ = nullptr;
|
||||
};
|
||||
|
||||
/// @brief For convenience, this class convert multiple immutable string
|
||||
/// references toward shared representation.
|
||||
class ConstStringRef {
|
||||
public:
|
||||
ConstStringRef(const std::wstring* ref) : address_(ref) {}
|
||||
ConstStringRef(std::wstring ref) : owned_(std::move(ref)) {}
|
||||
ConstStringRef(const wchar_t* ref) : ConstStringRef(std::wstring(ref)) {}
|
||||
ConstStringRef(const char* ref)
|
||||
: ConstStringRef(to_wstring(std::string(ref))) {}
|
||||
const std::wstring& operator*() { return address_ ? *address_ : owned_; }
|
||||
const std::wstring* operator->() { return address_ ? address_ : &owned_; }
|
||||
|
||||
private:
|
||||
const std::wstring owned_;
|
||||
const std::wstring* address_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_UTIL_REF_HPP */
|
||||
|
||||
// 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.
|
Reference in New Issue
Block a user