Skip to content

Commit

Permalink
Merge pull request #20032 from peppy/toast-notification-tray
Browse files Browse the repository at this point in the history
Add toast notification tray
  • Loading branch information
smoogipoo committed Aug 31, 2022
2 parents 000412c + afe2862 commit 6cadcc2
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 69 deletions.
44 changes: 23 additions & 21 deletions osu.Game.Tests/Visual/Gameplay/TestScenePlayerLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Framework.Utils;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets.Mods;
Expand All @@ -30,7 +29,6 @@
using osu.Game.Screens.Play.PlayerSettings;
using osu.Game.Utils;
using osuTK.Input;
using SkipOverlay = osu.Game.Screens.Play.SkipOverlay;

namespace osu.Game.Tests.Visual.Gameplay
{
Expand Down Expand Up @@ -83,6 +81,20 @@ public TestScenePlayerLoader()
[SetUp]
public void Setup() => Schedule(() => player = null);

[SetUpSteps]
public override void SetUpSteps()
{
base.SetUpSteps();

AddStep("read all notifications", () =>
{
notificationOverlay.Show();
notificationOverlay.Hide();
});

AddUntilStep("wait for no notifications", () => notificationOverlay.UnreadCount.Value, () => Is.EqualTo(0));
}

/// <summary>
/// Sets the input manager child to a new test player loader container instance.
/// </summary>
Expand Down Expand Up @@ -287,16 +299,9 @@ private void addVolumeSteps(string volumeName, Action beforeLoad, Func<bool> ass

saveVolumes();

AddAssert("check for notification", () => notificationOverlay.UnreadCount.Value == 1);
AddStep("click notification", () =>
{
var scrollContainer = (OsuScrollContainer)notificationOverlay.Children.Last();
var flowContainer = scrollContainer.Children.OfType<FillFlowContainer<NotificationSection>>().First();
var notification = flowContainer.First();
AddAssert("check for notification", () => notificationOverlay.UnreadCount.Value, () => Is.EqualTo(1));

InputManager.MoveMouseTo(notification);
InputManager.Click(MouseButton.Left);
});
clickNotificationIfAny();

AddAssert("check " + volumeName, assert);

Expand Down Expand Up @@ -366,15 +371,7 @@ public void TestLowBatteryNotification(bool onBattery, double? chargeLevel, bool
}));
AddUntilStep("wait for player", () => player?.LoadState == LoadState.Ready);
AddAssert($"notification {(shouldWarn ? "triggered" : "not triggered")}", () => notificationOverlay.UnreadCount.Value == (shouldWarn ? 1 : 0));
AddStep("click notification", () =>
{
var scrollContainer = (OsuScrollContainer)notificationOverlay.Children.Last();
var flowContainer = scrollContainer.Children.OfType<FillFlowContainer<NotificationSection>>().First();
var notification = flowContainer.First();
InputManager.MoveMouseTo(notification);
InputManager.Click(MouseButton.Left);
});
clickNotificationIfAny();
AddUntilStep("wait for player load", () => player.IsLoaded);
}

Expand Down Expand Up @@ -439,6 +436,11 @@ public void TestQuickRetry()
AddUntilStep("skip button not visible", () => !checkSkipButtonVisible());
}

private void clickNotificationIfAny()
{
AddStep("click notification", () => notificationOverlay.ChildrenOfType<Notification>().FirstOrDefault()?.TriggerClick());
}

private EpilepsyWarning getWarning() => loader.ChildrenOfType<EpilepsyWarning>().SingleOrDefault();

