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