using System.Numerics;
using System.Threading.Tasks;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Utility;
using Serilog;
namespace Dalamud.Interface.ImGuiNotification.Internal.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.
internal class TextureWrapTaskIconSource : INotificationIconSource.IInternal
{
/// 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 class.
/// The function.
public TextureWrapTaskIconSource(Func?>? taskFunc) =>
this.TextureWrapTaskFunc = taskFunc;
/// Gets the function that returns a task resulting in a new instance of .
///
/// Dalamud will take ownership of the result. Do not call .
public Func?>? TextureWrapTaskFunc { get; }
///
public INotificationIconSource Clone() => this;
///
public void Dispose()
{
}
///
public INotificationMaterializedIcon Materialize() =>
new MaterializedIcon(this.TextureWrapTaskFunc);
private sealed class MaterializedIcon : INotificationMaterializedIcon
{
private Task? task;
public MaterializedIcon(Func?>? taskFunc)
{
try
{
this.task = taskFunc?.Invoke();
}
catch (Exception e)
{
Log.Error(e, $"{nameof(TextureWrapTaskIconSource)}: failed to materialize the icon texture.");
this.task = null;
}
}
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);
}
}