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
This commit is contained in:
David Capello 2021-12-07 15:45:52 -03:00
parent b96d8c8c30
commit a2328a3793

View File

@ -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 =