private class TestPlayerLoader : PlayerLoader
Expand Down
10 changes: 9 additions & 1 deletion osu.Game.Tests/Visual/Menus/IntroTestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected IntroTestScene()
},
notifications = new NotificationOverlay
{
Depth = float.MinValue,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
}
Expand Down Expand Up @@ -82,7 +83,14 @@ public virtual void TestPlayIntro()
[Test]
public virtual void TestPlayIntroWithFailingAudioDevice()
{
AddStep("hide notifications", () => notifications.Hide());
AddStep("reset notifications", () =>
{
notifications.Show();
notifications.Hide();
});

AddUntilStep("wait for no notifications", () => notifications.UnreadCount.Value, () => Is.EqualTo(0));

AddStep("restart sequence", () =>
{
logo.FinishTransforms();
Expand Down
9 changes: 0 additions & 9 deletions osu.Game.Tests/Visual/Navigation/TestSceneFirstRunGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,7 @@ public override void SetUpSteps()
public void TestImportantNotificationDoesntInterruptSetup()
{
AddStep("post important notification", () => Game.Notifications.Post(new SimpleNotification { Text = "Important notification" }));
AddAssert("no notification posted", () => Game.Notifications.UnreadCount.Value == 0);
AddAssert("first-run setup still visible", () => Game.FirstRunOverlay.State.Value == Visibility.Visible);

AddUntilStep("finish first-run setup", () =>
{
Game.FirstRunOverlay.NextButton.TriggerClick();
return Game.FirstRunOverlay.State.Value == Visibility.Hidden;
});
AddWaitStep("wait for post delay", 5);
AddAssert("notifications shown", () => Game.Notifications.State.Value == Visibility.Visible);
AddAssert("notification posted", () => Game.Notifications.UnreadCount.Value == 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public void TestImportantWhileClosed()
{
AddStep(@"simple #1", sendHelloNotification);

AddAssert("Is visible", () => notificationOverlay.State.Value == Visibility.Visible);
AddAssert("toast displayed", () => notificationOverlay.ToastCount == 1);
AddAssert("is not visible", () => notificationOverlay.State.Value == Visibility.Hidden);

checkDisplayedCount(1);

Expand Down Expand Up @@ -183,7 +184,7 @@ protected override void Update()
}

private void checkDisplayedCount(int expected) =>
AddAssert($"Displayed count is {expected}", () => notificationOverlay.UnreadCount.Value == expected);
AddUntilStep($"Displayed count is {expected}", () => notificationOverlay.UnreadCount.Value == expected);

private void sendDownloadProgress()
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/OsuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -804,8 +804,8 @@ protected override void LoadComplete()
Children = new Drawable[]
{
overlayContent = new Container { RelativeSizeAxes = Axes.Both },
rightFloatingOverlayContent = new Container { RelativeSizeAxes = Axes.Both },
leftFloatingOverlayContent = new Container { RelativeSizeAxes = Axes.Both },
rightFloatingOverlayContent = new Container { RelativeSizeAxes = Axes.Both },
}
},
topMostOverlayContent = new Container { RelativeSizeAxes = Axes.Both },
Expand Down
109 changes: 74 additions & 35 deletions osu.Game/Overlays/NotificationOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using osu.Game.Graphics.Containers;
using osu.Game.Overlays.Notifications;
using osu.Game.Resources.Localisation.Web;
using osuTK;
using NotificationsStrings = osu.Game.Localisation.NotificationsStrings;

namespace osu.Game.Overlays
Expand All @@ -37,58 +38,83 @@ public class NotificationOverlay : OsuFocusedOverlayContainer, INamedOverlayComp
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);

private readonly IBindable<Visibility> firstRunSetupVisibility = new Bindable<Visibility>();
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos)
{
if (State.Value == Visibility.Visible)
return base.ReceivePositionalInputAt(screenSpacePos);

if (toastTray.IsDisplayingToasts)
return toastTray.ReceivePositionalInputAt(screenSpacePos);

return false;
}

public override bool PropagatePositionalInputSubTree => base.PropagatePositionalInputSubTree || toastTray.IsDisplayingToasts;

private NotificationOverlayToastTray toastTray = null!;

private Container mainContent = null!;

[BackgroundDependencyLoader]
private void load(FirstRunSetupOverlay? firstRunSetup)
private void load()
{
X = WIDTH;
Width = WIDTH;
RelativeSizeAxes = Axes.Y;

Children = new Drawable[]
{
new Box
toastTray = new NotificationOverlayToastTray
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4,
ForwardNotificationToPermanentStore = addPermanently,
Origin = Anchor.TopRight,
},
new OsuScrollContainer
mainContent = new Container
{
Masking = true,
AlwaysPresent = true,
RelativeSizeAxes = Axes.Both,
Children = new[]
Children = new Drawable[]
{
sections = new FillFlowContainer<NotificationSection>
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4,
},
new OsuScrollContainer
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Masking = true,
RelativeSizeAxes = Axes.Both,
Children = new[]
{
new NotificationSection(AccountsStrings.NotificationsTitle, new[] { typeof(SimpleNotification) }, "Clear All"),
new NotificationSection(@"Running Tasks", new[] { typeof(ProgressNotification) }, @"Cancel All"),
sections = new FillFlowContainer<NotificationSection>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Children = new[]
{
new NotificationSection(AccountsStrings.NotificationsTitle, new[] { typeof(SimpleNotification) }, "Clear All"),
new NotificationSection(@"Running Tasks", new[] { typeof(ProgressNotification) }, @"Cancel All"),
}
}
}
}
}
}
},
};

