File tree

1 file changed

+55
-21
lines changed

1 file changed

+55
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,36 @@ def new_file() -> str:
216216
global text_last_saved_manually
217217
global text_to_save
218218

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+
219228
WINDOW['-BODY-'].update(value='')
220229
WINDOW['-FILE_INFO-'].update(value='New File:')
221230
WINDOW.set_title('untitled - ' + APP_NAME)
222231
text_last_saved_manually = ''
223232
text_to_save = ''
224233

225234
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+
227249
try:
228250
file_name = sg.popup_get_file('Open File', no_window=True)
229251
except: # pylint: disable=bare-except
@@ -234,6 +256,7 @@ def open_file() -> str:
234256
WINDOW['-FILE_INFO-'].update(value=file_name)
235257

236258
WINDOW.set_title(file_name + ' - ' + APP_NAME)
259+
text_last_saved_manually = VALUES.get('-BODY-')
237260
return file_name
238261

239262
def save_file(file_name: str):
@@ -369,6 +392,29 @@ def AboutNotepadPyPlus():
369392
ShowMessageBox(title='About NotepadPy+',
370393
message='A simple Notepad like application created using PySimpleGUI framework.')
371394

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+
372418
# read the events and take appropriate actions.
373419
while True:
374420

@@ -378,25 +424,12 @@ def AboutNotepadPyPlus():
378424
# Get the filename if already saved in the same session.
379425
file_name = WINDOW['-FILE_INFO-'].DisplayText
380426

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)
395428

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
400433

401434
# finally breakout of the event loop and end the application.
402435
break
@@ -516,9 +549,10 @@ def AboutNotepadPyPlus():
516549
# record the text after each event to ensure the
517550
# file/text is saved.
518551
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
520553
# 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.
522556
if EVENT != file_new:
523557
text_to_save = VALUES['-BODY-']
524558
except: # pylint: disable=bare-except

0 commit comments

Comments
 (0)