fix(undo): 修复撤销按钮状态不刷新及空指针防护

- 将 refresh_undo_redo_buttons 提升为公共函数(声明在 callbacks_internal.h,实现在 callbacks.c)
- 在所有 push_record 的操作回调末尾调用 refresh_undo_redo_buttons,确保按钮状态实时更新
- 修复 redo() 中 OP_CLEAN/OP_IMPORT 的 new_paths 空指针风险
- 移除 undo_redo.c 中废弃的 apply_record 函数

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-01 23:12:42 +08:00
parent bbcfc25aea
commit ec0ca5a3f6
5 changed files with 37 additions and 27 deletions
+3
View File
@@ -20,4 +20,7 @@ StringList *get_current_raw_data(Ihandle *dlg);
// 获取当前活动的列表 UI 控件 // 获取当前活动的列表 UI 控件
Ihandle *get_current_list(Ihandle *dlg); Ihandle *get_current_list(Ihandle *dlg);
// 刷新撤销/重做按钮的启用状态
void refresh_undo_redo_buttons(Ihandle *dlg);
#endif // CALLBACKS_INTERNAL_H #endif // CALLBACKS_INTERNAL_H
+17
View File
@@ -1,6 +1,7 @@
#include "controller/callbacks.h" #include "controller/callbacks.h"
#include "controller/callbacks_internal.h" #include "controller/callbacks_internal.h"
#include "core/app_context.h" #include "core/app_context.h"
#include "core/undo_redo.h"
#include "utils/ui_constants.h" #include "utils/ui_constants.h"
#include <iup.h> #include <iup.h>
@@ -45,3 +46,19 @@ Ihandle *get_current_list(Ihandle *dlg)
return IupGetDialogChild(dlg, CTRL_LIST_USER); return IupGetDialogChild(dlg, CTRL_LIST_USER);
return IupGetDialogChild(dlg, CTRL_LIST_SYS); return IupGetDialogChild(dlg, CTRL_LIST_SYS);
} }
// 刷新撤销/重做按钮的启用状态
void refresh_undo_redo_buttons(Ihandle *dlg)
{
AppContext *ctx = get_app_context_from_dlg(dlg);
if (!ctx || !ctx->undo_redo_mgr)
return;
Ihandle *btn_undo = IupGetDialogChild(dlg, CTRL_BTN_UNDO);
Ihandle *btn_redo = IupGetDialogChild(dlg, CTRL_BTN_REDO);
if (btn_undo)
IupSetAttribute(btn_undo, "ACTIVE", can_undo(ctx->undo_redo_mgr) ? "YES" : "NO");
if (btn_redo)
IupSetAttribute(btn_redo, "ACTIVE", can_redo(ctx->undo_redo_mgr) ? "YES" : "NO");
}
+7
View File
@@ -76,6 +76,8 @@ int btn_new_cb(Ihandle *self)
int count = IupGetInt(current_list, "COUNT"); int count = IupGetInt(current_list, "COUNT");
IupSetInt(current_list, "VALUE", count); IupSetInt(current_list, "VALUE", count);
refresh_undo_redo_buttons(dlg);
} }
} }
return IUP_DEFAULT; return IUP_DEFAULT;
@@ -114,6 +116,8 @@ int btn_edit_cb(Ihandle *self)
sync_string_list_to_ui(current_list, raw_data); sync_string_list_to_ui(current_list, raw_data);
IupSetInt(current_list, "VALUE", selected); IupSetInt(current_list, "VALUE", selected);
refresh_undo_redo_buttons(dlg);
} }
} }
return IUP_DEFAULT; return IUP_DEFAULT;
@@ -168,6 +172,8 @@ int btn_browse_cb(Ihandle *self)
int count = IupGetInt(current_list, "COUNT"); int count = IupGetInt(current_list, "COUNT");
IupSetInt(current_list, "VALUE", count); IupSetInt(current_list, "VALUE", count);
refresh_undo_redo_buttons(dlg);
} }
} }
IupDestroy(filedlg); IupDestroy(filedlg);
@@ -208,5 +214,6 @@ int btn_del_cb(Ihandle *self)
if (lbl_status) if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "deleted")); IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "deleted"));
refresh_undo_redo_buttons(dlg);
return IUP_DEFAULT; return IUP_DEFAULT;
} }
+4 -16
View File
@@ -71,6 +71,7 @@ int btn_up_cb(Ihandle *self)
sync_string_list_to_ui(current_list, raw_data); sync_string_list_to_ui(current_list, raw_data);
IupSetInt(current_list, "VALUE", selected - 1); IupSetInt(current_list, "VALUE", selected - 1);
refresh_undo_redo_buttons(dlg);
return IUP_DEFAULT; return IUP_DEFAULT;
} }
@@ -104,6 +105,7 @@ int btn_down_cb(Ihandle *self)
sync_string_list_to_ui(current_list, raw_data); sync_string_list_to_ui(current_list, raw_data);
IupSetInt(current_list, "VALUE", selected + 1); IupSetInt(current_list, "VALUE", selected + 1);
refresh_undo_redo_buttons(dlg);
return IUP_DEFAULT; return IUP_DEFAULT;
} }
@@ -142,25 +144,11 @@ int btn_clean_cb(Ihandle *self)
char msg[128]; char msg[128];
snprintf(msg, sizeof(msg), _("Cleanup completed! Removed %d invalid or duplicate paths."), removed); snprintf(msg, sizeof(msg), _("Cleanup completed! Removed %d invalid or duplicate paths."), removed);
IupMessage(_("Info"), msg); IupMessage(_("Info"), msg);
refresh_undo_redo_buttons(dlg);
return IUP_DEFAULT; return IUP_DEFAULT;
} }
// 刷新撤销/重做按钮的启用状态
static void refresh_undo_redo_buttons(Ihandle *dlg)
{
AppContext *ctx = get_app_context_from_dlg(dlg);
if (!ctx || !ctx->undo_redo_mgr)
return;
Ihandle *btn_undo = IupGetDialogChild(dlg, CTRL_BTN_UNDO);
Ihandle *btn_redo = IupGetDialogChild(dlg, CTRL_BTN_REDO);
if (btn_undo)
IupSetAttribute(btn_undo, "ACTIVE", can_undo(ctx->undo_redo_mgr) ? "YES" : "NO");
if (btn_redo)
IupSetAttribute(btn_redo, "ACTIVE", can_redo(ctx->undo_redo_mgr) ? "YES" : "NO");
}
int btn_undo_cb(Ihandle *self) int btn_undo_cb(Ihandle *self)
{ {
Ihandle *dlg = IupGetDialog(self); Ihandle *dlg = IupGetDialog(self);
+3 -8
View File
@@ -128,14 +128,6 @@ int push_undo_record(UndoRedoManager *mgr, const OpRecord *record)
return 0; return 0;
} }
static void apply_record(UndoRedoManager *mgr, int record_index, int is_undo)
{
(void)mgr;
(void)record_index;
(void)is_undo;
// 此函数已废弃,撤销/重做逻辑在 undo() 和 redo() 中直接实现
}
int undo(UndoRedoManager *mgr, StringList *sys_paths, StringList *user_paths) int undo(UndoRedoManager *mgr, StringList *sys_paths, StringList *user_paths)
{ {
if (!mgr || !can_undo(mgr)) if (!mgr || !can_undo(mgr))
@@ -268,11 +260,14 @@ int redo(UndoRedoManager *mgr, StringList *sys_paths, StringList *user_paths)
case OP_IMPORT: case OP_IMPORT:
// 重做清理/导入:应用新列表 // 重做清理/导入:应用新列表
clear_string_list(target); clear_string_list(target);
if (rec->new_paths)
{
for (int i = 0; i < rec->count; i++) for (int i = 0; i < rec->count; i++)
{ {
if (rec->new_paths[i]) if (rec->new_paths[i])
add_string_list(target, rec->new_paths[i]); add_string_list(target, rec->new_paths[i]);
} }
}
break; break;
case OP_CLEAR: case OP_CLEAR: