Skip to content

Commit

Permalink
pythongh-112898: Remove the false "Cancel" button in the "Save On Clo…
Browse files Browse the repository at this point in the history
…se" dialog

On macOS, when it is asked just before quiting the application, the user
has no option to cancel quiting.
  • Loading branch information
serhiy-storchaka committed Dec 12, 2023
1 parent e0fb700 commit b44372b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Lib/idlelib/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,18 +1110,18 @@ def close_event(self, event):
self.close()
return "break"

def maybesave(self):
def maybesave(self, force=False):
if self.io:
if not self.get_saved():
if self.top.state()!='normal':
self.top.deiconify()
self.top.lower()
self.top.lift()
return self.io.maybesave()
return self.io.maybesave(force=force)

def close(self):
def close(self, force=False):
try:
reply = self.maybesave()
reply = self.maybesave(force=force)
if str(reply) != "cancel":
self._close()
return reply
Expand Down
7 changes: 5 additions & 2 deletions Lib/idlelib/filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ def gotofileline(self, filename, lineno=None):
def new(self, filename=None):
return self.EditorWindow(self, filename)

def close_all_callback(self, *args, **kwds):
def close_all_callback(self, *args, force=False, **kwds):
for edit in list(self.inversedict):
reply = edit.close()
reply = edit.close(force=force)
if reply == "cancel":
break
return "break"

def close_all_force_callback(self, *args, **kwds):
return self.close_all_callback(*args, force=True, **kwds)

def unregister_maybe_terminate(self, edit):
try:
key = self.inversedict[edit]
Expand Down
12 changes: 8 additions & 4 deletions Lib/idlelib/iomenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def loadfile(self, filename):
self.updaterecentfileslist(filename)
return True

def maybesave(self):
def maybesave(self, force=False):
"""Return 'yes', 'no', 'cancel' as appropriate.
Tkinter messagebox.askyesnocancel converts these tk responses
Expand All @@ -190,15 +190,19 @@ def maybesave(self):
message = ("Do you want to save "
f"{self.filename or 'this untitled document'}"
" before closing?")
confirm = messagebox.askyesnocancel(
makemsgbox = messagebox.askyesno if force else messagebox.askyesnocancel
confirm = makemsgbox(
title="Save On Close",
message=message,
default=messagebox.YES,
parent=self.text)
reply = "no" if force else "cancel"
if confirm:
self.save(None)
reply = "yes" if self.get_saved() else "cancel"
else: reply = "cancel" if confirm is None else "no"
if self.get_saved():
reply = "yes"
elif confirm is not None:
reply = "no"
self.text.focus_set()
return reply

Expand Down
2 changes: 1 addition & 1 deletion Lib/idlelib/macosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def help_dialog(event=None):
# The binding above doesn't reliably work on all versions of Tk
# on macOS. Adding command definition below does seem to do the
# right thing for now.
root.createcommand('::tk::mac::Quit', flist.close_all_callback)
root.createcommand('::tk::mac::Quit', flist.close_all_force_callback)

if isCarbonTk():
# for Carbon AquaTk, replace the default Tk apple menu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Remove the "Cancel" button in the "Save On Close" message box on macOS when
the user has no option to cancel quiting.

0 comments on commit b44372b

Please sign in to comment.