11import json
22from pathlib import Path
33from tkinter import Frame , StringVar
4+ from typing import Callable
45
56import customtkinter as ctk
67import i18n
@@ -105,14 +106,12 @@ def save(self):
105106 with open (path , "w" , encoding = "utf-8" ) as f :
106107 f .write (json .dumps (self .data .to_dict ()))
107108
108- def failed_save (self ):
109- path = DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" )
110- path .unlink ()
109+ def save_handler (self , fn : Callable [[], None ]):
110+ pass
111111
112112 @error_toast
113113 def bypass_callback (self ):
114- self .save ()
115- try :
114+ def fn ():
116115 task = Schtasks (self .filename .get ())
117116 if task .check ():
118117 task .set ()
@@ -121,53 +120,52 @@ def bypass_callback(self):
121120 except Exception :
122121 name , icon = self .filename .get (), None
123122 self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
124-
125123 sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
126124 args = ["/run" , "/tn" , task .name ]
127125 Shortcut ().create (sorce = sorce , target = Env .SCHTASKS , args = args , icon = icon )
128126 self .toast .info (i18n .t ("app.shortcut.save_success" ))
129- except Exception :
130- self .failed_save ()
131- raise
127+
128+ self .save_handler (fn )
132129
133130 @error_toast
134131 def uac_callback (self ):
135- self .save ()
136- try :
132+ def fn ():
137133 try :
138- name , icon , admin = self .get_game_info ()
134+ try :
135+ name , icon , admin = self .get_game_info ()
136+ except Exception :
137+ name , icon = self .filename .get (), None
138+ self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
139+ sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
140+ args = [self .filename .get ()]
141+ Shortcut ().create (sorce = sorce , args = args , icon = icon )
142+ self .toast .info (i18n .t ("app.shortcut.save_success" ))
139143 except Exception :
140- name , icon = self .filename .get (), None
141- self . toast . error ( i18n . t ( "app.shortcut.game_info_error" ))
144+ DataPathConfig . SHORTCUT . joinpath ( self .filename .get ()). with_suffix ( ".json" ). unlink ()
145+ raise
142146
143- sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
144- args = [self .filename .get ()]
145- Shortcut ().create (sorce = sorce , args = args , icon = icon )
146- self .toast .info (i18n .t ("app.shortcut.save_success" ))
147- except Exception :
148- self .failed_save ()
149- raise
147+ self .save_handler (fn )
150148
151149 @error_toast
152150 def save_callback (self ):
153- self .save ()
154- try :
151+ def fn ():
155152 try :
156- name , icon , admin = self .get_game_info ()
153+ try :
154+ name , icon , admin = self .get_game_info ()
155+ except Exception :
156+ name , icon , admin = self .filename .get (), None , False
157+ self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
158+ if admin :
159+ raise Exception (i18n .t ("app.shortcut.administrator_error" ))
157160 except Exception :
158- name , icon , admin = self .filename .get (), None , False
159- self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
160-
161- if admin :
162- raise Exception (i18n .t ("app.shortcut.administrator_error" ))
163-
161+ DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
162+ raise
164163 sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
165164 args = [self .filename .get ()]
166165 Shortcut ().create (sorce = sorce , args = args , icon = icon )
167166 self .toast .info (i18n .t ("app.shortcut.save_success" ))
168- except Exception :
169- self .failed_save ()
170- raise
167+
168+ self .save_handler (fn )
171169
172170 @error_toast
173171 def save_only_callback (self ):
@@ -207,6 +205,10 @@ def create(self):
207205 super ().create ()
208206 return self
209207
208+ def save_handler (self , fn : Callable [[], None ]):
209+ self .save ()
210+ fn ()
211+
210212
211213class ShortcutEdit (ShortcutBase ):
212214 selected : StringVar
@@ -231,23 +233,23 @@ def create(self):
231233
232234 return self
233235
234- def save (self ):
235- selected = DataPathConfig .SHORTCUT .joinpath (self .selected .get ()). with_suffix ( ".json" )
236- selected .rename (selected .with_suffix (".json.bak" ))
236+ def save_handler (self , fn : Callable [[], None ] ):
237+ selected = DataPathConfig .SHORTCUT .joinpath (self .selected .get ())
238+ selected .with_suffix ( ".json" ). rename (selected .with_suffix (".json.bak" ))
237239 try :
238- super ().save ()
239- selected .with_suffix (".json.bak" ).unlink ()
240- self .values .remove (self .selected .get ())
241- self .values .append (self .filename .get ())
242- self .selected .set (self .filename .get ())
243- self .option_callback ("_" )
240+ self .save ()
241+ try :
242+ fn ()
243+ except Exception :
244+ DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
244245 except Exception :
245- self . failed_save ( )
246+ selected . with_suffix ( ".json.bak" ). rename ( selected . with_suffix ( ".json" ) )
246247 raise
247-
248- def failed_save (self ):
249- selected = DataPathConfig .SHORTCUT .joinpath (self .selected .get ()).with_suffix (".json" )
250- selected .with_suffix (".json.bak" ).rename (selected .with_suffix (".json" ))
248+ selected .with_suffix (".json.bak" ).unlink ()
249+ self .values .remove (self .selected .get ())
250+ self .values .append (self .filename .get ())
251+ self .selected .set (self .filename .get ())
252+ self .option_callback ("_" )
251253
252254 @error_toast
253255 def delete_callback (self ):
0 commit comments