11import json
22from pathlib import Path
33from tkinter import Frame , StringVar
4+ from typing import Callable
45
56import customtkinter as ctk
67import i18n
@@ -105,10 +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
109+ def save_handler (self , fn : Callable [[], None ]):
110+ pass
111+
108112 @error_toast
109113 def bypass_callback (self ):
110- self .save ()
111- try :
114+ def fn ():
112115 task = Schtasks (self .filename .get ())
113116 if task .check ():
114117 task .set ()
@@ -117,53 +120,52 @@ def bypass_callback(self):
117120 except Exception :
118121 name , icon = self .filename .get (), None
119122 self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
120-
121123 sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
122124 args = ["/run" , "/tn" , task .name ]
123125 Shortcut ().create (sorce = sorce , target = Env .SCHTASKS , args = args , icon = icon )
124126 self .toast .info (i18n .t ("app.shortcut.save_success" ))
125- except Exception :
126- DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
127- raise
127+
128+ self .save_handler (fn )
128129
129130 @error_toast
130131 def uac_callback (self ):
131- self .save ()
132- try :
132+ def fn ():
133133 try :
134- 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" ))
135143 except Exception :
136- name , icon = self .filename .get (), None
137- self . toast . error ( i18n . t ( "app.shortcut.game_info_error" ))
144+ DataPathConfig . SHORTCUT . joinpath ( self .filename .get ()). with_suffix ( ".json" ). unlink ()
145+ raise
138146
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" ))
143- except Exception :
144- DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
145- raise
147+ self .save_handler (fn )
146148
147149 @error_toast
148150 def save_callback (self ):
149- self .save ()
150- try :
151+ def fn ():
151152 try :
152- 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" ))
153160 except Exception :
154- name , icon , admin = self .filename .get (), None , False
155- self .toast .error (i18n .t ("app.shortcut.game_info_error" ))
156-
157- if admin :
158- raise Exception (i18n .t ("app.shortcut.administrator_error" ))
159-
161+ DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
162+ raise
160163 sorce = Env .DESKTOP .joinpath (name ).with_suffix (".lnk" )
161164 args = [self .filename .get ()]
162165 Shortcut ().create (sorce = sorce , args = args , icon = icon )
163166 self .toast .info (i18n .t ("app.shortcut.save_success" ))
164- except Exception :
165- DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
166- raise
167+
168+ self .save_handler (fn )
167169
168170 @error_toast
169171 def save_only_callback (self ):
@@ -203,6 +205,10 @@ def create(self):
203205 super ().create ()
204206 return self
205207
208+ def save_handler (self , fn : Callable [[], None ]):
209+ self .save ()
210+ fn ()
211+
206212
207213class ShortcutEdit (ShortcutBase ):
208214 selected : StringVar
@@ -227,19 +233,23 @@ def create(self):
227233
228234 return self
229235
230- def save (self ):
231- selected = DataPathConfig .SHORTCUT .joinpath (self .selected .get ()). with_suffix ( ".json" )
232- 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" ))
233239 try :
234- super ().save ()
235- selected .with_suffix (".json.bak" ).unlink ()
236- self .values .remove (self .selected .get ())
237- self .values .append (self .filename .get ())
238- self .selected .set (self .filename .get ())
239- self .option_callback ("_" )
240+ self .save ()
241+ try :
242+ fn ()
243+ except Exception :
244+ DataPathConfig .SHORTCUT .joinpath (self .filename .get ()).with_suffix (".json" ).unlink ()
240245 except Exception :
241246 selected .with_suffix (".json.bak" ).rename (selected .with_suffix (".json" ))
242247 raise
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 ("_" )
243253
244254 @error_toast
245255 def delete_callback (self ):
0 commit comments