Make notifications minimizable, remove interactable

This commit is contained in:
Soreepeong 2024-02-26 19:51:00 +09:00
parent e96089f8b2
commit 0040f61125
12 changed files with 688 additions and 424 deletions

View file

@ -1,7 +1,5 @@
using System.Threading;
using Dalamud.Interface.Internal.Notifications;
namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Represents an active notification.</summary>
@ -17,7 +15,6 @@ public interface IActiveNotification : INotification
/// <summary>Invoked upon clicking on the notification.</summary>
/// <remarks>
/// This event is not applicable when <see cref="INotification.Interactable"/> is set to <c>false</c>.
/// Note that this function may be called even after <see cref="Dismiss"/> has been invoked.
/// Refer to <see cref="IsDismissed"/>.
/// </remarks>
@ -25,7 +22,6 @@ public interface IActiveNotification : INotification
/// <summary>Invoked when the mouse enters the notification window.</summary>
/// <remarks>
/// This event is applicable regardless of <see cref="INotification.Interactable"/>.
/// Note that this function may be called even after <see cref="Dismiss"/> has been invoked.
/// Refer to <see cref="IsDismissed"/>.
/// </remarks>
@ -33,7 +29,6 @@ public interface IActiveNotification : INotification
/// <summary>Invoked when the mouse leaves the notification window.</summary>
/// <remarks>
/// This event is applicable regardless of <see cref="INotification.Interactable"/>.
/// Note that this function may be called even after <see cref="Dismiss"/> has been invoked.
/// Refer to <see cref="IsDismissed"/>.
/// </remarks>
@ -41,42 +36,18 @@ public interface IActiveNotification : INotification
/// <summary>Invoked upon drawing the action bar of the notification.</summary>
/// <remarks>
/// This event is applicable regardless of <see cref="INotification.Interactable"/>.
/// Note that this function may be called even after <see cref="Dismiss"/> has been invoked.
/// Refer to <see cref="IsDismissed"/>.
/// </remarks>
event Action<IActiveNotification> DrawActions;
/// <inheritdoc cref="INotification.Content"/>
new string Content { get; set; }
/// <inheritdoc cref="INotification.Title"/>
new string? Title { get; set; }
/// <inheritdoc cref="INotification.Type"/>
new NotificationType Type { get; set; }
/// <inheritdoc cref="INotification.Expiry"/>
new DateTime Expiry { get; set; }
/// <inheritdoc cref="INotification.ShowIndeterminateIfNoExpiry"/>
new bool ShowIndeterminateIfNoExpiry { get; set; }
/// <inheritdoc cref="INotification.Interactable"/>
new bool Interactable { get; set; }
/// <inheritdoc cref="INotification.UserDismissable"/>
new bool UserDismissable { get; set; }
/// <inheritdoc cref="INotification.HoverExtendDuration"/>
new TimeSpan HoverExtendDuration { get; set; }
/// <inheritdoc cref="INotification.Progress"/>
new float Progress { get; set; }
/// <summary>Gets the ID of this notification.</summary>
long Id { get; }
/// <summary>Gets the effective expiry time.</summary>
/// <remarks>Contains <see cref="DateTime.MaxValue"/> if the notification does not expire.</remarks>
DateTime EffectiveExpiry { get; }
/// <summary>Gets a value indicating whether the mouse cursor is on the notification window.</summary>
bool IsMouseHovered { get; }
@ -87,16 +58,15 @@ public interface IActiveNotification : INotification
/// <summary>Dismisses this notification.</summary>
void DismissNow();
/// <summary>Extends this notifiation.</summary>
/// <param name="extension">The extension time.</param>
/// <remarks>This does not override <see cref="INotification.HardExpiry"/>.</remarks>
void ExtendBy(TimeSpan extension);
/// <summary>Loads the icon again using the same <see cref="INotification.IconSource"/>.</summary>
/// <remarks>If <see cref="IsDismissed"/> is <c>true</c>, then this function is a no-op.</remarks>
void UpdateIcon();
/// <summary>Disposes the previous icon source, take ownership of the new icon source,
/// and calls <see cref="UpdateIcon"/>.</summary>
/// <param name="newIconSource">Thew new icon source.</param>
/// <remarks>If <see cref="IsDismissed"/> is <c>true</c>, then this function is a no-op.</remarks>
void UpdateIconSource(INotificationIconSource? newIconSource);
/// <summary>Generates a new value to use for <see cref="Id"/>.</summary>
/// <returns>The new value.</returns>
internal static long CreateNewId() => Interlocked.Increment(ref idCounter);

View file

