logmeow/event_loop.cpp

135 lines
4.0 KiB
C++

#include <imgui.h>
#include <imgui_stdlib.h>
#include "log.h"
#include "config.h"
static inline void write_config_and_update_structures(const Config& config) {
try {
write_config(config);
} catch (const std::exception& e) {
log("Failed to write config", e);
return;
}
}
static inline void settings_window(Config& config, float* config_write_timer, bool* show_settings_window) {
if (!ImGui::Begin("Settings", show_settings_window)) {
ImGui::End();
return;
}
// TODO actually have process control
ImGui::Text("Logcat command only takes effect when logcat is not running");
if (ImGui::InputTextWithHint("Logcat command", "adb logcat -Dv 'threadtime UTC epoch usec'", &config.logcat_command)) {
*config_write_timer = *config_write_timer > 0.0f ? *config_write_timer : 5.0f;
}
ImGui::End();
}
static inline void logs_window(bool* show_logs_window) {
if (!ImGui::Begin("LogMeow Logs", show_logs_window)) {
ImGui::End();
return;
}
if (ImGui::Button("Clear")) {
log_entries.clear();
}
ImGui::SameLine();
if (ImGui::Button("Copy")) {
ImGui::SetClipboardText(log_entries.c_str());
}
// copied from imgui/imgui_demo.cpp: [SECTION] Example App: Debug Console / ShowExampleAppConsole()
ImGui::Separator();
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) {
ImGui::TextUnformatted(log_entries.data(), &log_entries[log_entries.size()]);
}
ImGui::EndChild();
ImGui::End();
}
static inline void main_window(Config& active_config, bool* show_settings_window, bool* show_logs_window, bool* exit_requested_rev) {
if (!ImGui::Begin("LogMeow", exit_requested_rev)) {
ImGui::End();
return;
}
if (ImGui::Button("Settings")) {
*show_settings_window = true;
}
ImGui::SameLine();
if (ImGui::Button("Logs")) {
*show_logs_window = true;
}
ImGui::End();
}
static inline void debug_window() {
static bool show_demo_window = false;
static size_t add_log_entry_presses = 1;
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
if (!ImGui::Begin("LogMeow Debug")) {
ImGui::End();
return;
}
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::Checkbox("Show Dear ImGui Demo Window", &show_demo_window);
ImGui::Separator();
if (ImGui::Button("Add Log Entry")) {
std::exception e;
log(std::string("Add log entry button pressed ") + std::to_string(add_log_entry_presses++) + " time(s)", e);
}
ImGui::End();
}
static inline void exit_modal_if_necessary(bool* run_event_loop) {
if (!ImGui::BeginPopupModal("Exit?", nullptr, ImGuiWindowFlags_NoResize)) {
return;
}
ImGui::Text("Are you sure you want to exit?");
if (ImGui::Button("Yes", ImVec2(120, 0))) {
*run_event_loop = false;
}
ImGui::SameLine();
if (ImGui::Button("No", ImVec2(120, 0))) {
ImGui::CloseCurrentPopup();
}
ImGui::SetItemDefaultFocus();
ImGui::EndPopup();
}
void event_loop(Config& config, float* config_write_timer, bool* run_event_loop) {
static bool show_settings_window = false;
static bool show_logs_window = false;
static bool exit_requested_rev = true;
debug_window();
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) {
logs_window(&show_logs_window);
}
if (!exit_requested_rev) {
ImGui::OpenPopup("Exit?");
exit_requested_rev = true;
}
exit_modal_if_necessary(run_event_loop);
main_window(config, &show_settings_window, &show_logs_window, &exit_requested_rev);
}