Skip to content

Commit 51951c5

Browse files
committed
format
s f
1 parent 53bbe0e commit 51951c5

5 files changed

Lines changed: 105 additions & 95 deletions

File tree

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919
res.set("FileDescription", "Starter for ScreenCapture");
2020
res.set(
2121
"LegalCopyright",
22-
"Copyright (C) Mikachu2333 2025. MIT License.",
22+
"Copyright (C) Mikachu2333 2025-2026. MIT License.",
2323
);
2424

2525
if let Err(e) = res.compile() {

src/config.rs

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
path::{Path, PathBuf},
1717
};
1818
use 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
/// ### 参数

src/file_ops.rs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,14 @@ static RES_CONF: &[u8] = include_bytes!("../res/config.toml");
112112
/// * 如果配置文件不存在,释放配置文件并执行初始化操作
113113
/// * 首次释放配置文件后会自动打开配置文件并提示重启程序
114114
pub fn unzip_res(paths: &PathInfos, exists: &FileExist) {
115-
let screen_capture_res = RES_EXE;
116-
let config_res = RES_CONF;
117-
118115
if (!exists.exe_exist) || (!exists.exe_latest) {
119-
fs::write(&paths.exe_path, screen_capture_res).expect("Error write EXE file.");
116+
fs::write(&paths.exe_path, RES_EXE).expect("Error write EXE file.");
120117
println!("EXE: Release exe file.");
121118
} else {
122119
println!("EXE: No need to release.");
123120
}
124121
if !exists.conf_exist {
125-
fs::write(&paths.conf_path, config_res).expect("Error write config file.");
122+
fs::write(&paths.conf_path, RES_CONF).expect("Error write config file.");
126123
println!("CONF: Release config file.");
127124
operate_exe(&paths.conf_path, "conf", HashMap::new());
128125
operate_exe(Path::new(""), "restart", HashMap::new());
@@ -183,15 +180,13 @@ impl OperateMode for Vec<String> {
183180
/// ### 参数
184181
/// - `path`: 要操作的程序路径
185182
/// - `mode`: 操作模式字符串
186-
/// - `gui`: GUI相关参数的HashMap,包含normal和long模式的参数
183+
/// - `gui`: GUI相关参数(保留参数,用于兼容trait签名)
187184
///
188185
/// ### 操作模式
189186
/// - `pin`: 启动钉图功能,从剪贴板获取图像并钉在屏幕上
190-
/// - `exit`: 退出程序,显示退出消息后终止进程
191187
/// - `conf`: 使用记事本打开配置文件进行编辑
192188
/// - `restart`: 显示重启提示消息并退出程序
193-
/// - 其他参数模式: 执行截屏相关操作,支持按'*'分割的多参数格式
194-
fn execute_string_mode(path: &Path, mode: &str, gui: HashMap<String, String>) {
189+
fn execute_string_mode(path: &Path, mode: &str, _gui: HashMap<String, String>) {
195190
match mode {
196191
"pin" => {
197192
let _ = Command::new(path).arg("--pin:clipboard").spawn();
@@ -213,31 +208,9 @@ fn execute_string_mode(path: &Path, mode: &str, gui: HashMap<String, String>) {
213208
);
214209
std::process::exit(0);
215210
}
216-
parm => {
217-
let default_empty = String::new();
218-
println!("parm: {:?}\narg: {:?}\n", parm, gui.clone());
219-
let gui_arg = if parm.contains("long") {
220-
gui.get("long").unwrap_or(&default_empty)
221-
} else {
222-
gui.get("normal").unwrap_or(&default_empty)
223-
};
224-
if parm.contains('*') {
225-
// 包含多个参数,按'*'分割
226-
let temp = parm.split('*').map(String::from);
227-
let mut cmd = Command::new(path);
228-
cmd.args(temp);
229-
if !gui_arg.is_empty() {
230-
cmd.arg(gui_arg);
231-
}
232-
let _ = cmd.spawn();
233-
} else {
234-
// 单个参数
235-
let mut cmd = Command::new(path);
236-
if !gui_arg.is_empty() {
237-
cmd.arg(gui_arg);
238-
}
239-
let _ = cmd.spawn();
240-
}
211+
_ => {
212+
// 未知模式,忽略
213+
eprintln!("Unknown operate mode: {}", mode);
241214
}
242215
}
243216
}

src/hotkeys.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ pub fn set_hotkeys(
7575

7676
// 为截屏快捷键准备共享状态的克隆
7777
let current_path_sc = current_save_path.clone();
78-
let use_last_path_sc = use_last_path;
79-
let default_save_path_sc = save_path.clone();
8078

8179
// 注册截屏快捷键
8280
let hotkey_sc = hkm.register(
@@ -91,17 +89,6 @@ pub fn set_hotkeys(
9189

9290
let args = crate::file_ops::build_capture_args(comp_val, scale_val, &path_to_use, false);
9391
operate_exe(&exe_path_clone, args, gui_clone.clone());
94-
95-
// 如果启用了 last_path 功能且路径非空,更新配置
96-
if use_last_path_sc && !path_to_use.as_os_str().is_empty() {
97-
// 更新共享状态(如果路径来自默认配置,首次使用后记录)
98-
{
99-
let mut guard = current_path_sc.lock().unwrap();
100-
if guard.as_os_str().is_empty() {
101-
*guard = default_save_path_sc.clone();
102-
}
103-
}
104-
}
10592
},
10693
);
10794
if hotkey_sc.is_err() {
@@ -205,8 +192,6 @@ pub fn set_hotkeys(
205192

206193
// 为截长屏快捷键准备共享状态的克隆
207194
let current_path_scl = current_save_path.clone();
208-
let use_last_path_scl = use_last_path;
209-
let default_save_path_scl = save_path.clone();
210195

211196
// 注册截长屏快捷键
212197
let hotkey_scl = hkm.register(
@@ -221,16 +206,6 @@ pub fn set_hotkeys(
221206

222207
let args = crate::file_ops::build_capture_args(comp_val2, scale_val2, &path_to_use, true);
223208
operate_exe(&exe_path_clone, args, gui_clone.clone());
224-
225-
// 如果启用了 last_path 功能且路径非空,更新配置
226-
if use_last_path_scl && !path_to_use.as_os_str().is_empty() {
227-
{
228-
let mut guard = current_path_scl.lock().unwrap();
229-
if guard.as_os_str().is_empty() {
230-
*guard = default_save_path_scl.clone();
231-
}
232-
}
233-
}
234209
},
235210
);
236211
if hotkey_scl.is_err() {

src/types.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl SettingsCollection {
198198
fn key_str(&self, key: &str) -> String {
199199
self.keys_collection
200200
.get(key)
201-
.map(|v| v.to_string().replace("\"", ""))
201+
.map(|v| v.to_string())
202202
.unwrap()
203203
}
204204

@@ -325,13 +325,9 @@ pub struct HotkeyValue {
325325
}
326326
impl std::fmt::Display for HotkeyValue {
327327
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
328-
let mods_str: Vec<String> = self
329-
.mod_keys
330-
.iter()
331-
.map(|m| format!("{:?}", m.to_string()))
332-
.collect();
328+
let mods_str: Vec<String> = self.mod_keys.iter().map(|m| m.to_string()).collect();
333329

334-
write!(f, "{}@{:?}", mods_str.join("+"), self.vkey.to_string())
330+
write!(f, "{}@{}", mods_str.join("+"), self.vkey)
335331
}
336332
}
337333

0 commit comments

Comments
 (0)