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
11 namespace ftxui {
12 class ComponentBase;
13
14 using Component = std::shared_ptr<ComponentBase>;
15 class 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
56 class 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
component_base.hpp
ftxui::Loop::HasQuitted
bool HasQuitted()
Whether the loop has quitted.
Definition loop.cpp:30
ftxui::Loop::Loop
Loop(const ScreenInteractive &)=delete
ftxui::Loop::~Loop
~Loop()
Definition loop.cpp:25
ftxui::Loop::Run
void Run()
Definition loop.cpp:49
ftxui::Loop::Loop
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
ftxui::Loop::operator=
Loop & operator=(const Loop &)=delete
ftxui::Loop::RunOnce
void RunOnce()
Execute the loop. Make the component to process every pending tasks/events. A new frame might be draw...
Definition loop.cpp:37
ftxui::Loop::Loop
Loop(const Loop &)=default
ftxui::Loop::operator=
Loop & operator=(Loop &&)=delete
ftxui::Loop::Loop
Loop(Loop &&)=delete
ftxui::Loop::RunOnceBlocking
void RunOnceBlocking()
Wait for at least one event to be handled and execute Loop::RunOnce().
Definition loop.cpp:43
ftxui::Loop
Loop is a class that manages the event loop for a component.
Definition loop.hpp:56
ftxui::ScreenInteractive
ScreenInteractive is a Screen that can handle events, run a main loop, and manage components.
Definition screen_interactive.hpp:33
ftxui
The FTXUI ftxui:: namespace.
Definition animation.hpp:10
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24