@@ -216,14 +216,36 @@ def new_file() -> str:
216
216
global text_last_saved_manually
217
217
global text_to_save
218
218
219
+ fname = WINDOW ['-FILE_INFO-' ].DisplayText
220
+
221
+ save_current_file = SaveBeforeClose (fname )
222
+
223
+ if save_current_file == 'Yes' :
224
+ save_file (fname )
225
+ elif save_current_file == 'No' :
226
+ pass
227
+
219
228
WINDOW ['-BODY-' ].update (value = '' )
220
229
WINDOW ['-FILE_INFO-' ].update (value = 'New File:' )
221
230
WINDOW .set_title ('untitled - ' + APP_NAME )
222
231
text_last_saved_manually = ''
223
232
text_to_save = ''
224
233
225
234
def open_file () -> str :
226
- ''' Open file and update the infobar '''
235
+ ''' Open file and update the infobar.'''
236
+
237
+ global text_last_saved_manually
238
+ fname = WINDOW ['-FILE_INFO-' ].DisplayText
239
+
240
+ save_current_file = SaveBeforeClose (fname )
241
+
242
+ if save_current_file == 'Yes' :
243
+ save_file (fname )
244
+ elif save_current_file == 'No' :
245
+ pass
246
+
247
+ WINDOW ['-BODY-' ].update (value = '' )
248
+
227
249
try :
228
250
file_name = sg .popup_get_file ('Open File' , no_window = True )
229
251
except : # pylint: disable=bare-except
@@ -234,6 +256,7 @@ def open_file() -> str:
234
256
WINDOW ['-FILE_INFO-' ].update (value = file_name )
235
257
236
258
WINDOW .set_title (file_name + ' - ' + APP_NAME )
259
+ text_last_saved_manually = VALUES .get ('-BODY-' )
237
260
return file_name
238
261
239
262
def save_file (file_name : str ):
@@ -369,6 +392,29 @@ def AboutNotepadPyPlus():
369
392
ShowMessageBox (title = 'About NotepadPy+' ,
370
393
message = 'A simple Notepad like application created using PySimpleGUI framework.' )
371
394
395
+ def SaveBeforeClose (fname : str ):
396
+ '''Save before close if the user wants to save
397
+ the documentbefore closing the application.'''
398
+
399
+ save_before_close : str = 'No'
400
+ if fname not in (None , '' ) and \
401
+ text_to_save .rstrip () != '' and \
402
+ text_last_saved_manually != text_to_save :
403
+ # display a user prompt incase the note is not yet saved asking the
404
+ # user 'Do you want to save changes to Untitled?'
405
+ user_prompt_msg : str = ''
406
+ if fname == 'New File:' :
407
+ user_prompt_msg = 'Untitled'
408
+ else :
409
+ user_prompt_msg = fname
410
+
411
+ save_before_close = sg .popup_yes_no ('Do you want to save changes to ' +
412
+ user_prompt_msg + "?" ,
413
+ title = 'NotepayPy+' , modal = True ,
414
+ icon = APPLICATION_ICON )
415
+
416
+ return save_before_close
417
+
372
418
# read the events and take appropriate actions.
373
419
while True :
374
420
@@ -378,25 +424,12 @@ def AboutNotepadPyPlus():
378
424
# Get the filename if already saved in the same session.
379
425
file_name = WINDOW ['-FILE_INFO-' ].DisplayText
380
426
381
- if file_name not in (None , '' ) and \
382
- text_to_save .rstrip () != '' and \
383
- text_last_saved_manually != text_to_save :
384
- # display a user prompt incase the note is not yet saved asking the
385
- # user 'Do you want to save changes to Untitled?'
386
- user_prompt_msg : str = ''
387
- if file_name == 'New File:' :
388
- user_prompt_msg = 'Untitled'
389
- else :
390
- user_prompt_msg = file_name
391
- user_prompt_action = sg .popup_yes_no ('Do you want to save changes to ' +
392
- user_prompt_msg + "?" ,
393
- title = 'NotepayPy+' , modal = True ,
394
- icon = APPLICATION_ICON )
427
+ user_prompt_action = SaveBeforeClose (file_name )
395
428
396
- if user_prompt_action == 'Yes' :
397
- save_file (FILE_NAME )
398
- elif user_prompt_action == 'No' :
399
- break
429
+ if user_prompt_action == 'Yes' :
430
+ save_file (FILE_NAME )
431
+ elif user_prompt_action == 'No' :
432
+ break
400
433
401
434
# finally breakout of the event loop and end the application.
402
435
break
@@ -516,9 +549,10 @@ def AboutNotepadPyPlus():
516
549
# record the text after each event to ensure the
517
550
# file/text is saved.
518
551
try :
519
- # if File -> New menu option is chosen and the new blank editor window is closed, then we do
552
+ # if File -> New menu option is chosen and the new blank editor window is closed, then we do
520
553
# not want to display the Save File prompt. Executing this block on the event of a new file
521
- # resets the 'text_to_save' variable to old text in the editor and causes to display the save prompt.
554
+ # resets the 'text_to_save' variable to old text in the editor and
555
+ # causes to display the save prompt.
522
556
if EVENT != file_new :
523
557
text_to_save = VALUES ['-BODY-' ]
524
558
except : # pylint: disable=bare-except
0 commit comments