From b7965881336e04fe8ea97aebf8e3ab0be9ed0521 Mon Sep 17 00:00:00 2001 From: Ammerhai Date: Tue, 26 May 2026 16:42:52 +0200 Subject: [PATCH] add shortcuts --- src/work-calendar.cpp | 98 ++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 30 deletions(-) diff --git a/src/work-calendar.cpp b/src/work-calendar.cpp index af2d03e..2843c5a 100644 --- a/src/work-calendar.cpp +++ b/src/work-calendar.cpp @@ -97,6 +97,13 @@ static Localized_String month_names[] = { LS_JANUARY, LS_FEBRUARY, LS_MARCH, LS_ static char *save_file_path = NULL; static const char *wiki_url = "https://gitea.ammerhai.com/Ammerhai/work-calendar/wiki"; +static ImGuiID close_popup; +static ImGuiID close_calendar_popup; +static ImGuiID close_and_load_calendar_popup; +static ImGuiID save_password_popup; +static ImGuiID open_password_popup; +static ImGuiID about_popup; + void init() { hydro_init(); ImGuiIO &io = ImGui::GetIO(); @@ -213,6 +220,15 @@ void save(){ unsaved_changes = false; } +void interaction_save_as(){ + char *new_save_file_path = save_file_dialog(); + if (new_save_file_path) { + if (save_file_path) free(save_file_path); + save_file_path = new_save_file_path; + ImGui::OpenPopup(save_password_popup); + } +} + void load(){ FILE *save_file = fopen(save_file_path, "rb"); size_t num_bytes = fread(encrypted_buffer, 1, sizeof(encrypted_buffer), save_file); @@ -268,6 +284,19 @@ void load(){ unsaved_changes = false; } +void interaction_open(){ + if(unsaved_changes) { + ImGui::OpenPopup(close_and_load_calendar_popup); + } else { + char *new_save_file_path = open_file_dialog(); + if (new_save_file_path) { + if (save_file_path) free(save_file_path); + save_file_path = new_save_file_path; + ImGui::OpenPopup(open_password_popup); + } + } +} + void close_calendar(){ if (save_file_path) free(save_file_path); save_file_path = (char *)calloc(1, 1); @@ -280,10 +309,43 @@ void close_calendar(){ unsaved_changes = false; } +void interaction_close_calendar() { + if(unsaved_changes) { + ImGui::OpenPopup(close_calendar_popup); + } else { + close_calendar(); + } +} + + + void per_frame(){ current_language = selected_language; - if(last_frame_unsaved_changes != unsaved_changes) { + close_popup = ImGui::GetID("###Close"); + close_calendar_popup = ImGui::GetID("###CloseCalendar"); + close_and_load_calendar_popup = ImGui::GetID("###CloseLoadCalendar"); + save_password_popup = ImGui::GetID("###Save"); + open_password_popup = ImGui::GetID("###Open"); + about_popup = ImGui::GetID("###About"); + + if (ImGui::IsKeyDown(ImGuiMod_Ctrl) && ImGui::IsKeyPressed(ImGuiKey_S, false)) { + if (ImGui::IsKeyDown(ImGuiMod_Shift)) { + interaction_save_as(); + } else if (strlen(save_file_path)) { + save(); + } + } + + if (ImGui::IsKeyDown(ImGuiMod_Ctrl) && ImGui::IsKeyPressed(ImGuiKey_O, false)) { + interaction_open(); + } + + if (ImGui::IsKeyDown(ImGuiMod_Ctrl) && ImGui::IsKeyPressed(ImGuiKey_X, false)) { + interaction_close_calendar(); + } + + if (last_frame_unsaved_changes != unsaved_changes) { char *title = (char *)calloc(1, strlen("Work Calendar - ") + strlen(save_file_path) + 2); memcpy(title, "Work Calendar - ", strlen("Work Calendar - ")); memcpy(title + strlen(title), save_file_path, strlen(save_file_path)); @@ -314,48 +376,24 @@ void per_frame(){ ImGui::DockBuilderFinish(main_viewport_dock); } ImGui::DockSpaceOverViewport(main_viewport_dock, ImGui::GetMainViewport(), ImGuiDockNodeFlags_NoTabBar); - - ImGuiID close_popup = ImGui::GetID("###Close"); - ImGuiID close_calendar_popup = ImGui::GetID("###CloseCalendar"); - ImGuiID close_and_load_calendar_popup = ImGui::GetID("###CloseLoadCalendar"); - ImGuiID save_password_popup = ImGui::GetID("###Save"); - ImGuiID open_password_popup = ImGui::GetID("###Open"); - ImGuiID about_popup = ImGui::GetID("###About"); if (ImGui::BeginMainMenuBar()) { if (ImGui::BeginMenu(get_localized_string(LS_FILE))) { if (ImGui::MenuItem(get_localized_string(LS_OPEN), "Ctrl + O")) { - if(unsaved_changes) { - ImGui::OpenPopup(close_and_load_calendar_popup); - } else { - char *new_save_file_path = open_file_dialog(); - if (new_save_file_path) { - if (save_file_path) free(save_file_path); - save_file_path = new_save_file_path; - ImGui::OpenPopup(open_password_popup); - } - } + interaction_open(); } - if (ImGui::MenuItem(get_localized_string(LS_SAVE), "Ctrl + S", false, !!save_file_path)) { + + if (ImGui::MenuItem(get_localized_string(LS_SAVE), "Ctrl + S", false, strlen(save_file_path))) { save(); } if (ImGui::MenuItem(get_localized_string(LS_SAVE_AS), "Ctrl + Shift + S")) { - char *new_save_file_path = save_file_dialog(); - if (new_save_file_path) { - if (save_file_path) free(save_file_path); - save_file_path = new_save_file_path; - ImGui::OpenPopup(save_password_popup); - } + interaction_save_as(); } ImGui::Separator(); if (ImGui::MenuItem(get_localized_string(LS_CLOSE_CALENDAR), "Ctrl + X")) { - if(unsaved_changes) { - ImGui::OpenPopup(close_calendar_popup); - } else { - close_calendar(); - } + interaction_close_calendar(); } ImGui::Separator();