mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-06 17:21:13 +08:00

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>
109 lines
2.9 KiB
C++
109 lines
2.9 KiB
C++
#include <memory> // for make_shared
|
|
#include <utility> // for move
|
|
|
|
#include "ftxui/dom/elements.hpp" // for Element, Decorator, bgcolor, color
|
|
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
#include "ftxui/screen/color.hpp" // for Color
|
|
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
|
|
|
|
namespace ftxui {
|
|
|
|
class BgColor : public NodeDecorator {
|
|
public:
|
|
BgColor(Element child, Color color)
|
|
: NodeDecorator(std::move(child)), color_(color) {}
|
|
|
|
void Render(Screen& screen) override {
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
|
screen.PixelAt(x, y).background_color = color_;
|
|
}
|
|
}
|
|
NodeDecorator::Render(screen);
|
|
}
|
|
|
|
Color color_;
|
|
};
|
|
|
|
class FgColor : public NodeDecorator {
|
|
public:
|
|
FgColor(Element child, Color color)
|
|
: NodeDecorator(std::move(child)), color_(color) {}
|
|
|
|
void Render(Screen& screen) override {
|
|
for (int y = box_.y_min; y <= box_.y_max; ++y) {
|
|
for (int x = box_.x_min; x <= box_.x_max; ++x) {
|
|
screen.PixelAt(x, y).foreground_color = color_;
|
|
}
|
|
}
|
|
NodeDecorator::Render(screen);
|
|
}
|
|
|
|
Color color_;
|
|
};
|
|
|
|
/// @brief Set the foreground color of an element.
|
|
/// @param color The color of the output element.
|
|
/// @param child The input element.
|
|
/// @return The output element colored.
|
|
/// @ingroup dom
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```cpp
|
|
/// Element document = color(Color::Green, text("Success")),
|
|
/// ```
|
|
Element color(Color color, Element child) {
|
|
return std::make_shared<FgColor>(std::move(child), color);
|
|
}
|
|
|
|
/// @brief Set the background color of an element.
|
|
/// @param color The color of the output element.
|
|
/// @param child The input element.
|
|
/// @return The output element colored.
|
|
/// @ingroup dom
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```cpp
|
|
/// Element document = bgcolor(Color::Green, text("Success")),
|
|
/// ```
|
|
Element bgcolor(Color color, Element child) {
|
|
return std::make_shared<BgColor>(std::move(child), color);
|
|
}
|
|
|
|
/// @brief Decorate using a foreground color.
|
|
/// @param c The foreground color to be applied.
|
|
/// @return The Decorator applying the color.
|
|
/// @ingroup dom
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```cpp
|
|
/// Element document = text("red") | color(Color::Red);
|
|
/// ```
|
|
Decorator color(Color c) {
|
|
return [c](Element child) { return color(c, std::move(child)); };
|
|
}
|
|
|
|
/// @brief Decorate using a background color.
|
|
/// @param color The background color to be applied.
|
|
/// @return The Decorator applying the color.
|
|
/// @ingroup dom
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```cpp
|
|
/// Element document = text("red") | bgcolor(Color::Red);
|
|
/// ```
|
|
Decorator bgcolor(Color color) {
|
|
return [color](Element child) { return bgcolor(color, std::move(child)); };
|
|
}
|
|
|
|
} // namespace ftxui
|
|
|
|
// 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.
|