Improve ScreenInteractive wait function.

This commit is contained in:
ArthurSonzogni 2019-02-02 16:28:44 +01:00
parent 17cd6d25a5
commit 15da9cdc18
3 changed files with 11 additions and 15 deletions

View File

@ -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"};

View File

@ -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;

View File

@ -74,22 +74,18 @@ ScreenInteractive ScreenInteractive::FitComponent() {
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();
}
}