Skip to content

Commit e782503

Browse files
committed
update
1 parent a6f2edd commit e782503

7 files changed

Lines changed: 85 additions & 137 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "SC_Starter"
3-
version = "2.10.0"
3+
version = "2.10.2"
44
# 记得更新 types.rs 中 res的版本
55
edition = "2021"
66
build = "build.rs"
@@ -24,8 +24,8 @@ tauri-winres = "0.3.5"
2424
directories = "6.0.0"
2525
single-instance = "0.3.3"
2626
windows-hotkeys = "0.2.1"
27-
toml = "0.9.8"
28-
toml_edit = "0.23.9"
27+
toml = "1.1.0+spec-1.1.0"
28+
toml_edit = "0.25.8+spec-1.1.0"
2929
tray-icon = "0.21.3"
3030
tao = "0.35.0"
3131
mslnk = "0.1.8"

res/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ scale_ratio = 100
9696
# Language, Chinese is 1, English is 0
9797
lang = 1
9898

99+
# 通知是否开启
100+
# Whether notifications are enabled
101+
# true->启用, false->禁用
102+
notification = false
103+
99104
[gui]
100105
# GUI配置,默认全部启用
101106
# rect:方框

src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,18 @@ fn get_sundry_settings(default: Sundry, config: &Value) -> Sundry {
264264
.and_then(|t| t.get("lang"))
265265
.and_then(|v| v.as_integer())
266266
.unwrap_or(-1);
267+
268+
let notification_bool = sundry_section
269+
.and_then(|t| t.get("notification"))
270+
.and_then(|v| v.as_bool())
271+
.unwrap_or(default.notification);
272+
267273
Sundry {
268274
auto_start: startup_bool,
269275
comp_level: comp,
270276
scale_level: scale,
271277
lang: lang_code == 1,
278+
notification: notification_bool,
272279
}
273280
}
274281

src/file_ops.rs

