@@ -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: {:?}\n arg: {:?}\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
0 commit comments