@ -1,4 +1,3 @@
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal.Notifications;
namespace Dalamud.Interface.ImGuiNotification;
@ -6,63 +5,69 @@ namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Represents a notification.</summary>
public interface INotification : IDisposable
{
/// <summary>Gets the content body of the notification.</summary>
string Content { get; }
/// <summary>Gets or sets the content body of the notification.</summary>
string Content { get; set; }
/// <summary>Gets the title of the notification.</summary>
string? Title { get; }
/// <summary>Gets or sets the title of the notification.</summary>
string? Title { get; set; }
/// <summary>Gets the type of the notification.</summary>
NotificationType Type { get; }
/// <summary>Gets or sets the text to display when the notification is minimized.</summary>
string? MinimizedText { get; set; }
/// <summary>Gets the icon source.</summary>
/// <summary>Gets or sets the type of the notification.</summary>
NotificationType Type { get; set; }
/// <summary>Gets or sets the icon source.</summary>
/// <remarks>
/// The assigned value will be disposed upon the <see cref="IDisposable.Dispose"/> call on this instance of
/// <see cref="INotification"/>.<br />
/// <br />
/// The following icon sources are currently available.<br />
/// <ul>
/// <li><see cref="SeIconCharIconSource"/></li>
/// <li><see cref="FontAwesomeIconIconSource"/></li>
/// <li><see cref="TextureWrapIconSource"/></li>
/// <li><see cref="TextureWrapTaskIconSource"/></li>
/// <li><see cref="GamePathIconSource"/></li>
/// <li><see cref="FilePathIconSource"/></li>
/// </ul>
/// <para>Assigning a new value that does not equal to the previous value will dispose the old value. The ownership
/// of the new value is transferred to this <see cref="INotification"/>. <b>Even if the assignment throws an
/// exception</b>, the ownership is transferred, causing the value to be disposed. Assignment should not throw an
/// exception though, so wrapping the assignment in try...catch block is not required.</para>
/// <para>The assigned value will be disposed upon the <see cref="IDisposable.Dispose"/> call on this instance of
/// <see cref="INotification"/>, unless the same value is assigned, in which case it will do nothing.</para>
/// <para>If this <see cref="INotification"/> is an <see cref="IActiveNotification"/>, then updating this property
/// will change the icon being displayed (calls <see cref="IActiveNotification.UpdateIcon"/>), unless
/// <see cref="IActiveNotification.IsDismissed"/> is <c>true</c>.</para>
/// </remarks>
INotificationIconSource? IconSource { get; }
INotificationIconSource? IconSource { get; set; }
/// <summary>Gets the expiry.</summary>
/// <remarks>Set to <see cref="DateTime.MaxValue"/> to make the notification not have an expiry time
/// (sticky, indeterminate, permanent, or persistent).</remarks>
DateTime Expiry { get; }
/// <summary>Gets a value indicating whether to show an indeterminate expiration animation if <see cref="Expiry"/>
/// is set to <see cref="DateTime.MaxValue"/>.</summary>
bool ShowIndeterminateIfNoExpiry { get; }
/// <summary>Gets a value indicating whether this notification may be interacted.</summary>
/// <summary>Gets or sets the hard expiry.</summary>
/// <remarks>
/// Set this value to <c>true</c> if you want to respond to user inputs from
/// <see cref="IActiveNotification.DrawActions"/>.
/// Note that the close buttons for notifications are always provided and interactable.
/// If set to <c>true</c>, then clicking on the notification itself will be interpreted as user-initiated dismissal,
/// unless <see cref="IActiveNotification.Click"/> is set or <see cref="UserDismissable"/> is unset.
/// Setting this value will override <see cref="InitialDuration"/> and <see cref="HoverExtendDuration"/>, in that
/// the notification will be dismissed when this expiry expires.<br />
/// Set to <see cref="DateTime.MaxValue"/> to make only <see cref="InitialDuration"/> take effect.<br />
/// If neither <see cref="HardExpiry"/> nor <see cref="InitialDuration"/> is not MaxValue, then the notification
/// will not expire after a set time. It must be explicitly dismissed by the user of via calling
/// <see cref="IActiveNotification.DismissNow"/>.<br />
/// Updating this value will reset the dismiss timer.
/// </remarks>
bool Interactable { get; }
DateTime HardExpiry { get; set; }
/// <summary>Gets a value indicating whether the user can dismiss the notification by themselves.</summary>
/// <summary>Gets or sets the initial duration.</summary>
/// <remarks>Set to <see cref="TimeSpan.MaxValue"/> to make only <see cref="HardExpiry"/> take effect.</remarks>
/// <remarks>Updating this value will reset the dismiss timer.</remarks>
TimeSpan InitialDuration { get; set; }
/// <summary>Gets or sets the new duration for this notification once the mouse cursor leaves the window.</summary>
/// <remarks>
/// If set to <see cref="TimeSpan.Zero"/> or less, then this feature is turned off, and hovering the mouse on the
/// notification will not make the notification stay.<br />
/// Updating this value will reset the dismiss timer.
/// </remarks>
TimeSpan HoverExtendDuration { get; set; }
/// <summary>Gets or sets a value indicating whether to show an indeterminate expiration animation if
/// <see cref="HardExpiry"/> is set to <see cref="DateTime.MaxValue"/>.</summary>
bool ShowIndeterminateIfNoExpiry { get; set; }
/// <summary>Gets or sets a value indicating whether the notification has been minimized.</summary>
bool Minimized { get; set; }
/// <summary>Gets or sets a value indicating whether the user can dismiss the notification by themselves.</summary>
/// <remarks>Consider adding a cancel button to <see cref="IActiveNotification.DrawActions"/>.</remarks>
bool UserDismissable { get; }
bool UserDismissable { get; set; }
/// <summary>Gets the new duration for this notification if mouse cursor is on the notification window.</summary>
/// <remarks>
/// If set to <see cref="TimeSpan.Zero"/> or less, then this feature is turned off.
/// This property is applicable regardless of <see cref="Interactable"/>.
/// </remarks>
TimeSpan HoverExtendDuration { get; }
/// <summary>Gets the progress for the background progress bar of the notification.</summary>
/// <summary>Gets or sets the progress for the background progress bar of the notification.</summary>
/// <remarks>The progress should be in the range between 0 and 1.</remarks>
float Progress { get; }
float Progress { get; set; }
}

View file

