Skip to content

Commit

Permalink
Merge pull request #357 from jetelain/prompt-unsaved-document
Browse files Browse the repository at this point in the history
Show save prompt when trying to close a "dirty" document
  • Loading branch information
tgjones authored Jul 30, 2023
2 parents 3099ce3 + 78a9013 commit 4641fc5
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 72 deletions.
27 changes: 24 additions & 3 deletions src/Gemini/Framework/PersistedDocument.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Gemini.Framework.Commands;
using Gemini.Modules.Shell.Commands;
using Gemini.Properties;

namespace Gemini.Framework
{
Expand Down Expand Up @@ -37,10 +41,22 @@ public bool IsDirty
}
}

public override Task<bool> CanCloseAsync(CancellationToken cancellationToken)
public override async Task<bool> CanCloseAsync(CancellationToken cancellationToken)
{
// TODO: Show save prompt.
return Task.FromResult(!IsDirty);
if (IsDirty)
{
var response = MessageBox.Show(string.Format(Resources.DocumentDirtyCloseConfirmText, FileName), Resources.DocumentDirtyCloseConfirmTitle, MessageBoxButton.YesNoCancel);
if (response == MessageBoxResult.Cancel)
{
return false;
}
if (response == MessageBoxResult.Yes)
{
await Save();
return !IsNew; // User may discard file picker prompt
}
}
return true;
}

private void UpdateDisplayName()
Expand Down Expand Up @@ -93,5 +109,10 @@ public async Task Save(string filePath)
}

protected abstract Task DoSave(string filePath);

public Task Save()
{
return ((ICommandHandler<SaveFileCommandDefinition>)this).Run(null);
}
}
}
Loading

0 comments on commit 4641fc5

Please sign in to comment.