mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-28 16:29:34 +08:00
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:
44
src/ftxui/component/loop.cpp
Normal file
44
src/ftxui/component/loop.cpp
Normal 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.
|
Reference in New Issue
Block a user