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
11namespace ftxui {
12class ComponentBase;
13
14using Component = std::shared_ptr<ComponentBase>;
15class ScreenInteractive;
16
17/// @brief Loop is a class that manages the event loop for a component.
18///
19/// It is responsible for running the component, handling events, and
20/// updating the screen.
21///
22/// The Loop class is designed to be used with a ScreenInteractive object,
23/// which represents the terminal screen.
24///
25/// **Example**
26/// ```cpp
27/// #include <ftxui/component/component.hpp>
28/// #include <ftxui/component/screen_interactive.hpp>
29/// #include <ftxui/component/loop.hpp>
30///
31/// int main() {
32/// auto screen = ftxui::ScreenInteractive::TerminalOutput();
33/// auto component = ftxui::Button("Click me", [] { ... });
34///
35/// ftxui::Loop loop(screen.get(), component);
36///
37/// // Either
38/// loop.Run(); // Blocking until the component quits.
39///
40/// // Or
41/// loop.RunOnce(); // Non-blocking, returns immediately.
42///
43/// // Or
44/// loop.RunOnceBlocking(); // Blocking until handling one event.
45///
46/// // Or in a loop:
47/// while (!loop.HasQuitted()) {
48/// loop.RunOnce();
49///
50/// // Do something else like running a different library loop function.
51/// }
52/// }
53/// ```
54///
55/// @ingroup component
56class Loop {
57 public:
58 Loop(ScreenInteractive* screen, Component component);
59 ~Loop();
60
61 bool HasQuitted();
62 void RunOnce();
63 void RunOnceBlocking();
64 void Run();
65
66 // This class is non copyable/movable.
67 Loop(const Loop&) = default;
68 Loop(Loop&&) = delete;
69 Loop& operator=(Loop&&) = delete;
70 Loop(const ScreenInteractive&) = delete;
71 Loop& operator=(const Loop&) = delete;
72
73 private:
74 ScreenInteractive* screen_;
75 Component component_;
76};
77
78} // namespace ftxui
79
80#endif // FTXUI_COMPONENT_LOOP_HPP
bool HasQuitted()
Whether the loop has quitted.
Definition loop.cpp:30
Loop(const ScreenInteractive &)=delete
void Run()
Definition loop.cpp:49
Loop(ScreenInteractive *screen, Component component)
A Loop is a wrapper around a Component and a ScreenInteractive. It is used to run a Component in a te...
Definition loop.cpp:20
Loop & operator=(const Loop &)=delete
void RunOnce()
Execute the loop. Make the component to process every pending tasks/events. A new frame might be draw...
Definition loop.cpp:37
Loop(const Loop &)=default
Loop & operator=(Loop &&)=delete
Loop(Loop &&)=delete
void RunOnceBlocking()
Wait for at least one event to be handled and execute Loop::RunOnce().
Definition loop.cpp:43
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
ScreenInteractive is a Screen that can handle events, run a main loop, and manage components.
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
std::shared_ptr< ComponentBase > Component