mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-25 20:27:31 +08:00
Improve ScreenInteractive wait function.
This commit is contained in:
parent
17cd6d25a5
commit
15da9cdc18
@ -7,7 +7,7 @@
|
||||
|
||||
int main(int argc, const char* argv[]) {
|
||||
using namespace ftxui;
|
||||
auto screen = ScreenInteractive::FixedSize(30, 3);
|
||||
auto screen = ScreenInteractive::TerminalOutput();
|
||||
|
||||
Menu menu;
|
||||
menu.entries = {L"entry 1", L"entry 2", L"entry 3"};
|
||||
|
@ -40,7 +40,7 @@ class ScreenInteractive : public Screen {
|
||||
Dimension dimension_ = Dimension::Fixed;
|
||||
ScreenInteractive(int dimx, int dimy, Dimension dimension);
|
||||
|
||||
std::condition_variable events_queue_wait;
|
||||
std::condition_variable events_queue_cv;
|
||||
std::mutex events_queue_mutex;
|
||||
std::queue<Event> events_queue;
|
||||
std::atomic<bool> quit_ = false;
|
||||
|
@ -71,25 +71,21 @@ ScreenInteractive ScreenInteractive::FitComponent() {
|
||||
return ScreenInteractive(0, 0, Dimension::FitComponent);
|
||||
}
|
||||
|
||||
void ScreenInteractive::PostEvent(Event event) {
|
||||
void ScreenInteractive::PostEvent(Event event) {
|
||||
std::unique_lock<std::mutex> lock(events_queue_mutex);
|
||||
events_queue.push(event);
|
||||
events_queue_wait.notify_one();
|
||||
events_queue_cv.notify_one();
|
||||
}
|
||||
|
||||
void ScreenInteractive::EventLoop(Component* component) {
|
||||
bool handled = 0;
|
||||
for (;;) {
|
||||
std::unique_lock<std::mutex> lock(events_queue_mutex);
|
||||
while (!events_queue.empty()) {
|
||||
component->OnEvent(events_queue.front());
|
||||
events_queue.pop();
|
||||
handled = true;
|
||||
}
|
||||
std::unique_lock<std::mutex> lock(events_queue_mutex);
|
||||
while (!quit_ && events_queue.empty())
|
||||
events_queue_cv.wait(lock);
|
||||
|
||||
if (handled)
|
||||
return;
|
||||
events_queue_wait.wait(lock);
|
||||
// After the wait, we own the lock.
|
||||
while (!events_queue.empty()) {
|
||||
component->OnEvent(events_queue.front());
|
||||
events_queue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user