if (firstRunSetup != null)
firstRunSetupVisibility.BindTo(firstRunSetup.State);
}

private ScheduledDelegate? notificationsEnabler;

private void updateProcessingMode()
{
bool enabled = (OverlayActivationMode.Value == OverlayActivation.All && firstRunSetupVisibility.Value != Visibility.Visible) || State.Value == Visibility.Visible;
bool enabled = OverlayActivationMode.Value == OverlayActivation.All || State.Value == Visibility.Visible;

notificationsEnabler?.Cancel();

if (enabled)
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
notificationsEnabler = Scheduler.AddDelayed(() => processingPosts = true, State.Value == Visibility.Visible ? 0 : 1000);
notificationsEnabler = Scheduler.AddDelayed(() => processingPosts = true, State.Value == Visibility.Visible ? 0 : 100);
else
processingPosts = false;
}
Expand All @@ -98,12 +124,13 @@ protected override void LoadComplete()
base.LoadComplete();

State.BindValueChanged(_ => updateProcessingMode());
firstRunSetupVisibility.BindValueChanged(_ => updateProcessingMode());
OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true);
}

public IBindable<int> UnreadCount => unreadCount;

public int ToastCount => toastTray.UnreadCount;

private readonly BindableInt unreadCount = new BindableInt();

private int runningDepth;
Expand All @@ -127,17 +154,27 @@ protected override void LoadComplete()
if (notification is IHasCompletionTarget hasCompletionTarget)
hasCompletionTarget.CompletionTarget = Post;
playDebouncedSample(notification.PopInSampleName);
if (State.Value == Visibility.Hidden)
toastTray.Post(notification);
else
addPermanently(notification);
updateCounts();
});

private void addPermanently(Notification notification)
{
var ourType = notification.GetType();
int depth = notification.DisplayOnTop ? -runningDepth : runningDepth;

var section = sections.Children.FirstOrDefault(s => s.AcceptedNotificationTypes.Any(accept => accept.IsAssignableFrom(ourType)));
section?.Add(notification, notification.DisplayOnTop ? -runningDepth : runningDepth);
var section = sections.Children.First(s => s.AcceptedNotificationTypes.Any(accept => accept.IsAssignableFrom(ourType)));

if (notification.IsImportant)
Show();
section.Add(notification, depth);

updateCounts();
playDebouncedSample(notification.PopInSampleName);
});
}

protected override void Update()
{
Expand All @@ -152,7 +189,9 @@ protected override void PopIn()
base.PopIn();

this.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
mainContent.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);

toastTray.FlushAllToasts();
}

protected override void PopOut()
Expand All @@ -162,7 +201,7 @@ protected override void PopOut()
markAllRead();

this.MoveToX(WIDTH, TRANSITION_LENGTH, Easing.OutQuint);
this.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
mainContent.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
}

private void notificationClosed()
Expand All @@ -183,16 +222,16 @@ private void playDebouncedSample(string sampleName)
}
}

private void updateCounts()
{
unreadCount.Value = sections.Select(c => c.UnreadCount).Sum();
}

private void markAllRead()
{
sections.Children.ForEach(s => s.MarkAllRead());

toastTray.MarkAllRead();
updateCounts();
}

private void updateCounts()
{
unreadCount.Value = sections.Select(c => c.UnreadCount).Sum() + toastTray.UnreadCount;
}
}
}
Loading

0 comments on commit 6cadcc2

Please sign in to comment.