mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-07 01:41:12 +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>
56 lines
1.6 KiB
C++
56 lines
1.6 KiB
C++
#include <functional> // for function
|
|
#include <memory> // for __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
|
|
#include <utility> // for move
|
|
|
|
#include "ftxui/component/component.hpp" // for Component, Make, CatchEvent
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
#include "ftxui/component/event.hpp" // for Event
|
|
|
|
namespace ftxui {
|
|
|
|
class CatchEventBase : public ComponentBase {
|
|
public:
|
|
// Constructor.
|
|
CatchEventBase(std::function<bool(Event)> on_event)
|
|
: on_event_(std::move(on_event)) {}
|
|
|
|
// Component implementation.
|
|
bool OnEvent(Event event) override {
|
|
if (on_event_(event))
|
|
return true;
|
|
else
|
|
return ComponentBase::OnEvent(event);
|
|
}
|
|
|
|
protected:
|
|
std::function<bool(Event)> on_event_;
|
|
};
|
|
|
|
/// @brief Return a component, using |on_event| to catch events. This function
|
|
/// must returns true when the event has been handled, false otherwise.
|
|
/// @param child The wrapped component.
|
|
/// @param on_event The function drawing the interface.
|
|
/// @ingroup component
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// ```cpp
|
|
/// auto screen = ScreenInteractive::TerminalOutput();
|
|
/// auto renderer = Renderer([] {
|
|
/// return text("My interface");
|
|
/// });
|
|
/// screen.Loop(renderer);
|
|
/// ```
|
|
Component CatchEvent(Component child,
|
|
std::function<bool(Event event)> on_event) {
|
|
auto out = Make<CatchEventBase>(std::move(on_event));
|
|
out->Add(std::move(child));
|
|
return out;
|
|
}
|
|
|
|
} // namespace ftxui
|
|
|
|
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be found in
|
|
// the LICENSE file.
|