diff --git a/osu.Game/Overlays/NowPlayingOverlay.cs b/osu.Game/Overlays/NowPlayingOverlay.cs index 78539fde7a..4132db42bc 100644 --- a/osu.Game/Overlays/NowPlayingOverlay.cs +++ b/osu.Game/Overlays/NowPlayingOverlay.cs @@ -1,13 +1,12 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -#nullable disable - using System; -using System.Threading.Tasks; +using System.Threading; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.Color4Extensions; +using osu.Framework.Extensions.ObjectExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Effects; @@ -40,33 +39,33 @@ namespace osu.Game.Overlays private const float bottom_black_area_height = 55; private const float margin = 10; - private Drawable background; - private ProgressBar progressBar; + private Drawable background = null!; + private ProgressBar progressBar = null!; - private IconButton prevButton; - private IconButton playButton; - private IconButton nextButton; - private IconButton playlistButton; + private IconButton prevButton = null!; + private IconButton playButton = null!; + private IconButton nextButton = null!; + private IconButton playlistButton = null!; - private SpriteText title, artist; + private SpriteText title = null!, artist = null!; - private PlaylistOverlay playlist; + private PlaylistOverlay? playlist; - private Container dragContainer; - private Container playerContainer; - private Container playlistContainer; + private Container dragContainer = null!; + private Container playerContainer = null!; + private Container playlistContainer = null!; protected override string PopInSampleName => "UI/now-playing-pop-in"; protected override string PopOutSampleName => "UI/now-playing-pop-out"; [Resolved] - private MusicController musicController { get; set; } + private MusicController musicController { get; set; } = null!; [Resolved] - private Bindable beatmap { get; set; } + private Bindable beatmap { get; set; } = null!; [Resolved] - private OsuColour colours { get; set; } + private OsuColour colours { get; set; } = null!; private Bindable allowTrackControl; @@ -290,31 +289,34 @@ namespace osu.Game.Overlays } } - private Action pendingBeatmapSwitch; + private Action? pendingBeatmapSwitch; + + private CancellationTokenSource? backgroundLoadCancellation; + + private WorkingBeatmap? currentBeatmap; private void trackChanged(WorkingBeatmap beatmap, TrackChangeDirection direction = TrackChangeDirection.None) { + currentBeatmap = beatmap; + // avoid using scheduler as our scheduler may not be run for a long time, holding references to beatmaps. pendingBeatmapSwitch = delegate { - // todo: this can likely be replaced with WorkingBeatmap.GetBeatmapAsync() - Task.Run(() => - { - if (beatmap?.Beatmap == null) // this is not needed if a placeholder exists - { - title.Text = @"Nothing to play"; - artist.Text = @"Nothing to play"; - } - else - { - BeatmapMetadata metadata = beatmap.Metadata; - title.Text = new RomanisableString(metadata.TitleUnicode, metadata.Title); - artist.Text = new RomanisableString(metadata.ArtistUnicode, metadata.Artist); - } - }); + BeatmapMetadata metadata = beatmap.Metadata; + + title.Text = new RomanisableString(metadata.TitleUnicode, metadata.Title); + artist.Text = new RomanisableString(metadata.ArtistUnicode, metadata.Artist); + + backgroundLoadCancellation?.Cancel(); LoadComponentAsync(new Background(beatmap) { Depth = float.MaxValue }, newBackground => { + if (beatmap != currentBeatmap) + { + newBackground.Dispose(); + return; + } + switch (direction) { case TrackChangeDirection.Next: @@ -334,7 +336,7 @@ namespace osu.Game.Overlays background = newBackground; playerContainer.Add(newBackground); - }); + }, (backgroundLoadCancellation = new CancellationTokenSource()).Token); }; } @@ -356,7 +358,7 @@ namespace osu.Game.Overlays { base.Dispose(isDisposing); - if (musicController != null) + if (musicController.IsNotNull()) musicController.TrackChanged -= trackChanged; } @@ -389,7 +391,7 @@ namespace osu.Game.Overlays private readonly Sprite sprite; private readonly WorkingBeatmap beatmap; - public Background(WorkingBeatmap beatmap = null) + public Background(WorkingBeatmap beatmap) : base(cachedFrameBuffer: true) { this.beatmap = beatmap; @@ -419,7 +421,7 @@ namespace osu.Game.Overlays [BackgroundDependencyLoader] private void load(LargeTextureStore textures) { - sprite.Texture = beatmap?.GetBackground() ?? textures.Get(@"Backgrounds/bg4"); + sprite.Texture = beatmap.GetBackground() ?? textures.Get(@"Backgrounds/bg4"); } }