FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
loop.hpp
Go to the documentation of this file.
1// Copyright 2022 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#ifndef FTXUI_COMPONENT_LOOP_HPP
5#define FTXUI_COMPONENT_LOOP_HPP
6
7#include <memory> // for shared_ptr
8
9#include "ftxui/component/component_base.hpp" // for ComponentBase
10#include "ftxui/util/export.hpp" // for FTXUI_EXPORT
11
12namespace ftxui {
13class ComponentBase;
14
15using Component = std::shared_ptr<ComponentBase>;
16class App;
17
18/// @brief Loop is a class that manages the event loop for a component.
19///
20/// It is responsible for running the component, handling events, and
21/// updating the screen.
22///
23/// The Loop class is designed to be used with an App object,
24/// which represents the terminal screen.
25///
26/// **Example**
27/// ```cpp
28/// #include <ftxui/component/component.hpp>
29/// #include <ftxui/component/app.hpp>
30/// #include <ftxui/component/loop.hpp>
31///
32/// int main() {
33/// auto screen = ftxui::App::TerminalOutput();
34/// auto component = ftxui::Button("Click me", [] { ... });
35///
36/// ftxui::Loop loop(screen.get(), component);
37///
38/// // Either
39/// loop.Run(); // Blocking until the component quits.
40///
41/// // Or
42/// loop.RunOnce(); // Non-blocking, returns immediately.
43///
44/// // Or
45/// loop.RunOnceBlocking(); // Blocking until handling one event.
46///
47/// // Or in a loop:
48/// while (!loop.HasQuitted()) {
49/// loop.RunOnce();
50///
51/// // Do something else like running a different library loop function.
52/// }
53/// }
54/// ```
55///
56/// @ingroup component
57class FTXUI_EXPORT(COMPONENT) Loop {
58 public:
59 Loop(App* screen, Component component);
60 ~Loop();
61
62 bool HasQuitted();
63 void RunOnce();
64 void RunOnceBlocking();
65 void Run();
66
67 // This class is non copyable/movable.
68 Loop& operator=(Loop&&) = delete;
69 Loop& operator=(const Loop&) = delete;
70 Loop(Loop&&) = delete;
71 Loop(const Loop&) = delete;
72
73 private:
74 App* screen_;
75 Component component_;
76};
77
78} // namespace ftxui
79
80#endif // FTXUI_COMPONENT_LOOP_HPP
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23