FTXUI/src/ftxui/component/catch_event.cpp

92 lines
2.8 KiB
C++
Raw Normal View History

2021-05-23 18:53:20 +08:00
#include <functional> // for function
#include <memory> // for __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
2022-12-20 01:51:25 +08:00
#include <type_traits> // for remove_reference, remove_reference<>::type
#include <utility> // for move
2021-05-23 18:53:20 +08:00
2022-12-20 01:51:25 +08:00
#include "ftxui/component/component.hpp" // for Make, CatchEvent, ComponentDecorator
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
2021-05-23 18:53:20 +08:00
#include "ftxui/component/event.hpp" // for Event
namespace ftxui {
class CatchEventBase : public ComponentBase {
public:
// Constructor.
2022-03-31 08:17:43 +08:00
explicit CatchEventBase(std::function<bool(Event)> on_event)
2021-05-23 18:53:20 +08:00
: on_event_(std::move(on_event)) {}
// Component implementation.
bool OnEvent(Event event) override {
2022-03-31 08:17:43 +08:00
if (on_event_(event)) {
2021-05-23 18:53:20 +08:00
return true;
2022-03-31 08:17:43 +08:00
} else {
2021-05-23 18:53:20 +08:00
return ComponentBase::OnEvent(event);
2022-03-31 08:17:43 +08:00
}
2021-05-23 18:53:20 +08:00
}
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.
2021-07-10 20:23:46 +08:00
/// @param child The wrapped component.
2021-05-23 18:53:20 +08:00
/// @param on_event The function drawing the interface.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer = Renderer([] {
/// return text("My interface");
2021-05-23 18:53:20 +08:00
/// });
/// auto component = CatchEvent(renderer, [&](Event event) {
/// if (event == Event::Character('q')) {
/// screen.ExitLoopClosure()();
/// return true;
/// }
/// return false;
/// });
/// screen.Loop(component);
2021-05-23 18:53:20 +08:00
/// ```
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;
}
/// @brief Decorate a component, using |on_event| to catch events. This function
/// must returns true when the event has been handled, false otherwise.
/// @param on_event The function drawing the interface.
/// @ingroup component
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::TerminalOutput();
/// auto renderer = Renderer([] { return text("Hello world"); });
/// renderer |= CatchEvent([&](Event event) {
/// if (event == Event::Character('q')) {
/// screen.ExitLoopClosure()();
/// return true;
/// }
/// return false;
/// });
/// screen.Loop(renderer);
/// ```
ComponentDecorator CatchEvent(std::function<bool(Event)> on_event) {
return [on_event = std::move(on_event)](Component child) {
2022-03-31 08:17:43 +08:00
return CatchEvent(std::move(child), [on_event = on_event](Event event) {
return on_event(std::move(event));
});
};
}
2021-05-23 18:53:20 +08:00
} // 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.