using System.Numerics;
using System.Threading.Tasks;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Utility;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
/// Represents the use of future as the icon of a notification.
/// If there was no texture loaded for any reason, the plugin icon will be displayed instead.
public readonly struct TextureWrapTaskIconSource : INotificationIconSource.IInternal
{
/// The function that returns a task resulting in a new instance of .
///
/// Dalamud will take ownership of the result. Do not call .
public readonly Func?>? TextureWrapTaskFunc;
/// Gets the default materialized icon, for the purpose of displaying the plugin icon.
internal static readonly INotificationMaterializedIcon DefaultMaterializedIcon = new MaterializedIcon(null);
/// Initializes a new instance of the struct.
/// The function.
public TextureWrapTaskIconSource(Func?>? taskFunc) =>
this.TextureWrapTaskFunc = taskFunc;
///
public INotificationIconSource Clone() => this;
///
void IDisposable.Dispose()
{
}
///
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() =>
new MaterializedIcon(this.TextureWrapTaskFunc);
private sealed class MaterializedIcon : INotificationMaterializedIcon
{
private Task? task;
public MaterializedIcon(Func?>? taskFunc) => this.task = taskFunc?.Invoke();
public void Dispose()
{
this.task?.ToContentDisposedTask(true);
this.task = null;
}
public void DrawIcon(Vector2 minCoord, Vector2 maxCoord, Vector4 color, LocalPlugin? initiatorPlugin) =>
NotificationUtilities.DrawTexture(
this.task?.IsCompletedSuccessfully is true ? this.task.Result : null,
minCoord,
maxCoord,
initiatorPlugin);
}
}