From a2328a37935b261a3a78b0886e1f1d4c401fce34 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 7 Dec 2021 15:45:52 -0300 Subject: [PATCH] Restore the previous active DocView when we close the non-active sprite Fixes https://github.com/aseprite/aseprite/pull/2727#issuecomment-852524240 Close #2030, close #2727, close #3080, close #3089 Originally reported in: https://community.aseprite.org/t/preview-file-to-save-when-closing/2779 --- src/app/ui/doc_view.cpp | 76 +++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/src/app/ui/doc_view.cpp b/src/app/ui/doc_view.cpp index 4de9d498a..0af9e8051 100644 --- a/src/app/ui/doc_view.cpp +++ b/src/app/ui/doc_view.cpp @@ -54,6 +54,32 @@ namespace app { using namespace ui; +namespace { + +// Used to show a view temporarily (the one with the file to be +// closed) and restore the previous view. E.g. When we close the +// non-active sprite pressing the cross button in a sprite tab. +class SetRestoreDocView { +public: + SetRestoreDocView(UIContext* ctx, DocView* newView) + : m_ctx(ctx) + , m_oldView(ctx->activeView()) { + if (newView != m_oldView) + m_ctx->setActiveView(newView); + else + m_oldView = nullptr; + } + + ~SetRestoreDocView() { + if (m_oldView) + m_ctx->setActiveView(m_oldView); + } + +private: + UIContext* m_ctx; + DocView* m_oldView; +}; + class AppEditor : public Editor, public EditorObserver, public EditorCustomizationDelegate { @@ -192,6 +218,8 @@ public: } }; +} // anonymous namespace + DocView::DocView(Doc* document, Type type, DocViewPreviewDelegate* previewDelegate) : Box(VERTICAL) @@ -275,45 +303,41 @@ bool DocView::onCloseView(Workspace* workspace, bool quitting) } UIContext* ctx = UIContext::instance(); + SetRestoreDocView restoreView(ctx, this); bool save_it; bool try_again = true; while (try_again) { // This flag indicates if we have to sabe the sprite before to destroy it save_it = false; - { - // see if the sprite has changes - while (m_document->isModified()) { - // Show user what they are about to close - ctx->setActiveView(this); - // ask what want to do the user with the changes in the sprite - int ret = Alert::show( - fmt::format( - Strings::alerts_save_sprite_changes(), - m_document->name(), - (quitting ? Strings::alerts_save_sprite_changes_quitting(): - Strings::alerts_save_sprite_changes_closing()))); + // See if the sprite has changes + while (m_document->isModified()) { + // ask what want to do the user with the changes in the sprite + int ret = Alert::show( + fmt::format( + Strings::alerts_save_sprite_changes(), + m_document->name(), + (quitting ? Strings::alerts_save_sprite_changes_quitting(): + Strings::alerts_save_sprite_changes_closing()))); - if (ret == 1) { - // "save": save the changes - save_it = true; - break; - } - else if (ret != 2) { - // "cancel" or "ESC" */ - return false; // we back doing nothing - } - else { - // "discard" - break; - } + if (ret == 1) { + // "save": save the changes + save_it = true; + break; + } + else if (ret != 2) { + // "cancel" or "ESC" */ + return false; // we back doing nothing + } + else { + // "discard" + break; } } // Does we need to save the sprite? if (save_it) { - ctx->setActiveView(this); ctx->updateFlags(); Command* save_command =