@ -25,6 +25,7 @@ internal sealed class ActiveNotification : IActiveNotification
private readonly Easing showEasing;
private readonly Easing hideEasing;
private readonly Easing progressEasing;
private readonly Easing expandoEasing;
/// <summary>The progress before for the progress bar animation with <see cref="progressEasing"/>.</summary>
private float progressBefore;
@ -38,6 +39,9 @@ internal sealed class ActiveNotification : IActiveNotification
/// <summary>New progress value to be updated on next call to <see cref="UpdateAnimations"/>.</summary>
private float? newProgress;
/// <summary>New minimized value to be updated on next call to <see cref="UpdateAnimations"/>.</summary>
private bool? newMinimized;
/// <summary>Initializes a new instance of the <see cref="ActiveNotification"/> class.</summary>
/// <param name="underlyingNotification">The underlying notification.</param>
/// <param name="initiatorPlugin">The initiator plugin. Use <c>null</c> if originated by Dalamud.</param>
@ -51,6 +55,7 @@ internal sealed class ActiveNotification : IActiveNotification
this.showEasing = new InCubic(NotificationConstants.ShowAnimationDuration);
this.hideEasing = new OutCubic(NotificationConstants.HideAnimationDuration);
this.progressEasing = new InOutCubic(NotificationConstants.ProgressChangeAnimationDuration);
this.expandoEasing = new InOutCubic(NotificationConstants.ExpandoAnimationDuration);
this.showEasing.Start();
this.progressEasing.Start();
@ -88,9 +93,12 @@ internal sealed class ActiveNotification : IActiveNotification
public DateTime CreatedAt { get; } = DateTime.Now;
/// <summary>Gets the time of starting to count the timer for the expiration.</summary>
public DateTime ExpiryRelativeToTime { get; private set; } = DateTime.Now;
public DateTime HoverRelativeToTime { get; private set; } = DateTime.Now;
/// <inheritdoc cref="IActiveNotification.Content"/>
/// <summary>Gets the extended expiration time from <see cref="ExtendBy"/>.</summary>
public DateTime ExtendedExpiry { get; private set; } = DateTime.Now;
/// <inheritdoc/>
public string Content
{
get => this.underlyingNotification.Content;
@ -102,7 +110,7 @@ internal sealed class ActiveNotification : IActiveNotification
}
}
/// <inheritdoc cref="IActiveNotification.Title"/>
/// <inheritdoc/>
public string? Title
{
get => this.underlyingNotification.Title;
@ -114,7 +122,19 @@ internal sealed class ActiveNotification : IActiveNotification
}
}
/// <inheritdoc cref="IActiveNotification.Type"/>
/// <inheritdoc/>
public string? MinimizedText
{
get => this.underlyingNotification.MinimizedText;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.MinimizedText = value;
}
}
/// <inheritdoc/>
public NotificationType Type
{
get => this.underlyingNotification.Type;
@ -127,22 +147,98 @@ internal sealed class ActiveNotification : IActiveNotification
}
/// <inheritdoc/>
public INotificationIconSource? IconSource => this.underlyingNotification.IconSource;
/// <inheritdoc cref="IActiveNotification.Expiry"/>
public DateTime Expiry
public INotificationIconSource? IconSource
{
get => this.underlyingNotification.Expiry;
get => this.underlyingNotification.IconSource;
set
{
if (this.underlyingNotification.Expiry == value || this.IsDismissed)
if (this.IsDismissed)
{
value?.Dispose();
return;
this.underlyingNotification.Expiry = value;
this.ExpiryRelativeToTime = DateTime.Now;
}
this.underlyingNotification.IconSource = value;
this.UpdateIcon();
}
}
/// <inheritdoc cref="IActiveNotification.ShowIndeterminateIfNoExpiry"/>
/// <inheritdoc/>
public DateTime HardExpiry
{
get => this.underlyingNotification.HardExpiry;
set
{
if (this.underlyingNotification.HardExpiry == value || this.IsDismissed)
return;
this.underlyingNotification.HardExpiry = value;
this.HoverRelativeToTime = DateTime.Now;
}
}
/// <inheritdoc/>
public TimeSpan InitialDuration
{
get => this.underlyingNotification.InitialDuration;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.InitialDuration = value;
this.HoverRelativeToTime = DateTime.Now;
}
}
/// <inheritdoc/>
public TimeSpan HoverExtendDuration
{
get => this.underlyingNotification.HoverExtendDuration;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.HoverExtendDuration = value;
this.HoverRelativeToTime = DateTime.Now;
}
}
/// <inheritdoc/>
public DateTime EffectiveExpiry
{
get
{
var initialDuration = this.InitialDuration;
var expiryInitial =
initialDuration == TimeSpan.MaxValue
? DateTime.MaxValue
: this.CreatedAt + initialDuration;
DateTime expiry;
var hoverExtendDuration = this.HoverExtendDuration;
if (hoverExtendDuration > TimeSpan.Zero && this.IsMouseHovered)
{
expiry = DateTime.MaxValue;
}
else
{
var expiryExtend =
hoverExtendDuration == TimeSpan.MaxValue
? DateTime.MaxValue
: this.HoverRelativeToTime + hoverExtendDuration;
expiry = expiryInitial > expiryExtend ? expiryInitial : expiryExtend;
if (expiry < this.ExtendedExpiry)
expiry = this.ExtendedExpiry;
}
var he = this.HardExpiry;
if (he < expiry)
expiry = he;
return expiry;
}
}
/// <inheritdoc/>
public bool ShowIndeterminateIfNoExpiry
{
get => this.underlyingNotification.ShowIndeterminateIfNoExpiry;
@ -154,19 +250,19 @@ internal sealed class ActiveNotification : IActiveNotification
}
}
/// <inheritdoc cref="IActiveNotification.Interactable"/>
public bool Interactable
/// <inheritdoc/>
public bool Minimized
{
get => this.underlyingNotification.Interactable;
get => this.newMinimized ?? this.underlyingNotification.Minimized;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.Interactable = value;
this.newMinimized = value;
}
}
/// <inheritdoc cref="IActiveNotification.UserDismissable"/>
/// <inheritdoc/>
public bool UserDismissable
{
get => this.underlyingNotification.UserDismissable;
@ -178,19 +274,7 @@ internal sealed class ActiveNotification : IActiveNotification
}
}
/// <inheritdoc cref="IActiveNotification.HoverExtendDuration"/>
public TimeSpan HoverExtendDuration
{
get => this.underlyingNotification.HoverExtendDuration;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.HoverExtendDuration = value;
}
}
/// <inheritdoc cref="IActiveNotification.Progress"/>
/// <inheritdoc/>
public float Progress
{
get => this.newProgress ?? this.underlyingNotification.Progress;
@ -198,7 +282,6 @@ internal sealed class ActiveNotification : IActiveNotification
{
if (this.IsDismissed)
return;
this.newProgress = value;
}
}
@ -244,13 +327,13 @@ internal sealed class ActiveNotification : IActiveNotification
};
/// <summary>Gets the default icon of the notification.</summary>
private string? DefaultIconString => this.Type switch
private char? DefaultIconChar => this.Type switch
{
NotificationType.None => null,
NotificationType.Success => FontAwesomeIcon.CheckCircle.ToIconString(),
NotificationType.Warning => FontAwesomeIcon.ExclamationCircle.ToIconString(),
NotificationType.Error => FontAwesomeIcon.TimesCircle.ToIconString(),
NotificationType.Info => FontAwesomeIcon.InfoCircle.ToIconString(),
NotificationType.Success => FontAwesomeIcon.CheckCircle.ToIconChar(),
NotificationType.Warning => FontAwesomeIcon.ExclamationCircle.ToIconChar(),
NotificationType.Error => FontAwesomeIcon.TimesCircle.ToIconChar(),
NotificationType.Info => FontAwesomeIcon.InfoCircle.ToIconChar(),
_ => null,
};
@ -273,6 +356,9 @@ internal sealed class ActiveNotification : IActiveNotification
? NotificationConstants.UnloadedInitiatorNameFormat.Format(initiatorPlugin.Name)
: initiatorPlugin.Name;
/// <summary>Gets the effective text to display when minimized.</summary>
private string EffectiveMinimizedText => (this.MinimizedText ?? this.Content).ReplaceLineEndings(" ");
/// <inheritdoc/>
public void Dispose()
{
@ -314,16 +400,38 @@ internal sealed class ActiveNotification : IActiveNotification
this.showEasing.Update();
this.hideEasing.Update();
this.progressEasing.Update();
if (this.newProgress is { } p)
if (this.expandoEasing.IsRunning)
{
this.progressBefore = this.ProgressEased;
this.underlyingNotification.Progress = p;
this.progressEasing.Restart();
this.progressEasing.Update();
this.expandoEasing.Update();
if (this.expandoEasing.IsDone)
this.expandoEasing.Stop();
}
if (this.newProgress is { } newProgressValue)
{
if (Math.Abs(this.underlyingNotification.Progress - newProgressValue) > float.Epsilon)
{
this.progressBefore = this.ProgressEased;
this.underlyingNotification.Progress = newProgressValue;
this.progressEasing.Restart();
this.progressEasing.Update();
}
this.newProgress = null;
}
if (this.newMinimized is { } newMinimizedValue)
{
if (this.underlyingNotification.Minimized != newMinimizedValue)
{
this.underlyingNotification.Minimized = newMinimizedValue;
this.expandoEasing.Restart();
this.expandoEasing.Update();
}
this.newMinimized = null;
}
return this.hideEasing.IsRunning && this.hideEasing.IsDone;
}
@ -333,12 +441,9 @@ internal sealed class ActiveNotification : IActiveNotification
/// <returns>The height of the notification.</returns>
public float Draw(float maxWidth, float offsetY)
{
if (!this.IsDismissed
&& DateTime.Now > this.Expiry
&& (this.HoverExtendDuration <= TimeSpan.Zero || !this.IsMouseHovered))
{
var effectiveExpiry = this.EffectiveExpiry;
if (!this.IsDismissed && DateTime.Now > effectiveExpiry)
this.DismissNow(NotificationDismissReason.Timeout);
}
var opacity =
Math.Clamp(
@ -375,6 +480,12 @@ internal sealed class ActiveNotification : IActiveNotification
unboundedWidth += NotificationConstants.ScaledWindowPadding * 3;
unboundedWidth += NotificationConstants.ScaledIconSize;
var actionWindowHeight =
// Content
ImGui.GetTextLineHeight() +
// Top and bottom padding
(NotificationConstants.ScaledWindowPadding * 2);
var width = Math.Min(maxWidth, unboundedWidth);
var viewport = ImGuiHelpers.MainViewport;
@ -384,6 +495,7 @@ internal sealed class ActiveNotification : IActiveNotification
ImGui.PushID(this.Id.GetHashCode());
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, opacity);
ImGui.PushStyleVar(ImGuiStyleVar.WindowRounding, 0f);
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, new Vector2(NotificationConstants.ScaledWindowPadding));
unsafe
{
ImGui.PushStyleColor(
@ -402,83 +514,49 @@ internal sealed class ActiveNotification : IActiveNotification
new Vector2(0, offsetY),
ImGuiCond.Always,
Vector2.One);
ImGui.SetNextWindowSizeConstraints(new(width, 0), new(width, float.MaxValue));
ImGui.PushStyleVar(
ImGuiStyleVar.WindowPadding,
new Vector2(NotificationConstants.ScaledWindowPadding, 0));
ImGui.SetNextWindowSizeConstraints(
new(width, actionWindowHeight),
new(
width,
!this.underlyingNotification.Minimized || this.expandoEasing.IsRunning
? float.MaxValue
: actionWindowHeight));
ImGui.Begin(
$"##NotifyMainWindow{this.Id}",
ImGuiWindowFlags.AlwaysAutoResize |
ImGuiWindowFlags.NoDecoration |
(this.Interactable
? ImGuiWindowFlags.None
: ImGuiWindowFlags.NoInputs | ImGuiWindowFlags.NoBringToFrontOnFocus) |
ImGuiWindowFlags.NoNav |
ImGuiWindowFlags.NoMove |
ImGuiWindowFlags.NoFocusOnAppearing |
ImGuiWindowFlags.NoDocking);
this.DrawWindowBackgroundProgressBar();
this.DrawNotificationMainWindowContent(width);
this.DrawTopBar(interfaceManager, width, actionWindowHeight);
if (!this.underlyingNotification.Minimized && !this.expandoEasing.IsRunning)
{
this.DrawContentArea(width, actionWindowHeight);
}
else if (this.expandoEasing.IsRunning)
{
if (this.underlyingNotification.Minimized)
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, opacity * (1f - (float)this.expandoEasing.Value));
else
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, opacity * (float)this.expandoEasing.Value);
this.DrawContentArea(width, actionWindowHeight);
ImGui.PopStyleVar();
}
this.DrawExpiryBar(effectiveExpiry);
var windowPos = ImGui.GetWindowPos();
var windowSize = ImGui.GetWindowSize();
var hovered = ImGui.IsWindowHovered();
ImGui.End();
ImGui.PopStyleVar();
offsetY += windowSize.Y;
var actionWindowHeight =
// Content
ImGui.GetTextLineHeight() +
// Top and bottom padding
(NotificationConstants.ScaledWindowPadding * 2);
ImGuiHelpers.ForceNextWindowMainViewport();
ImGui.SetNextWindowPos(
(viewportPos + viewportSize) -
new Vector2(NotificationConstants.ScaledViewportEdgeMargin) -
new Vector2(0, offsetY),
ImGuiCond.Always,
Vector2.One);
ImGui.SetNextWindowSizeConstraints(new(width, actionWindowHeight), new(width, actionWindowHeight));
ImGui.PushStyleVar(ImGuiStyleVar.WindowPadding, Vector2.Zero);
ImGui.Begin(
$"##NotifyActionWindow{this.Id}",
ImGuiWindowFlags.NoDecoration |
ImGuiWindowFlags.NoNav |
ImGuiWindowFlags.NoFocusOnAppearing |
ImGuiWindowFlags.NoDocking);
this.DrawWindowBackgroundProgressBar();
this.DrawNotificationActionWindowContent(interfaceManager, width);
windowSize.Y += actionWindowHeight;
windowPos.Y -= actionWindowHeight;
hovered |= ImGui.IsWindowHovered();
ImGui.End();
ImGui.PopStyleVar();
ImGui.PopStyleColor();
ImGui.PopStyleVar(2);
ImGui.PopStyleVar(3);
ImGui.PopID();
if (hovered)
{
if (this.Click is null)
{
if (this.UserDismissable && ImGui.IsMouseClicked(ImGuiMouseButton.Left))
this.DismissNow(NotificationDismissReason.Manual);
}
else
{
if (ImGui.IsMouseClicked(ImGuiMouseButton.Left)
|| ImGui.IsMouseClicked(ImGuiMouseButton.Right)
|| ImGui.IsMouseClicked(ImGuiMouseButton.Middle))
this.Click.InvokeSafely(this);
}
}
if (windowPos.X <= ImGui.GetIO().MousePos.X
&& windowPos.Y <= ImGui.GetIO().MousePos.Y
&& ImGui.GetIO().MousePos.X < windowPos.X + windowSize.X
@ -489,19 +567,28 @@ internal sealed class ActiveNotification : IActiveNotification
this.IsMouseHovered = true;
this.MouseEnter.InvokeSafely(this);
}
if (this.HoverExtendDuration > TimeSpan.Zero)
this.HoverRelativeToTime = DateTime.Now;
if (hovered)
{
if (this.Click is null)
{
if (this.UserDismissable && ImGui.IsMouseClicked(ImGuiMouseButton.Left))
this.DismissNow(NotificationDismissReason.Manual);
}
else
{
if (ImGui.IsMouseClicked(ImGuiMouseButton.Left)
|| ImGui.IsMouseClicked(ImGuiMouseButton.Right)
|| ImGui.IsMouseClicked(ImGuiMouseButton.Middle))
this.Click.InvokeSafely(this);
}
}
}
else if (this.IsMouseHovered)
{
if (this.HoverExtendDuration > TimeSpan.Zero)
{
var newExpiry = DateTime.Now + this.HoverExtendDuration;
if (newExpiry > this.Expiry)
{
this.underlyingNotification.Expiry = newExpiry;
this.ExpiryRelativeToTime = DateTime.Now;
}
}
this.IsMouseHovered = false;
this.MouseLeave.InvokeSafely(this);
}
@ -509,6 +596,14 @@ internal sealed class ActiveNotification : IActiveNotification
return windowSize.Y;
}
/// <inheritdoc/>
public void ExtendBy(TimeSpan extension)
{
var newExpiry = DateTime.Now + extension;
if (this.ExtendedExpiry < newExpiry)
this.ExtendedExpiry = newExpiry;
}
/// <inheritdoc/>
public void UpdateIcon()
{
@ -518,17 +613,6 @@ internal sealed class ActiveNotification : IActiveNotification
this.MaterializedIcon = (this.IconSource as INotificationIconSource.IInternal)?.Materialize();
}
/// <inheritdoc/>
public void UpdateIconSource(INotificationIconSource? newIconSource)
{
if (this.IsDismissed || this.underlyingNotification.IconSource == newIconSource)
return;
this.underlyingNotification.IconSource?.Dispose();
this.underlyingNotification.IconSource = newIconSource;
this.UpdateIcon();
}
/// <summary>Removes non-Dalamud invocation targets from events.</summary>
public void RemoveNonDalamudInvocations()
{
@ -539,14 +623,13 @@ internal sealed class ActiveNotification : IActiveNotification
this.MouseEnter = RemoveNonDalamudInvocationsCore(this.MouseEnter);
this.MouseLeave = RemoveNonDalamudInvocationsCore(this.MouseLeave);
this.Interactable = true;
this.IsInitiatorUnloaded = true;
this.UserDismissable = true;
this.HoverExtendDuration = NotificationConstants.DefaultHoverExtendDuration;
var newMaxExpiry = DateTime.Now + NotificationConstants.DefaultDisplayDuration;
if (this.Expiry > newMaxExpiry)
this.Expiry = newMaxExpiry;
if (this.EffectiveExpiry > newMaxExpiry)
this.HardExpiry = newMaxExpiry;
return;
@ -617,23 +700,209 @@ internal sealed class ActiveNotification : IActiveNotification
ImGui.PopClipRect();
}
private void DrawNotificationMainWindowContent(float width)
private void DrawTopBar(InterfaceManager interfaceManager, float width, float height)
{
var basePos = ImGui.GetCursorPos();
var windowPos = ImGui.GetWindowPos();
var windowSize = ImGui.GetWindowSize();
var rtOffset = new Vector2(width, 0);
using (interfaceManager.IconFontHandle?.Push())
{
ImGui.PushClipRect(windowPos, windowPos + windowSize with { Y = height }, false);
if (this.UserDismissable)
{
if (this.DrawIconButton(FontAwesomeIcon.Times, rtOffset, height))
this.DismissNow(NotificationDismissReason.Manual);
rtOffset.X -= height;
}
if (this.underlyingNotification.Minimized)
{
if (this.DrawIconButton(FontAwesomeIcon.ChevronDown, rtOffset, height))
this.Minimized = false;
}
else
{
if (this.DrawIconButton(FontAwesomeIcon.ChevronUp, rtOffset, height))
this.Minimized = true;
}
rtOffset.X -= height;
ImGui.PopClipRect();
}
float relativeOpacity;
if (this.expandoEasing.IsRunning)
{
relativeOpacity =
this.underlyingNotification.Minimized
? 1f - (float)this.expandoEasing.Value
: (float)this.expandoEasing.Value;
}
else
{
relativeOpacity = this.underlyingNotification.Minimized ? 0f : 1f;
}
if (this.IsMouseHovered)
ImGui.PushClipRect(windowPos, windowPos + rtOffset with { Y = height }, false);
else
ImGui.PushClipRect(windowPos, windowPos + windowSize with { Y = height }, false);
if (relativeOpacity > 0)
{
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * relativeOpacity);
ImGui.SetCursorPos(new(NotificationConstants.ScaledWindowPadding));
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.WhenTextColor);
ImGui.TextUnformatted(
this.IsMouseHovered
? this.CreatedAt.FormatAbsoluteDateTime()
: this.CreatedAt.FormatRelativeDateTime());
ImGui.PopStyleColor();
ImGui.PopStyleVar();
}
if (relativeOpacity < 1)
{
rtOffset = new(width - NotificationConstants.ScaledWindowPadding, 0);
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * (1f - relativeOpacity));
var ltOffset = new Vector2(NotificationConstants.ScaledWindowPadding);
this.DrawIcon(ltOffset, new(height - (2 * NotificationConstants.ScaledWindowPadding)));
ltOffset.X = height;
var agoText = this.CreatedAt.FormatRelativeDateTimeShort();
var agoSize = ImGui.CalcTextSize(agoText);
rtOffset.X -= agoSize.X;
ImGui.SetCursorPos(rtOffset with { Y = NotificationConstants.ScaledWindowPadding });
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.WhenTextColor);
ImGui.TextUnformatted(agoText);
ImGui.PopStyleColor();
rtOffset.X -= NotificationConstants.ScaledWindowPadding;
ImGui.PushClipRect(
windowPos + ltOffset with { Y = 0 },
windowPos + rtOffset with { Y = height },
true);
ImGui.SetCursorPos(ltOffset with { Y = NotificationConstants.ScaledWindowPadding });
ImGui.TextUnformatted(this.EffectiveMinimizedText);
ImGui.PopClipRect();
ImGui.PopStyleVar();
}
ImGui.PopClipRect();
}
private bool DrawIconButton(FontAwesomeIcon icon, Vector2 rt, float size)
{
ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero);
if (!this.IsMouseHovered)
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 0f);
ImGui.PushStyleColor(ImGuiCol.Button, 0);
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.CloseTextColor);
ImGui.SetCursorPos(rt - new Vector2(size, 0));
var r = ImGui.Button(icon.ToIconString(), new(size));
ImGui.PopStyleColor(2);
if (!this.IsMouseHovered)
ImGui.PopStyleVar();
ImGui.PopStyleVar();
return r;
}
private void DrawContentArea(float width, float actionWindowHeight)
{
var textColumnX = (NotificationConstants.ScaledWindowPadding * 2) + NotificationConstants.ScaledIconSize;
var textColumnWidth = width - textColumnX - NotificationConstants.ScaledWindowPadding;
var textColumnOffset = new Vector2(textColumnX, actionWindowHeight);
this.DrawIcon(
basePos,
basePos + new Vector2(NotificationConstants.ScaledIconSize));
basePos.X += NotificationConstants.ScaledIconSize + NotificationConstants.ScaledWindowPadding;
width -= NotificationConstants.ScaledIconSize + (NotificationConstants.ScaledWindowPadding * 2);
this.DrawTitle(basePos, basePos + new Vector2(width, 0));
basePos.Y = ImGui.GetCursorPosY();
this.DrawContentBody(basePos, basePos + new Vector2(width, 0));
new(NotificationConstants.ScaledWindowPadding, actionWindowHeight),
new(NotificationConstants.ScaledIconSize));
// Intention was to have left, right, and bottom have the window padding and top have the component gap,
// but as ImGui only allows horz/vert padding, we add the extra bottom padding.
// Top padding is zero, as the action window will add the padding.
ImGui.Dummy(new(NotificationConstants.ScaledWindowPadding));
textColumnOffset.Y += this.DrawTitle(textColumnOffset, textColumnWidth);
textColumnOffset.Y += NotificationConstants.ScaledComponentGap;
this.DrawContentBody(textColumnOffset, textColumnWidth);
}
private void DrawIcon(Vector2 minCoord, Vector2 size)
{
var maxCoord = minCoord + size;
if (this.MaterializedIcon is not null)
{
this.MaterializedIcon.DrawIcon(minCoord, maxCoord, this.DefaultIconColor, this.InitiatorPlugin);
return;
}
var defaultIconChar = this.DefaultIconChar;
if (defaultIconChar is not null)
{
NotificationUtilities.DrawIconString(
Service<NotificationManager>.Get().IconFontAwesomeFontHandle,
defaultIconChar.Value,
minCoord,
maxCoord,
this.DefaultIconColor);
return;
}
TextureWrapTaskIconSource.DefaultMaterializedIcon.DrawIcon(
minCoord,
maxCoord,
this.DefaultIconColor,
this.InitiatorPlugin);
}
private float DrawTitle(Vector2 minCoord, float width)
{
ImGui.PushTextWrapPos(minCoord.X + width);
ImGui.SetCursorPos(minCoord);
if ((this.Title ?? this.DefaultTitle) is { } title)
{
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.TitleTextColor);
ImGui.TextUnformatted(title);
ImGui.PopStyleColor();
}
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.BlameTextColor);
ImGui.SetCursorPos(minCoord with { Y = ImGui.GetCursorPosY() });
ImGui.TextUnformatted(this.InitiatorString);
ImGui.PopStyleColor();
ImGui.PopTextWrapPos();
return ImGui.GetCursorPosY() - minCoord.Y;
}
private void DrawContentBody(Vector2 minCoord, float width)
{
ImGui.SetCursorPos(minCoord);
ImGui.PushTextWrapPos(minCoord.X + width);
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.BodyTextColor);
ImGui.TextUnformatted(this.Content);
ImGui.PopStyleColor();
ImGui.PopTextWrapPos();
if (this.DrawActions is not null)
{
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + NotificationConstants.ScaledComponentGap);
try
{
this.DrawActions.Invoke(this);
}
catch
{
// ignore
}
}
}
private void DrawExpiryBar(DateTime effectiveExpiry)
{
float barL, barR;
if (this.IsDismissed)
{
@ -643,7 +912,14 @@ internal sealed class ActiveNotification : IActiveNotification
barL = midpoint - (length * v);
barR = midpoint + (length * v);
}
else if (this.Expiry == DateTime.MaxValue)
else if (this.HoverExtendDuration > TimeSpan.Zero && this.IsMouseHovered)
{
barL = 0f;
barR = 1f;
this.prevProgressL = barL;
this.prevProgressR = barR;
}
else if (effectiveExpiry == DateTime.MaxValue)
{
if (this.ShowIndeterminateIfNoExpiry)
{
@ -663,17 +939,10 @@ internal sealed class ActiveNotification : IActiveNotification
this.prevProgressR = barR = 1f;
}
}
else if (this.HoverExtendDuration > TimeSpan.Zero && this.IsMouseHovered)
{
barL = 0f;
barR = 1f;
this.prevProgressL = barL;
this.prevProgressR = barR;
}
else
{
barL = 1f - (float)((this.Expiry - DateTime.Now).TotalMilliseconds /
(this.Expiry - this.ExpiryRelativeToTime).TotalMilliseconds);
barL = 1f - (float)((effectiveExpiry - DateTime.Now).TotalMilliseconds /
(effectiveExpiry - this.HoverRelativeToTime).TotalMilliseconds);
barR = 1f;
this.prevProgressL = barL;
this.prevProgressR = barR;
@ -692,112 +961,4 @@ internal sealed class ActiveNotification : IActiveNotification
ImGui.GetColorU32(this.DefaultIconColor));
ImGui.PopClipRect();
}
private void DrawIcon(Vector2 minCoord, Vector2 maxCoord)
{
if (this.MaterializedIcon is not null)
{
this.MaterializedIcon.DrawIcon(minCoord, maxCoord, this.DefaultIconColor, this.InitiatorPlugin);
return;
}
var defaultIconString = this.DefaultIconString;
if (!string.IsNullOrWhiteSpace(defaultIconString))
{
FontAwesomeIconIconSource.DrawIconStatic(defaultIconString, minCoord, maxCoord, this.DefaultIconColor);
return;
}
TextureWrapTaskIconSource.DefaultMaterializedIcon.DrawIcon(
minCoord,
maxCoord,
this.DefaultIconColor,
this.InitiatorPlugin);
}
private void DrawTitle(Vector2 minCoord, Vector2 maxCoord)
{
ImGui.PushTextWrapPos(maxCoord.X);
ImGui.SetCursorPos(minCoord);
if ((this.Title ?? this.DefaultTitle) is { } title)
{
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.TitleTextColor);
ImGui.TextUnformatted(title);
ImGui.PopStyleColor();
}
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.BlameTextColor);
ImGui.SetCursorPos(minCoord with { Y = ImGui.GetCursorPosY() });
ImGui.TextUnformatted(this.InitiatorString);
ImGui.PopStyleColor();
ImGui.PopTextWrapPos();
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + NotificationConstants.ScaledComponentGap);
}
private void DrawContentBody(Vector2 minCoord, Vector2 maxCoord)
{
ImGui.SetCursorPos(minCoord);
ImGui.PushTextWrapPos(maxCoord.X);
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.BodyTextColor);
ImGui.TextUnformatted(this.Content);
ImGui.PopStyleColor();
ImGui.PopTextWrapPos();
if (this.DrawActions is not null)
{
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + NotificationConstants.ScaledComponentGap);
try
{
this.DrawActions.Invoke(this);
}
catch
{
// ignore
}
}
}
private void DrawNotificationActionWindowContent(InterfaceManager interfaceManager, float width)
{
ImGui.SetCursorPos(new(NotificationConstants.ScaledWindowPadding));
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.WhenTextColor);
ImGui.TextUnformatted(
this.IsMouseHovered
? this.CreatedAt.FormatAbsoluteDateTime()
: this.CreatedAt.FormatRelativeDateTime());
ImGui.PopStyleColor();
this.DrawCloseButton(
interfaceManager,
new(width - NotificationConstants.ScaledWindowPadding, NotificationConstants.ScaledWindowPadding),
NotificationConstants.ScaledWindowPadding);
}
private void DrawCloseButton(InterfaceManager interfaceManager, Vector2 rt, float pad)
{
if (!this.UserDismissable)
return;
using (interfaceManager.IconFontHandle?.Push())
{
var str = FontAwesomeIcon.Times.ToIconString();
var textSize = ImGui.CalcTextSize(str);
var size = Math.Max(textSize.X, textSize.Y);
ImGui.PushStyleVar(ImGuiStyleVar.FramePadding, Vector2.Zero);
if (!this.IsMouseHovered)
ImGui.PushStyleVar(ImGuiStyleVar.Alpha, 0f);
ImGui.PushStyleColor(ImGuiCol.Button, 0);
ImGui.PushStyleColor(ImGuiCol.Text, NotificationConstants.CloseTextColor);
ImGui.SetCursorPos(rt - new Vector2(size, 0) - new Vector2(pad));
if (ImGui.Button(str, new(size + (pad * 2))))
this.DismissNow(NotificationDismissReason.Manual);
ImGui.PopStyleColor(2);
if (!this.IsMouseHovered)
ImGui.PopStyleVar();
ImGui.PopStyleVar();
}
}
}

