2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using System;
|
2019-05-28 09:59:21 +00:00
|
|
|
|
using System.Threading;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
using osu.Framework.Graphics.Colour;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 05:54:23 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-10-30 16:50:13 +00:00
|
|
|
|
using osu.Framework.Localisation;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2017-12-25 15:44:35 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
namespace osu.Game.Overlays.Notifications
|
|
|
|
|
{
|
|
|
|
|
public partial class ProgressNotification : Notification, IHasCompletionTarget
|
|
|
|
|
{
|
2021-10-28 07:34:12 +00:00
|
|
|
|
private const float loading_spinner_size = 22;
|
|
|
|
|
|
2022-08-30 06:51:46 +00:00
|
|
|
|
public Func<bool>? CancelRequested { get; set; }
|
|
|
|
|
|
2022-09-26 05:30:40 +00:00
|
|
|
|
protected override bool AllowFlingDismiss => false;
|
|
|
|
|
|
2022-08-30 06:51:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The function to post completion notifications back to.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<Notification>? CompletionTarget { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An action to complete when the completion notification is clicked. Return true to close.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<bool>? CompletionClickAction { get; set; }
|
|
|
|
|
|
2021-11-09 12:34:36 +00:00
|
|
|
|
private LocalisableString text;
|
|
|
|
|
|
2022-01-25 14:38:46 +00:00
|
|
|
|
public override LocalisableString Text
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
2021-11-09 12:34:36 +00:00
|
|
|
|
get => text;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text = value;
|
|
|
|
|
Schedule(() => textDrawable.Text = text);
|
|
|
|
|
}
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2023-02-17 13:32:03 +00:00
|
|
|
|
public LocalisableString CompletionText { get; set; } = "Task has completed!";
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-06-20 10:05:33 +00:00
|
|
|
|
private float progress;
|
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public float Progress
|
|
|
|
|
{
|
2019-06-20 10:05:33 +00:00
|
|
|
|
get => progress;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
progress = value;
|
2022-08-30 08:33:08 +00:00
|
|
|
|
Scheduler.AddOnce(p => progressBar.Progress = p, progress);
|
2019-06-20 10:05:33 +00:00
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-08-30 08:37:43 +00:00
|
|
|
|
protected override IconUsage CloseButtonIcon => FontAwesome.Solid.Times;
|
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private OverlayColourProvider colourProvider { get; set; } = null!;
|
2021-10-14 08:52:19 +00:00
|
|
|
|
|
2017-02-12 05:50:42 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-05-05 01:31:11 +00:00
|
|
|
|
// we may have received changes before we were displayed.
|
2022-09-05 19:07:49 +00:00
|
|
|
|
Scheduler.AddOnce(updateState);
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-05-28 09:59:21 +00:00
|
|
|
|
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
public CancellationToken CancellationToken => cancellationTokenSource.Token;
|
|
|
|
|
|
2019-06-20 10:05:33 +00:00
|
|
|
|
public ProgressNotificationState State
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => state;
|
2019-06-20 10:05:33 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (state == value) return;
|
|
|
|
|
|
|
|
|
|
state = value;
|
|
|
|
|
|
2022-09-05 19:07:49 +00:00
|
|
|
|
Scheduler.AddOnce(updateState);
|
|
|
|
|
attemptPostCompletion();
|
2019-06-20 10:05:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
|
{
|
2021-10-28 07:34:12 +00:00
|
|
|
|
const double colour_fade_duration = 200;
|
|
|
|
|
|
2019-06-20 10:05:33 +00:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Queued:
|
|
|
|
|
Light.Colour = colourQueued;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
|
2022-08-30 08:40:35 +00:00
|
|
|
|
IconContent.FadeColour(ColourInfo.GradientVertical(colourQueued, colourQueued.Lighten(0.5f)), colour_fade_duration);
|
2021-10-28 07:34:12 +00:00
|
|
|
|
loadingSpinner.Show();
|
2019-06-20 10:05:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
Light.Colour = colourActive;
|
|
|
|
|
Light.Pulsate = true;
|
|
|
|
|
progressBar.Active = true;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
|
2022-08-30 08:40:35 +00:00
|
|
|
|
IconContent.FadeColour(ColourInfo.GradientVertical(colourActive, colourActive.Lighten(0.5f)), colour_fade_duration);
|
2021-10-28 07:34:12 +00:00
|
|
|
|
loadingSpinner.Show();
|
2019-06-20 10:05:33 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Cancelled:
|
|
|
|
|
cancellationTokenSource.Cancel();
|
|
|
|
|
|
2022-08-30 08:40:35 +00:00
|
|
|
|
IconContent.FadeColour(ColourInfo.GradientVertical(Color4.Gray, Color4.Gray.Lighten(0.5f)), colour_fade_duration);
|
2021-10-28 07:34:12 +00:00
|
|
|
|
loadingSpinner.Hide();
|
|
|
|
|
|
|
|
|
|
var icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.Solid.Ban,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Size = new Vector2(loading_spinner_size),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IconContent.Add(icon);
|
|
|
|
|
|
|
|
|
|
icon.FadeInFromZero(200, Easing.OutQuint);
|
|
|
|
|
|
2019-06-20 10:05:33 +00:00
|
|
|
|
Light.Colour = colourCancelled;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Completed:
|
2021-10-28 07:34:12 +00:00
|
|
|
|
loadingSpinner.Hide();
|
2022-09-16 08:54:44 +00:00
|
|
|
|
attemptPostCompletion();
|
2019-06-20 10:05:33 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-02 05:47:56 +00:00
|
|
|
|
private int completionSent;
|
2022-09-05 19:07:49 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempt to post a completion notification.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void attemptPostCompletion()
|
|
|
|
|
{
|
|
|
|
|
if (state != ProgressNotificationState.Completed) return;
|
|
|
|
|
|
|
|
|
|
// This notification may not have been posted yet (and thus may not have a target to post the completion to).
|
|
|
|
|
// Completion posting will be re-attempted in a scheduled invocation.
|
|
|
|
|
if (CompletionTarget == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-11-02 05:47:56 +00:00
|
|
|
|
// Thread-safe barrier, as this may be called by a web request and also scheduled to the update thread at the same time.
|
2022-11-02 06:47:30 +00:00
|
|
|
|
if (Interlocked.Exchange(ref completionSent, 1) == 1)
|
2022-09-05 19:07:49 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CompletionTarget.Invoke(CreateCompletionNotification());
|
2022-09-16 07:36:56 +00:00
|
|
|
|
|
|
|
|
|
Close(false);
|
2022-09-05 19:07:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private ProgressNotificationState state;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-13 06:02:45 +00:00
|
|
|
|
protected virtual Notification CreateCompletionNotification() => new ProgressCompletionNotification
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
|
|
|
|
Activated = CompletionClickAction,
|
2017-12-18 09:59:25 +00:00
|
|
|
|
Text = CompletionText
|
2017-02-12 05:50:42 +00:00
|
|
|
|
};
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public override bool DisplayOnTop => false;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-09-01 15:58:10 +00:00
|
|
|
|
public override bool IsImportant => false;
|
|
|
|
|
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly ProgressBar progressBar;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private Color4 colourQueued;
|
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourCancelled;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-08-30 06:51:46 +00:00
|
|
|
|
private LoadingSpinner loadingSpinner = null!;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
|
2017-07-28 07:52:29 +00:00
|
|
|
|
private readonly TextFlowContainer textDrawable;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-12 10:39:54 +00:00
|
|
|
|
public ProgressNotification()
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
2022-08-31 03:49:28 +00:00
|
|
|
|
Content.Add(textDrawable = new OsuTextFlowContainer(t => t.Font = t.Font.With(size: 14, weight: FontWeight.Medium))
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-08-30 08:49:29 +00:00
|
|
|
|
MainContent.Add(progressBar = new ProgressBar
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-10-28 07:34:12 +00:00
|
|
|
|
// make some extra space for the progress bar.
|
|
|
|
|
IconContent.Margin = new MarginPadding { Bottom = 5 };
|
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
State = ProgressNotificationState.Queued;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-28 07:53:49 +00:00
|
|
|
|
// don't close on click by default.
|
|
|
|
|
Activated = () => false;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-12 10:39:54 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourQueued = colours.YellowDark;
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
colourCancelled = colours.Red;
|
2021-10-28 07:34:12 +00:00
|
|
|
|
|
|
|
|
|
IconContent.AddRange(new Drawable[]
|
|
|
|
|
{
|
2022-08-30 08:40:35 +00:00
|
|
|
|
new Box
|
2021-10-28 07:34:12 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2022-08-30 08:40:35 +00:00
|
|
|
|
Colour = colourProvider.Background5,
|
2022-09-12 05:58:46 +00:00
|
|
|
|
Depth = float.MaxValue,
|
2021-10-28 07:34:12 +00:00
|
|
|
|
},
|
|
|
|
|
loadingSpinner = new LoadingSpinner
|
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(loading_spinner_size),
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-02-12 10:39:54 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-09-12 09:57:18 +00:00
|
|
|
|
public override void Close(bool runFlingAnimation)
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
switch (State)
|
|
|
|
|
{
|
2022-09-16 07:36:56 +00:00
|
|
|
|
case ProgressNotificationState.Completed:
|
2017-02-10 07:26:43 +00:00
|
|
|
|
case ProgressNotificationState.Cancelled:
|
2022-09-12 09:57:18 +00:00
|
|
|
|
base.Close(runFlingAnimation);
|
2017-02-10 07:26:43 +00:00
|
|
|
|
break;
|
2019-04-01 03:44:46 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
case ProgressNotificationState.Queued:
|
2017-08-24 11:15:45 +00:00
|
|
|
|
if (CancelRequested?.Invoke() != false)
|
|
|
|
|
State = ProgressNotificationState.Cancelled;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private partial class ProgressBar : Container
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
2017-07-28 07:53:37 +00:00
|
|
|
|
private readonly Box box;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourInactive;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private float progress;
|
2019-02-28 04:31:40 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public float Progress
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => progress;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (progress == value) return;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
progress = value;
|
2017-07-22 18:50:25 +00:00
|
|
|
|
box.ResizeTo(new Vector2(progress, 1), 100, Easing.OutQuad);
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private bool active;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public bool Active
|
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => active;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
active = value;
|
2017-07-14 16:18:12 +00:00
|
|
|
|
this.FadeColour(active ? colourActive : colourInactive, 100);
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-28 07:53:37 +00:00
|
|
|
|
public ProgressBar()
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
box = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Width = 0,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-07-28 07:53:37 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
Colour = colourInactive = OsuColour.Gray(0.5f);
|
|
|
|
|
Height = 5;
|
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public enum ProgressNotificationState
|
|
|
|
|
{
|
|
|
|
|
Queued,
|
|
|
|
|
Active,
|
|
|
|
|
Completed,
|
|
|
|
|
Cancelled
|
|
|
|
|
}
|
2017-10-18 01:07:20 +00:00
|
|
|
|
}
|