2022-01-21 22:41:45 +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 osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
using osu.Framework.Threading;
|
|
|
|
|
2022-02-14 16:51:39 +08:00
|
|
|
namespace osu.Game.Graphics.Containers
|
2022-01-21 22:41:45 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
2022-02-04 10:45:12 +08:00
|
|
|
/// Represents a <see cref="Container"/> with the ability to expand/contract on hover.
|
2022-01-21 22:41:45 +08:00
|
|
|
/// </summary>
|
2022-02-04 10:45:12 +08:00
|
|
|
public partial class ExpandingContainer : Container, IExpandingContainer
|
2022-01-21 22:41:45 +08:00
|
|
|
{
|
|
|
|
private readonly float contractedWidth;
|
|
|
|
private readonly float expandedWidth;
|
|
|
|
|
|
|
|
public BindableBool Expanded { get; } = new BindableBool();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Delay before the container switches to expanded state from hover.
|
|
|
|
/// </summary>
|
|
|
|
protected virtual double HoverExpansionDelay => 0;
|
|
|
|
|
2023-12-27 01:33:02 +08:00
|
|
|
protected virtual bool ExpandOnHover => true;
|
|
|
|
|
2022-01-21 22:41:45 +08:00
|
|
|
protected override Container<Drawable> Content => FillFlow;
|
|
|
|
|
|
|
|
protected FillFlowContainer FillFlow { get; }
|
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
protected ExpandingContainer(float contractedWidth, float expandedWidth)
|
2022-01-21 22:41:45 +08:00
|
|
|
{
|
|
|
|
this.contractedWidth = contractedWidth;
|
|
|
|
this.expandedWidth = expandedWidth;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
Width = contractedWidth;
|
|
|
|
|
|
|
|
InternalChild = new OsuScrollContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
ScrollbarVisible = false,
|
|
|
|
Child = FillFlow = new FillFlowContainer
|
|
|
|
{
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-12-27 01:33:02 +08:00
|
|
|
private ScheduledDelegate? hoverExpandEvent;
|
2022-01-21 22:41:45 +08:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
Expanded.BindValueChanged(v =>
|
|
|
|
{
|
|
|
|
this.ResizeWidthTo(v.NewValue ? expandedWidth : contractedWidth, 500, Easing.OutQuint);
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
2022-02-04 10:45:12 +08:00
|
|
|
updateHoverExpansion();
|
2022-01-21 22:41:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
|
|
|
{
|
2022-02-04 10:45:12 +08:00
|
|
|
updateHoverExpansion();
|
2022-01-21 22:41:45 +08:00
|
|
|
return base.OnMouseMove(e);
|
|
|
|
}
|
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2022-01-21 22:41:45 +08:00
|
|
|
{
|
2022-02-04 10:45:12 +08:00
|
|
|
if (hoverExpandEvent != null)
|
|
|
|
{
|
|
|
|
hoverExpandEvent?.Cancel();
|
|
|
|
hoverExpandEvent = null;
|
2022-01-21 22:41:45 +08:00
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
Expanded.Value = false;
|
|
|
|
return;
|
|
|
|
}
|
2022-01-21 22:41:45 +08:00
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
base.OnHoverLost(e);
|
2022-01-21 22:41:45 +08:00
|
|
|
}
|
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
private void updateHoverExpansion()
|
2022-01-27 20:57:56 +08:00
|
|
|
{
|
2023-12-27 01:33:02 +08:00
|
|
|
if (!ExpandOnHover)
|
|
|
|
return;
|
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
hoverExpandEvent?.Cancel();
|
2022-01-27 20:57:56 +08:00
|
|
|
|
2022-02-04 10:45:12 +08:00
|
|
|
if (IsHovered && !Expanded.Value)
|
|
|
|
hoverExpandEvent = Scheduler.AddDelayed(() => Expanded.Value = true, HoverExpansionDelay);
|
2022-01-27 20:57:56 +08:00
|
|
|
}
|
2022-01-21 22:41:45 +08:00
|
|
|
}
|
|
|
|
}
|