View file

@ -2,8 +2,6 @@ using System.Numerics;
using Dalamud.Plugin.Internal.Types;
using ImGuiNET;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of <see cref="FontAwesomeIcon"/> as the icon of a notification.</summary>
@ -27,35 +25,22 @@ internal class FontAwesomeIconIconSource : INotificationIconSource.IInternal
/// <inheritdoc/>
public INotificationMaterializedIcon Materialize() => new MaterializedIcon(this.IconChar);
/// <summary>Draws the icon.</summary>
/// <param name="iconString">The icon string.</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="color">The foreground color.</param>
internal static void DrawIconStatic(string iconString, Vector2 minCoord, Vector2 maxCoord, Vector4 color)
{
using (Service<NotificationManager>.Get().IconFontAwesomeFontHandle.Push())
{
var size = ImGui.CalcTextSize(iconString);
var pos = ((minCoord + maxCoord) - size) / 2;
ImGui.SetCursorPos(pos);
ImGui.PushStyleColor(ImGuiCol.Text, color);
ImGui.TextUnformatted(iconString);
ImGui.PopStyleColor();
}
}
private sealed class MaterializedIcon : INotificationMaterializedIcon
{
private readonly string iconString;
private readonly char iconChar;
public MaterializedIcon(FontAwesomeIcon c) => this.iconString = c.ToIconString();
public MaterializedIcon(FontAwesomeIcon c) => this.iconChar = c.ToIconChar();
public void Dispose()
{
}
public void DrawIcon(Vector2 minCoord, Vector2 maxCoord, Vector4 color, LocalPlugin? initiatorPlugin) =>
DrawIconStatic(this.iconString, minCoord, maxCoord, color);
NotificationUtilities.DrawIconString(
Service<NotificationManager>.Get().IconFontAwesomeFontHandle,
this.iconChar,
minCoord,
maxCoord,
color);
}
}

