FTXUI 7.0.3
C++ functional terminal UI.
Loading...
Searching...
No Matches
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 <utility> // for move
6
7#include "ftxui/component/component.hpp" // for Make, CatchEvent, ComponentDecorator
8#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
9#include "ftxui/component/event.hpp" // for Event
10
11namespace ftxui {
12
13class CatchEventBase : public ComponentBase {
14 public:
15 // Constructor.
16 explicit CatchEventBase(std::function<bool(Event)> on_event)
17 : on_event_(std::move(on_event)) {}
18
19 // Component implementation.
20 Component ActiveChild() override {
21 return Active() ? ComponentBase::ActiveChild() : nullptr;
22 }
23
24 bool OnEvent(Event event) override {
25 if (on_event_(event)) {
26 return true;
27 } else {
28 return ComponentBase::OnEvent(event);
29 }
30 }
31
32 protected:
33 std::function<bool(Event)> on_event_;
34};
35
36/// @brief Return a component, using |on_event| to catch events. This function
37/// must returns true when the event has been handled, false otherwise.
38/// @param child The wrapped component.
39/// @param on_event The function drawing the interface.
40/// @ingroup component
41///
42/// ### Example
43///
44/// ```cpp
45/// auto screen = App::TerminalOutput();
46/// auto renderer = Renderer([] {
47/// return text("My interface");
48/// });
49/// auto component = CatchEvent(renderer, [&](Event event) {
50/// if (event == Event::Character('q')) {
51/// screen.ExitLoopClosure()();
52/// return true;
53/// }
54/// return false;
55/// });
56/// screen.Loop(component);
57/// ```
59 std::function<bool(Event event)> on_event) {
60 auto out = Make<CatchEventBase>(std::move(on_event));
61 out->Add(std::move(child));
62 return out;
63}
64
65/// @brief Decorate a component, using |on_event| to catch events. This function
66/// must returns true when the event has been handled, false otherwise.
67/// @param on_event The function drawing the interface.
68/// @ingroup component
69///
70/// ### Example
71///
72/// ```cpp
73/// auto screen = App::TerminalOutput();
74/// auto renderer = Renderer([] { return text("Hello world"); });
75/// renderer |= CatchEvent([&](Event event) {
76/// if (event == Event::Character('q')) {
77/// screen.ExitLoopClosure()();
78/// return true;
79/// }
80/// return false;
81/// });
82/// screen.Loop(renderer);
83/// ```
84ComponentDecorator CatchEvent(std::function<bool(Event)> on_event) {
85 return [on_event = std::move(on_event)](Component child) {
86 return CatchEvent(std::move(child), [on_event = on_event](Event event) {
87 return on_event(std::move(event));
88 });
89 };
90}
91
92} // namespace ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:28
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:33
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23
Component CatchEvent(Component child, std::function< bool(Event)>)