Turn impls of IconSource internal

This commit is contained in:
Soreepeong 2024-02-26 11:54:24 +09:00
parent 42b6f8fd4b
commit f434946137
11 changed files with 153 additions and 68 deletions

View file

@ -1,4 +1,4 @@
using Dalamud.Interface.ImGuiNotification.IconSource;
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal.Notifications;
namespace Dalamud.Interface.ImGuiNotification;

View file

@ -1,3 +1,10 @@
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Dalamud.Game.Text;
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal;
namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Icon source for <see cref="INotification"/>.</summary>
@ -12,6 +19,67 @@ public interface INotificationIconSource : ICloneable, IDisposable
INotificationMaterializedIcon Materialize();
}
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from an
/// <see cref="SeIconChar"/>.</summary>
/// <param name="iconChar">The icon character.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource From(SeIconChar iconChar) => new SeIconCharIconSource(iconChar);
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from an
/// <see cref="FontAwesomeIcon"/>.</summary>
/// <param name="iconChar">The icon character.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource From(FontAwesomeIcon iconChar) => new FontAwesomeIconIconSource(iconChar);
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from an
/// <see cref="IDalamudTextureWrap"/>.</summary>
/// <param name="wrap">The texture wrap.</param>
/// <param name="takeOwnership">
/// If <c>true</c>, this class will own the passed <paramref name="wrap"/>, and you <b>must not</b> call
/// <see cref="IDisposable.Dispose"/> on the passed wrap.
/// If <c>false</c>, this class will create a new reference of the passed wrap, and you <b>should</b> call
/// <see cref="IDisposable.Dispose"/> on the passed wrap.
/// In both cases, the returned object must be disposed after use.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
/// <remarks>If any errors are thrown or <paramref name="wrap"/> is <c>null</c>, the default icon will be displayed
/// instead.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource From(IDalamudTextureWrap? wrap, bool takeOwnership = true) =>
new TextureWrapIconSource(wrap, takeOwnership);
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from an
/// <see cref="Func{TResult}"/> returning a <see cref="Task{TResult}"/> resulting in an
/// <see cref="IDalamudTextureWrap"/>.</summary>
/// <param name="wrapTaskFunc">The function that returns a task that results a texture wrap.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
/// <remarks>If any errors are thrown or <paramref name="wrapTaskFunc"/> is <c>null</c>, the default icon will be
/// displayed instead.<br />
/// Use <see cref="Task.FromResult{TResult}"/> if you will have a wrap available without waiting.<br />
/// <paramref name="wrapTaskFunc"/> should not contain a reference to a resource; if it does, the resource will be
/// released when all instances of <see cref="INotificationIconSource"/> derived from the returned object are freed
/// by the garbage collector, which will result in non-deterministic resource releases.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource From(Func<Task<IDalamudTextureWrap?>?>? wrapTaskFunc) =>
new TextureWrapTaskIconSource(wrapTaskFunc);
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from a texture
/// file shipped as a part of the game resources.</summary>
/// <param name="gamePath">The path to a texture file in the game virtual file system.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
/// <remarks>If any errors are thrown, the default icon will be displayed instead.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource FromGame(string gamePath) => new GamePathIconSource(gamePath);
/// <summary>Gets a new instance of <see cref="INotificationIconSource"/> that will source the icon from an image
/// file from the file system.</summary>
/// <param name="filePath">The path to an image file in the file system.</param>
/// <returns>A new instance of <see cref="INotificationIconSource"/> that should be disposed after use.</returns>
/// <remarks>If any errors are thrown, the default icon will be displayed instead.</remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource FromFile(string filePath) => new FilePathIconSource(filePath);
/// <inheritdoc cref="ICloneable.Clone"/>
new INotificationIconSource Clone();

View file

