2022-08-30 19:57:12 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2022-09-05 17:35:02 +08:00
|
|
|
using System.Collections.Generic;
|
2022-08-30 20:31:06 +08:00
|
|
|
using System.Diagnostics;
|
2022-08-30 19:57:12 +08:00
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2022-08-31 11:46:43 +08:00
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2022-08-30 19:57:12 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Effects;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Utils;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A tray which attaches to the left of <see cref="NotificationOverlay"/> to show temporary toasts.
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class NotificationOverlayToastTray : CompositeDrawable
|
2022-08-30 19:57:12 +08:00
|
|
|
{
|
2022-09-05 18:35:15 +08:00
|
|
|
public override bool IsPresent => toastContentBackground.Height > 0 || toastFlow.Count > 0;
|
2022-09-05 17:42:37 +08:00
|
|
|
|
2022-09-14 12:07:30 +08:00
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => toastFlow.ReceivePositionalInputAt(screenSpacePos);
|
|
|
|
|
2023-06-23 13:58:19 +08:00
|
|
|
/// <summary>
|
|
|
|
/// All notifications currently being displayed by the toast tray.
|
|
|
|
/// </summary>
|
|
|
|
public IEnumerable<Notification> Notifications => toastFlow;
|
|
|
|
|
2022-09-06 16:30:24 +08:00
|
|
|
public bool IsDisplayingToasts => toastFlow.Count > 0;
|
2022-08-30 19:57:12 +08:00
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
private FillFlowContainer<Notification> toastFlow = null!;
|
2022-08-30 19:57:12 +08:00
|
|
|
private BufferedContainer toastContentBackground = null!;
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private OverlayColourProvider colourProvider { get; set; } = null!;
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
public Action<Notification>? ForwardNotificationToPermanentStore { get; set; }
|
2022-08-30 19:57:12 +08:00
|
|
|
|
2022-09-06 16:30:24 +08:00
|
|
|
public int UnreadCount => allDisplayedNotifications.Count(n => !n.WasClosed && !n.Read);
|
2022-09-05 17:35:02 +08:00
|
|
|
|
2022-09-06 16:30:24 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Notifications contained in the toast flow, or in a detached state while they animate during forwarding to the main overlay.
|
|
|
|
/// </summary>
|
|
|
|
private IEnumerable<Notification> allDisplayedNotifications => toastFlow.Concat(InternalChildren.OfType<Notification>());
|
2022-08-30 23:57:30 +08:00
|
|
|
|
2022-08-30 19:57:12 +08:00
|
|
|
private int runningDepth;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
Padding = new MarginPadding(20);
|
|
|
|
|
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
toastContentBackground = (new Box
|
|
|
|
{
|
|
|
|
Anchor = Anchor.TopRight,
|
|
|
|
Origin = Anchor.TopRight,
|
|
|
|
Colour = ColourInfo.GradientVertical(
|
|
|
|
colourProvider.Background6.Opacity(0.7f),
|
|
|
|
colourProvider.Background6.Opacity(0.5f)),
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2022-09-05 18:35:15 +08:00
|
|
|
Height = 0,
|
2022-08-30 19:57:12 +08:00
|
|
|
}.WithEffect(new BlurEffect
|
|
|
|
{
|
|
|
|
PadExtent = true,
|
|
|
|
Sigma = new Vector2(20),
|
|
|
|
}).With(postEffectDrawable =>
|
|
|
|
{
|
|
|
|
postEffectDrawable.Scale = new Vector2(1.5f, 1);
|
|
|
|
postEffectDrawable.Position += new Vector2(70, -50);
|
|
|
|
postEffectDrawable.AutoSizeAxes = Axes.None;
|
|
|
|
postEffectDrawable.RelativeSizeAxes = Axes.X;
|
|
|
|
})),
|
2022-09-05 17:57:33 +08:00
|
|
|
toastFlow = new FillFlowContainer<Notification>
|
2022-08-30 19:57:12 +08:00
|
|
|
{
|
|
|
|
LayoutDuration = 150,
|
|
|
|
LayoutEasing = Easing.OutQuart,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-31 11:46:03 +08:00
|
|
|
public void MarkAllRead()
|
|
|
|
{
|
|
|
|
toastFlow.Children.ForEach(n => n.Read = true);
|
|
|
|
InternalChildren.OfType<Notification>().ForEach(n => n.Read = true);
|
|
|
|
}
|
|
|
|
|
2022-08-30 19:57:12 +08:00
|
|
|
public void FlushAllToasts()
|
|
|
|
{
|
2022-08-30 20:31:06 +08:00
|
|
|
foreach (var notification in toastFlow.ToArray())
|
|
|
|
forwardNotification(notification);
|
2022-08-30 19:57:12 +08:00
|
|
|
}
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
public void Post(Notification notification)
|
2022-08-30 19:57:12 +08:00
|
|
|
{
|
|
|
|
++runningDepth;
|
|
|
|
|
2022-09-12 19:18:40 +08:00
|
|
|
notification.ForwardToOverlay = () => forwardNotification(notification);
|
|
|
|
|
2022-08-30 19:57:12 +08:00
|
|
|
int depth = notification.DisplayOnTop ? -runningDepth : runningDepth;
|
|
|
|
|
|
|
|
toastFlow.Insert(depth, notification);
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
scheduleDismissal();
|
2022-08-30 20:19:51 +08:00
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
void scheduleDismissal() => Scheduler.AddDelayed(() =>
|
2022-08-30 19:57:12 +08:00
|
|
|
{
|
2022-08-30 20:31:06 +08:00
|
|
|
// Notification dismissed by user.
|
2022-08-30 19:57:12 +08:00
|
|
|
if (notification.WasClosed)
|
|
|
|
return;
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
// Notification forwarded away.
|
|
|
|
if (notification.Parent != toastFlow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Notification hovered; delay dismissal.
|
2022-09-11 21:05:44 +08:00
|
|
|
if (notification.IsHovered || notification.IsDragged)
|
2022-08-30 20:19:51 +08:00
|
|
|
{
|
2022-08-30 20:31:06 +08:00
|
|
|
scheduleDismissal();
|
2022-08-30 20:19:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
// All looks good, forward away!
|
|
|
|
forwardNotification(notification);
|
|
|
|
}, notification.IsImportant ? 12000 : 2500);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void forwardNotification(Notification notification)
|
|
|
|
{
|
2022-09-12 19:18:40 +08:00
|
|
|
if (!notification.IsInToastTray)
|
|
|
|
return;
|
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
Debug.Assert(notification.Parent == toastFlow);
|
2022-08-30 19:57:12 +08:00
|
|
|
|
2022-08-31 11:46:43 +08:00
|
|
|
// Temporarily remove from flow so we can animate the position off to the right.
|
2022-09-01 22:05:34 +08:00
|
|
|
toastFlow.Remove(notification, false);
|
2022-08-30 20:31:06 +08:00
|
|
|
AddInternal(notification);
|
2022-08-30 19:57:12 +08:00
|
|
|
|
2022-08-30 20:31:06 +08:00
|
|
|
notification.MoveToOffset(new Vector2(400, 0), NotificationOverlay.TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
notification.FadeOut(NotificationOverlay.TRANSITION_LENGTH, Easing.OutQuint).OnComplete(_ =>
|
|
|
|
{
|
2022-09-01 22:05:34 +08:00
|
|
|
RemoveInternal(notification, false);
|
2022-08-30 20:31:06 +08:00
|
|
|
ForwardNotificationToPermanentStore?.Invoke(notification);
|
|
|
|
|
|
|
|
notification.FadeIn(300, Easing.OutQuint);
|
|
|
|
});
|
2022-08-30 19:57:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
2022-09-05 18:35:15 +08:00
|
|
|
float height = toastFlow.Count > 0 ? toastFlow.DrawHeight + 120 : 0;
|
2022-09-05 17:35:02 +08:00
|
|
|
float alpha = toastFlow.Count > 0 ? MathHelper.Clamp(toastFlow.DrawHeight / 41, 0, 1) * toastFlow.Children.Max(n => n.Alpha) : 0;
|
2022-08-30 19:57:12 +08:00
|
|
|
|
|
|
|
toastContentBackground.Height = (float)Interpolation.DampContinuously(toastContentBackground.Height, height, 10, Clock.ElapsedFrameTime);
|
|
|
|
toastContentBackground.Alpha = (float)Interpolation.DampContinuously(toastContentBackground.Alpha, alpha, 10, Clock.ElapsedFrameTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|