FTXUI  5.0.0
C++ functional terminal UI.
catch_event.cpp
Go to the documentation of this file.
1// Copyright 2021 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <functional> // for function
5#include <memory> // for __shared_ptr_access, __shared_ptr_access<>::element_type, shared_ptr
6#include <type_traits> // for remove_reference, remove_reference<>::type
7#include <utility> // for move
8
9#include "ftxui/component/component.hpp" // for Make, CatchEvent, ComponentDecorator
10#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
11#include "ftxui/component/event.hpp" // for Event
12
13namespace ftxui {
14
15class CatchEventBase : public ComponentBase {
16 public:
17 // Constructor.
18 explicit CatchEventBase(std::function<bool(Event)> on_event)
19 : on_event_(std::move(on_event)) {}
20
21 // Component implementation.
22 bool OnEvent(Event event) override {
23 if (on_event_(event)) {
24 return true;
25 } else {
26 return ComponentBase::OnEvent(event);
27 }
28 }
29
30 protected:
31 std::function<bool(Event)> on_event_;
32};
33
34/// @brief Return a component, using |on_event| to catch events. This function
35/// must returns true when the event has been handled, false otherwise.
36/// @param child The wrapped component.
37/// @param on_event The function drawing the interface.
38/// @ingroup component
39///
40/// ### Example
41///
42/// ```cpp
43/// auto screen = ScreenInteractive::TerminalOutput();
44/// auto renderer = Renderer([] {
45/// return text("My interface");
46/// });
47/// auto component = CatchEvent(renderer, [&](Event event) {
48/// if (event == Event::Character('q')) {
49/// screen.ExitLoopClosure()();
50/// return true;
51/// }
52/// return false;
53/// });
54/// screen.Loop(component);
55/// ```
57 std::function<bool(Event event)> on_event) {
58 auto out = Make<CatchEventBase>(std::move(on_event));
59 out->Add(std::move(child));
60 return out;
61}
62
63/// @brief Decorate a component, using |on_event| to catch events. This function
64/// must returns true when the event has been handled, false otherwise.
65/// @param on_event The function drawing the interface.
66/// @ingroup component
67///
68/// ### Example
69///
70/// ```cpp
71/// auto screen = ScreenInteractive::TerminalOutput();
72/// auto renderer = Renderer([] { return text("Hello world"); });
73/// renderer |= CatchEvent([&](Event event) {
74/// if (event == Event::Character('q')) {
75/// screen.ExitLoopClosure()();
76/// return true;
77/// }
78/// return false;
79/// });
80/// screen.Loop(renderer);
81/// ```
82ComponentDecorator CatchEvent(std::function<bool(Event)> on_event) {
83 return [on_event = std::move(on_event)](Component child) {
84 return CatchEvent(std::move(child), [on_event = on_event](Event event) {
85 return on_event(std::move(event));
86 });
87 };
88}
89
90} // namespace ftxui
virtual bool OnEvent(Event)
Called in response to an event.
Definition: component.cpp:106
std::shared_ptr< ComponentBase > Component
std::function< Component(Component)> ComponentDecorator
Definition: component.hpp:33
Component CatchEvent(Component child, std::function< bool(Event)>)
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:29