Make IFontHandle.Pop return a concrete struct

This commit is contained in:
Soreepeong 2024-01-21 00:45:46 +09:00
parent dd5cbdfd5d
commit 8afe277c02
4 changed files with 49 additions and 7 deletions

View file

@ -2,6 +2,7 @@ using System.Numerics;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.ManagedFontAtlas.Internals;
using Dalamud.Utility;
using ImGuiNET;
@ -10,6 +11,7 @@ namespace Dalamud.Interface.GameFonts;
/// <summary>
/// ABI-compatible wrapper for <see cref="IFontHandle"/>.
/// </summary>
[Api10ToDo(Api10ToDoAttribute.DeleteCompatBehavior)]
public sealed class GameFontHandle : IFontHandle
{
private readonly IFontHandle.IInternal fontHandle;
@ -53,8 +55,14 @@ public sealed class GameFontHandle : IFontHandle
/// <inheritdoc />
public void Dispose() => this.fontHandle.Dispose();
/// <inheritdoc/>
/// <summary>
/// Pushes the font.
/// </summary>
/// <returns>An <see cref="IDisposable"/> that can be used to pop the font on dispose.</returns>
public IDisposable Push() => this.fontHandle.Push();
/// <inheritdoc/>
IFontHandle.FontPopper IFontHandle.Push() => this.fontHandle.Push();
/// <summary>
/// Creates a new <see cref="GameFontLayoutPlan.Builder"/>.<br />

View file

@ -1,4 +1,6 @@
using ImGuiNET;
using Dalamud.Utility;
using ImGuiNET;
namespace Dalamud.Interface.ManagedFontAtlas;
@ -38,5 +40,39 @@ public interface IFontHandle : IDisposable
/// You may not access the font once you dispose this object.
/// </summary>
/// <returns>A disposable object that will call <see cref="ImGui.PopFont"/>(1) on dispose.</returns>
IDisposable Push();
/// <exception cref="InvalidOperationException">If called outside of the main thread.</exception>
FontPopper Push();
/// <summary>
/// The wrapper for popping fonts.
/// </summary>
public struct FontPopper : IDisposable
{
private int count;
/// <summary>
/// Initializes a new instance of the <see cref="FontPopper"/> struct.
/// </summary>
/// <param name="fontPtr">The font to push.</param>
/// <param name="push">Whether to push.</param>
internal FontPopper(ImFontPtr fontPtr, bool push)
{
if (!push)
return;
ThreadSafety.AssertMainThread();
this.count = 1;
ImGui.PushFont(fontPtr);
}
/// <inheritdoc />
public void Dispose()
{
ThreadSafety.AssertMainThread();
while (this.count-- > 0)
ImGui.PopFont();
}
}
}

View file

@ -2,7 +2,6 @@
using System.Linq;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Logging.Internal;
using Dalamud.Utility;
@ -53,7 +52,7 @@ internal class DelegateFontHandle : IFontHandle.IInternal
}
/// <inheritdoc/>
public IDisposable Push() => ImRaii.PushFont(this.ImFont, this.Available);
public IFontHandle.FontPopper Push() => new(this.ImFont, this.Available);
/// <summary>
/// Manager for <see cref="DelegateFontHandle"/>s.

View file

@ -9,7 +9,6 @@ using Dalamud.Game.Text;
using Dalamud.Interface.GameFonts;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Utility;
using Dalamud.Interface.Utility.Raii;
using Dalamud.Utility;
using ImGuiNET;
@ -117,7 +116,7 @@ internal class GamePrebakedFontHandle : IFontHandle.IInternal
}
/// <inheritdoc/>
public IDisposable Push() => ImRaii.PushFont(this.ImFont, this.Available);
public IFontHandle.FontPopper Push() => new(this.ImFont, this.Available);
/// <summary>
/// Manager for <see cref="GamePrebakedFontHandle"/>s.