Expose DTR entries in public interface (#1778)

* expose DTR entries in public interface

* add IReadOnlyDtrBarEntry.UserHidden
This commit is contained in:
goat 2024-04-20 14:46:04 +02:00 committed by GitHub
parent 62863becd5
commit 808f66edc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 123 additions and 18 deletions

View file

@ -73,13 +73,16 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
this.configuration.QueueSave();
}
/// <inheritdoc/>
public IReadOnlyList<IReadOnlyDtrBarEntry> Entries => this.entries;
/// <inheritdoc/>
public DtrBarEntry Get(string title, SeString? text = null)
{
if (this.entries.Any(x => x.Title == title) || this.newEntries.Any(x => x.Title == title))
throw new ArgumentException("An entry with the same title already exists.");
var entry = new DtrBarEntry(title, null);
var entry = new DtrBarEntry(this.configuration, title, null);
entry.Text = text;
// Add the entry to the end of the order list, if it's not there already.
@ -196,7 +199,7 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
foreach (var data in this.entries)
{
var isHide = this.configuration.DtrIgnore!.Any(x => x == data.Title) || !data.Shown;
var isHide = data.UserHidden || !data.Shown;
if (data is { Dirty: true, Added: true, Text: not null, TextNode: not null })
{
@ -500,6 +503,9 @@ internal class DtrBarPluginScoped : IInternalDisposableService, IDtrBar
private readonly Dictionary<string, DtrBarEntry> pluginEntries = new();
/// <inheritdoc/>
public IReadOnlyList<IReadOnlyDtrBarEntry> Entries => this.dtrBarService.Entries;
/// <inheritdoc/>
void IInternalDisposableService.DisposeService()
{

View file

@ -1,37 +1,115 @@
using System;
using System.Linq;
using Dalamud.Configuration.Internal;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Utility;
using FFXIVClientStructs.FFXIV.Component.GUI;
namespace Dalamud.Game.Gui.Dtr;
/// <summary>
/// Interface representing a read-only entry in the server info bar.
/// </summary>
public interface IReadOnlyDtrBarEntry
{
/// <summary>
/// Gets the title of this entry.
/// </summary>
public string Title { get; }
/// <summary>
/// Gets a value indicating whether this entry has a click action.
/// </summary>
public bool HasClickAction { get; }
/// <summary>
/// Gets the text of this entry.
/// </summary>
public SeString Text { get; }
/// <summary>
/// Gets a tooltip to be shown when the user mouses over the dtr entry.
/// </summary>
public SeString Tooltip { get; }
/// <summary>
/// Gets a value indicating whether this entry should be shown.
/// </summary>
public bool Shown { get; }
/// <summary>
/// Gets a value indicating whether or not the user has hidden this entry from view through the Dalamud settings.
/// </summary>
public bool UserHidden { get; }
/// <summary>
/// Triggers the click action of this entry.
/// </summary>
/// <returns>True, if a click action was registered and executed.</returns>
public bool TriggerClickAction();
}
/// <summary>
/// Interface representing an entry in the server info bar.
/// </summary>
public interface IDtrBarEntry : IReadOnlyDtrBarEntry
{
/// <summary>
/// Gets or sets the text of this entry.
/// </summary>
public new SeString? Text { get; set; }
/// <summary>
/// Gets or sets a tooltip to be shown when the user mouses over the dtr entry.
/// </summary>
public new SeString? Tooltip { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this entry is visible.
/// </summary>
public new bool Shown { get; set; }
/// <summary>
/// Gets or sets a action to be invoked when the user clicks on the dtr entry.
/// </summary>
public Action? OnClick { get; set; }
/// <summary>
/// Remove this entry from the bar.
/// You will need to re-acquire it from DtrBar to reuse it.
/// </summary>
public void Remove();
}
/// <summary>
/// Class representing an entry in the server info bar.
/// </summary>
public sealed unsafe class DtrBarEntry : IDisposable
public sealed unsafe class DtrBarEntry : IDisposable, IDtrBarEntry
{
private readonly DalamudConfiguration configuration;
private bool shownBacking = true;
private SeString? textBacking = null;
/// <summary>
/// Initializes a new instance of the <see cref="DtrBarEntry"/> class.
/// </summary>
/// <param name="configuration">Dalamud configuration, used to check if the entry is hidden by the user.</param>
/// <param name="title">The title of the bar entry.</param>
/// <param name="textNode">The corresponding text node.</param>
internal DtrBarEntry(string title, AtkTextNode* textNode)
internal DtrBarEntry(DalamudConfiguration configuration, string title, AtkTextNode* textNode)
{
this.configuration = configuration;
this.Title = title;
this.TextNode = textNode;
}
/// <summary>
/// Gets the title of this entry.
/// </summary>
/// <inheritdoc/>
public string Title { get; init; }
/// <summary>
/// Gets or sets the text of this entry.
/// </summary>
/// <inheritdoc cref="IDtrBarEntry.Text" />
public SeString? Text
{
get => this.textBacking;
@ -42,9 +120,7 @@ public sealed unsafe class DtrBarEntry : IDisposable
}
}
/// <summary>
/// Gets or sets a tooltip to be shown when the user mouses over the dtr entry.
/// </summary>
/// <inheritdoc cref="IDtrBarEntry.Tooltip" />
public SeString? Tooltip { get; set; }
/// <summary>
@ -52,9 +128,10 @@ public sealed unsafe class DtrBarEntry : IDisposable
/// </summary>
public Action? OnClick { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this entry is visible.
/// </summary>
/// <inheritdoc/>
public bool HasClickAction => this.OnClick != null;
/// <inheritdoc cref="IDtrBarEntry.Shown" />
public bool Shown
{
get => this.shownBacking;
@ -65,6 +142,10 @@ public sealed unsafe class DtrBarEntry : IDisposable
}
}
/// <inheritdoc/>
[Api10ToDo("Maybe make this config scoped to internalname?")]
public bool UserHidden => this.configuration.DtrIgnore?.Any(x => x == this.Title) ?? false;
/// <summary>
/// Gets or sets the internal text node of this entry.
/// </summary>
@ -85,6 +166,16 @@ public sealed unsafe class DtrBarEntry : IDisposable
/// </summary>
internal bool Added { get; set; } = false;
/// <inheritdoc/>
public bool TriggerClickAction()
{
if (this.OnClick == null)
return false;
this.OnClick.Invoke();
return true;
}
/// <summary>
/// Remove this entry from the bar.
/// You will need to re-acquire it from DtrBar to reuse it.

View file

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using Dalamud.Game.Gui.Dtr;
using Dalamud.Game.Text.SeStringHandling;
using Dalamud.Utility;
namespace Dalamud.Plugin.Services;
@ -10,6 +12,11 @@ namespace Dalamud.Plugin.Services;
/// </summary>
public interface IDtrBar
{
/// <summary>
/// Gets a read-only list of all DTR bar entries.
/// </summary>
public IReadOnlyList<IReadOnlyDtrBarEntry> Entries { get; }
/// <summary>
/// Get a DTR bar entry.
/// This allows you to add your own text, and users to sort it.
@ -18,6 +25,7 @@ public interface IDtrBar
/// <param name="text">The text the entry shows.</param>
/// <returns>The entry object used to update, hide and remove the entry.</returns>
/// <exception cref="ArgumentException">Thrown when an entry with the specified title exists.</exception>
[Api10ToDo("Return IDtrBarEntry instead of DtrBarEntry")]
public DtrBarEntry Get(string title, SeString? text = null);
/// <summary>