refactor: 重构代码以提取配置和全局变量

- 将 Windows 消息常量和 UI 配置常量分别提取到 globals.h 和 config.h 头文件中,提高可维护性
- 将全局变量和控件定义从 main.c 移至独立的 globals.c 源文件,实现关注点分离
- 更新 Makefile 以包含新的源文件 globals.c
- 在 ui.c 和 main.c 中引用 config.h,使用配置常量替代硬编码的 UI 参数
This commit is contained in:
2026-03-18 21:01:50 +08:00
parent 7db190306c
commit 7fac2aab35
7 changed files with 82 additions and 45 deletions
+14
View File
@@ -0,0 +1,14 @@
#include "globals.h"
#include <windows.h>
#include <iup.h>
// 全局变量定义
StringList raw_sys_paths = {0};
StringList raw_user_paths = {0};
// 全局控件定义
Ihandle *dlg, *tabs_main, *list_sys, *list_user, *lbl_status; // 主窗口、标签页、系统路径列表、用户路径列表、状态标签
Ihandle *btn_new, *btn_edit, *btn_browse, *btn_del, *btn_up, *btn_down; // 右侧按钮
Ihandle *btn_ok, *btn_cancel, *btn_help; // 确认取消帮助按钮
Ihandle *btn_clean; // 一键清理按钮
Ihandle *txt_search; // 搜索框
+6 -25
View File
@@ -8,26 +8,7 @@
#include "registry.h"
#include "callbacks.h"
#include "ui.h"
// 定义 Windows 消息常量
#ifndef WM_COPYGLOBALDATA
#define WM_COPYGLOBALDATA 0x0049
#endif
#ifndef MSGFLT_ADD
#define MSGFLT_ADD 1
#endif
// 全局变量定义
StringList raw_sys_paths = {0};
StringList raw_user_paths = {0};
// 全局控件定义
Ihandle *dlg, *tabs_main, *list_sys, *list_user, *lbl_status; // 主窗口、标签页、系统路径列表、用户路径列表、状态标签
Ihandle *btn_new, *btn_edit, *btn_browse, *btn_del, *btn_up, *btn_down; // 右侧按钮
Ihandle *btn_ok, *btn_cancel, *btn_help; // 确认取消帮助按钮
Ihandle *btn_clean; // 一键清理按钮
Ihandle *txt_search; // 搜索框
#include "config.h"
// 主函数
int main(int argc, char **argv)
@@ -80,8 +61,8 @@ int main(int argc, char **argv)
// 上部布局:Tabs + 按钮
Ihandle *hbox_main = IupHbox(tabs_main, vbox_btns, NULL);
IupSetAttribute(hbox_main, "GAP", "10");
IupSetAttribute(hbox_main, "MARGIN", "10x10");
IupSetAttribute(hbox_main, "GAP", UI_HBOX_GAP);
IupSetAttribute(hbox_main, "MARGIN", UI_HBOX_MARGIN);
// 状态标签
lbl_status = IupLabel("状态: 就绪");
@@ -97,13 +78,13 @@ int main(int argc, char **argv)
hbox_main,
hbox_bottom,
NULL);
IupSetAttribute(vbox_all, "MARGIN", "10x10");
IupSetAttribute(vbox_all, "GAP", "5");
IupSetAttribute(vbox_all, "MARGIN", UI_VBOX_ALL_MARGIN);
IupSetAttribute(vbox_all, "GAP", UI_VBOX_ALL_GAP);
// 创建对话框
dlg = IupDialog(vbox_all);
IupSetAttribute(dlg, "TITLE", "编辑环境变量 (IUP版)");
IupSetAttribute(dlg, "SIZE", "500x400"); // 稍微调大一点
IupSetAttribute(dlg, "SIZE", UI_DLG_SIZE); // 稍微调大一点
IupSetAttribute(dlg, "MINBOX", "NO");
IupSetAttribute(dlg, "MAXBOX", "NO");
+19 -18
View File
@@ -1,6 +1,7 @@
#include "ui.h"
#include "globals.h"
#include "callbacks.h"
#include "config.h"
#include <stdlib.h>
// 创建列表控件
@@ -8,8 +9,8 @@ Ihandle *create_path_list()
{
Ihandle *list = IupFlatList();
IupSetAttribute(list, "EXPAND", "YES");
IupSetAttribute(list, "ITEMPADDING", "5x5");
IupSetAttribute(list, "BACKCOLOR", "255 255 255");
IupSetAttribute(list, "ITEMPADDING", UI_LIST_ITEM_PADDING);
IupSetAttribute(list, "BACKCOLOR", UI_LIST_BACKCOLOR);
IupSetAttribute(list, "BORDER", "YES");
IupSetAttribute(list, "CANFOCUS", "YES");
IupSetAttribute(list, "HLINE", "NO");
@@ -40,14 +41,14 @@ Ihandle *create_main_buttons()
IupSetCallback(btn_down, "ACTION", (Icallback)btn_down_cb);
IupSetCallback(btn_clean, "ACTION", (Icallback)btn_clean_cb);
// 设置按钮大小 (宽度和高度都增加约1/4)
IupSetAttribute(btn_new, "RASTERSIZE", "100x32");
IupSetAttribute(btn_edit, "RASTERSIZE", "100x32");
IupSetAttribute(btn_browse, "RASTERSIZE", "100x32");
IupSetAttribute(btn_del, "RASTERSIZE", "100x32");
IupSetAttribute(btn_up, "RASTERSIZE", "100x32");
IupSetAttribute(btn_down, "RASTERSIZE", "100x32");
IupSetAttribute(btn_clean, "RASTERSIZE", "100x32");
// 设置按钮大小
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);
Ihandle *vbox_btns = IupVbox(
btn_new, btn_edit, btn_browse, btn_del,
@@ -56,8 +57,8 @@ Ihandle *create_main_buttons()
IupFill(),
btn_up, btn_down,
NULL);
IupSetAttribute(vbox_btns, "GAP", "5");
IupSetAttribute(vbox_btns, "MARGIN", "0x0");
IupSetAttribute(vbox_btns, "GAP", UI_VBOX_GAP);
IupSetAttribute(vbox_btns, "MARGIN", UI_VBOX_MARGIN);
return vbox_btns;
}
@@ -74,14 +75,14 @@ Ihandle *create_bottom_buttons()
IupSetCallback(btn_cancel, "ACTION", (Icallback)btn_cancel_cb);
IupSetCallback(btn_help, "ACTION", (Icallback)btn_help_cb);
IupSetAttribute(btn_ok, "RASTERSIZE", "100x32");
IupSetAttribute(btn_cancel, "RASTERSIZE", "100x32");
IupSetAttribute(btn_help, "RASTERSIZE", "100x32");
IupSetAttribute(btn_ok, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_cancel, "RASTERSIZE", UI_BTN_RASTERSIZE);
IupSetAttribute(btn_help, "RASTERSIZE", UI_BTN_RASTERSIZE);
Ihandle *hbox_bottom = IupHbox(lbl_status, IupFill(), btn_help, btn_ok, btn_cancel, NULL);
IupSetAttribute(hbox_bottom, "GAP", "10");
IupSetAttribute(hbox_bottom, "MARGIN", "10x10");
IupSetAttribute(hbox_bottom, "ALIGNMENT", "ACENTER");
IupSetAttribute(hbox_bottom, "GAP", UI_HBOX_GAP);
IupSetAttribute(hbox_bottom, "MARGIN", UI_HBOX_MARGIN);
IupSetAttribute(hbox_bottom, "ALIGNMENT", UI_HBOX_ALIGNMENT);
return hbox_bottom;
}