Introduce Loop. (#476)

It can be used to give developers a better control on the loop. Users
can use it not to take full control of the thread, and poll FTXUI from
time to time as part of an external loop.

This resolves: https://github.com/ArthurSonzogni/FTXUI/issues/474
This commit is contained in:
Arthur Sonzogni
2022-10-18 21:29:27 +02:00
committed by GitHub
parent 26d63bc56f
commit 0acfd8f255
9 changed files with 273 additions and 79 deletions

View File

@@ -0,0 +1,44 @@
#include "ftxui/component/loop.hpp"
#include "ftxui/component/screen_interactive.hpp"
namespace ftxui {
Loop::Loop(ScreenInteractive* screen, Component component)
: screen_(screen), component_(component) {
screen_->PreMain();
}
Loop::~Loop() {
screen_->PostMain();
}
bool Loop::HasQuitted() {
return screen_->HasQuitted();
}
/// @brief Execute the loop. Make the `component` to process every pending
/// tasks/events. A new frame might be drawn if the previous was invalidated.
/// Return true until the loop hasn't completed.
void Loop::RunOnce() {
screen_->RunOnce(component_);
}
/// @brief Wait for at least one event to be handled and execute
/// `Loop::RunOnce()`.
void Loop::RunOnceBlocking() {
screen_->RunOnceBlocking(component_);
}
/// Execute the loop, blocking the current thread, up until the loop has
/// quitted.
void Loop::Run() {
while (!HasQuitted()) {
RunOnceBlocking();
}
}
} // namespace ftxui
// Copyright 2022 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.