diff --git a/fragments/filters.cpp b/fragments/filters.cpp index 3338588..5b128e6 100644 --- a/fragments/filters.cpp +++ b/fragments/filters.cpp @@ -4,6 +4,7 @@ #include "../group_panel.h" #include "../filters.h" #include "../config.h" +#include "ok_buttons_fragment.h" #include "filters.h" static inline void render_integer_filter(IntegerFilter* filter); @@ -255,21 +256,13 @@ void filters_fragment(Config& active_config, Filters& __restrict active_filters, } ImGui::Separator(); - ImVec2 button_size(4 * ImGui::GetFontSize(), 0); - if (ImGui::Button("OK", button_size)) { + ok_buttons_fragment(p_open, [&]() { active_filters = std::move(inactive_filters); try_write_config(active_config); update_logcat_entries(active_config, logcat_entries, filtered_logcat_entry_offsets); - *p_open = false; - } - ImGui::SameLine(); - if (ImGui::Button("Cancel", button_size)) { - *p_open = false; - } - ImGui::SameLine(); - if (ImGui::Button("Apply", button_size)) { + }, [&]() { copy_filters(active_filters, inactive_filters); try_write_config(active_config); update_logcat_entries(active_config, logcat_entries, filtered_logcat_entry_offsets); - } + }); } diff --git a/fragments/ok_buttons_fragment.h b/fragments/ok_buttons_fragment.h new file mode 100644 index 0000000..594a250 --- /dev/null +++ b/fragments/ok_buttons_fragment.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include "../misc.h" + +void ok_buttons_fragment(bool* p_open, auto ok, auto apply); + +// function bodies must be in the header because https://stackoverflow.com/a/999383 + +void ok_buttons_fragment(bool* p_open, auto ok, auto apply) { + ImVec2 button_size(4 * ImGui::GetFontSize(), 0); + bool ctrl_s_pressed = ImGui::IsKeyPressed(ImGuiMod_Shortcut | ImGuiKey_S); + + if (ImGui::Button("OK", button_size)) { + ok(); + *p_open = false; + } + ImGui::SameLine(); + if (ImGui::Button("Cancel", button_size)) { + *p_open = false; + } + ImGui::SameLine(); + if (ImGui::Button("Apply", button_size) || ctrl_s_pressed) { + apply(); + } +} diff --git a/misc.cpp b/misc.cpp index 5d980e2..bff96be 100644 --- a/misc.cpp +++ b/misc.cpp @@ -67,6 +67,11 @@ bool ImGui::BeginWithCloseShortcut(const char* label, bool* p_open, ImGuiWindowF if (p_open && ImGui::Shortcut(ImGuiMod_Shortcut | ImGuiKey_W, 0, ImGuiInputFlags_Repeat)) { *p_open = false; + return false; } return res; } + +bool ImGui::IsKeyPressed(ImGuiKeyChord key_chord, bool repeat) { + return ImGui::Shortcut(key_chord, 0, repeat ? ImGuiInputFlags_Repeat : 0); +} diff --git a/misc.h b/misc.h index 5aed951..da4a104 100644 --- a/misc.h +++ b/misc.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include std::string quote(const std::string& str); void throw_system_error(int err, const char* what); @@ -13,4 +13,6 @@ namespace ImGui { bool RedButton(const char* label); bool Button(const char* label, bool enabled); bool BeginWithCloseShortcut(const char* label, bool* p_open, ImGuiWindowFlags flags = 0); + // don't spill __all__ of imgui_internal.h + bool IsKeyPressed(ImGuiKeyChord key_chord, bool repeat = true); }; // namespace ImGui diff --git a/windows/settings.cpp b/windows/settings.cpp index 58b51cd..a177e3f 100644 --- a/windows/settings.cpp +++ b/windows/settings.cpp @@ -3,6 +3,7 @@ #include "../misc.h" #include "../config.h" +#include "../fragments/ok_buttons_fragment.h" #include "settings.h" static void try_write_config(const Config& config) { @@ -28,24 +29,16 @@ void settings_window(Config& __restrict active_config, Config& __restrict inacti 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)) { + ok_buttons_fragment(p_open, [&]() { active_config.logcat_command = std::move(inactive_config.logcat_command); active_config.normal_font_size = inactive_config.normal_font_size; active_config.monospace_font_size = inactive_config.monospace_font_size; try_write_config(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.logcat_command = inactive_config.logcat_command; active_config.normal_font_size = inactive_config.normal_font_size; active_config.monospace_font_size = inactive_config.monospace_font_size; try_write_config(active_config); - } + }); ImGui::End(); }