新增预设管理、映射备注及托盘稳定性修复v2#3
Open
qq1229037592 wants to merge 6 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
用AI写的代码,修正了体验。
本次 PR 为 Sorahk v0.5.0 新增三项功能:
(1) 预设管理系统——支持保存/加载/删除/重命名/一键切换按键映射预设;
(2) 映射备注编辑——每个按键映射可添加备注说明,在设置面板编辑、主界面查看;
(3) 托盘稳定性修复——修复 Win11 上托盘图标不显示、最小化到托盘后窗口无法恢复的兼容性问题,覆盖 Win10/Win11。
以下是移除所有 emoji 后的版本:
具体改动
src/config.rs— 数据模型改动点 1:新增
Preset结构体(name+mappings),在AppConfig中新增presets: Vec<Preset>和current_preset: String字段。默认创建一个名称为"无"的空映射预设。改动原因:用户可在不同游戏/场景之间一键切换映射配置,无需手动重配。预设数据与当前映射数据完全独立序列化。
改动点 2:
KeyMapping结构体新增note: String字段(带#[serde(default)])。改动原因:允许用户为每个映射添加文字备注(如"BOSS战用"、"仅限法师职业")。
改动点 3:
save_to_file()增加预设和备注的 TOML 序列化。空映射预设显式写入mappings = [];输出note和current_preset。改动原因:手工拼接 TOML 必须保证所有反序列化必需字段有输出。空映射预设若不显式写
mappings = [],重启后反序列化会报missing field 'mappings'(toml 0.9.x 的array-of-tables不触发#[serde(default)])。src/tray.rs— 系统托盘改动点 1:
cbSize从size_of::<NOTIFYICONDATAW>()改为offset_of!(NOTIFYICONDATAW, guidItem)(V2 尺寸)。改动原因:全尺寸
NOTIFYICONDATAW在 Win11 某些配置上导致NIM_ADD静默失败,托盘图标不显示。V2 尺寸(Vista+)全版本兼容。改动点 2:移除所有 3 处
NIF_SHOWTIP标志(初始uFlags、update_tooltip、show_legacy_notification)。改动原因:
NIF_SHOWTIP是 Win11 专属标志,与 V2 cbSize 不兼容,保留会导致托盘操作失败。改动点 3:新增
restore_main_window()静态函数——通过FindWindowW按窗口标题查找 →ShowWindow(SW_SHOW)→SetForegroundWindow。在托盘双击(WM_LBUTTONDBLCLK)和右键菜单"显示窗口"两个入口调用。改动原因:eframe/winit 的事件循环在窗口隐藏/最小化后可能被 Win11 节流,导致
ViewportCommand::Minimized(false)路径失效。直接 Win32 API 调用绕过 eframe 事件循环限制,确保恢复窗口可靠执行。改动点 4:
NIM_ADD返回值从let _ =改为检查并eprintln!警告。改动原因:原代码静默吞掉托盘创建失败,故障无法排查。
src/gui/main_window.rs— 主窗口改动点 1:标题栏增加预设快速切换
ComboBox。切换时自动save_to_file+reload_config+ 同步temp_config。切到"无"时同步清空current_preset和mappings。改动原因:主界面一键切换预设,无需进入设置面板。
改动点 2:引入 Win32 API 导入(
FindWindowW、ShowWindow、SetForegroundWindow)。改动点 3:关闭对话框"最小化到托盘"按钮用
ShowWindow(SW_HIDE)直接隐藏窗口,替换原ViewportCommand::Minimized(true)。改动原因:
SW_HIDE隐藏窗口但不修改 eframe 内部"期望可见"状态,事件循环保持正常运行。Minimized(true)会触发 winit 标记窗口为最小化,Win11 下事件循环被节流。改动点 4:
check_and_clear_show_window_request处理中增加直接 Win32 恢复调用。改动原因:双保险机制——eframe ViewportCommand 先尝试恢复,Win32
ShowWindow(SW_SHOW)作为兜底。改动点 5:映射卡片中增加备注行显示(
note_label()+mapping.note)。改动原因:主界面快速查看映射备注。
src/gui/settings_dialog/— 设置面板(4 个文件)改动点 1(
general.rs):在切换键设置与全局配置之间新增"预设管理"卡片——预设下拉选择器、保存按钮(带名称输入)、删除/重命名按钮。切"无"或删除预设时同步清空mappings。改动原因:集中管理预设的增删改查。
改动点 2(
mapping_list.rs):每个现有映射行末尾增加备注TextEdit,直接绑定mapping.note。改动原因:在设置面板编辑已有映射的备注。
改动点 3(
mapping_editor.rs):新增映射构造时保存note: self.new_mapping_note.clone(),添加成功后清理 note 字段。改动原因:新建映射时同步保存备注。
改动点 4(
mod.rs):保存逻辑增加"更新当前活动预设的映射"步骤,取消/保存清理逻辑增加 preset 和 note 字段清空。改动原因:确保预设数据与映射变更保持同步,防止脏数据残留。
src/gui/mod.rs— GUI 状态改动点:
SorahkGui结构体新增new_mapping_note、show_preset_name_input、preset_name_input、preset_rename_target、preset_rename_input字段及构造函数初始化。改动原因:支持备注编辑和预设管理的 UI 状态。
src/i18n/— 国际化(8 个文件)改动点:新增 8 个翻译键(
PresetTitle、PresetSaveBtn、PresetDeleteBtn、PresetRenameBtn、PresetNameHint、NoPreset、NoteLabel、NoteHint),覆盖英文/简中/繁中/日文/韩文 5 种语言。涉及mod.rs(结构体+初始化)、raw_key.rs(枚举)、accessors.rs(访问器)、5 个语言文件(翻译字符串)。改动原因:支持预设管理和备注功能的国际化。
src/main.rs— 入口改动点:
TrayIcon::new()从.expect()改为match错误处理(eprintln!记录错误日志)。改动原因:托盘初始化失败时优雅降级而非 panic,提高程序健壮性。
测试验证
如何验证:
cargo check和cargo build --release零错误通过[[presets]]current_preset清空current_preset同步missing field 'mappings'潜在风险/回滚方案
风险点:
restore_main_window()通过FindWindowW(None, "Sorahk - Auto Key Press Tool")按窗口标题查找。若系统存在同名窗口,可能恢复错误目标。概率极低。ShowWindow(SW_HIDE)绕过 eframe 的 ViewportCommand 机制,winit 内部状态可能与实际窗口状态不一致。若未来 eframe/winit 版本增加对隐藏窗口的自动恢复逻辑,需重新适配。回滚方案:如需回滚,恢复以下 21 个文件到 v0.5.0 原版:
src/config.rs、src/main.rs、src/tray.rs、src/gui/mod.rs、src/gui/main_window.rs、src/i18n/*(8个文件)、src/gui/settings_dialog/*(4个文件)。相关历史教训(防重复犯错)