1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 17:53:15 +08:00

Remove code bloat from ClickableAvatar

This commit is contained in:
Joseph Madamba 2023-02-09 12:59:26 -08:00
parent ab81397119
commit 0b41dbf579
2 changed files with 5 additions and 42 deletions

View File

@ -4,26 +4,17 @@
#nullable disable #nullable disable
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Users.Drawables namespace osu.Game.Users.Drawables
{ {
public partial class ClickableAvatar : Container public partial class ClickableAvatar : OsuClickableContainer
{ {
private const string default_tooltip_text = "view profile"; private const string default_tooltip_text = "view profile";
/// <summary> public override LocalisableString TooltipText { get; set; } = default_tooltip_text;
/// Whether to open the user's profile when clicked.
/// </summary>
public bool OpenOnClick
{
set => clickableArea.Enabled.Value = clickableArea.Action != null && value;
}
/// <summary> /// <summary>
/// By default, the tooltip will show "view profile" as avatars are usually displayed next to a username. /// By default, the tooltip will show "view profile" as avatars are usually displayed next to a username.
@ -31,7 +22,7 @@ namespace osu.Game.Users.Drawables
/// </summary> /// </summary>
public bool ShowUsernameTooltip public bool ShowUsernameTooltip
{ {
set => clickableArea.TooltipText = value ? (user?.Username ?? string.Empty) : default_tooltip_text; set => TooltipText = value ? (user?.Username ?? string.Empty) : default_tooltip_text;
} }
private readonly APIUser user; private readonly APIUser user;
@ -39,30 +30,22 @@ namespace osu.Game.Users.Drawables
[Resolved(CanBeNull = true)] [Resolved(CanBeNull = true)]
private OsuGame game { get; set; } private OsuGame game { get; set; }
private readonly ClickableArea clickableArea;
/// <summary> /// <summary>
/// A clickable avatar for the specified user, with UI sounds included. /// A clickable avatar for the specified user, with UI sounds included.
/// If <see cref="OpenOnClick"/> is <c>true</c>, clicking will open the user's profile.
/// </summary> /// </summary>
/// <param name="user">The user. A null value will get a placeholder avatar.</param> /// <param name="user">The user. A null value will get a placeholder avatar.</param>
public ClickableAvatar(APIUser user = null) public ClickableAvatar(APIUser user = null)
{ {
this.user = user; this.user = user;
Add(clickableArea = new ClickableArea
{
RelativeSizeAxes = Axes.Both,
});
if (user?.Id != APIUser.SYSTEM_USER_ID) if (user?.Id != APIUser.SYSTEM_USER_ID)
clickableArea.Action = openProfile; Action = openProfile;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
LoadComponentAsync(new DrawableAvatar(user), clickableArea.Add); LoadComponentAsync(new DrawableAvatar(user), Add);
} }
private void openProfile() private void openProfile()
@ -70,24 +53,5 @@ namespace osu.Game.Users.Drawables
if (user?.Id > 1 || !string.IsNullOrEmpty(user?.Username)) if (user?.Id > 1 || !string.IsNullOrEmpty(user?.Username))
game?.ShowUser(user); game?.ShowUser(user);
} }
private partial class ClickableArea : OsuClickableContainer
{
private LocalisableString tooltip = default_tooltip_text;
public override LocalisableString TooltipText
{
get => Enabled.Value ? tooltip : default;
set => tooltip = value;
}
protected override bool OnClick(ClickEvent e)
{
if (!Enabled.Value)
return false;
return base.OnClick(e);
}
}
} }
} }

View File

@ -76,7 +76,6 @@ namespace osu.Game.Users.Drawables
{ {
return new ClickableAvatar(user) return new ClickableAvatar(user)
{ {
OpenOnClick = true,
ShowUsernameTooltip = showUsernameTooltip, ShowUsernameTooltip = showUsernameTooltip,
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
}; };