Add UserDismissable

This commit is contained in:
Soreepeong 2024-02-26 01:56:06 +09:00
parent c12bdaabb3
commit 1ca2d2000b
5 changed files with 39 additions and 8 deletions

View file

@ -67,6 +67,9 @@ public interface IActiveNotification : INotification
/// <inheritdoc cref="INotification.Interactible"/>
new bool Interactible { get; set; }
/// <inheritdoc cref="INotification.UserDismissable"/>
new bool UserDismissable { get; set; }
/// <inheritdoc cref="INotification.HoverExtendDuration"/>
new TimeSpan HoverExtendDuration { get; set; }

View file

@ -38,9 +38,13 @@ public interface INotification
/// <see cref="IActiveNotification.DrawActions"/>.
/// Note that the close buttons for notifications are always provided and interactible.
/// 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.
/// unless <see cref="IActiveNotification.Click"/> is set or <see cref="UserDismissable"/> is unset.
/// </remarks>
bool Interactible { get; }
/// <summary>Gets 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; }
/// <summary>Gets the new duration for this notification if mouse cursor is on the notification window.</summary>
/// <remarks>

View file

@ -148,6 +148,18 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
}
}
/// <inheritdoc cref="IActiveNotification.UserDismissable"/>
public bool UserDismissable
{
get => this.underlyingNotification.UserDismissable;
set
{
if (this.IsDismissed)
return;
this.underlyingNotification.UserDismissable = value;
}
}
/// <inheritdoc cref="IActiveNotification.HoverExtendDuration"/>
public TimeSpan HoverExtendDuration
{
@ -317,7 +329,6 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
if (opacity <= 0)
return 0;
var notificationManager = Service<NotificationManager>.Get();
var interfaceManager = Service<InterfaceManager>.Get();
var unboundedWidth = ImGui.CalcTextSize(this.Content).X;
float closeButtonHorizontalSpaceReservation;
@ -386,7 +397,7 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
ImGuiWindowFlags.NoFocusOnAppearing |
ImGuiWindowFlags.NoDocking);
this.DrawNotificationMainWindowContent(notificationManager, width);
this.DrawNotificationMainWindowContent(width);
var windowPos = ImGui.GetWindowPos();
var windowSize = ImGui.GetWindowSize();
var hovered = ImGui.IsWindowHovered();
@ -433,7 +444,7 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
{
if (this.Click is null)
{
if (ImGui.IsMouseClicked(ImGuiMouseButton.Left))
if (this.UserDismissable && ImGui.IsMouseClicked(ImGuiMouseButton.Left))
this.DismissNow(NotificationDismissReason.Manual);
}
else
@ -546,7 +557,7 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
this.MaterializedIcon = null;
}
private void DrawNotificationMainWindowContent(NotificationManager notificationManager, float width)
private void DrawNotificationMainWindowContent(float width)
{
var basePos = ImGui.GetCursorPos();
this.DrawIcon(
@ -706,6 +717,9 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
private void DrawCloseButton(InterfaceManager interfaceManager, Vector2 rt, float pad)
{
if (!this.UserDismissable)
return;
using (interfaceManager.IconFontHandle?.Push())
{
var str = FontAwesomeIcon.Times.ToIconString();
@ -719,7 +733,7 @@ internal sealed class ActiveNotification : IActiveNotification, IDisposable
ImGui.SetCursorPos(rt - new Vector2(size, 0) - new Vector2(pad));
if (ImGui.Button(str, new(size + (pad * 2))))
this.DismissNow();
this.DismissNow(NotificationDismissReason.Manual);
ImGui.PopStyleColor(2);
if (!this.IsMouseHovered)

View file

@ -24,6 +24,9 @@ public sealed record Notification : INotification
/// <inheritdoc/>
public bool Interactible { get; set; }
/// <inheritdoc/>
public bool UserDismissable { get; set; }
/// <inheritdoc/>
public TimeSpan HoverExtendDuration { get; set; } = NotificationConstants.DefaultHoverExtendDuration;

View file

@ -120,7 +120,11 @@ internal class ImGuiWidget : IDataWindowWidget
ImGui.Checkbox("Interactible", ref this.notificationTemplate.Interactible);
ImGui.Checkbox("Action Bar", ref this.notificationTemplate.ActionBar);
ImGui.Checkbox("User Dismissable", ref this.notificationTemplate.UserDismissable);
ImGui.Checkbox(
"Action Bar (always on if not user dismissable for the example)",
ref this.notificationTemplate.ActionBar);
if (ImGui.Button("Add notification"))
{
@ -144,6 +148,7 @@ internal class ImGuiWidget : IDataWindowWidget
Title = title,
Type = type,
Interactible = this.notificationTemplate.Interactible,
UserDismissable = this.notificationTemplate.UserDismissable,
Expiry = duration == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.Now + duration,
Progress = this.notificationTemplate.ProgressMode switch
{
@ -203,7 +208,7 @@ internal class ImGuiWidget : IDataWindowWidget
break;
}
if (this.notificationTemplate.ActionBar)
if (this.notificationTemplate.ActionBar || !this.notificationTemplate.UserDismissable)
{
var nclick = 0;
n.Click += _ => nclick++;
@ -326,6 +331,7 @@ internal class ImGuiWidget : IDataWindowWidget
public int TypeInt;
public int DurationInt;
public bool Interactible;
public bool UserDismissable;
public bool ActionBar;
public int ProgressMode;
@ -342,6 +348,7 @@ internal class ImGuiWidget : IDataWindowWidget
this.TypeInt = (int)NotificationType.None;
this.DurationInt = 2;
this.Interactible = true;
this.UserDismissable = true;
this.ActionBar = true;
this.ProgressMode = 0;
}