Merge pull request #20244 from peppy/notification-fling

Add ability to "fling" notifications to dismiss them
This commit is contained in:
Dean Herbert 2022-09-13 19:50:34 +09:00 committed by GitHub
commit 566a61e770
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 230 additions and 20 deletions

View File

@ -91,7 +91,7 @@ namespace osu.Game.Tests.Online
{ {
AddStep("download beatmap", () => beatmaps.Download(test_db_model)); AddStep("download beatmap", () => beatmaps.Download(test_db_model));
AddStep("cancel download from notification", () => recentNotification.Close()); AddStep("cancel download from notification", () => recentNotification.Close(true));
AddUntilStep("is removed from download list", () => beatmaps.GetExistingDownload(test_db_model) == null); AddUntilStep("is removed from download list", () => beatmaps.GetExistingDownload(test_db_model) == null);
AddAssert("is notification cancelled", () => recentNotification.State == ProgressNotificationState.Cancelled); AddAssert("is notification cancelled", () => recentNotification.State == ProgressNotificationState.Cancelled);

View File

@ -12,9 +12,9 @@ using osu.Framework.Utils;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Updater;
using osuTK; using osuTK;
using osuTK.Input; using osuTK.Input;
using osu.Game.Updater;
namespace osu.Game.Tests.Visual.UserInterface namespace osu.Game.Tests.Visual.UserInterface
{ {
@ -48,6 +48,40 @@ namespace osu.Game.Tests.Visual.UserInterface
notificationOverlay.UnreadCount.ValueChanged += count => { displayedCount.Text = $"displayed count: {count.NewValue}"; }; notificationOverlay.UnreadCount.ValueChanged += count => { displayedCount.Text = $"displayed count: {count.NewValue}"; };
}); });
[Test]
public void TestDismissWithoutActivationFling()
{
bool activated = false;
SimpleNotification notification = null!;
AddStep("post", () =>
{
activated = false;
notificationOverlay.Post(notification = new SimpleNotification
{
Text = @"Welcome to osu!. Enjoy your stay!",
Activated = () => activated = true,
});
});
AddStep("start drag", () =>
{
InputManager.MoveMouseTo(notification.ChildrenOfType<Notification>().Single());
InputManager.PressButton(MouseButton.Left);
InputManager.MoveMouseTo(notification.ChildrenOfType<Notification>().Single().ScreenSpaceDrawQuad.Centre + new Vector2(-500, 0));
});
AddStep("fling away", () =>
{
InputManager.ReleaseButton(MouseButton.Left);
});
AddUntilStep("wait for closed", () => notification.WasClosed);
AddAssert("was not activated", () => !activated);
AddStep("reset mouse position", () => InputManager.MoveMouseTo(Vector2.Zero));
AddAssert("unread count zero", () => notificationOverlay.UnreadCount.Value == 0);
}
[Test] [Test]
public void TestDismissWithoutActivationCloseButton() public void TestDismissWithoutActivationCloseButton()
{ {
@ -75,6 +109,7 @@ namespace osu.Game.Tests.Visual.UserInterface
AddUntilStep("wait for closed", () => notification.WasClosed); AddUntilStep("wait for closed", () => notification.WasClosed);
AddAssert("was not activated", () => !activated); AddAssert("was not activated", () => !activated);
AddStep("reset mouse position", () => InputManager.MoveMouseTo(Vector2.Zero)); AddStep("reset mouse position", () => InputManager.MoveMouseTo(Vector2.Zero));
AddAssert("unread count zero", () => notificationOverlay.UnreadCount.Value == 0);
} }
[Test] [Test]
@ -220,6 +255,26 @@ namespace osu.Game.Tests.Visual.UserInterface
AddStep("cancel notification", () => notification.State = ProgressNotificationState.Cancelled); AddStep("cancel notification", () => notification.State = ProgressNotificationState.Cancelled);
} }
[Test]
public void TestReadState()
{
SimpleNotification notification = null!;
AddStep(@"post", () => notificationOverlay.Post(notification = new BackgroundNotification { Text = @"Welcome to osu!. Enjoy your stay!" }));
AddUntilStep("check is toast", () => !notification.IsInToastTray);
AddAssert("light is not visible", () => notification.ChildrenOfType<Notification.NotificationLight>().Single().Alpha == 0);
AddUntilStep("wait for forward to overlay", () => !notification.IsInToastTray);
setState(Visibility.Visible);
AddAssert("state is not read", () => !notification.Read);
AddUntilStep("light is visible", () => notification.ChildrenOfType<Notification.NotificationLight>().Single().Alpha == 1);
setState(Visibility.Hidden);
setState(Visibility.Visible);
AddAssert("state is read", () => notification.Read);
AddUntilStep("light is not visible", () => notification.ChildrenOfType<Notification.NotificationLight>().Single().Alpha == 0);
}
[Test] [Test]
public void TestUpdateNotificationFlow() public void TestUpdateNotificationFlow()
{ {

View File

@ -111,7 +111,7 @@ namespace osu.Game.Database
{ {
if (error is WebException webException && webException.Message == @"TooManyRequests") if (error is WebException webException && webException.Message == @"TooManyRequests")
{ {
notification.Close(); notification.Close(false);
PostNotification?.Invoke(new TooManyDownloadsNotification()); PostNotification?.Invoke(new TooManyDownloadsNotification());
} }
else else

View File

@ -158,7 +158,10 @@ namespace osu.Game.Overlays
playDebouncedSample(notification.PopInSampleName); playDebouncedSample(notification.PopInSampleName);
if (State.Value == Visibility.Hidden) if (State.Value == Visibility.Hidden)
{
notification.IsInToastTray = true;
toastTray.Post(notification); toastTray.Post(notification);
}
else else
addPermanently(notification); addPermanently(notification);
@ -167,6 +170,8 @@ namespace osu.Game.Overlays
private void addPermanently(Notification notification) private void addPermanently(Notification notification)
{ {
notification.IsInToastTray = false;
var ourType = notification.GetType(); var ourType = notification.GetType();
int depth = notification.DisplayOnTop ? -runningDepth : runningDepth; int depth = notification.DisplayOnTop ? -runningDepth : runningDepth;

View File

@ -78,7 +78,6 @@ namespace osu.Game.Overlays
{ {
LayoutDuration = 150, LayoutDuration = 150,
LayoutEasing = Easing.OutQuart, LayoutEasing = Easing.OutQuart,
Spacing = new Vector2(3),
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
}, },
@ -118,7 +117,7 @@ namespace osu.Game.Overlays
return; return;
// Notification hovered; delay dismissal. // Notification hovered; delay dismissal.
if (notification.IsHovered) if (notification.IsHovered || notification.IsDragged)
{ {
scheduleDismissal(); scheduleDismissal();
return; return;

View File

@ -2,16 +2,19 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects; using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Framework.Utils;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osuTK; using osuTK;
@ -56,13 +59,40 @@ namespace osu.Game.Overlays.Notifications
protected Container MainContent; protected Container MainContent;
private readonly DragContainer dragContainer;
public virtual bool Read { get; set; } public virtual bool Read { get; set; }
public new bool IsDragged => dragContainer.IsDragged;
protected virtual IconUsage CloseButtonIcon => FontAwesome.Solid.Check; protected virtual IconUsage CloseButtonIcon => FontAwesome.Solid.Check;
[Resolved] [Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!; private OverlayColourProvider colourProvider { get; set; } = null!;
public override bool PropagatePositionalInputSubTree => base.PropagatePositionalInputSubTree && !WasClosed;
private bool isInToastTray;
/// <summary>
/// Whether this notification is in the <see cref="NotificationOverlayToastTray"/>.
/// </summary>
public bool IsInToastTray
{
get => isInToastTray;
set
{
isInToastTray = value;
if (!isInToastTray)
{
dragContainer.ResetPosition();
if (!Read)
Light.FadeIn(100);
}
}
}
private readonly Box initialFlash; private readonly Box initialFlash;
private Box background = null!; private Box background = null!;
@ -76,11 +106,19 @@ namespace osu.Game.Overlays.Notifications
{ {
Light = new NotificationLight Light = new NotificationLight
{ {
Alpha = 0,
Margin = new MarginPadding { Right = 5 }, Margin = new MarginPadding { Right = 5 },
Anchor = Anchor.CentreLeft, Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreRight, Origin = Anchor.CentreRight,
}, },
MainContent = new Container dragContainer = new DragContainer(this)
{
// Use margin instead of FillFlow spacing to fix extra padding appearing when notification shrinks
// in height.
Padding = new MarginPadding { Vertical = 3f },
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}.WithChild(MainContent = new Container
{ {
CornerRadius = 6, CornerRadius = 6,
Masking = true, Masking = true,
@ -130,7 +168,7 @@ namespace osu.Game.Overlays.Notifications
}, },
new CloseButton(CloseButtonIcon) new CloseButton(CloseButtonIcon)
{ {
Action = Close, Action = () => Close(true),
Anchor = Anchor.TopRight, Anchor = Anchor.TopRight,
Origin = Anchor.TopRight, Origin = Anchor.TopRight,
} }
@ -144,7 +182,7 @@ namespace osu.Game.Overlays.Notifications
Blending = BlendingParameters.Additive, Blending = BlendingParameters.Additive,
}, },
} }
} })
}; };
} }
@ -176,7 +214,7 @@ namespace osu.Game.Overlays.Notifications
// right click doesn't trigger OnClick so we need to handle here until that changes. // right click doesn't trigger OnClick so we need to handle here until that changes.
if (e.Button != MouseButton.Left) if (e.Button != MouseButton.Left)
{ {
Close(); Close(true);
return true; return true;
} }
@ -189,7 +227,7 @@ namespace osu.Game.Overlays.Notifications
if (e.Button == MouseButton.Left) if (e.Button == MouseButton.Left)
Activated?.Invoke(); Activated?.Invoke();
Close(); Close(false);
return true; return true;
} }
@ -207,17 +245,131 @@ namespace osu.Game.Overlays.Notifications
public bool WasClosed; public bool WasClosed;
public virtual void Close() public virtual void Close(bool runFlingAnimation)
{ {
if (WasClosed) return; if (WasClosed) return;
WasClosed = true; WasClosed = true;
if (runFlingAnimation && dragContainer.FlingLeft())
this.FadeOut(600, Easing.In);
else
this.FadeOut(100);
Closed?.Invoke(); Closed?.Invoke();
this.FadeOut(100);
Expire(); Expire();
} }
private class DragContainer : Container
{
private Vector2 velocity;
private Vector2 lastPosition;
private readonly Notification notification;
public DragContainer(Notification notification)
{
this.notification = notification;
}
public override RectangleF BoundingBox
{
get
{
var childBounding = Children.First().BoundingBox;
if (X < 0) childBounding *= new Vector2(1, Math.Max(0, 1 + (X / 300)));
if (Y > 0) childBounding *= new Vector2(1, Math.Max(0, 1 - (Y / 200)));
return childBounding;
}
}
protected override bool OnDragStart(DragStartEvent e) => notification.IsInToastTray;
protected override void OnDrag(DragEvent e)
{
if (!notification.IsInToastTray)
return;
Vector2 change = e.MousePosition - e.MouseDownPosition;
// Diminish the drag distance as we go further to simulate "rubber band" feeling.
change *= change.Length <= 0 ? 0 : MathF.Pow(change.Length, 0.8f) / change.Length;
// Only apply Y change if dragging to the left.
if (change.X >= 0)
change.Y = 0;
else
change.Y *= (float)Interpolation.ApplyEasing(Easing.InOutQuart, Math.Min(1, -change.X / 200));
this.MoveTo(change);
}
protected override void OnDragEnd(DragEndEvent e)
{
if (Rotation < -10 || velocity.X < -0.3f)
notification.Close(true);
else
ResetPosition();
base.OnDragEnd(e);
}
private bool flinging;
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
Rotation = Math.Min(0, X * 0.1f);
if (flinging)
{
velocity.Y += (float)Clock.ElapsedFrameTime * 0.005f;
Position += (float)Clock.ElapsedFrameTime * velocity;
}
else if (Clock.ElapsedFrameTime > 0)
{
Vector2 change = (Position - lastPosition) / (float)Clock.ElapsedFrameTime;
if (velocity.X == 0)
velocity = change;
else
{
velocity = new Vector2(
(float)Interpolation.DampContinuously(velocity.X, change.X, 40, Clock.ElapsedFrameTime),
(float)Interpolation.DampContinuously(velocity.Y, change.Y, 40, Clock.ElapsedFrameTime)
);
}
lastPosition = Position;
}
}
public bool FlingLeft()
{
if (!notification.IsInToastTray)
return false;
if (flinging)
return true;
if (velocity.X > -0.3f)
velocity.X = -0.3f - 0.5f * RNG.NextSingle();
flinging = true;
ClearTransforms();
return true;
}
public void ResetPosition()
{
this.MoveTo(Vector2.Zero, 800, Easing.OutElastic);
this.RotateTo(0, 800, Easing.OutElastic);
}
}
internal class CloseButton : OsuClickableContainer internal class CloseButton : OsuClickableContainer
{ {
private SpriteIcon icon = null!; private SpriteIcon icon = null!;

View File

@ -106,14 +106,13 @@ namespace osu.Game.Overlays.Notifications
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
LayoutDuration = 150, LayoutDuration = 150,
LayoutEasing = Easing.OutQuart, LayoutEasing = Easing.OutQuart,
Spacing = new Vector2(3),
} }
}); });
} }
private void clearAll() private void clearAll()
{ {
notifications.Children.ForEach(c => c.Close()); notifications.Children.ForEach(c => c.Close(true));
} }
protected override void Update() protected override void Update()