View file

@ -3,8 +3,6 @@ using System.Numerics;
using Dalamud.Game.Text;
using Dalamud.Plugin.Internal.Types;
using ImGuiNET;
namespace Dalamud.Interface.ImGuiNotification.Internal.IconSource;
/// <summary>Represents the use of <see cref="SeIconChar"/> as the icon of a notification.</summary>
@ -30,25 +28,20 @@ internal class SeIconCharIconSource : INotificationIconSource.IInternal
private sealed class MaterializedIcon : INotificationMaterializedIcon
{
private readonly string iconString;
private readonly char iconChar;
public MaterializedIcon(SeIconChar c) => this.iconString = c.ToIconString();
public MaterializedIcon(SeIconChar c) => this.iconChar = c.ToIconChar();
public void Dispose()
{
}
public void DrawIcon(Vector2 minCoord, Vector2 maxCoord, Vector4 color, LocalPlugin? initiatorPlugin)
{
using (Service<NotificationManager>.Get().IconAxisFontHandle.Push())
{
var size = ImGui.CalcTextSize(this.iconString);
var pos = ((minCoord + maxCoord) - size) / 2;
ImGui.SetCursorPos(pos);
ImGui.PushStyleColor(ImGuiCol.Text, color);
ImGui.TextUnformatted(this.iconString);
ImGui.PopStyleColor();
}
}
public void DrawIcon(Vector2 minCoord, Vector2 maxCoord, Vector4 color, LocalPlugin? initiatorPlugin) =>
NotificationUtilities.DrawIconString(
Service<NotificationManager>.Get().IconAxisFontHandle,
this.iconChar,
minCoord,
maxCoord,
color);
}
}

