Properly remove DtrEntries from DtrBarPluginScoped (#1969)

fixes #1862
This commit is contained in:
Haselnussbomber 2024-07-25 00:59:34 +02:00 committed by GitHub
parent 7a5bdb6bde
commit 5650bf3ae0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -76,6 +76,17 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
this.configuration.QueueSave();
}
/// <summary>
/// Event type fired each time a DtrEntry was removed.
/// </summary>
/// <param name="title">The title of the bar entry.</param>
internal delegate void DtrEntryRemovedDelegate(string title);
/// <summary>
/// Event fired each time a DtrEntry was removed.
/// </summary>
internal event DtrEntryRemovedDelegate? DtrEntryRemoved;
/// <inheritdoc/>
public IReadOnlyList<IReadOnlyDtrBarEntry> Entries => this.entries;
@ -136,6 +147,7 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
if (data.ShouldBeRemoved)
{
this.RemoveEntry(data);
this.DtrEntryRemoved?.Invoke(data.Title);
}
}
@ -525,7 +537,6 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
/// Plugin-scoped version of a AddonEventManager service.
/// </summary>
[PluginInterface]
[ServiceManager.ScopedService]
#pragma warning disable SA1015
[ResolveVia<IDtrBar>]
@ -536,13 +547,23 @@ internal class DtrBarPluginScoped : IInternalDisposableService, IDtrBar
private readonly DtrBar dtrBarService = Service<DtrBar>.Get();
private readonly Dictionary<string, IDtrBarEntry> pluginEntries = new();
/// <summary>
/// Initializes a new instance of the <see cref="DtrBarPluginScoped"/> class.
/// </summary>
internal DtrBarPluginScoped()
{
this.dtrBarService.DtrEntryRemoved += this.OnDtrEntryRemoved;
}
/// <inheritdoc/>
public IReadOnlyList<IReadOnlyDtrBarEntry> Entries => this.dtrBarService.Entries;
/// <inheritdoc/>
void IInternalDisposableService.DisposeService()
{
this.dtrBarService.DtrEntryRemoved -= this.OnDtrEntryRemoved;
foreach (var entry in this.pluginEntries)
{
entry.Value.Remove();
@ -569,4 +590,9 @@ internal class DtrBarPluginScoped : IInternalDisposableService, IDtrBar
this.pluginEntries.Remove(title);
}
}
private void OnDtrEntryRemoved(string title)
{
this.pluginEntries.Remove(title);
}
}