Unify OK/Cancel/Apply buttons

This commit is contained in:
blankie 2023-02-11 22:15:21 +07:00
parent 5b777dc309
commit 432e3c4c71
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
5 changed files with 42 additions and 23 deletions

View File

@ -4,6 +4,7 @@
#include "../group_panel.h" #include "../group_panel.h"
#include "../filters.h" #include "../filters.h"
#include "../config.h" #include "../config.h"
#include "ok_buttons_fragment.h"
#include "filters.h" #include "filters.h"
static inline void render_integer_filter(IntegerFilter* filter); static inline void render_integer_filter(IntegerFilter* filter);
@ -255,21 +256,13 @@ void filters_fragment(Config& active_config, Filters& __restrict active_filters,
} }
ImGui::Separator(); ImGui::Separator();
ImVec2 button_size(4 * ImGui::GetFontSize(), 0); ok_buttons_fragment(p_open, [&]() {
if (ImGui::Button("OK", button_size)) {
active_filters = std::move(inactive_filters); active_filters = std::move(inactive_filters);
try_write_config(active_config); try_write_config(active_config);
update_logcat_entries(active_config, logcat_entries, filtered_logcat_entry_offsets); 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); copy_filters(active_filters, inactive_filters);
try_write_config(active_config); try_write_config(active_config);
update_logcat_entries(active_config, logcat_entries, filtered_logcat_entry_offsets); update_logcat_entries(active_config, logcat_entries, filtered_logcat_entry_offsets);
} });
} }

View File

@ -0,0 +1,26 @@
#pragma once
#include <imgui.h>
#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();
}
}

View File

@ -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)) { if (p_open && ImGui::Shortcut(ImGuiMod_Shortcut | ImGuiKey_W, 0, ImGuiInputFlags_Repeat)) {
*p_open = false; *p_open = false;
return false;
} }
return res; return res;
} }
bool ImGui::IsKeyPressed(ImGuiKeyChord key_chord, bool repeat) {
return ImGui::Shortcut(key_chord, 0, repeat ? ImGuiInputFlags_Repeat : 0);
}

4
misc.h
View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <imgui.h>
#include <string> #include <string>
#include <imgui.h>
std::string quote(const std::string& str); std::string quote(const std::string& str);
void throw_system_error(int err, const char* what); void throw_system_error(int err, const char* what);
@ -13,4 +13,6 @@ namespace ImGui {
bool RedButton(const char* label); bool RedButton(const char* label);
bool Button(const char* label, bool enabled); bool Button(const char* label, bool enabled);
bool BeginWithCloseShortcut(const char* label, bool* p_open, ImGuiWindowFlags flags = 0); 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 }; // namespace ImGui

View File

@ -3,6 +3,7 @@
#include "../misc.h" #include "../misc.h"
#include "../config.h" #include "../config.h"
#include "../fragments/ok_buttons_fragment.h"
#include "settings.h" #include "settings.h"
static void try_write_config(const Config& config) { 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::InputFloat("Monospace font size", &inactive_config.monospace_font_size, 0.5f, 1.0f, "%.3f");
ImGui::Separator(); ImGui::Separator();
ImVec2 button_size(4 * ImGui::GetFontSize(), 0); ok_buttons_fragment(p_open, [&]() {
if (ImGui::Button("OK", button_size)) {
active_config.logcat_command = std::move(inactive_config.logcat_command); active_config.logcat_command = std::move(inactive_config.logcat_command);
active_config.normal_font_size = inactive_config.normal_font_size; active_config.normal_font_size = inactive_config.normal_font_size;
active_config.monospace_font_size = inactive_config.monospace_font_size; active_config.monospace_font_size = inactive_config.monospace_font_size;
try_write_config(active_config); 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.logcat_command = inactive_config.logcat_command;
active_config.normal_font_size = inactive_config.normal_font_size; active_config.normal_font_size = inactive_config.normal_font_size;
active_config.monospace_font_size = inactive_config.monospace_font_size; active_config.monospace_font_size = inactive_config.monospace_font_size;
try_write_config(active_config); try_write_config(active_config);
} });
ImGui::End(); ImGui::End();
} }