Lines changed: 44 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ pub fn check_res_exist(infos: &PathInfos) -> FileExist {
5151

5252
files_exist.exe_exist = infos.exe_path.exists();
5353
files_exist.conf_exist = infos.conf_path.exists();
54+
files_exist.conf_example_exist = infos.conf_example_path.exists();
5455

5556
if files_exist.exe_exist {
56-
files_exist.exe_latest = check_exe_latest(&infos.exe_path);
57+
files_exist.exe_latest = check_latest(&infos.exe_path);
5758
}
5859
files_exist
5960
}
@@ -70,7 +71,7 @@ pub fn check_res_exist(infos: &PathInfos) -> FileExist {
7071
/// - 使用Windows系统的certutil工具计算哈希值
7172
/// - 通过CREATE_NO_WINDOW标志隐藏命令行窗口
7273
/// - 将计算结果与内置的RES_HASH常量进行比较
73-
fn check_exe_latest(file_path: &Path) -> bool {
74+
fn check_latest(file_path: &Path) -> bool {
7475
let hash = match std::process::Command::new("certutil")
7576
.arg("-hashfile")
7677
.arg(file_path)
@@ -121,135 +122,60 @@ pub fn unzip_res(paths: &PathInfos, exists: &FileExist) {
121122
if !exists.conf_exist {
122123
fs::write(&paths.conf_path, RES_CONF).expect("Error write config file.");
123124
println!("CONF: Release config file.");
124-
operate_exe(&paths.conf_path, "conf", HashMap::new());
125-
operate_exe(Path::new(""), "restart", HashMap::new());
125+
open_config(&paths.conf_path);
126+
restart_prompt();
126127
} else {
127128
println!("CONF: No need to release.");
128129
}
129-
}
130-
131-
/// 程序操作控制函数
132-
///
133-
/// ### 参数
134-
/// - `path`: 要操作的程序路径
135-
/// - `mode`: 操作模式,可以是字符串或字符串向量
136-
/// - `gui`: GUI相关参数的HashMap,包含normal和long模式的参数
137-
///
138-
/// ### 操作模式
139-
/// - 字符串模式:
140-
/// - `pin`: 启动钉图功能,从剪贴板获取图像并钉在屏幕上
141-
/// - `exit`: 退出程序,显示退出消息后终止进程
142-
/// - `conf`: 使用记事本打开配置文件进行编辑
143-
/// - `restart`: 显示重启提示消息并退出程序
144-
/// - 其他包含参数的模式: 执行截屏相关操作
145-
/// - 向量模式: 直接将向量中的所有参数传递给程序
146-
pub fn operate_exe<T>(path: &Path, mode: T, gui: HashMap<String, String>)
147-
where
148-
T: OperateMode,
149-
{
150-
mode.execute(path, gui);
151-
pause(1);
152-
}
153-
154-
/// 操作模式trait,定义不同类型的执行方式
155-
///
156-
/// ### 说明
157-
/// - 为 `&str` 实现时执行字符串模式逻辑(向后兼容原有功能)
158-
/// - 为 `Vec<String>` 实现时执行向量模式逻辑(直接传递参数列表)
159-
/// - 允许 `operate_exe` 函数接受不同类型的模式参数
160-
pub trait OperateMode {
161-
fn execute(self, path: &Path, gui: HashMap<String, String>);
162-
}
163-
164-
/// 为字符串实现操作模式
165-
impl OperateMode for &str {
166-
fn execute(self, path: &Path, gui: HashMap<String, String>) {
167-
execute_string_mode(path, self, gui);
130+
if !exists.conf_example_exist {
131+
fs::write(&paths.conf_example_path, RES_CONF).expect("Error write config file.");
132+
println!("CONF: Release config example file.");
133+
} else {
134+
println!("CONF: No need to release.");
168135
}
169136
}
170137

171-
/// 为字符串向量实现操作模式
172-
impl OperateMode for Vec<String> {
173-
fn execute(self, path: &Path, gui: HashMap<String, String>) {
174-
execute_vector_mode(path, self, gui);
175-
}
138+
/// 使用记事本打开配置文件
139+
/// - `path`: 配置文件路径
140+
pub fn open_config(path: &Path) {
141+
match Command::new("notepad.exe").arg(path).spawn() {
142+
Ok(_) => (),
143+
Err(_) => {
144+
error_msgbox("Error to open the config file with notepad.", "", 0);
145+
}
146+
};
176147
}
177148

178-
/// 执行字符串模式的内部函数
179-
///
180-
/// ### 参数
181-
/// - `path`: 要操作的程序路径
182-
/// - `mode`: 操作模式字符串
183-
/// - `gui`: GUI相关参数(保留参数,用于兼容trait签名)
184-
///
185-
/// ### 操作模式
186-
/// - `pin`: 启动钉图功能,从剪贴板获取图像并钉在屏幕上
187-
/// - `conf`: 使用记事本打开配置文件进行编辑
188-
/// - `restart`: 显示重启提示消息并退出程序
189-
fn execute_string_mode(path: &Path, mode: &str, gui: HashMap<String, String>) {
190-
match mode {
191-
"pin" => {
192-
let _ = Command::new(path).arg("--pin:clipboard").spawn();
193-
}
194-
"conf" => {
195-
match Command::new("notepad.exe").arg(path).spawn() {
196-
Ok(_) => (),
197-
Err(_) => {
198-
error_msgbox("Error to open the config file with notepad.", "", 0);
199-
}
200-
};
201-
}
202-
"restart" => {
203-
pause(3);
204-
info_msgbox(
205-
"Please restart the program to apply your custom settings.",
206-
"Restart",
207-
5,
208-
);
209-
std::process::exit(0);
210-
}
211-
parm => {
212-
let default_empty = String::new();
213-
println!("parm: {:?}\narg: {:?}\n", parm, gui.clone());
214-
let gui_arg = if parm.contains("long") {
215-
gui.get("long").unwrap_or(&default_empty)
216-
} else {
217-
gui.get("normal").unwrap_or(&default_empty)
218-
};
219-
if parm.contains('*') {
220-
// 包含多个参数,按'*'分割
221-
let temp = parm.split('*').map(String::from);
222-
let mut cmd = Command::new(path);
223-
cmd.args(temp);
224-
if !gui_arg.is_empty() {
225-
cmd.arg(gui_arg);
226-
}
227-
let _ = cmd.spawn();
228-
} else {
229-
// 单个参数
230-
let mut cmd = Command::new(path);
231-
if !gui_arg.is_empty() {
232-
cmd.arg(gui_arg);
233-
}
234-
let _ = cmd.spawn();
235-
}
236-
}
237-
}
149+
/// 显示重启提示并退出程序
150+
pub fn restart_prompt() {
151+
pause(3);
152+
info_msgbox(
153+
"Please restart the program to apply your custom settings.",
154+
"Restart",
155+
5,
156+
);
157+
std::process::exit(0);
238158
}
239159

240-
/// 执行向量模式的内部函数
160+
/// 程序操作控制函数
241161
///
242162
/// ### 参数
243163
/// - `path`: 要执行的程序路径
244164
/// - `args`: 参数向量,包含所有命令行参数
245165
/// - `gui`: GUI相关参数的HashMap,包含normal和long模式的参数
166+
/// - `notification`: 是否显示通知
246167
///
247168
/// ### 功能
248169
/// - 直接将向量中的所有参数传递给程序
249170
/// - 根据参数中是否包含"long"选择对应的GUI参数
250171
/// - 自动添加GUI参数(如果非空)
251172
/// - 异步启动程序,不阻塞主线程
252-
fn execute_vector_mode(path: &Path, args: Vec<String>, gui: HashMap<String, String>) {
173+
pub fn execute_process(
174+
path: &Path,
175+
args: Vec<String>,
176+
gui: HashMap<String, String>,
177+
notification: bool,
178+
) {
253179
println!("args: {:?}\n", &args);
254180

255181
let default_empty = String::new();
@@ -286,18 +212,12 @@ fn execute_vector_mode(path: &Path, args: Vec<String>, gui: HashMap<String, Stri
286212
Ok(status) => {
287213
if let Some(code) = status.code() {
288214
println!("Exit code: {}", code);
289-
if code == 8 {
290-
notify_msgbox_standalone(
291-
"SC_Starter",
292-
"已保存到文件",
293-
1500,
294-
);
295-
} else if code == 9 {
296-
notify_msgbox_standalone(
297-
"SC_Starter",
298-
"已保存到剪贴板",
299-
1500,
300-
);
215+
if notification {
216+
if code == 8 {
217+
notify_msgbox_standalone("SC_Starter", "已保存到文件", 1500);
218+
} else if code == 9 {
219+
notify_msgbox_standalone("SC_Starter", "已保存到剪贴板", 1500);
220+
}
301221
}
302222
}
303223
}
@@ -342,10 +262,11 @@ pub fn spawn_capture(
342262
exe_path: &std::path::Path,
343263
args: Vec<String>,
344264
gui: std::collections::HashMap<String, String>,
265+
notification: bool,
345266
) {
346267
let exe = exe_path.to_path_buf();
347268
std::thread::spawn(move || {
348-
operate_exe(&exe, args, gui);
269+
execute_process(&exe, args, gui, notification);
349270
});
350271
}
351272

src/hotkeys.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
99
use crate::types::*;
1010
use crate::window_handle::{is_process_running, set_window_topmost_by_pid};
11-
use crate::{file_ops::operate_exe, msgbox::error_msgbox};
11+
use crate::{file_ops::{execute_process, open_config}, msgbox::error_msgbox};
1212
use std::{
13-
collections::HashMap,
1413
sync::{
1514
atomic::{AtomicBool, Ordering},
1615
mpsc, Arc, Mutex,
@@ -55,6 +54,7 @@ pub fn set_hotkeys(
5554

5655
let comp_val = settings_collected.sundry.comp_level;
5756
let scale_val = settings_collected.sundry.scale_level;
57+
let notification = settings_collected.sundry.notification;
5858
let exe_path_clone = exe_path.clone();
5959
let save_path_clone = save_path.clone();
6060
let gui_clone = gui.clone();
@@ -65,7 +65,7 @@ pub fn set_hotkeys(
6565
&key_groups.get("screen_capture").unwrap().mod_keys,
6666
move || {
6767
let args = crate::file_ops::build_capture_args(comp_val, scale_val, &save_path_clone, false);
68-
operate_exe(&exe_path_clone, args, gui_clone.clone());
68+
execute_process(&exe_path_clone, args, gui_clone.clone(), notification);
6969
},
7070
);
7171
if hotkey_sc.is_err() {
@@ -164,6 +164,7 @@ pub fn set_hotkeys(
164164

165165
let comp_val2 = settings_collected.sundry.comp_level;
166166
let scale_val2 = settings_collected.sundry.scale_level;
167+
let notification2 = settings_collected.sundry.notification;
167168
let exe_path_clone = exe_path.clone();
168169
let gui_clone = gui.clone();
169170
let save_path_clone = save_path.clone();
@@ -174,7 +175,7 @@ pub fn set_hotkeys(
174175
&key_groups.get("screen_capture_long").unwrap().mod_keys,
175176
move || {
176177
let args = crate::file_ops::build_capture_args(comp_val2, scale_val2, &save_path_clone, true);
177-
operate_exe(&exe_path_clone, args, gui_clone.clone());
178+
execute_process(&exe_path_clone, args, gui_clone.clone(), notification2);
178179
},
179180
);
180181
if hotkey_scl.is_err() {
@@ -191,7 +192,7 @@ pub fn set_hotkeys(
191192
key_groups.get("pin_to_screen").unwrap().vkey,
192193
&key_groups.get("pin_to_screen").unwrap().mod_keys,
193194
move || {
194-
operate_exe(&exe_path_clone, "pin", gui_clone.clone());
195+
execute_process(&exe_path_clone, vec!["--pin:clipboard".to_string()], gui_clone.clone(), false);
195196
},
196197
);
197198
if hotkey_pin.is_err() {
@@ -204,7 +205,7 @@ pub fn set_hotkeys(
204205
let hotkey_conf = hkm.register(
205206
key_groups.get("open_conf").unwrap().vkey,
206207
&key_groups.get("open_conf").unwrap().mod_keys,
207-
move || operate_exe(&conf_path, "conf", HashMap::new()),
208+
move || open_config(&conf_path),
208209
);
209210
if hotkey_conf.is_err() {
210211
let temp = "Failed reg Hotkey conf.";

0 commit comments

Comments
 (0)