FTXUI  0.11.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 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 protected:
26 std::function<bool(Event)> on_event_;
27};
28
29/// @brief Return a component, using |on_event| to catch events. This function
30/// must returns true when the event has been handled, false otherwise.
31/// @param child The wrapped component.
32/// @param on_event The function drawing the interface.
33/// @ingroup component
34///
35/// ### Example
36///
37/// ```cpp
38/// auto screen = ScreenInteractive::TerminalOutput();
39/// auto renderer = Renderer([] {
40/// return text("My interface");
41/// });
42/// screen.Loop(renderer);
43/// ```
45 std::function<bool(Event event)> on_event) {
46 auto out = Make<CatchEventBase>(std::move(on_event));
47 out->Add(std::move(child));
48 return out;
49}
50
51} // namespace ftxui
52
53// Copyright 2021 Arthur Sonzogni. All rights reserved.
54// Use of this source code is governed by the MIT license that can be found in
55// the LICENSE file.
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:95
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:25
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:25