mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00
Prefer std::string over std::wstring. (#179)
In the past, FTXUI switched from std::string to std::wstring to support fullwidth characters. The reasons was that fullwidth characters can be stored inside a single wchar_t. Then FTXUI added support for combining characters. A single glygh doesn't even fit a wchar_t. Instead, a glyph can be arbitrary large. The usage of wstring doesn't really fit the new model and have several drawbacks: 1. It doesn't simplify the implementation of FTXUI, because of combining characters. 2. It reduces drawing performance by 2x. 3. It increase Screen's memory allocation by 2x. This patch converts FTXUI to use std::string internally. It now exposes std::string based API. The std::wstring API remains, but is now deprecated. Tests and examples haven't been update to show the breakage is limited. They will be updated in a second set of patches. Bug: https://github.com/ArthurSonzogni/FTXUI/issues/153 Co-authored-by: Tushar Maheshwari <tushar27192@gmail.com>
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#include <algorithm> // for max, min
|
||||
#include <functional> // for function
|
||||
#include <memory> // for shared_ptr, allocator_traits<>::value_type
|
||||
#include <string> // for operator+, wstring
|
||||
#include <string> // for operator+, string
|
||||
#include <utility> // for move
|
||||
#include <vector> // for vector, __alloc_traits<>::value_type
|
||||
|
||||
@@ -23,16 +23,14 @@ namespace ftxui {
|
||||
/// @ingroup component
|
||||
class MenuBase : public ComponentBase {
|
||||
public:
|
||||
MenuBase(const std::vector<std::wstring>* entries,
|
||||
int* selected,
|
||||
Ref<MenuOption> option)
|
||||
MenuBase(ConstStringListRef entries, int* selected, Ref<MenuOption> option)
|
||||
: entries_(entries), selected_(selected), option_(option) {}
|
||||
|
||||
Element Render() {
|
||||
Elements elements;
|
||||
bool is_menu_focused = Focused();
|
||||
boxes_.resize(entries_->size());
|
||||
for (size_t i = 0; i < entries_->size(); ++i) {
|
||||
boxes_.resize(entries_.size());
|
||||
for (size_t i = 0; i < entries_.size(); ++i) {
|
||||
bool is_focused = (focused_entry() == int(i)) && is_menu_focused;
|
||||
bool is_selected = (*selected_ == int(i));
|
||||
|
||||
@@ -43,9 +41,9 @@ class MenuBase : public ComponentBase {
|
||||
auto focus_management = !is_selected ? nothing
|
||||
: is_menu_focused ? focus
|
||||
: select;
|
||||
auto icon = is_selected ? L"> " : L" ";
|
||||
elements.push_back(text(icon + entries_->at(i)) | style |
|
||||
focus_management | reflect(boxes_[i]));
|
||||
auto icon = is_selected ? "> " : " ";
|
||||
elements.push_back(text(icon + entries_[i]) | style | focus_management |
|
||||
reflect(boxes_[i]));
|
||||
}
|
||||
return vbox(std::move(elements));
|
||||
}
|
||||
@@ -64,12 +62,12 @@ class MenuBase : public ComponentBase {
|
||||
(*selected_)--;
|
||||
if (event == Event::ArrowDown || event == Event::Character('j'))
|
||||
(*selected_)++;
|
||||
if (event == Event::Tab && entries_->size())
|
||||
*selected_ = (*selected_ + 1) % entries_->size();
|
||||
if (event == Event::TabReverse && entries_->size())
|
||||
*selected_ = (*selected_ + entries_->size() - 1) % entries_->size();
|
||||
if (event == Event::Tab && entries_.size())
|
||||
*selected_ = (*selected_ + 1) % entries_.size();
|
||||
if (event == Event::TabReverse && entries_.size())
|
||||
*selected_ = (*selected_ + entries_.size() - 1) % entries_.size();
|
||||
|
||||
*selected_ = std::max(0, std::min(int(entries_->size()) - 1, *selected_));
|
||||
*selected_ = std::max(0, std::min(int(entries_.size()) - 1, *selected_));
|
||||
|
||||
if (*selected_ != old_selected) {
|
||||
focused_entry() = *selected_;
|
||||
@@ -106,12 +104,11 @@ class MenuBase : public ComponentBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Focusable() const final { return entries_->size(); }
|
||||
|
||||
bool Focusable() const final { return entries_.size(); }
|
||||
int& focused_entry() { return option_->focused_entry(); }
|
||||
|
||||
protected:
|
||||
const std::vector<std::wstring>* const entries_;
|
||||
ConstStringListRef entries_;
|
||||
int* selected_ = 0;
|
||||
Ref<MenuOption> option_;
|
||||
|
||||
@@ -129,10 +126,10 @@ class MenuBase : public ComponentBase {
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto screen = ScreenInteractive::TerminalOutput();
|
||||
/// std::vector<std::wstring> entries = {
|
||||
/// L"entry 1",
|
||||
/// L"entry 2",
|
||||
/// L"entry 3",
|
||||
/// std::vector<std::string> entries = {
|
||||
/// "entry 1",
|
||||
/// "entry 2",
|
||||
/// "entry 3",
|
||||
/// };
|
||||
/// int selected = 0;
|
||||
/// auto menu = Menu(&entries, &selected);
|
||||
@@ -146,7 +143,7 @@ class MenuBase : public ComponentBase {
|
||||
/// entry 2
|
||||
/// entry 3
|
||||
/// ```
|
||||
Component Menu(const std::vector<std::wstring>* entries,
|
||||
Component Menu(ConstStringListRef entries,
|
||||
int* selected,
|
||||
Ref<MenuOption> option) {
|
||||
return Make<MenuBase>(entries, selected, std::move(option));
|
||||
|
Reference in New Issue
Block a user