Skip to content

Commit

Permalink
Merge pull request #17403 from peppy/editor-exit-harsh-blocking
Browse files Browse the repository at this point in the history
Disallow exiting the editor without saving (unless explicitly confirming)
  • Loading branch information
smoogipoo authored Mar 22, 2022
2 parents d84865f + 8591630 commit cda46ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
7 changes: 7 additions & 0 deletions osu.Game.Tests/Visual/Editing/TestSceneEditorSaving.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ namespace osu.Game.Tests.Visual.Editing
{
public class TestSceneEditorSaving : EditorSavingTestScene
{
[Test]
public void TestCantExitWithoutSaving()
{
AddRepeatStep("Exit", () => InputManager.Key(Key.Escape), 10);
AddAssert("Editor is still active screen", () => Game.ScreenStack.CurrentScreen is Editor);
}

[Test]
public void TestMetadata()
{
Expand Down
16 changes: 6 additions & 10 deletions osu.Game/Screens/Edit/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
using osu.Game.Input.Bindings;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Edit;
Expand Down Expand Up @@ -98,7 +97,7 @@ protected bool HasUnsavedChanges

private bool canSave;

private bool exitConfirmed;
protected bool ExitConfirmed { get; private set; }

private string lastSavedHash;

Expand Down Expand Up @@ -587,7 +586,7 @@ private void dimBackground()

public override bool OnExiting(IScreen next)
{
if (!exitConfirmed)
if (!ExitConfirmed)
{
// dialog overlay may not be available in visual tests.
if (dialogOverlay == null)
Expand All @@ -596,12 +595,9 @@ public override bool OnExiting(IScreen next)
return true;
}

// if the dialog is already displayed, confirm exit with no save.
if (dialogOverlay.CurrentDialog is PromptForSaveDialog saveDialog)
{
saveDialog.PerformAction<PopupDialogDangerousButton>();
// if the dialog is already displayed, block exiting until the user explicitly makes a decision.
if (dialogOverlay.CurrentDialog is PromptForSaveDialog)
return true;
}

if (isNewBeatmap || HasUnsavedChanges)
{
Expand Down Expand Up @@ -646,7 +642,7 @@ private void confirmExitWithSave()
{
Save();

exitConfirmed = true;
ExitConfirmed = true;
this.Exit();
}

Expand All @@ -669,7 +665,7 @@ private void confirmExit()
Beatmap.SetDefault();
}

exitConfirmed = true;
ExitConfirmed = true;
this.Exit();
}

Expand Down
19 changes: 19 additions & 0 deletions osu.Game/Tests/Visual/EditorTestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
using osu.Framework.Audio;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit;
Expand Down Expand Up @@ -93,6 +96,10 @@ protected class TestEditorLoader : EditorLoader

protected class TestEditor : Editor
{
[Resolved(canBeNull: true)]
[CanBeNull]
private DialogOverlay dialogOverlay { get; set; }

public new void Undo() => base.Undo();

public new void Redo() => base.Redo();
Expand All @@ -111,6 +118,18 @@ protected class TestEditor : Editor

public new bool HasUnsavedChanges => base.HasUnsavedChanges;

public override bool OnExiting(IScreen next)
{
// For testing purposes allow the screen to exit without saving on second attempt.
if (!ExitConfirmed && dialogOverlay?.CurrentDialog is PromptForSaveDialog saveDialog)
{
saveDialog.PerformAction<PopupDialogDangerousButton>();
return true;
}

return base.OnExiting(next);
}

public TestEditor(EditorLoader loader = null)
: base(loader)
{
Expand Down

0 comments on commit cda46ec

Please sign in to comment.