logmeow/event_loop.cpp

137 lines
4.1 KiB
C++
Raw Normal View History

#include <imgui.h>
#include <imgui_stdlib.h>
2023-01-05 10:27:14 +00:00
#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) {
2023-01-05 10:27:14 +00:00
log("Failed to write config", e);
return;
}
}
2023-01-05 10:27:14 +00:00
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
2023-01-05 10:27:14 +00:00
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();
}
2023-01-05 10:27:14 +00:00
static inline void logs_window(bool* show_logs_window) {
if (!ImGui::Begin("LogMeow Logs", show_logs_window)) {
ImGui::End();
return;
}
2023-01-05 10:27:14 +00:00
if (ImGui::Button("Clear")) {
log_entries.clear();
}
2023-01-05 10:27:14 +00:00
ImGui::SameLine();
if (ImGui::Button("Copy")) {
ImGui::SetClipboardText(log_entries.c_str());
}
ImGui::Separator();
2023-01-05 13:53:23 +00:00
// copied from imgui/imgui_demo.cpp: [SECTION] Example App: Debug Console / ShowExampleAppConsole()
2023-01-05 10:27:14 +00:00
if (ImGui::BeginChild("ScrollingRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) {
ImGui::TextUnformatted(log_entries.data(), &log_entries[log_entries.size()]);
2023-01-05 13:53:23 +00:00
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) {
ImGui::SetScrollHereY(1.0f);
}
2023-01-05 10:27:14 +00:00
}
ImGui::EndChild();
ImGui::End();
}
2023-01-05 10:27:14 +00:00
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;
}
2023-01-05 10:27:14 +00:00
if (ImGui::Button("Settings")) {
*show_settings_window = true;
}
ImGui::SameLine();
2023-01-05 10:27:14 +00:00
if (ImGui::Button("Logs")) {
*show_logs_window = true;
}
2023-01-05 10:27:14 +00:00
ImGui::End();
}
2023-01-05 09:05:09 +00:00
static inline void debug_window() {
static bool show_demo_window = false;
2023-01-05 10:27:14 +00:00
static size_t add_log_entry_presses = 1;
if (show_demo_window) {
ImGui::ShowDemoWindow(&show_demo_window);
}
2023-01-05 09:05:09 +00:00
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);
2023-01-05 10:27:14 +00:00
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);
}
2023-01-05 09:05:09 +00:00
ImGui::End();
}
2023-01-05 10:27:14 +00:00
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();
}
2023-01-05 09:05:09 +00:00
void event_loop(Config& config, float* config_write_timer, bool* run_event_loop) {
static bool show_settings_window = false;
2023-01-05 10:27:14 +00:00
static bool show_logs_window = false;
2023-01-05 09:05:09 +00:00
static bool exit_requested_rev = true;
2023-01-05 10:27:14 +00:00
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);
}
}
2023-01-05 10:27:14 +00:00
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);
2023-01-05 10:27:14 +00:00
main_window(config, &show_settings_window, &show_logs_window, &exit_requested_rev);
}