1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 08:52:55 +08:00

Merge pull request #22588 from Joehuu/fix-clickable-avatar-bloat

Remove code bloat from `ClickableAvatar`
This commit is contained in:
Bartłomiej Dach 2023-02-10 22:07:49 +01:00 committed by GitHub
commit 65bb3a5cad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 50 deletions

View File

@ -1,11 +1,8 @@
// 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.
#nullable disable
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
@ -13,56 +10,49 @@ using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Users.Drawables
{
public partial class ClickableAvatar : Container
public partial class ClickableAvatar : OsuClickableContainer
{
private const string default_tooltip_text = "view profile";
/// <summary>
/// Whether to open the user's profile when clicked.
/// </summary>
public bool OpenOnClick
public override LocalisableString TooltipText
{
set => clickableArea.Enabled.Value = clickableArea.Action != null && value;
get
{
if (!Enabled.Value)
return string.Empty;
return ShowUsernameTooltip ? (user?.Username ?? string.Empty) : default_tooltip_text;
}
set => throw new NotSupportedException();
}
/// <summary>
/// By default, the tooltip will show "view profile" as avatars are usually displayed next to a username.
/// Setting this to <c>true</c> exposes the username via tooltip for special cases where this is not true.
/// </summary>
public bool ShowUsernameTooltip
{
set => clickableArea.TooltipText = value ? (user?.Username ?? string.Empty) : default_tooltip_text;
}
public bool ShowUsernameTooltip { get; set; }
private readonly APIUser user;
private readonly APIUser? user;
[Resolved(CanBeNull = true)]
private OsuGame game { get; set; }
private readonly ClickableArea clickableArea;
[Resolved]
private OsuGame? game { get; set; }
/// <summary>
/// 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>
/// <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;
Add(clickableArea = new ClickableArea
{
RelativeSizeAxes = Axes.Both,
});
if (user?.Id != APIUser.SYSTEM_USER_ID)
clickableArea.Action = openProfile;
Action = openProfile;
}
[BackgroundDependencyLoader]
private void load()
{
LoadComponentAsync(new DrawableAvatar(user), clickableArea.Add);
LoadComponentAsync(new DrawableAvatar(user), Add);
}
private void openProfile()
@ -71,23 +61,12 @@ namespace osu.Game.Users.Drawables
game?.ShowUser(user);
}
private partial class ClickableArea : OsuClickableContainer
protected override bool OnClick(ClickEvent e)
{
private LocalisableString tooltip = default_tooltip_text;
if (!Enabled.Value)
return false;
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);
}
return base.OnClick(e);
}
}
}

View File

@ -1,8 +1,6 @@
// 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.
#nullable disable
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
@ -13,9 +11,9 @@ namespace osu.Game.Users.Drawables
/// <summary>
/// An avatar which can update to a new user when needed.
/// </summary>
public partial class UpdateableAvatar : ModelBackedDrawable<APIUser>
public partial class UpdateableAvatar : ModelBackedDrawable<APIUser?>
{
public APIUser User
public APIUser? User
{
get => Model;
set => Model = value;
@ -58,7 +56,7 @@ namespace osu.Game.Users.Drawables
/// <param name="isInteractive">If set to true, hover/click sounds will play and clicking the avatar will open the user's profile.</param>
/// <param name="showUsernameTooltip">Whether to show the username rather than "view profile" on the tooltip. (note: this only applies if <paramref name="isInteractive"/> is also true)</param>
/// <param name="showGuestOnNull">Whether to show a default guest representation on null user (as opposed to nothing).</param>
public UpdateableAvatar(APIUser user = null, bool isInteractive = true, bool showUsernameTooltip = false, bool showGuestOnNull = true)
public UpdateableAvatar(APIUser? user = null, bool isInteractive = true, bool showUsernameTooltip = false, bool showGuestOnNull = true)
{
this.isInteractive = isInteractive;
this.showUsernameTooltip = showUsernameTooltip;
@ -67,7 +65,7 @@ namespace osu.Game.Users.Drawables
User = user;
}
protected override Drawable CreateDrawable(APIUser user)
protected override Drawable? CreateDrawable(APIUser? user)
{
if (user == null && !showGuestOnNull)
return null;
@ -76,7 +74,6 @@ namespace osu.Game.Users.Drawables
{
return new ClickableAvatar(user)
{
OpenOnClick = true,
ShowUsernameTooltip = showUsernameTooltip,
RelativeSizeAxes = Axes.Both,
};