@ -4,7 +4,7 @@ using System.Runtime.Loader;
using Dalamud.Interface.Animation;
using Dalamud.Interface.Animation.EasingFunctions;
using Dalamud.Interface.Colors;
using Dalamud.Interface.ImGuiNotification.IconSource;
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Utility;
@ -18,7 +18,7 @@ using Serilog;
namespace Dalamud.Interface.ImGuiNotification.Internal;
/// <summary>Represents an active notification.</summary>
internal sealed class ActiveNotification : IActiveNotification, IDisposable
internal sealed class ActiveNotification : IActiveNotification
{
private readonly Notification underlyingNotification;

View file

@ -1,33 +1,32 @@
using System.IO;
using System.Numerics;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of a texture from a file as the icon of a notification.</summary>
/// <remarks>If there was no texture loaded for any reason, the plugin icon will be displayed instead.</remarks>
public readonly struct FilePathIconSource : INotificationIconSource.IInternal
internal class FilePathIconSource : INotificationIconSource.IInternal
{
/// <summary>The path to a .tex file inside the game resources.</summary>
public readonly string FilePath;
/// <summary>Initializes a new instance of the <see cref="FilePathIconSource"/> struct.</summary>
/// <summary>Initializes a new instance of the <see cref="FilePathIconSource"/> class.</summary>
/// <param name="filePath">The path to a .tex file inside the game resources.</param>
public FilePathIconSource(string filePath) => this.FilePath = filePath;
/// <summary>Gets the path to a .tex file inside the game resources.</summary>
public string FilePath { get; }
/// <inheritdoc/>
public INotificationIconSource Clone() => this;
/// <inheritdoc/>
void IDisposable.Dispose()
public void Dispose()
{
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() =>
public INotificationMaterializedIcon Materialize() =>
new MaterializedIcon(this.FilePath);
private sealed class MaterializedIcon : INotificationMaterializedIcon

View file

@ -1,32 +1,31 @@
using System.Numerics;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Plugin.Internal.Types;
using ImGuiNET;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of <see cref="FontAwesomeIcon"/> as the icon of a notification.</summary>
public readonly struct FontAwesomeIconIconSource : INotificationIconSource.IInternal
internal class FontAwesomeIconIconSource : INotificationIconSource.IInternal
{
/// <summary>The icon character.</summary>
public readonly FontAwesomeIcon Char;
/// <summary>Initializes a new instance of the <see cref="FontAwesomeIconIconSource"/> class.</summary>
/// <param name="iconChar">The character.</param>
public FontAwesomeIconIconSource(FontAwesomeIcon iconChar) => this.IconChar = iconChar;
/// <summary>Initializes a new instance of the <see cref="FontAwesomeIconIconSource"/> struct.</summary>
/// <param name="c">The character.</param>
public FontAwesomeIconIconSource(FontAwesomeIcon c) => this.Char = c;
/// <summary>Gets the icon character.</summary>
public FontAwesomeIcon IconChar { get; }
/// <inheritdoc/>
public INotificationIconSource Clone() => this;
/// <inheritdoc/>
void IDisposable.Dispose()
public void Dispose()
{
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() => new MaterializedIcon(this.Char);
public INotificationMaterializedIcon Materialize() => new MaterializedIcon(this.IconChar);
/// <summary>Draws the icon.</summary>
/// <param name="iconString">The icon string.</param>

View file

@ -1,34 +1,33 @@
using System.Numerics;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Plugin.Services;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of a game-shipped texture as the icon of a notification.</summary>
/// <remarks>If there was no texture loaded for any reason, the plugin icon will be displayed instead.</remarks>
public readonly struct GamePathIconSource : INotificationIconSource.IInternal
internal class GamePathIconSource : INotificationIconSource.IInternal
{
/// <summary>The path to a .tex file inside the game resources.</summary>
public readonly string GamePath;
/// <summary>Initializes a new instance of the <see cref="GamePathIconSource"/> struct.</summary>
/// <summary>Initializes a new instance of the <see cref="GamePathIconSource"/> class.</summary>
/// <param name="gamePath">The path to a .tex file inside the game resources.</param>
/// <remarks>Use <see cref="ITextureProvider.GetIconPath"/> to get the game path from icon IDs.</remarks>
public GamePathIconSource(string gamePath) => this.GamePath = gamePath;
/// <summary>Gets the path to a .tex file inside the game resources.</summary>
public string GamePath { get; }
/// <inheritdoc/>
public INotificationIconSource Clone() => this;
/// <inheritdoc/>
void IDisposable.Dispose()
public void Dispose()
{
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() =>
public INotificationMaterializedIcon Materialize() =>
new MaterializedIcon(this.GamePath);
private sealed class MaterializedIcon : INotificationMaterializedIcon

View file

@ -1,33 +1,32 @@
using System.Numerics;
using Dalamud.Game.Text;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Plugin.Internal.Types;
using ImGuiNET;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of <see cref="SeIconChar"/> as the icon of a notification.</summary>
public readonly struct SeIconCharIconSource : INotificationIconSource.IInternal
internal class SeIconCharIconSource : INotificationIconSource.IInternal
{
/// <summary>The icon character.</summary>
public readonly SeIconChar Char;
/// <summary>Initializes a new instance of the <see cref="SeIconCharIconSource"/> struct.</summary>
/// <summary>Initializes a new instance of the <see cref="SeIconCharIconSource"/> class.</summary>
/// <param name="c">The character.</param>
public SeIconCharIconSource(SeIconChar c) => this.Char = c;
public SeIconCharIconSource(SeIconChar c) => this.IconChar = c;
/// <summary>Gets the icon character.</summary>
public SeIconChar IconChar { get; }
/// <inheritdoc/>
public INotificationIconSource Clone() => this;
/// <inheritdoc/>
void IDisposable.Dispose()
public void Dispose()
{
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() => new MaterializedIcon(this.Char);
public INotificationMaterializedIcon Materialize() => new MaterializedIcon(this.IconChar);
private sealed class MaterializedIcon : INotificationMaterializedIcon
{

View file

@ -1,15 +1,14 @@
using System.Numerics;
using System.Threading;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of future <see cref="IDalamudTextureWrap"/> as the icon of a notification.</summary>
/// <remarks>If there was no texture loaded for any reason, the plugin icon will be displayed instead.</remarks>
public sealed class TextureWrapIconSource : INotificationIconSource.IInternal
internal class TextureWrapIconSource : INotificationIconSource.IInternal
{
private IDalamudTextureWrap? wrap;
@ -38,7 +37,7 @@ public sealed class TextureWrapIconSource : INotificationIconSource.IInternal
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() =>
public INotificationMaterializedIcon Materialize() =>
new MaterializedIcon(this.wrap?.CreateWrapSharingLowLevelResource());
private sealed class MaterializedIcon : INotificationMaterializedIcon

View file

@ -1,42 +1,41 @@
using System.Numerics;
using System.Threading.Tasks;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.Internal;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Utility;
using Serilog;
namespace Dalamud.Interface.ImGuiNotification.IconSource;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of future <see cref="IDalamudTextureWrap"/> as the icon of a notification.</summary>
/// <remarks>If there was no texture loaded for any reason, the plugin icon will be displayed instead.</remarks>
public readonly struct TextureWrapTaskIconSource : INotificationIconSource.IInternal
internal class TextureWrapTaskIconSource : INotificationIconSource.IInternal
{
/// <summary>The function that returns a task resulting in a new instance of <see cref="IDalamudTextureWrap"/>.
/// </summary>
/// <remarks>Dalamud will take ownership of the result. Do not call <see cref="IDisposable.Dispose"/>.</remarks>
public readonly Func<Task<IDalamudTextureWrap?>?>? TextureWrapTaskFunc;
/// <summary>Gets the default materialized icon, for the purpose of displaying the plugin icon.</summary>
internal static readonly INotificationMaterializedIcon DefaultMaterializedIcon = new MaterializedIcon(null);
/// <summary>Initializes a new instance of the <see cref="TextureWrapTaskIconSource"/> struct.</summary>
/// <summary>Initializes a new instance of the <see cref="TextureWrapTaskIconSource"/> class.</summary>
/// <param name="taskFunc">The function.</param>
public TextureWrapTaskIconSource(Func<Task<IDalamudTextureWrap?>?>? taskFunc) =>
this.TextureWrapTaskFunc = taskFunc;
/// <summary>Gets the function that returns a task resulting in a new instance of <see cref="IDalamudTextureWrap"/>.
/// </summary>
/// <remarks>Dalamud will take ownership of the result. Do not call <see cref="IDisposable.Dispose"/>.</remarks>
public Func<Task<IDalamudTextureWrap?>?>? TextureWrapTaskFunc { get; }
/// <inheritdoc/>
public INotificationIconSource Clone() => this;
/// <inheritdoc/>
void IDisposable.Dispose()
public void Dispose()
{
}
/// <inheritdoc/>
INotificationMaterializedIcon INotificationIconSource.IInternal.Materialize() =>
public INotificationMaterializedIcon Materialize() =>
new MaterializedIcon(this.TextureWrapTaskFunc);
private sealed class MaterializedIcon : INotificationMaterializedIcon

View file

@ -1,5 +1,8 @@
using System.IO;
using System.Numerics;
using System.Runtime.CompilerServices;
using Dalamud.Game.Text;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Windows;
using Dalamud.Plugin.Internal.Types;
@ -7,17 +10,37 @@ using Dalamud.Storage.Assets;
using ImGuiNET;
namespace Dalamud.Interface.ImGuiNotification.Internal;
namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Utilities for implementing stuff under <see cref="ImGuiNotification"/>.</summary>
internal static class NotificationUtilities
public static class NotificationUtilities
{
/// <inheritdoc cref="INotificationIconSource.From(SeIconChar)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource ToIconSource(this SeIconChar iconChar) =>
INotificationIconSource.From(iconChar);
/// <inheritdoc cref="INotificationIconSource.From(FontAwesomeIcon)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource ToIconSource(this FontAwesomeIcon iconChar) =>
INotificationIconSource.From(iconChar);
/// <inheritdoc cref="INotificationIconSource.From(IDalamudTextureWrap,bool)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource ToIconSource(this IDalamudTextureWrap? wrap, bool takeOwnership = true) =>
INotificationIconSource.From(wrap, takeOwnership);
/// <inheritdoc cref="INotificationIconSource.FromFile(string)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static INotificationIconSource ToIconSource(this FileInfo fileInfo) =>
INotificationIconSource.FromFile(fileInfo.FullName);
/// <summary>Draws the given texture, or the icon of the plugin if texture is <c>null</c>.</summary>
/// <param name="texture">The texture.</param>
/// <param name="minCoord">The coordinates of the top left of the icon area.</param>
/// <param name="maxCoord">The coordinates of the bottom right of the icon area.</param>
/// <param name="initiatorPlugin">The initiator plugin.</param>
public static void DrawTexture(
internal static void DrawTexture(
IDalamudTextureWrap? texture,
Vector2 minCoord,
Vector2 maxCoord,

View file

@ -3,8 +3,8 @@ using System.Threading.Tasks;
using Dalamud.Game.Text;
using Dalamud.Interface.ImGuiNotification;
using Dalamud.Interface.ImGuiNotification.IconSource;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Windowing;
using Dalamud.Storage.Assets;
@ -161,32 +161,32 @@ internal class ImGuiWidget : IDataWindowWidget
},
IconSource = this.notificationTemplate.IconSourceInt switch
{
1 => new SeIconCharIconSource(
1 => INotificationIconSource.From(
(SeIconChar)(this.notificationTemplate.IconSourceText.Length == 0
? 0
: this.notificationTemplate.IconSourceText[0])),
2 => new FontAwesomeIconIconSource(
2 => INotificationIconSource.From(
(FontAwesomeIcon)(this.notificationTemplate.IconSourceText.Length == 0
? 0
: this.notificationTemplate.IconSourceText[0])),
3 => new TextureWrapIconSource(
3 => INotificationIconSource.From(
Service<DalamudAssetManager>.Get().GetDalamudTextureWrap(
Enum.Parse<DalamudAsset>(
NotificationTemplate.AssetSources[
this.notificationTemplate.IconSourceAssetInt])),
false),
4 => new TextureWrapTaskIconSource(
4 => INotificationIconSource.From(
() =>
Service<DalamudAssetManager>.Get().GetDalamudTextureWrapAsync(
Enum.Parse<DalamudAsset>(
NotificationTemplate.AssetSources[
this.notificationTemplate.IconSourceAssetInt]))),
5 => new GamePathIconSource(this.notificationTemplate.IconSourceText),
6 => new FilePathIconSource(this.notificationTemplate.IconSourceText),
7 => new TextureWrapIconSource(
5 => INotificationIconSource.FromGame(this.notificationTemplate.IconSourceText),
6 => INotificationIconSource.FromFile(this.notificationTemplate.IconSourceText),
7 => INotificationIconSource.From(
Service<TextureManager>.Get().GetTextureFromGame(this.notificationTemplate.IconSourceText),
false),
8 => new TextureWrapIconSource(
8 => INotificationIconSource.From(
Service<TextureManager>.Get().GetTextureFromFile(
new(this.notificationTemplate.IconSourceText)),
false),