diff --git a/config.h b/config.h index 0b8f545..33d490e 100644 --- a/config.h +++ b/config.h @@ -12,3 +12,9 @@ std::string get_config_folder(); void create_config_folders_if_necessary(); Config load_config(); void write_config(const Config& config); + +constexpr bool operator!=(const Config& lhs, const Config& rhs) { + return lhs.logcat_command != rhs.logcat_command + || lhs.normal_font_size != rhs.normal_font_size + || lhs.monospace_font_size != rhs.monospace_font_size; +} diff --git a/event_loop.cpp b/event_loop.cpp index 0f689a6..93b8c80 100644 --- a/event_loop.cpp +++ b/event_loop.cpp @@ -11,15 +11,6 @@ #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& filtered_logcat_entries) { LogcatThreadItem* logcat_thread_item; @@ -36,7 +27,8 @@ static inline void check_for_logcat_items(LogcatThread& logcat_thread, std::vect } -void event_loop(ImFont* monospace_font, Config& config, float* config_write_timer, LogcatThread& logcat_thread, bool* run_event_loop) { +void event_loop(ImFont* monospace_font, Config& active_config, LogcatThread& logcat_thread, bool* run_event_loop) { + static Config inactive_config; static bool show_settings_window = false; static bool show_logs_window = false; static size_t log_entries_read = 0; @@ -49,13 +41,7 @@ void event_loop(ImFont* monospace_font, Config& config, float* config_write_time #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); - } + settings_window(active_config, inactive_config, &show_settings_window); } if (show_logs_window) { @@ -67,5 +53,8 @@ void event_loop(ImFont* monospace_font, Config& config, float* config_write_time } // 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); + main_window(log_entries_read == log_entries.size(), + monospace_font, filtered_logcat_entries, + active_config, inactive_config, &show_settings_window, + &show_logs_window, run_event_loop); } diff --git a/event_loop.h b/event_loop.h index c9a4103..683a7f3 100644 --- a/event_loop.h +++ b/event_loop.h @@ -5,4 +5,4 @@ #include "config.h" #include "logcat_thread.h" -void event_loop(ImFont* monospace_font, Config& config, float* config_write_timer, LogcatThread& logcat_thread, bool* run_event_loop); +void event_loop(ImFont* monospace_font, Config& active_config, LogcatThread& logcat_thread, bool* run_event_loop); diff --git a/main.cpp b/main.cpp index b63e322..ff35cf7 100644 --- a/main.cpp +++ b/main.cpp @@ -131,7 +131,6 @@ int main(int, char**) { // Main loop bool run_event_loop = true; - float config_write_timer = 0.0f; LogcatThread logcat_thread = [&]() { try { return LogcatThread(); @@ -162,7 +161,7 @@ int main(int, char**) { ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); - event_loop(monospace_font, config, &config_write_timer, logcat_thread, &run_event_loop); + event_loop(monospace_font, config, logcat_thread, &run_event_loop); // Rendering ImGui::Render(); @@ -182,14 +181,6 @@ int main(int, char**) { SDL_DestroyWindow(window); SDL_Quit(); - if (config_write_timer > 0.0f) { - try { - write_config(config); - } catch (const std::exception& e) { - fprintf(stderr, "Failed to write config: %s\n", e.what()); - return 1; - } - } logcat_thread.request_stop(); logcat_thread.join(); diff --git a/windows/main.cpp b/windows/main.cpp index e522f79..9d35097 100644 --- a/windows/main.cpp +++ b/windows/main.cpp @@ -50,13 +50,14 @@ static inline void main_scrolling_region(ImFont* monospace_font, std::vector& filtered_logcat_entries, - bool* show_settings_window, bool* show_logs_window, bool* run_event_loop) { + const Config& active_config, Config& inactive_config, bool* show_settings_window, bool* show_logs_window, bool* run_event_loop) { if (!ImGui::Begin("LogMeow", run_event_loop)) { ImGui::End(); return; } - if (ImGui::Button("Settings")) { + if (ImGui::Button("Settings") && !*show_settings_window) { + inactive_config = active_config; *show_settings_window = true; } diff --git a/windows/main.h b/windows/main.h index 2092149..1939e11 100644 --- a/windows/main.h +++ b/windows/main.h @@ -3,7 +3,8 @@ #include #include +#include "../config.h" #include "../logcat_entry.h" void main_window(bool latest_log_entries_read, ImFont* monospace_font, std::vector& filtered_logcat_entries, - bool* show_settings_window, bool* show_logs_window, bool* run_event_loop); + const Config& active_config, Config& inactive_config, bool* show_settings_window, bool* show_logs_window, bool* run_event_loop); diff --git a/windows/settings.cpp b/windows/settings.cpp index abc7e4f..f0baa44 100644 --- a/windows/settings.cpp +++ b/windows/settings.cpp @@ -4,24 +4,48 @@ #include "../config.h" #include "settings.h" -void settings_window(Config& config, float* config_write_timer, bool* p_open) { - if (!ImGui::Begin("Settings", p_open)) { +static 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; + } +} + +void settings_window(Config& active_config, Config& inactive_config, bool* p_open) { + ImGuiWindowFlags flags = active_config != inactive_config + ? ImGuiWindowFlags_UnsavedDocument + : 0; + if (!ImGui::Begin("Settings", p_open, flags)) { 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 uid'", &config.logcat_command)) { - *config_write_timer = *config_write_timer > 0.0f ? *config_write_timer : 5.0f; - } + ImGui::InputTextWithHint("Logcat command", "adb logcat -Dv 'threadtime UTC epoch usec uid'", &inactive_config.logcat_command); + ImGui::Text("Font sizes only take effect when LogMeow is restarted"); #ifdef USE_FONTCONFIG - if (ImGui::InputFloat("Normal font size", &config.normal_font_size, 0.5f, 1.0f, "%.3f")) { - *config_write_timer = *config_write_timer > 0.0f ? *config_write_timer : 5.0f; - } + ImGui::InputFloat("Normal font size", &inactive_config.normal_font_size, 0.5f, 1.0f, "%.3f"); #endif - if (ImGui::InputFloat("Monospace font size", &config.monospace_font_size, 0.5f, 1.0f, "%.3f")) { - *config_write_timer = *config_write_timer > 0.0f ? *config_write_timer : 5.0f; + ImGui::InputFloat("Monospace font size", &inactive_config.monospace_font_size, 0.5f, 1.0f, "%.3f"); + + ImGui::Separator(); + ImVec2 button_size(4 * ImGui::GetFontSize(), 0); + if (ImGui::Button("OK", button_size)) { + active_config = inactive_config; + write_config_and_update_structures(active_config); + *p_open = false; + } + ImGui::SameLine(); + if (ImGui::Button("Cancel", button_size)) { + *p_open = false; + } + ImGui::SameLine(); + if (ImGui::Button("Apply", button_size)) { + active_config = inactive_config; + write_config_and_update_structures(active_config); } ImGui::End(); } diff --git a/windows/settings.h b/windows/settings.h index 2e4237c..557575a 100644 --- a/windows/settings.h +++ b/windows/settings.h @@ -2,4 +2,4 @@ #include "../config.h" -void settings_window(Config& config, float* config_write_timer, bool* p_open); +void settings_window(Config& active_config, Config& inactive_config, bool* p_open);