feat: 引入 Lua 配置系统实现 UI 参数热更新

- 添加 Lua 5.5 库支持,包含头文件和动态链接库
- 新增 lua_config 模块,提供配置初始化、获取字符串/整型值等功能
- 创建 config.lua 配置文件,集中管理所有 UI 文本、尺寸和布局参数
- 移除原有的硬编码 config.h,将 UI 常量迁移至 Lua 配置
- 修改主窗口、对话框和回调函数,动态读取 Lua 配置值
- 更新 CMakeLists.txt,添加 Lua 库依赖和 DLL 复制步骤
- 删除过时的 Makefile,统一使用 CMake 构建
This commit is contained in:
2026-03-25 19:18:23 +08:00
parent bd1b05be55
commit ce232cb024
17 changed files with 2017 additions and 147 deletions
+7 -6
View File
@@ -2,6 +2,7 @@
#include "core/app_context.h"
#include "core/registry_service.h"
#include "core/path_manager.h"
#include "core/lua_config.h"
#include "utils/string_ext.h"
#include "utils/os_env.h"
#include "ui/ui_utils.h"
@@ -117,7 +118,7 @@ int btn_browse_cb(Ihandle *self)
Ihandle *dlg = IupGetDialog(self);
Ihandle *filedlg = IupFileDlg();
IupSetAttribute(filedlg, "DIALOGTYPE", "DIR");
IupSetAttribute(filedlg, "TITLE", "选择目录");
IupSetAttribute(filedlg, "TITLE", lua_config_get_string("dialog", "select_dir"));
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
@@ -160,7 +161,7 @@ int btn_del_cb(Ihandle *self)
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "状态: 已删除选中项");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "deleted"));
return IUP_DEFAULT;
}
@@ -288,7 +289,7 @@ int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y
{
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "提示: 只能拖拽文件夹添加到 PATH");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "drag_folder_only"));
}
return IUP_DEFAULT;
@@ -331,7 +332,7 @@ int btn_ok_cb(Ihandle *self)
SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL);
IupMessage("成功", "系统和用户 PATH 环境变量均已更新!");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "状态: 全部保存成功");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "saved"));
}
else if (sys_ok)
{
@@ -345,7 +346,7 @@ int btn_ok_cb(Ihandle *self)
{
IupMessage("错误", "保存失败!");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "状态: 保存失败");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "error"));
}
return IUP_DEFAULT;
}
@@ -403,5 +404,5 @@ void load_all_paths(void)
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "状态: 已加载系统和用户变量");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "loaded"));
}
+200
View File
@@ -0,0 +1,200 @@
#include "core/lua_config.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static lua_State* G_L = NULL;
static int G_loaded = 0;
static const char* G_config_path = "lua/config.lua";
static const char* get_string_default(const char* section, const char* key)
{
if (strcmp(section, "app") == 0)
{
if (strcmp(key, "name") == 0) return "PathEditor";
if (strcmp(key, "name_readonly") == 0) return "PathEditor (只读模式)";
}
else if (strcmp(section, "dialog") == 0)
{
if (strcmp(key, "size") == 0) return "800x800";
if (strcmp(key, "minsize") == 0) return "800x800";
if (strcmp(key, "select_dir") == 0) return "选择目录";
}
else if (strcmp(section, "list") == 0)
{
if (strcmp(key, "item_padding") == 0) return "5x5";
if (strcmp(key, "backcolor") == 0) return "255 255 255";
}
else if (strcmp(section, "button") == 0)
{
if (strcmp(key, "rastersize") == 0) return "100x32";
if (strcmp(key, "new") == 0) return "新建(N)";
if (strcmp(key, "edit") == 0) return "编辑(E)";
if (strcmp(key, "browse") == 0) return "浏览(B)...";
if (strcmp(key, "del") == 0) return "删除(D)";
if (strcmp(key, "up") == 0) return "上移(U)";
if (strcmp(key, "down") == 0) return "下移(O)";
if (strcmp(key, "clean") == 0) return "一键清理";
if (strcmp(key, "ok") == 0) return "确定";
if (strcmp(key, "cancel") == 0) return "取消";
if (strcmp(key, "help") == 0) return "帮助(?)";
}
else if (strcmp(section, "label") == 0)
{
if (strcmp(key, "title") == 0) return "环境变量编辑器:";
if (strcmp(key, "search_placeholder") == 0) return "输入关键词搜索...";
if (strcmp(key, "tab_sys") == 0) return "系统变量 (System)";
if (strcmp(key, "tab_user") == 0) return "用户变量 (User)";
}
else if (strcmp(section, "layout") == 0)
{
if (strcmp(key, "vbox_gap") == 0) return "5";
if (strcmp(key, "vbox_margin") == 0) return "0x0";
if (strcmp(key, "vbox_all_margin") == 0) return "10x10";
if (strcmp(key, "vbox_all_gap") == 0) return "5";
if (strcmp(key, "hbox_gap") == 0) return "10";
if (strcmp(key, "hbox_margin") == 0) return "10x10";
if (strcmp(key, "hbox_alignment") == 0) return "ACENTER";
}
else if (strcmp(section, "status") == 0)
{
if (strcmp(key, "normal") == 0) return "状态: 就绪";
if (strcmp(key, "readonly") == 0) return "状态: ⚠️ 只读模式 (无管理员权限)";
if (strcmp(key, "saving") == 0) return "状态: 保存中...";
if (strcmp(key, "saved") == 0) return "状态: ✓ 保存成功";
if (strcmp(key, "error") == 0) return "状态: ✗ 保存失败";
if (strcmp(key, "deleted") == 0) return "状态: 已删除选中项";
if (strcmp(key, "loaded") == 0) return "状态: 已加载系统和用户变量";
if (strcmp(key, "drag_folder_only") == 0) return "提示: 只能拖拽文件夹添加到 PATH";
}
return "";
}
int lua_config_init(void)
{
if (G_L != NULL)
{
return 0;
}
G_L = luaL_newstate();
if (G_L == NULL)
{
return -1;
}
luaL_openlibs(G_L);
if (luaL_dofile(G_L, G_config_path) != LUA_OK)
{
const char* err = lua_tostring(G_L, -1);
if (err)
{
fprintf(stderr, "[Lua Config] 加载配置文件失败: %s\n", err);
}
lua_settop(G_L, 0);
G_loaded = 0;
return 0;
}
lua_settop(G_L, 0);
G_loaded = 1;
return 0;
}
void lua_config_destroy(void)
{
if (G_L != NULL)
{
lua_close(G_L);
G_L = NULL;
}
G_loaded = 0;
}
const char* lua_config_get_string(const char* section, const char* key)
{
if (G_L == NULL || section == NULL || key == NULL)
{
return get_string_default(section, key);
}
lua_getglobal(G_L, "config");
if (!lua_istable(G_L, -1))
{
lua_settop(G_L, 0);
return get_string_default(section, key);
}
lua_getfield(G_L, -1, section);
if (!lua_istable(G_L, -1))
{
lua_settop(G_L, 0);
return get_string_default(section, key);
}
lua_getfield(G_L, -1, key);
if (!lua_isstring(G_L, -1))
{
lua_settop(G_L, 0);
return get_string_default(section, key);
}
const char* value = lua_tostring(G_L, -1);
lua_settop(G_L, 0);
return value ? value : get_string_default(section, key);
}
int lua_config_get_int(const char* section, const char* key, int default_value)
{
if (G_L == NULL || section == NULL || key == NULL)
{
return default_value;
}
lua_getglobal(G_L, "config");
if (!lua_istable(G_L, -1))
{
lua_settop(G_L, 0);
return default_value;
}
lua_getfield(G_L, -1, section);
if (!lua_istable(G_L, -1))
{
lua_settop(G_L, 0);
return default_value;
}
lua_getfield(G_L, -1, key);
if (!lua_isnumber(G_L, -1))
{
lua_settop(G_L, 0);
return default_value;
}
int value = (int)lua_tointeger(G_L, -1);
lua_settop(G_L, 0);
return value;
}
int lua_config_reload(void)
{
if (G_L != NULL)
{
lua_close(G_L);
G_L = NULL;
}
return lua_config_init();
}
int lua_config_is_loaded(void)
{
return G_loaded;
}
+8 -2
View File
@@ -4,11 +4,11 @@
#include <stdlib.h>
#include <wchar.h>
#include "core/app_context.h"
#include "core/lua_config.h"
#include "utils/string_ext.h"
#include "utils/os_env.h"
#include "controller/callbacks.h"
#include "ui/main_window.h"
#include "config.h"
/*
!编译命令:
@@ -39,6 +39,11 @@ int main(int argc, char **argv)
IupOpen(&argc, &argv);
IupSetGlobal("UTF8MODE", "YES");
if (lua_config_init() != 0)
{
IupMessage("警告", "Lua 配置系统初始化失败,将使用默认值");
}
// 在管理员模式下,解决无法拖拽文件到列表框的问题 (UIPI)
// 需要加载 User32.dll 获取 ChangeWindowMessageFilter 函数
HMODULE hUser32 = LoadLibraryW(L"user32.dll");
@@ -81,7 +86,7 @@ int main(int argc, char **argv)
{
Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS");
if (lbl_status)
IupSetAttribute(lbl_status, "TITLE", "状态: ⚠️ 只读模式 (无管理员权限)");
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "readonly"));
Ihandle *btn_new = IupGetDialogChild(dlg, "BTN_NEW");
Ihandle *btn_edit = IupGetDialogChild(dlg, "BTN_EDIT");
@@ -119,6 +124,7 @@ int main(int argc, char **argv)
IupMainLoop();
destroy_app_context(ctx);
lua_config_destroy();
IupClose();
return 0;
+5 -4
View File
@@ -1,4 +1,5 @@
#include "ui/dialogs.h"
#include "core/lua_config.h"
#include <iup.h>
#include <string.h>
@@ -27,13 +28,13 @@ int custom_input_dialog(const char *title, const char *label_text, char *buffer,
IupSetAttribute(text, "RASTERSIZE", "500x");
IupSetAttribute(text, "NAME", "INPUT_TEXT");
Ihandle *btn_ok = IupButton("确定", NULL);
Ihandle *btn_ok = IupButton(lua_config_get_string("button", "ok"), NULL);
IupSetCallback(btn_ok, "ACTION", on_dialog_ok);
IupSetAttribute(btn_ok, "RASTERSIZE", "100x32");
IupSetAttribute(btn_ok, "RASTERSIZE", lua_config_get_string("button", "rastersize"));
Ihandle *btn_cancel = IupButton("取消", NULL);
Ihandle *btn_cancel = IupButton(lua_config_get_string("button", "cancel"), NULL);
IupSetCallback(btn_cancel, "ACTION", on_dialog_cancel);
IupSetAttribute(btn_cancel, "RASTERSIZE", "100x32");
IupSetAttribute(btn_cancel, "RASTERSIZE", lua_config_get_string("button", "rastersize"));
Ihandle *vbox = IupVbox(
IupLabel(label_text),
+42 -41
View File
@@ -1,6 +1,6 @@
#include "ui/main_window.h"
#include "controller/callbacks.h"
#include "config.h"
#include "core/lua_config.h"
#include <stddef.h>
// 创建路径列表控件
@@ -9,8 +9,8 @@ static Ihandle *create_path_list(const char *name)
Ihandle *list = IupFlatList();
IupSetAttribute(list, "NAME", name);
IupSetAttribute(list, "EXPAND", "YES");
IupSetAttribute(list, "ITEMPADDING", UI_LIST_ITEM_PADDING);
IupSetAttribute(list, "BACKCOLOR", UI_LIST_BACKCOLOR);
IupSetAttribute(list, "ITEMPADDING", lua_config_get_string("list", "item_padding"));
IupSetAttribute(list, "BACKCOLOR", lua_config_get_string("list", "backcolor"));
IupSetAttribute(list, "BORDER", "YES");
IupSetAttribute(list, "CANFOCUS", "YES");
IupSetAttribute(list, "HLINE", "NO");
@@ -21,7 +21,7 @@ static Ihandle *create_path_list(const char *name)
}
// 创建主窗口
Ihandle* create_main_window(void)
Ihandle *create_main_window(void)
{
// 创建系统路径列表
Ihandle *list_sys = create_path_list("LIST_SYS");
@@ -32,7 +32,7 @@ Ihandle* create_main_window(void)
Ihandle *txt_search = IupText(NULL);
IupSetAttribute(txt_search, "NAME", "TXT_SEARCH");
IupSetAttribute(txt_search, "EXPAND", "HORIZONTAL");
IupSetAttribute(txt_search, "CUEBANNER", "输入关键词搜索...");
IupSetAttribute(txt_search, "CUEBANNER", lua_config_get_string("label", "search_placeholder"));
IupSetCallback(txt_search, "VALUECHANGED_CB", (Icallback)txt_search_cb);
// 创建选项卡
@@ -41,24 +41,24 @@ Ihandle* create_main_window(void)
IupVbox(list_user, NULL),
NULL);
IupSetAttribute(tabs_main, "NAME", "TABS_MAIN");
IupSetAttribute(tabs_main, "TABTITLE0", "系统变量 (System)");
IupSetAttribute(tabs_main, "TABTITLE1", "用户变量 (User)");
IupSetAttribute(tabs_main, "TABTITLE0", lua_config_get_string("label", "tab_sys"));
IupSetAttribute(tabs_main, "TABTITLE1", lua_config_get_string("label", "tab_user"));
IupSetAttribute(tabs_main, "TABTYPE", "TOP");
// 创建操作按钮
Ihandle *btn_new = IupButton("新建(N)", NULL);
Ihandle *btn_new = IupButton(lua_config_get_string("button", "new"), NULL);
IupSetAttribute(btn_new, "NAME", "BTN_NEW");
Ihandle *btn_edit = IupButton("编辑(E)", NULL);
Ihandle *btn_edit = IupButton(lua_config_get_string("button", "edit"), NULL);
IupSetAttribute(btn_edit, "NAME", "BTN_EDIT");
Ihandle *btn_browse = IupButton("浏览(B)...", NULL);
Ihandle *btn_browse = IupButton(lua_config_get_string("button", "browse"), NULL);
IupSetAttribute(btn_browse, "NAME", "BTN_BROWSE");
Ihandle *btn_del = IupButton("删除(D)", NULL);
Ihandle *btn_del = IupButton(lua_config_get_string("button", "del"), NULL);
IupSetAttribute(btn_del, "NAME", "BTN_DEL");
Ihandle *btn_up = IupButton("上移(U)", NULL);
Ihandle *btn_up = IupButton(lua_config_get_string("button", "up"), NULL);
IupSetAttribute(btn_up, "NAME", "BTN_UP");
Ihandle *btn_down = IupButton("下移(O)", NULL);
Ihandle *btn_down = IupButton(lua_config_get_string("button", "down"), NULL);
IupSetAttribute(btn_down, "NAME", "BTN_DOWN");
Ihandle *btn_clean = IupButton("一键清理", NULL);
Ihandle *btn_clean = IupButton(lua_config_get_string("button", "clean"), NULL);
IupSetAttribute(btn_clean, "NAME", "BTN_CLEAN");
// 设置按钮回调
@@ -71,13 +71,14 @@ Ihandle* create_main_window(void)
IupSetCallback(btn_clean, "ACTION", (Icallback)btn_clean_cb);
// 设置按钮大小
IupSetAttribute(btn_new, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_edit, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_browse, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_del, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_up, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_down, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_clean, "RASTERSIZE", UI_BTN_RASTERSIZE);
const char *btn_size = lua_config_get_string("button", "rastersize");
IupSetAttribute(btn_new, "RASTERSIZE", btn_size);
IupSetAttribute(btn_edit, "RASTERSIZE", btn_size);
IupSetAttribute(btn_browse, "RASTERSIZE", btn_size);
IupSetAttribute(btn_del, "RASTERSIZE", btn_size);
IupSetAttribute(btn_up, "RASTERSIZE", btn_size);
IupSetAttribute(btn_down, "RASTERSIZE", btn_size);
IupSetAttribute(btn_clean, "RASTERSIZE", btn_size);
// 创建操作按钮垂直布局
Ihandle *vbox_btns = IupVbox(
@@ -87,25 +88,25 @@ Ihandle* create_main_window(void)
IupFill(),
btn_up, btn_down,
NULL);
IupSetAttribute(vbox_btns, "GAP", UI_VBOX_GAP);
IupSetAttribute(vbox_btns, "MARGIN", UI_VBOX_MARGIN);
IupSetAttribute(vbox_btns, "GAP", lua_config_get_string("layout", "vbox_gap"));
IupSetAttribute(vbox_btns, "MARGIN", lua_config_get_string("layout", "vbox_margin"));
// 创建主窗口水平布局
Ihandle *hbox_main = IupHbox(tabs_main, vbox_btns, NULL);
IupSetAttribute(hbox_main, "GAP", UI_HBOX_GAP);
IupSetAttribute(hbox_main, "MARGIN", UI_HBOX_MARGIN);
IupSetAttribute(hbox_main, "GAP", lua_config_get_string("layout", "hbox_gap"));
IupSetAttribute(hbox_main, "MARGIN", lua_config_get_string("layout", "hbox_margin"));
// 创建状态标签
Ihandle *lbl_status = IupLabel("状态: 就绪");
Ihandle *lbl_status = IupLabel(lua_config_get_string("status", "normal"));
IupSetAttribute(lbl_status, "NAME", "LBL_STATUS");
IupSetAttribute(lbl_status, "EXPAND", "HORIZONTAL");
// 创建底部按钮
Ihandle *btn_ok = IupButton("确定", NULL);
Ihandle *btn_ok = IupButton(lua_config_get_string("button", "ok"), NULL);
IupSetAttribute(btn_ok, "NAME", "BTN_OK");
Ihandle *btn_cancel = IupButton("取消", NULL);
Ihandle *btn_cancel = IupButton(lua_config_get_string("button", "cancel"), NULL);
IupSetAttribute(btn_cancel, "NAME", "BTN_CANCEL");
Ihandle *btn_help = IupButton("帮助(?)", NULL);
Ihandle *btn_help = IupButton(lua_config_get_string("button", "help"), NULL);
IupSetAttribute(btn_help, "NAME", "BTN_HELP");
// 设置底部按钮回调
@@ -114,31 +115,31 @@ Ihandle* create_main_window(void)
IupSetCallback(btn_help, "ACTION", (Icallback)btn_help_cb);
// 设置底部按钮大小
IupSetAttribute(btn_ok, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_cancel, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_help, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_ok, "RASTERSIZE", btn_size);
IupSetAttribute(btn_cancel, "RASTERSIZE", btn_size);
IupSetAttribute(btn_help, "RASTERSIZE", btn_size);
// 创建底部按钮水平布局
Ihandle *hbox_bottom = IupHbox(lbl_status, IupFill(), btn_help, btn_ok, btn_cancel, NULL);
IupSetAttribute(hbox_bottom, "GAP", UI_HBOX_GAP);
IupSetAttribute(hbox_bottom, "MARGIN", UI_HBOX_MARGIN);
IupSetAttribute(hbox_bottom, "ALIGNMENT", UI_HBOX_ALIGNMENT);
IupSetAttribute(hbox_bottom, "GAP", lua_config_get_string("layout", "hbox_gap"));
IupSetAttribute(hbox_bottom, "MARGIN", lua_config_get_string("layout", "hbox_margin"));
IupSetAttribute(hbox_bottom, "ALIGNMENT", lua_config_get_string("layout", "hbox_alignment"));
// 创建主窗口垂直布局
Ihandle *vbox_all = IupVbox(
IupLabel("环境变量编辑器:"),
IupLabel(lua_config_get_string("label", "title")),
txt_search,
hbox_main,
hbox_bottom,
NULL);
IupSetAttribute(vbox_all, "MARGIN", UI_VBOX_ALL_MARGIN);
IupSetAttribute(vbox_all, "GAP", UI_VBOX_ALL_GAP);
IupSetAttribute(vbox_all, "MARGIN", lua_config_get_string("layout", "vbox_all_margin"));
IupSetAttribute(vbox_all, "GAP", lua_config_get_string("layout", "vbox_all_gap"));
// 创建主窗口对话框
Ihandle *dlg = IupDialog(vbox_all);
IupSetAttribute(dlg, "TITLE", APP_NAME);
IupSetAttribute(dlg, "RASTERSIZE", UI_DLG_SIZE);
IupSetAttribute(dlg, "MINSIZE", UI_DLG_MINSIZE);
IupSetAttribute(dlg, "TITLE", lua_config_get_string("app", "name"));
IupSetAttribute(dlg, "RASTERSIZE", lua_config_get_string("dialog", "size"));
IupSetAttribute(dlg, "MINSIZE", lua_config_get_string("dialog", "minsize"));
IupSetAttribute(dlg, "MINBOX", "NO");
IupSetAttribute(dlg, "MAXBOX", "NO");