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
11namespace ftxui {
12
13class CatchEventBase : public ComponentBase {
14 public:
15 // コンストラクタ。
16 explicit CatchEventBase(std::function<bool(Event)> on_event)
17 : on_event_(std::move(on_event)) {}
18
19 // コンポーネントの実装。
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 イベントをキャッチするために|on_event|を使用するコンポーネントを返します。この関数は、イベントが処理された場合はtrueを返し、それ以外の場合はfalseを返す必要があります。
33/// @param child ラップされたコンポーネント。
34/// @param on_event インターフェースを描画する関数。
35/// @ingroup component
36///
37/// ### 例
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 イベントをキャッチするために|on_event|を使用してコンポーネントを装飾します。この関数は、イベントが処理された場合はtrueを返し、それ以外の場合はfalseを返す必要があります。
61/// @param on_event インターフェースを描画する関数。
62/// @ingroup component
63///
64/// ### 例
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
virtual bool OnEvent(Event)
イベントに応じて呼び出されます。
イベントを表します。キープレスイベント、ターミナルのリサイズなど、さまざまなイベントがあります。
Definition event.hpp:28
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
std::shared_ptr< ComponentBase > Component
Component CatchEvent(Component child, std::function< bool(Event)>)