logmeow/event_loop.cpp

72 lines
2.4 KiB
C++

#include <imgui.h>
#include "log.h"
#include "config.h"
#include "logcat_thread.h"
#include "windows/logs.h"
#include "windows/settings.h"
#include "windows/main.h"
#ifndef NDEBUG
#include "windows/debug.h"
#endif
static inline void write_config_and_update_structures(const Config& config) {
try {
write_config(config);
} catch (const std::exception& e) {
log(std::string("Failed to write config: ") + e.what());
return;
}
}
static inline void check_for_logcat_items(LogcatThread& logcat_thread, std::vector<LogcatEntry>& filtered_logcat_entries) {
LogcatThreadItem* logcat_thread_item;
while ((logcat_thread_item = logcat_thread.atomic_ring_buffer.get())) {
if (std::holds_alternative<std::string>(*logcat_thread_item)) {
log_raw(std::move(std::get<std::string>(*logcat_thread_item)), false);
} else if (std::holds_alternative<LogcatEntry>(*logcat_thread_item)) {
filtered_logcat_entries.push_back(std::move(std::get<LogcatEntry>(*logcat_thread_item)));
} else {
throw std::runtime_error("Cannot handle all possible logcat thread item variants");
}
logcat_thread.atomic_ring_buffer.increment_read();
}
}
void event_loop(ImFont* monospace_font, Config& config, float* config_write_timer, LogcatThread& logcat_thread, bool* run_event_loop) {
static bool show_settings_window = false;
static bool show_logs_window = false;
static size_t log_entries_read = 0;
static std::vector<LogcatEntry> filtered_logcat_entries;
check_for_logcat_items(logcat_thread, filtered_logcat_entries);
#ifndef NDEBUG
debug_window(logcat_thread);
#endif
if (show_settings_window) {
settings_window(config, config_write_timer, &show_settings_window);
}
if (*config_write_timer > 0.0f) {
*config_write_timer -= ImGui::GetIO().DeltaTime;
if (*config_write_timer <= 0.0f) {
write_config_and_update_structures(config);
}
}
if (show_logs_window) {
bool autoscrolling = false;
logs_window(monospace_font, &autoscrolling, &show_logs_window);
if (autoscrolling) {
log_entries_read = log_entries.size();
}
}
// log_entries must not be mutated until the show logs button
main_window(log_entries_read == log_entries.size(), monospace_font, filtered_logcat_entries, &show_settings_window, &show_logs_window, run_event_loop);
}