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