Improve code around background screen handling to read better

This commit is contained in:
Dean Herbert 2021-09-07 14:33:58 +09:00
parent c7abda8f58
commit 93da531d13
2 changed files with 12 additions and 9 deletions

View File

@ -17,15 +17,21 @@ public BackgroundScreenStack()
Origin = Anchor.Centre; Origin = Anchor.Centre;
} }
public void Push(BackgroundScreen screen) /// <summary>
/// Attempt to push a new background screen to this stack.
/// </summary>
/// <param name="screen">The screen to attempt to push.</param>
/// <returns>Whether the push succeeded. For example, if the existing screen was already of the correct type this will return <c>false</c>.</returns>
public bool Push(BackgroundScreen screen)
{ {
if (screen == null) if (screen == null)
return; return false;
if (EqualityComparer<BackgroundScreen>.Default.Equals((BackgroundScreen)CurrentScreen, screen)) if (EqualityComparer<BackgroundScreen>.Default.Equals((BackgroundScreen)CurrentScreen, screen))
return; return false;
base.Push(screen); base.Push(screen);
return true;
} }
} }
} }

View File

@ -186,17 +186,14 @@ public override void OnEntering(IScreen last)
{ {
applyArrivingDefaults(false); applyArrivingDefaults(false);
backgroundStack?.Push(ownedBackground = CreateBackground()); if (backgroundStack?.Push(ownedBackground = CreateBackground()) != true)
background = backgroundStack?.CurrentScreen as BackgroundScreen;
if (background != ownedBackground)
{ {
// background may have not been replaced, at which point we don't want to track the background lifetime. // If the constructed instance was not actually pushed to the background stack, we don't want to track it unnecessarily.
ownedBackground?.Dispose(); ownedBackground?.Dispose();
ownedBackground = null; ownedBackground = null;
} }
background = backgroundStack?.CurrentScreen as BackgroundScreen;
base.OnEntering(last); base.OnEntering(last);
} }