performance notifcation addition, with some regular bugfixes regarding the flexing of the notifications
This commit is contained in:
@@ -22,6 +22,10 @@ public class LightlessNotificationUI : WindowMediatorSubscriberBase
|
||||
private const float WindowPaddingOffset = 6f;
|
||||
private const float SlideAnimationDistance = 100f;
|
||||
private const float OutAnimationSpeedMultiplier = 0.7f;
|
||||
private const float ContentPaddingX = 10f;
|
||||
private const float ContentPaddingY = 6f;
|
||||
private const float TitleMessageSpacing = 4f;
|
||||
private const float ActionButtonSpacing = 8f;
|
||||
|
||||
private readonly List<LightlessNotification> _notifications = new();
|
||||
private readonly object _notificationLock = new();
|
||||
@@ -462,81 +466,112 @@ public class LightlessNotificationUI : WindowMediatorSubscriberBase
|
||||
|
||||
private void DrawNotificationText(LightlessNotification notification, float alpha)
|
||||
{
|
||||
var padding = new Vector2(10f, 6f);
|
||||
var contentPos = new Vector2(padding.X, padding.Y);
|
||||
var contentPos = new Vector2(ContentPaddingX, ContentPaddingY);
|
||||
var windowSize = ImGui.GetWindowSize();
|
||||
var contentSize = new Vector2(windowSize.X - padding.X, windowSize.Y - padding.Y * 2);
|
||||
var contentWidth = CalculateContentWidth(windowSize.X);
|
||||
|
||||
ImGui.SetCursorPos(contentPos);
|
||||
|
||||
var titleHeight = DrawTitle(notification, contentSize.X, alpha);
|
||||
DrawMessage(notification, contentPos, contentSize.X, titleHeight, alpha);
|
||||
var titleHeight = DrawTitle(notification, contentWidth, alpha);
|
||||
DrawMessage(notification, contentPos, contentWidth, titleHeight, alpha);
|
||||
|
||||
if (notification.Actions.Count > 0)
|
||||
if (HasActions(notification))
|
||||
{
|
||||
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + ImGui.GetStyle().ItemSpacing.Y);
|
||||
ImGui.SetCursorPosX(contentPos.X);
|
||||
DrawNotificationActions(notification, contentSize.X, alpha);
|
||||
PositionActionsAtBottom(windowSize.Y);
|
||||
DrawNotificationActions(notification, contentWidth, alpha);
|
||||
}
|
||||
}
|
||||
|
||||
private float CalculateContentWidth(float windowWidth) =>
|
||||
windowWidth - (ContentPaddingX * 2);
|
||||
|
||||
private bool HasActions(LightlessNotification notification) =>
|
||||
notification.Actions.Count > 0;
|
||||
|
||||
private void PositionActionsAtBottom(float windowHeight)
|
||||
{
|
||||
var actionHeight = ImGui.GetFrameHeight();
|
||||
var bottomY = windowHeight - ContentPaddingY - actionHeight;
|
||||
ImGui.SetCursorPosY(bottomY);
|
||||
ImGui.SetCursorPosX(ContentPaddingX);
|
||||
}
|
||||
|
||||
private float DrawTitle(LightlessNotification notification, float contentWidth, float alpha)
|
||||
{
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, new Vector4(1f, 1f, 1f, alpha)))
|
||||
var titleColor = new Vector4(1f, 1f, 1f, alpha);
|
||||
var titleText = FormatTitleText(notification);
|
||||
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, titleColor))
|
||||
{
|
||||
ImGui.PushTextWrapPos(ImGui.GetCursorPosX() + contentWidth);
|
||||
var titleStartY = ImGui.GetCursorPosY();
|
||||
|
||||
var titleText = _configService.Current.ShowNotificationTimestamp
|
||||
? $"[{notification.CreatedAt.ToLocalTime():HH:mm:ss}] {notification.Title}"
|
||||
: notification.Title;
|
||||
|
||||
ImGui.TextWrapped(titleText);
|
||||
var titleHeight = ImGui.GetCursorPosY() - titleStartY;
|
||||
ImGui.PopTextWrapPos();
|
||||
return titleHeight;
|
||||
return DrawWrappedText(titleText, contentWidth);
|
||||
}
|
||||
}
|
||||
|
||||
private string FormatTitleText(LightlessNotification notification)
|
||||
{
|
||||
if (!_configService.Current.ShowNotificationTimestamp)
|
||||
return notification.Title;
|
||||
|
||||
var timestamp = notification.CreatedAt.ToLocalTime().ToString("HH:mm:ss");
|
||||
return $"[{timestamp}] {notification.Title}";
|
||||
}
|
||||
|
||||
private float DrawWrappedText(string text, float wrapWidth)
|
||||
{
|
||||
ImGui.PushTextWrapPos(ImGui.GetCursorPosX() + wrapWidth);
|
||||
var startY = ImGui.GetCursorPosY();
|
||||
ImGui.TextWrapped(text);
|
||||
var height = ImGui.GetCursorPosY() - startY;
|
||||
ImGui.PopTextWrapPos();
|
||||
return height;
|
||||
}
|
||||
|
||||
private void DrawMessage(LightlessNotification notification, Vector2 contentPos, float contentWidth, float titleHeight, float alpha)
|
||||
{
|
||||
if (string.IsNullOrEmpty(notification.Message)) return;
|
||||
|
||||
ImGui.SetCursorPos(contentPos + new Vector2(0f, titleHeight + 4f));
|
||||
ImGui.PushTextWrapPos(ImGui.GetCursorPosX() + contentWidth);
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, new Vector4(0.9f, 0.9f, 0.9f, alpha)))
|
||||
var messagePos = contentPos + new Vector2(0f, titleHeight + TitleMessageSpacing);
|
||||
var messageColor = new Vector4(0.9f, 0.9f, 0.9f, alpha);
|
||||
|
||||
ImGui.SetCursorPos(messagePos);
|
||||
|
||||
using (ImRaii.PushColor(ImGuiCol.Text, messageColor))
|
||||
{
|
||||
ImGui.TextWrapped(notification.Message);
|
||||
DrawWrappedText(notification.Message, contentWidth);
|
||||
}
|
||||
ImGui.PopTextWrapPos();
|
||||
}
|
||||
|
||||
private void DrawNotificationActions(LightlessNotification notification, float availableWidth, float alpha)
|
||||
{
|
||||
var buttonSpacing = 8f;
|
||||
var rightPadding = 10f;
|
||||
var usableWidth = availableWidth - rightPadding;
|
||||
var totalSpacing = (notification.Actions.Count - 1) * buttonSpacing;
|
||||
var buttonWidth = (usableWidth - totalSpacing) / notification.Actions.Count;
|
||||
var buttonWidth = CalculateActionButtonWidth(notification.Actions.Count, availableWidth);
|
||||
|
||||
_logger.LogDebug("Drawing {ActionCount} notification actions, buttonWidth: {ButtonWidth}, availableWidth: {AvailableWidth}",
|
||||
notification.Actions.Count, buttonWidth, availableWidth);
|
||||
|
||||
var startCursorPos = ImGui.GetCursorPos();
|
||||
var startX = ImGui.GetCursorPosX();
|
||||
|
||||
for (int i = 0; i < notification.Actions.Count; i++)
|
||||
{
|
||||
var action = notification.Actions[i];
|
||||
|
||||
if (i > 0)
|
||||
{
|
||||
ImGui.SameLine();
|
||||
var currentX = startCursorPos.X + i * (buttonWidth + buttonSpacing);
|
||||
ImGui.SetCursorPosX(currentX);
|
||||
PositionActionButton(i, startX, buttonWidth);
|
||||
}
|
||||
DrawActionButton(action, notification, alpha, buttonWidth);
|
||||
DrawActionButton(notification.Actions[i], notification, alpha, buttonWidth);
|
||||
}
|
||||
}
|
||||
|
||||
private float CalculateActionButtonWidth(int actionCount, float availableWidth)
|
||||
{
|
||||
var totalSpacing = (actionCount - 1) * ActionButtonSpacing;
|
||||
return (availableWidth - totalSpacing) / actionCount;
|
||||
}
|
||||
|
||||
private void PositionActionButton(int index, float startX, float buttonWidth)
|
||||
{
|
||||
var xPosition = startX + index * (buttonWidth + ActionButtonSpacing);
|
||||
ImGui.SetCursorPosX(xPosition);
|
||||
}
|
||||
|
||||
private void DrawActionButton(LightlessNotificationAction action, LightlessNotification notification, float alpha, float buttonWidth)
|
||||
{
|
||||
@@ -634,7 +669,7 @@ public class LightlessNotificationUI : WindowMediatorSubscriberBase
|
||||
|
||||
private float CalculateNotificationHeight(LightlessNotification notification)
|
||||
{
|
||||
var contentWidth = _configService.Current.NotificationWidth - 35f;
|
||||
var contentWidth = CalculateContentWidth(_configService.Current.NotificationWidth);
|
||||
var height = 12f;
|
||||
|
||||
height += CalculateTitleHeight(notification, contentWidth);
|
||||
@@ -681,6 +716,8 @@ public class LightlessNotificationUI : WindowMediatorSubscriberBase
|
||||
NotificationType.Error => UIColors.Get("DimRed"),
|
||||
NotificationType.PairRequest => UIColors.Get("LightlessBlue"),
|
||||
NotificationType.Download => UIColors.Get("LightlessGreen"),
|
||||
NotificationType.Performance => UIColors.Get("LightlessOrange"),
|
||||
|
||||
_ => UIColors.Get("LightlessPurple")
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user