FTXUI 6.1.9 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
11 namespace ftxui {
12
13 class 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 bool OnEvent(Event event) override {
21 if (on_event_(event)) {
22 return true;
23 } else {
24 return ComponentBase::OnEvent(event);
25 }
26 }
27
28 protected:
29 std::function<bool(Event)> on_event_;
30};
31
32/// @brief Return a component, using |on_event| to catch events. This function
33/// must returns true when the event has been handled, false otherwise.
34/// @param child The wrapped component.
35/// @param on_event The function drawing the interface.
36/// @ingroup component
37///
38/// ### Example
39///
40/// ```cpp
41/// auto screen = ScreenInteractive::TerminalOutput();
42/// auto renderer = Renderer([] {
43/// return text("My interface");
44/// });
45/// auto component = CatchEvent(renderer, [&](Event event) {
46/// if (event == Event::Character('q')) {
47/// screen.ExitLoopClosure()();
48/// return true;
49/// }
50/// return false;
51/// });
52/// screen.Loop(component);
53/// ```
54 Component CatchEvent(Component child,
55 std::function<bool(Event event)> on_event) {
56 auto out = Make<CatchEventBase>(std::move(on_event));
57 out->Add(std::move(child));
58 return out;
59}
60
61/// @brief Decorate a component, using |on_event| to catch events. This function
62/// must returns true when the event has been handled, false otherwise.
63/// @param on_event The function drawing the interface.
64/// @ingroup component
65///
66/// ### Example
67///
68/// ```cpp
69/// auto screen = ScreenInteractive::TerminalOutput();
70/// auto renderer = Renderer([] { return text("Hello world"); });
71/// renderer |= CatchEvent([&](Event event) {
72/// if (event == Event::Character('q')) {
73/// screen.ExitLoopClosure()();
74/// return true;
75/// }
76/// return false;
77/// });
78/// screen.Loop(renderer);
79/// ```
80 ComponentDecorator CatchEvent(std::function<bool(Event)> on_event) {
81 return [on_event = std::move(on_event)](Component child) {
82 return CatchEvent(std::move(child), [on_event = on_event](Event event) {
83 return on_event(std::move(event));
84 });
85 };
86}
87
88} // namespace ftxui
component.hpp
component_base.hpp
event.hpp
ftxui::ComponentBase::OnEvent
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:151
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::ComponentDecorator
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::CatchEvent
Component CatchEvent(Component child, std::function< bool(Event)>)