mirror of
https://github.com/ppy/osu.git
synced 2025-03-05 12:32:58 +08:00
Merge pull request #22588 from Joehuu/fix-clickable-avatar-bloat
Remove code bloat from `ClickableAvatar`
This commit is contained in:
commit
65bb3a5cad
@ -1,11 +1,8 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
#nullable disable
|
using System;
|
||||||
|
|
||||||
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.Input.Events;
|
||||||
using osu.Framework.Localisation;
|
using osu.Framework.Localisation;
|
||||||
using osu.Game.Graphics.Containers;
|
using osu.Game.Graphics.Containers;
|
||||||
@ -13,56 +10,49 @@ 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
|
||||||
/// Whether to open the user's profile when clicked.
|
|
||||||
/// </summary>
|
|
||||||
public bool OpenOnClick
|
|
||||||
{
|
{
|
||||||
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>
|
/// <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.
|
||||||
/// Setting this to <c>true</c> exposes the username via tooltip for special cases where this is not true.
|
/// Setting this to <c>true</c> exposes the username via tooltip for special cases where this is not true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool ShowUsernameTooltip
|
public bool ShowUsernameTooltip { get; set; }
|
||||||
{
|
|
||||||
set => clickableArea.TooltipText = value ? (user?.Username ?? string.Empty) : default_tooltip_text;
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly APIUser user;
|
private readonly APIUser? user;
|
||||||
|
|
||||||
[Resolved(CanBeNull = true)]
|
[Resolved]
|
||||||
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()
|
||||||
@ -71,23 +61,12 @@ namespace osu.Game.Users.Drawables
|
|||||||
game?.ShowUser(user);
|
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
|
return base.OnClick(e);
|
||||||
{
|
|
||||||
get => Enabled.Value ? tooltip : default;
|
|
||||||
set => tooltip = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override bool OnClick(ClickEvent e)
|
|
||||||
{
|
|
||||||
if (!Enabled.Value)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return base.OnClick(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
#nullable disable
|
|
||||||
|
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Effects;
|
using osu.Framework.Graphics.Effects;
|
||||||
@ -13,9 +11,9 @@ namespace osu.Game.Users.Drawables
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// An avatar which can update to a new user when needed.
|
/// An avatar which can update to a new user when needed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class UpdateableAvatar : ModelBackedDrawable<APIUser>
|
public partial class UpdateableAvatar : ModelBackedDrawable<APIUser?>
|
||||||
{
|
{
|
||||||
public APIUser User
|
public APIUser? User
|
||||||
{
|
{
|
||||||
get => Model;
|
get => Model;
|
||||||
set => Model = value;
|
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="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="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>
|
/// <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.isInteractive = isInteractive;
|
||||||
this.showUsernameTooltip = showUsernameTooltip;
|
this.showUsernameTooltip = showUsernameTooltip;
|
||||||
@ -67,7 +65,7 @@ namespace osu.Game.Users.Drawables
|
|||||||
User = user;
|
User = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override Drawable CreateDrawable(APIUser user)
|
protected override Drawable? CreateDrawable(APIUser? user)
|
||||||
{
|
{
|
||||||
if (user == null && !showGuestOnNull)
|
if (user == null && !showGuestOnNull)
|
||||||
return null;
|
return null;
|
||||||
@ -76,7 +74,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,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user