2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
|
|
|
|
public class NotificationOverlay : OsuFocusedOverlayContainer
|
|
|
|
|
{
|
|
|
|
|
private const float width = 320;
|
|
|
|
|
|
|
|
|
|
public const float TRANSITION_LENGTH = 600;
|
|
|
|
|
|
|
|
|
|
private FlowContainer<NotificationSection> sections;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provide a source for the toolbar height.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<float> GetToolbarHeight;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
|
|
|
|
Width = width;
|
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = Color4.Black,
|
|
|
|
|
Alpha = 0.6f
|
|
|
|
|
},
|
|
|
|
|
new OsuScrollContainer
|
|
|
|
|
{
|
|
|
|
|
Masking = true,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
sections = new FillFlowContainer<NotificationSection>
|
|
|
|
|
{
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
2019-06-20 16:41:12 +08:00
|
|
|
|
new NotificationSection(@"Notifications", @"Clear All")
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
AcceptTypes = new[] { typeof(SimpleNotification) }
|
|
|
|
|
},
|
2019-06-20 16:41:12 +08:00
|
|
|
|
new NotificationSection(@"Running Tasks", @"Cancel All")
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
AcceptTypes = new[] { typeof(ProgressNotification) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 14:49:58 +08:00
|
|
|
|
private ScheduledDelegate notificationsEnabler;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-06-06 14:49:58 +08:00
|
|
|
|
private void updateProcessingMode()
|
|
|
|
|
{
|
2019-06-11 13:28:52 +08:00
|
|
|
|
bool enabled = OverlayActivationMode.Value == OverlayActivation.All || State.Value == Visibility.Visible;
|
2018-06-06 14:49:58 +08:00
|
|
|
|
|
|
|
|
|
notificationsEnabler?.Cancel();
|
|
|
|
|
|
|
|
|
|
if (enabled)
|
|
|
|
|
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
|
2019-06-11 13:28:52 +08:00
|
|
|
|
notificationsEnabler = Scheduler.AddDelayed(() => processingPosts = true, State.Value == Visibility.Visible ? 0 : 1000);
|
2018-06-06 14:49:58 +08:00
|
|
|
|
else
|
|
|
|
|
processingPosts = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2019-06-11 13:28:52 +08:00
|
|
|
|
State.ValueChanged += _ => updateProcessingMode();
|
2018-08-23 10:20:20 +08:00
|
|
|
|
OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true);
|
2018-06-06 14:49:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public readonly BindableInt UnreadCount = new BindableInt();
|
|
|
|
|
|
|
|
|
|
private int runningDepth;
|
|
|
|
|
|
|
|
|
|
private void notificationClosed() => updateCounts();
|
|
|
|
|
|
|
|
|
|
private readonly Scheduler postScheduler = new Scheduler();
|
|
|
|
|
|
2019-06-20 18:06:21 +08:00
|
|
|
|
public override bool IsPresent => base.IsPresent || postScheduler.HasPendingTasks;
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private bool processingPosts = true;
|
|
|
|
|
|
|
|
|
|
public void Post(Notification notification) => postScheduler.Add(() =>
|
|
|
|
|
{
|
|
|
|
|
++runningDepth;
|
|
|
|
|
|
|
|
|
|
notification.Closed += notificationClosed;
|
|
|
|
|
|
2019-02-28 13:35:00 +08:00
|
|
|
|
if (notification is IHasCompletionTarget hasCompletionTarget)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
hasCompletionTarget.CompletionTarget = Post;
|
|
|
|
|
|
|
|
|
|
var ourType = notification.GetType();
|
|
|
|
|
|
|
|
|
|
var section = sections.Children.FirstOrDefault(s => s.AcceptTypes.Any(accept => accept.IsAssignableFrom(ourType)));
|
|
|
|
|
section?.Add(notification, notification.DisplayOnTop ? -runningDepth : runningDepth);
|
|
|
|
|
|
2018-07-13 20:25:08 +08:00
|
|
|
|
if (notification.IsImportant)
|
2019-06-11 13:28:52 +08:00
|
|
|
|
Show();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
updateCounts();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
if (processingPosts)
|
|
|
|
|
postScheduler.Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopIn()
|
|
|
|
|
{
|
|
|
|
|
base.PopIn();
|
|
|
|
|
|
|
|
|
|
this.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopOut()
|
|
|
|
|
{
|
|
|
|
|
base.PopOut();
|
|
|
|
|
|
|
|
|
|
markAllRead();
|
|
|
|
|
|
|
|
|
|
this.MoveToX(width, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
this.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateCounts()
|
|
|
|
|
{
|
2019-06-20 18:06:21 +08:00
|
|
|
|
UnreadCount.Value = sections.Select(c => c.UnreadCount).Sum();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void markAllRead()
|
|
|
|
|
{
|
|
|
|
|
sections.Children.ForEach(s => s.MarkAllRead());
|
|
|
|
|
|
|
|
|
|
updateCounts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
|
|
|
|
Padding = new MarginPadding { Top = GetToolbarHeight?.Invoke() ?? 0 };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|