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