View File

@ -142,7 +142,7 @@ namespace osu.Game.Overlays.Notifications
case ProgressNotificationState.Completed: case ProgressNotificationState.Completed:
loadingSpinner.Hide(); loadingSpinner.Hide();
attemptPostCompletion(); attemptPostCompletion();
base.Close(); base.Close(false);
break; break;
} }
} }
@ -235,12 +235,12 @@ namespace osu.Game.Overlays.Notifications
}); });
} }
public override void Close() public override void Close(bool runFlingAnimation)
{ {
switch (State) switch (State)
{ {
case ProgressNotificationState.Cancelled: case ProgressNotificationState.Cancelled:
base.Close(); base.Close(runFlingAnimation);
break; break;
case ProgressNotificationState.Active: case ProgressNotificationState.Active:

View File

@ -148,7 +148,7 @@ namespace osu.Game.Updater
StartDownload(); StartDownload();
} }
public override void Close() public override void Close(bool runFlingAnimation)
{ {
// cancelling updates is not currently supported by the underlying updater. // cancelling updates is not currently supported by the underlying updater.
// only allow dismissing for now. // only allow dismissing for now.
@ -156,7 +156,7 @@ namespace osu.Game.Updater
switch (State) switch (State)
{ {
case ProgressNotificationState.Cancelled: case ProgressNotificationState.Cancelled:
base.Close(); base.Close(runFlingAnimation);
break; break;
} }
} }
@ -177,7 +177,7 @@ namespace osu.Game.Updater
public void FailDownload() public void FailDownload()
{ {
State = ProgressNotificationState.Cancelled; State = ProgressNotificationState.Cancelled;
Close(); Close(false);
} }
} }
} }