Skip to content

Commit

Permalink
added save confirmation before closing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nokse22 committed Mar 8, 2024
1 parent d261413 commit 580eed6
Showing 1 changed file with 51 additions and 20 deletions.
71 changes: 51 additions & 20 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def on_about_action(self, *args):
application_name=_("ASCII Draw"),
application_icon='io.github.nokse22.asciidraw',
developer_name='Nokse',
version='0.2.0',
version='0.3.0',
website='https://github.com/Nokse22/ascii-draw',
issue_url='https://github.com/Nokse22/ascii-draw/issues',
developers=['Nokse'],
Expand Down Expand Up @@ -196,33 +196,64 @@ def create_action(self, name, callback, shortcuts=None):
self.set_accels_for_action(f"app.{name}", shortcuts)

def on_shutdown(self, *args):
dialog = Adw.MessageDialog(
heading=_("Save Changes"),
body="A valid password is needed to continue",
close_response="cancel",
modal=True,
transient_for=self.win,
)

dialog.add_response("cancel", _("Cancel"))
dialog.add_response("discard", _("Discard"))
dialog.add_response("save", _("Save"))
if not self.win.canvas.is_saved and self.win.file_path == "":
dialog = Adw.MessageDialog(
heading=_("Save this file?"),
body=_("You have never saved this file."),
close_response="cancel",
modal=True,
transient_for=self.win,
)

dialog.set_response_appearance("discard", Adw.ResponseAppearance.DESTRUCTIVE)
dialog.set_response_appearance("save", Adw.ResponseAppearance.SUGGESTED)
dialog.add_response("cancel", _("Cancel"))
dialog.add_response("discard", _("Discard"))
dialog.add_response("save", _("Save"))

# entry = Gtk.Entry()
# dialog.set_extra_child(entry)
dialog.set_response_appearance("discard", Adw.ResponseAppearance.DESTRUCTIVE)
dialog.set_response_appearance("save", Adw.ResponseAppearance.SUGGESTED)

# dialog.choose(None, self.on_response_selected_advanced)
# dialog.choose_finish(task)
# entry = Adw.EntryRow(title="Filename")
# list_box = Gtk.ListBox(css_classes=["boxed-list"], width_request=340)
# list_box.append(entry)

# dialog.run()
# dialog.set_extra_child(list_box)

print(quit)
dialog.choose(None, self.on_save_file_with_name_response)
return True

def on_response_selected_advanced(self, dialog, task, *args):
if not self.win.canvas.is_saved and self.win.file_path != "":
dialog = Adw.MessageDialog(
heading=_("Save changes?"),
body=_("Not saving will result in losing all changes"),
close_response="cancel",
modal=True,
transient_for=self.win,
)

dialog.add_response("cancel", _("Cancel"))
dialog.add_response("discard", _("Discard"))
dialog.add_response("save", _("Save"))

dialog.set_response_appearance("discard", Adw.ResponseAppearance.DESTRUCTIVE)
dialog.set_response_appearance("save", Adw.ResponseAppearance.SUGGESTED)

dialog.choose(None, self.on_save_file_with_name_response)
return True

def on_save_file_with_name_response(self, dialog, task, *args):
response = dialog.choose_finish(task)
if response == "save":
self.win.save(self.quit)
elif response == "discard":
self.quit()

def on_save_changes(self, dialog, task, *args):
response = dialog.choose_finish(task)
if response == "save":
self.win.save()
elif response == "discard":
self.quit()

def select_rectangle_tool(self, widget, _):
self.win.select_rectangle_tool()
Expand Down

0 comments on commit 580eed6

Please sign in to comment.