View file

@ -106,14 +106,15 @@ internal class NotificationManager : INotificationManager, IServiceType, IDispos
var maxWidth = Math.Max(320 * ImGuiHelpers.GlobalScale, viewportSize.X / 3);
this.notifications.RemoveAll(static x =>
{
if (!x.UpdateAnimations())
return false;
this.notifications.RemoveAll(
static x =>
{
if (!x.UpdateAnimations())
return false;
x.Dispose();
return true;
});
x.Dispose();
return true;
});
foreach (var tn in this.notifications)
height += tn.Draw(maxWidth, height) + NotificationConstants.ScaledWindowGap;
}

View file

@ -1,3 +1,5 @@
using System.Threading;
using Dalamud.Interface.Internal.Notifications;
namespace Dalamud.Interface.ImGuiNotification;
@ -5,40 +7,88 @@ namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Represents a blueprint for a notification.</summary>
public sealed record Notification : INotification
{
private INotificationIconSource? iconSource;
/// <summary>Initializes a new instance of the <see cref="Notification"/> class.</summary>
public Notification()
{
}
/// <summary>Initializes a new instance of the <see cref="Notification"/> class.</summary>
/// <param name="notification">The instance of <see cref="INotification"/> to copy from.</param>
public Notification(INotification notification) => this.CopyValuesFrom(notification);
/// <summary>Initializes a new instance of the <see cref="Notification"/> class.</summary>
/// <param name="notification">The instance of <see cref="Notification"/> to copy from.</param>
public Notification(Notification notification) => this.CopyValuesFrom(notification);
/// <inheritdoc/>
public string Content { get; set; } = string.Empty;
/// <inheritdoc/>
public string? Title { get; set; }
/// <inheritdoc/>
public string? MinimizedText { get; set; }
/// <inheritdoc/>
public NotificationType Type { get; set; } = NotificationType.None;
/// <inheritdoc/>
public INotificationIconSource? IconSource { get; set; }
public INotificationIconSource? IconSource
{
get => this.iconSource;
set
{
var prevSource = Interlocked.Exchange(ref this.iconSource, value);
if (prevSource != value)
prevSource?.Dispose();
}
}
/// <inheritdoc/>
public DateTime Expiry { get; set; } = DateTime.Now + NotificationConstants.DefaultDisplayDuration;
public DateTime HardExpiry { get; set; } = DateTime.MaxValue;
/// <inheritdoc/>
public TimeSpan InitialDuration { get; set; } = NotificationConstants.DefaultDisplayDuration;
/// <inheritdoc/>
public TimeSpan HoverExtendDuration { get; set; } = NotificationConstants.DefaultHoverExtendDuration;
/// <inheritdoc/>
public bool ShowIndeterminateIfNoExpiry { get; set; } = true;
/// <inheritdoc/>
public bool Interactable { get; set; } = true;
public bool Minimized { get; set; } = true;
/// <inheritdoc/>
public bool UserDismissable { get; set; } = true;
/// <inheritdoc/>
public TimeSpan HoverExtendDuration { get; set; } = NotificationConstants.DefaultHoverExtendDuration;
/// <inheritdoc/>
public float Progress { get; set; } = 1f;
/// <inheritdoc/>
public void Dispose()
{
this.IconSource?.Dispose();
// Assign to the property; it will take care of disposing
this.IconSource = null;
}
/// <summary>Copy values from the given instance of <see cref="INotification"/>.</summary>
/// <param name="copyFrom">The instance of <see cref="INotification"/> to copy from.</param>
private void CopyValuesFrom(INotification copyFrom)
{
this.Content = copyFrom.Content;
this.Title = copyFrom.Title;
this.MinimizedText = copyFrom.MinimizedText;
this.Type = copyFrom.Type;
this.IconSource = copyFrom.IconSource?.Clone();
this.HardExpiry = copyFrom.HardExpiry;
this.InitialDuration = copyFrom.InitialDuration;
this.HoverExtendDuration = copyFrom.HoverExtendDuration;
this.ShowIndeterminateIfNoExpiry = copyFrom.ShowIndeterminateIfNoExpiry;
this.Minimized = copyFrom.Minimized;
this.UserDismissable = copyFrom.UserDismissable;
this.Progress = copyFrom.Progress;
}
}

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Numerics;
using Dalamud.Interface.Utility;
@ -56,6 +57,9 @@ public static class NotificationConstants
/// <summary>Duration of progress change animation.</summary>
internal static readonly TimeSpan ProgressChangeAnimationDuration = TimeSpan.FromMilliseconds(200);
/// <summary>Duration of expando animation.</summary>
internal static readonly TimeSpan ExpandoAnimationDuration = TimeSpan.FromMilliseconds(300);
/// <summary>Text color for the when.</summary>
internal static readonly Vector4 WhenTextColor = new(0.8f, 0.8f, 0.8f, 1f);
@ -92,6 +96,16 @@ public static class NotificationConstants
(TimeSpan.MinValue, "just now"),
};
/// <summary>Gets the relative time format strings.</summary>
private static readonly (TimeSpan MinSpan, string FormatString)[] RelativeFormatStringsShort =
{
(TimeSpan.FromDays(1), "{0:%d}d"),
(TimeSpan.FromHours(1), "{0:%h}h"),
(TimeSpan.FromMinutes(1), "{0:%m}m"),
(TimeSpan.FromSeconds(1), "{0:%s}s"),
(TimeSpan.MinValue, "now"),
};
/// <summary>Gets the scaled padding of the window (dot(.) in the above diagram).</summary>
internal static float ScaledWindowPadding => MathF.Round(16 * ImGuiHelpers.GlobalScale);
@ -137,4 +151,21 @@ public static class NotificationConstants
/// <param name="when">When.</param>
/// <returns>The formatted string.</returns>
internal static string FormatAbsoluteDateTime(this DateTime when) => $"{when:G}";
/// <summary>Formats an instance of <see cref="DateTime"/> as a relative time.</summary>
/// <param name="when">When.</param>
/// <returns>The formatted string.</returns>
internal static string FormatRelativeDateTimeShort(this DateTime when)
{
var ts = DateTime.Now - when;
foreach (var (minSpan, formatString) in RelativeFormatStringsShort)
{
if (ts < minSpan)
continue;
return string.Format(formatString, ts);
}
Debug.Assert(false, "must not reach here");
return "???";
}
}

