From 808f66edc62a433429a0b7f26dbbd8260c94cc32 Mon Sep 17 00:00:00 2001
From: goat <16760685+goaaats@users.noreply.github.com>
Date: Sat, 20 Apr 2024 14:46:04 +0200
Subject: [PATCH] Expose DTR entries in public interface (#1778)
* expose DTR entries in public interface
* add IReadOnlyDtrBarEntry.UserHidden
---
Dalamud/Game/Gui/Dtr/DtrBar.cs | 12 ++-
Dalamud/Game/Gui/Dtr/DtrBarEntry.cs | 121 ++++++++++++++++++++++++----
Dalamud/Plugin/Services/IDtrBar.cs | 8 ++
3 files changed, 123 insertions(+), 18 deletions(-)
diff --git a/Dalamud/Game/Gui/Dtr/DtrBar.cs b/Dalamud/Game/Gui/Dtr/DtrBar.cs
index b28c9a7d9..8a7982b07 100644
--- a/Dalamud/Game/Gui/Dtr/DtrBar.cs
+++ b/Dalamud/Game/Gui/Dtr/DtrBar.cs
@@ -73,13 +73,16 @@ internal sealed unsafe class DtrBar : IInternalDisposableService, IDtrBar
this.configuration.QueueSave();
}
+ ///
+ public IReadOnlyList Entries => this.entries;
+
///
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 })
{
@@ -499,6 +502,9 @@ internal class DtrBarPluginScoped : IInternalDisposableService, IDtrBar
private readonly DtrBar dtrBarService = Service.Get();
private readonly Dictionary pluginEntries = new();
+
+ ///
+ public IReadOnlyList Entries => this.dtrBarService.Entries;
///
void IInternalDisposableService.DisposeService()
@@ -510,7 +516,7 @@ internal class DtrBarPluginScoped : IInternalDisposableService, IDtrBar
this.pluginEntries.Clear();
}
-
+
///
public DtrBarEntry Get(string title, SeString? text = null)
{
diff --git a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
index f04e1427d..ef4ce062b 100644
--- a/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
+++ b/Dalamud/Game/Gui/Dtr/DtrBarEntry.cs
@@ -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;
+///
+/// Interface representing a read-only entry in the server info bar.
+///
+public interface IReadOnlyDtrBarEntry
+{
+ ///
+ /// Gets the title of this entry.
+ ///
+ public string Title { get; }
+
+ ///
+ /// Gets a value indicating whether this entry has a click action.
+ ///
+ public bool HasClickAction { get; }
+
+ ///
+ /// Gets the text of this entry.
+ ///
+ public SeString Text { get; }
+
+ ///
+ /// Gets a tooltip to be shown when the user mouses over the dtr entry.
+ ///
+ public SeString Tooltip { get; }
+
+ ///
+ /// Gets a value indicating whether this entry should be shown.
+ ///
+ public bool Shown { get; }
+
+ ///
+ /// Gets a value indicating whether or not the user has hidden this entry from view through the Dalamud settings.
+ ///
+ public bool UserHidden { get; }
+
+ ///
+ /// Triggers the click action of this entry.
+ ///
+ /// True, if a click action was registered and executed.
+ public bool TriggerClickAction();
+}
+
+///
+/// Interface representing an entry in the server info bar.
+///
+public interface IDtrBarEntry : IReadOnlyDtrBarEntry
+{
+ ///
+ /// Gets or sets the text of this entry.
+ ///
+ public new SeString? Text { get; set; }
+
+ ///
+ /// Gets or sets a tooltip to be shown when the user mouses over the dtr entry.
+ ///
+ public new SeString? Tooltip { get; set; }
+
+ ///
+ /// Gets or sets a value indicating whether this entry is visible.
+ ///
+ public new bool Shown { get; set; }
+
+ ///
+ /// Gets or sets a action to be invoked when the user clicks on the dtr entry.
+ ///
+ public Action? OnClick { get; set; }
+
+ ///
+ /// Remove this entry from the bar.
+ /// You will need to re-acquire it from DtrBar to reuse it.
+ ///
+ public void Remove();
+}
+
///
/// Class representing an entry in the server info bar.
///
-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;
///
/// Initializes a new instance of the class.
///
+ /// Dalamud configuration, used to check if the entry is hidden by the user.
/// The title of the bar entry.
/// The corresponding text node.
- internal DtrBarEntry(string title, AtkTextNode* textNode)
+ internal DtrBarEntry(DalamudConfiguration configuration, string title, AtkTextNode* textNode)
{
+ this.configuration = configuration;
this.Title = title;
this.TextNode = textNode;
}
- ///
- /// Gets the title of this entry.
- ///
+ ///
public string Title { get; init; }
- ///
- /// Gets or sets the text of this entry.
- ///
+ ///
public SeString? Text
{
get => this.textBacking;
@@ -41,10 +119,8 @@ public sealed unsafe class DtrBarEntry : IDisposable
this.Dirty = true;
}
}
-
- ///
- /// Gets or sets a tooltip to be shown when the user mouses over the dtr entry.
- ///
+
+ ///
public SeString? Tooltip { get; set; }
///
@@ -52,9 +128,10 @@ public sealed unsafe class DtrBarEntry : IDisposable
///
public Action? OnClick { get; set; }
- ///
- /// Gets or sets a value indicating whether this entry is visible.
- ///
+ ///
+ public bool HasClickAction => this.OnClick != null;
+
+ ///
public bool Shown
{
get => this.shownBacking;
@@ -65,6 +142,10 @@ public sealed unsafe class DtrBarEntry : IDisposable
}
}
+ ///
+ [Api10ToDo("Maybe make this config scoped to internalname?")]
+ public bool UserHidden => this.configuration.DtrIgnore?.Any(x => x == this.Title) ?? false;
+
///
/// Gets or sets the internal text node of this entry.
///
@@ -84,6 +165,16 @@ public sealed unsafe class DtrBarEntry : IDisposable
/// Gets or sets a value indicating whether this entry has just been added.
///
internal bool Added { get; set; } = false;
+
+ ///
+ public bool TriggerClickAction()
+ {
+ if (this.OnClick == null)
+ return false;
+
+ this.OnClick.Invoke();
+ return true;
+ }
///
/// Remove this entry from the bar.
diff --git a/Dalamud/Plugin/Services/IDtrBar.cs b/Dalamud/Plugin/Services/IDtrBar.cs
index a5a750cf6..6019bb1e6 100644
--- a/Dalamud/Plugin/Services/IDtrBar.cs
+++ b/Dalamud/Plugin/Services/IDtrBar.cs
@@ -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;
///
public interface IDtrBar
{
+ ///
+ /// Gets a read-only list of all DTR bar entries.
+ ///
+ public IReadOnlyList Entries { get; }
+
///
/// 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
/// The text the entry shows.
/// The entry object used to update, hide and remove the entry.
/// Thrown when an entry with the specified title exists.
+ [Api10ToDo("Return IDtrBarEntry instead of DtrBarEntry")]
public DtrBarEntry Get(string title, SeString? text = null);
///