@@ -16,7 +16,7 @@ use std::{
1616 path:: { Path , PathBuf } ,
1717} ;
1818use toml:: Value ;
19- use toml_edit:: { value, DocumentMut } ;
19+ use toml_edit:: { value, DocumentMut , Item , Table } ;
2020
2121/// 读取并解析TOML配置文件
2222///
@@ -114,41 +114,68 @@ fn write_config(conf_path: &PathBuf, settings: &SettingsCollection, original_con
114114 doc[ "gui" ] = toml_edit:: table ( ) ;
115115 }
116116
117- // 写入 hotkey 配置
118- for ( key, hv) in & settings. keys_collection {
119- doc[ "hotkey" ] [ * key] = value ( format_hotkey_for_config ( hv) ) ;
117+ // 写入 hotkey 配置(保留注释)
118+ if let Some ( hotkey_table) = doc. get_mut ( "hotkey" ) . and_then ( |v| v. as_table_mut ( ) ) {
119+ for ( key, hv) in & settings. keys_collection {
120+ update_value_preserve_decor ( hotkey_table, key, format_hotkey_for_config ( hv) ) ;
121+ }
120122 }
121123
122- // 写入 path 配置
123- let save_path_str = if settings. path . save_path . as_os_str ( ) . is_empty ( ) {
124- "&" . to_string ( )
125- } else {
126- normalize_path_for_config ( & settings. path . save_path )
127- } ;
128- doc[ "path" ] [ "dir" ] = value ( save_path_str) ;
129- doc[ "path" ] [ "launch_app_path" ] =
130- value ( normalize_path_for_config ( & settings. path . launch_app . path ) ) ;
131- doc[ "path" ] [ "launch_app_args" ] = value ( settings. path . launch_app . args . join ( "\t " ) ) ;
132- doc[ "path" ] [ "last_path" ] = value ( settings. path . last_path ) ;
133-
134- // 写入 sundry 配置
135- doc[ "sundry" ] [ "startup" ] = value ( settings. sundry . auto_start ) ;
136- doc[ "sundry" ] [ "comp_level" ] = value ( settings. sundry . comp_level as i64 ) ;
137- doc[ "sundry" ] [ "scale_ratio" ] = value ( settings. sundry . scale_level as i64 ) ;
138- doc[ "sundry" ] [ "lang" ] = value ( if settings. sundry . lang { 1i64 } else { 0i64 } ) ;
139-
140- // 写入 gui 配置
124+ // 写入 path 配置(保留注释)
125+ if let Some ( path_table) = doc. get_mut ( "path" ) . and_then ( |v| v. as_table_mut ( ) ) {
126+ let save_path_str = if settings. path . save_path . as_os_str ( ) . is_empty ( ) {
127+ "&" . to_string ( )
128+ } else {
129+ normalize_path_for_config ( & settings. path . save_path )
130+ } ;
131+ update_value_preserve_decor ( path_table, "dir" , save_path_str) ;
132+ update_value_preserve_decor (
133+ path_table,
134+ "launch_app_path" ,
135+ normalize_path_for_config ( & settings. path . launch_app . path ) ,
136+ ) ;
137+ update_value_preserve_decor (
138+ path_table,
139+ "launch_app_args" ,
140+ settings. path . launch_app . args . join ( "\t " ) ,
141+ ) ;
142+ update_bool_preserve_decor ( path_table, "last_path" , settings. path . last_path ) ;
143+ }
144+
145+ // 写入 sundry 配置(保留注释)
146+ if let Some ( sundry_table) = doc. get_mut ( "sundry" ) . and_then ( |v| v. as_table_mut ( ) ) {
147+ update_bool_preserve_decor ( sundry_table, "startup" , settings. sundry . auto_start ) ;
148+ update_int_preserve_decor (
149+ sundry_table,
150+ "comp_level" ,
151+ settings. sundry . comp_level as i64 ,
152+ ) ;
153+ update_int_preserve_decor (
154+ sundry_table,
155+ "scale_ratio" ,
156+ settings. sundry . scale_level as i64 ,
157+ ) ;
158+ update_int_preserve_decor (
159+ sundry_table,
160+ "lang" ,
161+ if settings. sundry . lang { 1i64 } else { 0i64 } ,
162+ ) ;
163+ }
164+
165+ // 写入 gui 配置(保留注释)
141166 // 从 "--tool:\"xxx\"" 格式中提取原始值
142167 let extract_gui_value = |s : & str | -> String {
143168 s. trim_start_matches ( "--tool:\" " )
144169 . trim_end_matches ( '"' )
145170 . to_string ( )
146171 } ;
147- if let Some ( normal) = settings. gui . get ( "normal" ) {
148- doc[ "gui" ] [ "gui_config" ] = value ( extract_gui_value ( normal) ) ;
149- }
150- if let Some ( long) = settings. gui . get ( "long" ) {
151- doc[ "gui" ] [ "long_gui_config" ] = value ( extract_gui_value ( long) ) ;
172+ if let Some ( gui_table) = doc. get_mut ( "gui" ) . and_then ( |v| v. as_table_mut ( ) ) {
173+ if let Some ( normal) = settings. gui . get ( "normal" ) {
174+ update_value_preserve_decor ( gui_table, "gui_config" , extract_gui_value ( normal) ) ;
175+ }
176+ if let Some ( long) = settings. gui . get ( "long" ) {
177+ update_value_preserve_decor ( gui_table, "long_gui_config" , extract_gui_value ( long) ) ;
178+ }
152179 }
153180
154181 // 写回文件
@@ -157,6 +184,45 @@ fn write_config(conf_path: &PathBuf, settings: &SettingsCollection, original_con
157184 }
158185}
159186
187+ /// 更新表中的字符串值,保留原有的注释装饰
188+ fn update_value_preserve_decor ( table : & mut Table , key : & str , new_value : String ) {
189+ if let Some ( Item :: Value ( v) ) = table. get_mut ( key) {
190+ // 保存原有的装饰(注释)
191+ let decor = v. decor ( ) . clone ( ) ;
192+ // 创建新值并应用装饰
193+ let mut new_val = toml_edit:: Value :: from ( new_value) ;
194+ * new_val. decor_mut ( ) = decor;
195+ * v = new_val;
196+ return ;
197+ }
198+ // 键不存在,直接插入新值
199+ table[ key] = value ( new_value) ;
200+ }
201+
202+ /// 更新表中的布尔值,保留原有的注释装饰
203+ fn update_bool_preserve_decor ( table : & mut Table , key : & str , new_value : bool ) {
204+ if let Some ( Item :: Value ( v) ) = table. get_mut ( key) {
205+ let decor = v. decor ( ) . clone ( ) ;
206+ let mut new_val = toml_edit:: Value :: from ( new_value) ;
207+ * new_val. decor_mut ( ) = decor;
208+ * v = new_val;
209+ return ;
210+ }
211+ table[ key] = value ( new_value) ;
212+ }
213+
214+ /// 更新表中的整数值,保留原有的注释装饰
215+ fn update_int_preserve_decor ( table : & mut Table , key : & str , new_value : i64 ) {
216+ if let Some ( Item :: Value ( v) ) = table. get_mut ( key) {
217+ let decor = v. decor ( ) . clone ( ) ;
218+ let mut new_val = toml_edit:: Value :: from ( new_value) ;
219+ * new_val. decor_mut ( ) = decor;
220+ * v = new_val;
221+ return ;
222+ }
223+ table[ key] = value ( new_value) ;
224+ }
225+
160226/// 将 HotkeyValue 格式化为配置文件格式
161227///
162228/// ### 参数
0 commit comments