View file

@ -3,7 +3,7 @@ namespace Dalamud.Interface.ImGuiNotification;
/// <summary>Specifies the reason of dismissal for a notification.</summary>
public enum NotificationDismissReason
{
/// <summary>The notification is dismissed because the expiry specified from <see cref="INotification.Expiry"/> is
/// <summary>The notification is dismissed because the expiry specified from <see cref="INotification.HardExpiry"/> is
/// met.</summary>
Timeout = 1,

View file

@ -5,6 +5,8 @@ using System.Runtime.CompilerServices;
using Dalamud.Game.Text;
using Dalamud.Interface.Internal;
using Dalamud.Interface.Internal.Windows;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.Interface.Utility;
using Dalamud.Plugin.Internal.Types;
using Dalamud.Storage.Assets;
@ -19,22 +21,56 @@ public static class NotificationUtilities
[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 an icon string.</summary>
/// <param name="fontHandleLarge">The font handle to use.</param>
/// <param name="c">The icon character.</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="color">The foreground color.</param>
internal static unsafe void DrawIconString(
IFontHandle fontHandleLarge,
char c,
Vector2 minCoord,
Vector2 maxCoord,
Vector4 color)
{
var smallerDim = Math.Max(maxCoord.Y - minCoord.Y, maxCoord.X - minCoord.X);
using (fontHandleLarge.Push())
{
var font = ImGui.GetFont();
ref readonly var glyph = ref *(ImGuiHelpers.ImFontGlyphReal*)font.FindGlyph(c).NativePtr;
var size = glyph.XY1 - glyph.XY0;
var smallerSizeDim = Math.Min(size.X, size.Y);
var scale = smallerSizeDim > smallerDim ? smallerDim / smallerSizeDim : 1f;
size *= scale;
var pos = ((minCoord + maxCoord) - size) / 2;
pos += ImGui.GetWindowPos();
ImGui.GetWindowDrawList().AddImage(
font.ContainerAtlas.Textures[glyph.TextureIndex].TexID,
pos,
pos + size,
glyph.UV0,
glyph.UV1,
ImGui.GetColorU32(color with { W = color.W * ImGui.GetStyle().Alpha }));
}
}
/// <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>

View file

@ -4,7 +4,6 @@ using System.Threading.Tasks;
using Dalamud.Game.Text;
using Dalamud.Interface.ImGuiNotification;
using Dalamud.Interface.ImGuiNotification.Internal;
using Dalamud.Interface.ImGuiNotification.Internal.IconSource;
using Dalamud.Interface.Internal.Notifications;
using Dalamud.Interface.Windowing;
using Dalamud.Storage.Assets;
@ -64,6 +63,10 @@ internal class ImGuiWidget : IDataWindowWidget
ImGui.SameLine();
ImGui.InputText("Title##title", ref this.notificationTemplate.Title, 255);
ImGui.Checkbox("##manualMinimizedText", ref this.notificationTemplate.ManualMinimizedText);
ImGui.SameLine();
ImGui.InputText("MinimizedText##minimizedText", ref this.notificationTemplate.MinimizedText, 255);
ImGui.Checkbox("##manualType", ref this.notificationTemplate.ManualType);
ImGui.SameLine();
ImGui.Combo(
@ -107,10 +110,16 @@ internal class ImGuiWidget : IDataWindowWidget
}
ImGui.Combo(
"Duration",
ref this.notificationTemplate.DurationInt,
NotificationTemplate.DurationTitles,
NotificationTemplate.DurationTitles.Length);
"Initial Duration",
ref this.notificationTemplate.InitialDurationInt,
NotificationTemplate.InitialDurationTitles,
NotificationTemplate.InitialDurationTitles.Length);
ImGui.Combo(
"Hover Extend Duration",
ref this.notificationTemplate.HoverExtendDurationInt,
NotificationTemplate.HoverExtendDurationTitles,
NotificationTemplate.HoverExtendDurationTitles.Length);
ImGui.Combo(
"Progress",
@ -118,7 +127,7 @@ internal class ImGuiWidget : IDataWindowWidget
NotificationTemplate.ProgressModeTitles,
NotificationTemplate.ProgressModeTitles.Length);
ImGui.Checkbox("Interactable", ref this.notificationTemplate.Interactable);
ImGui.Checkbox("Minimized", ref this.notificationTemplate.Minimized);
ImGui.Checkbox("Show Indeterminate If No Expiry", ref this.notificationTemplate.ShowIndeterminateIfNoExpiry);
@ -141,18 +150,26 @@ internal class ImGuiWidget : IDataWindowWidget
if (this.notificationTemplate.ManualType)
type = (NotificationType)this.notificationTemplate.TypeInt;
var duration = NotificationTemplate.Durations[this.notificationTemplate.DurationInt];
var n = notifications.AddNotification(
new()
{
Content = text,
Title = title,
MinimizedText = this.notificationTemplate.ManualMinimizedText
? this.notificationTemplate.MinimizedText
: null,
Type = type,
ShowIndeterminateIfNoExpiry = this.notificationTemplate.ShowIndeterminateIfNoExpiry,
Interactable = this.notificationTemplate.Interactable,
Minimized = this.notificationTemplate.Minimized,
UserDismissable = this.notificationTemplate.UserDismissable,
Expiry = duration == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.Now + duration,
InitialDuration =
this.notificationTemplate.InitialDurationInt == 0
? TimeSpan.MaxValue
: NotificationTemplate.Durations[this.notificationTemplate.InitialDurationInt],
HoverExtendDuration =
this.notificationTemplate.HoverExtendDurationInt == 0
? TimeSpan.Zero
: NotificationTemplate.Durations[this.notificationTemplate.HoverExtendDurationInt],
Progress = this.notificationTemplate.ProgressMode switch
{
0 => 1f,
@ -220,7 +237,8 @@ internal class ImGuiWidget : IDataWindowWidget
n.Progress = i / 10f;
}
n.Expiry = DateTime.Now + NotificationConstants.DefaultDisplayDuration;
n.ExtendBy(NotificationConstants.DefaultDisplayDuration);
n.InitialDuration = NotificationConstants.DefaultDisplayDuration;
});
break;
}
@ -324,7 +342,7 @@ internal class ImGuiWidget : IDataWindowWidget
nameof(NotificationType.Info),
};
public static readonly string[] DurationTitles =
public static readonly string[] InitialDurationTitles =
{
"Infinite",
"1 seconds",
@ -332,9 +350,17 @@ internal class ImGuiWidget : IDataWindowWidget
"10 seconds",
};
public static readonly string[] HoverExtendDurationTitles =
{
"Disable",
"1 seconds",
"3 seconds (default)",
"10 seconds",
};
public static readonly TimeSpan[] Durations =
{
TimeSpan.MaxValue,
TimeSpan.Zero,
TimeSpan.FromSeconds(1),
NotificationConstants.DefaultDisplayDuration,
TimeSpan.FromSeconds(10),
@ -344,14 +370,17 @@ internal class ImGuiWidget : IDataWindowWidget
public string Content;
public bool ManualTitle;
public string Title;
public bool ManualMinimizedText;
public string MinimizedText;
public int IconSourceInt;
public string IconSourceText;
public int IconSourceAssetInt;
public bool ManualType;
public int TypeInt;
public int DurationInt;
public int InitialDurationInt;
public int HoverExtendDurationInt;
public bool ShowIndeterminateIfNoExpiry;
public bool Interactable;
public bool Minimized;
public bool UserDismissable;
public bool ActionBar;
public int ProgressMode;
@ -362,14 +391,17 @@ internal class ImGuiWidget : IDataWindowWidget
this.Content = string.Empty;
this.ManualTitle = false;
this.Title = string.Empty;
this.ManualMinimizedText = false;
this.MinimizedText = string.Empty;
this.IconSourceInt = 0;
this.IconSourceText = "ui/icon/000000/000004_hr1.tex";
this.IconSourceAssetInt = 0;
this.ManualType = false;
this.TypeInt = (int)NotificationType.None;
this.DurationInt = 2;
this.InitialDurationInt = 2;
this.HoverExtendDurationInt = 2;
this.ShowIndeterminateIfNoExpiry = true;
this.Interactable = true;
this.Minimized = true;
this.UserDismissable = true;
this.ActionBar = true;
this.ProgressMode = 0;

View file

@ -579,7 +579,7 @@ public sealed class UiBuilder : IDisposable
Content = content,
Title = title,
Type = type,
Expiry = DateTime.Now + TimeSpan.FromMilliseconds(msDelay),
InitialDuration = TimeSpan.FromMilliseconds(msDelay),
},
true,
this.localPlugin);