using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.XR.Management.Metadata;
using UnityEngine;
using Styles = UnityEditor.XR.Management.XRSettingsManager.Styles;
namespace UnityEditor.XR.Management
{
///
/// This class holds information that should be displayed in an Editor tooltip for a given package.
///
public class PackageNotificationInfo
{
private PackageNotificationInfo() {}
///
/// Constructs a container for package notification information that displays in the XR Plug-in Management window.
///
///
/// The GUIContent icon to display in the XR Plug-in Management window. If the tooltip of this
/// icon is empty, null, only whitespace, or otherwise invalid, the constructor will throw an exception.
///
///
/// Used to surface a URI that points to additional information about the notification. For example, clicking the
/// icon directly could send the user to the package documentation website.
///
///
/// Thrown if either does not contain a valid tooltip or if
/// is not empty and isn't a valid URI string.
///
public PackageNotificationInfo(GUIContent userInterfaceIcon, string tooltip, string additionalInfoUri = default)
{
if (string.IsNullOrWhiteSpace(tooltip) || tooltip.Length == 0)
throw new ArgumentException("The package warning tooltip must contain a displayable message!");
if (additionalInfoUri != default)
{
if (!(Uri.TryCreate(additionalInfoUri, UriKind.Absolute, out var uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)))
throw new ArgumentException($"The supplied information URI {additionalInfoUri} must be a well formatted URI string!");
userInterfaceIcon.tooltip = $"{tooltip}\n\nClick the icon for additional information.";
}
else
userInterfaceIcon.tooltip = tooltip;
this.additionalInfoUri = additionalInfoUri;
this.userInterfaceIcon = userInterfaceIcon;
}
///
/// A read-only string that contains a link to additional information about the warning.
///
///
/// If this is null or empty, the window will not redirect the user.
///
public readonly string additionalInfoUri;
///
/// The GUI icon and tooltip that will be drawn for this PackageNotificationInfo.
///
public readonly GUIContent userInterfaceIcon;
}
///
/// Static utility class for managing package notifications for packages.
///
public static class PackageNotificationUtils
{
static Dictionary s_RegisteredPackagesWithNotifications = new Dictionary();
///
/// Dictionary of packages that have notification to report. When a package is added to the project,
/// that package will register itself with this container if it requires access to notification functionality.
///
///
/// This is a read-only dictionary and cannot be modified. To modify the dictionary, use the
/// method.
///
public static IReadOnlyDictionary registeredPackagesWithNotifications =>
s_RegisteredPackagesWithNotifications.ToDictionary(pair => pair.Key, pair => pair.Value);
///
/// Registers a given package ID as having a notification and supplies that notification.
///
///
/// The metadata identifier for a given package
///
///
/// The for the package that corresponds to .
///
public static void RegisterPackageNotificationInformation(string packageId, PackageNotificationInfo notificationInfo)
{
if (s_RegisteredPackagesWithNotifications.ContainsKey(packageId))
s_RegisteredPackagesWithNotifications[packageId] = notificationInfo;
else
s_RegisteredPackagesWithNotifications.Add(packageId, notificationInfo);
}
const int k_RectPixelOffsetWidth = 5;
internal static void DrawNotificationIconUI(PackageNotificationInfo notificationInfo, Rect guiRect, int pixelOffset = k_RectPixelOffsetWidth)
{
var position = new Vector2(guiRect.xMax - (notificationInfo.userInterfaceIcon.image.width + pixelOffset), guiRect.y);
var size = new Vector2(notificationInfo.userInterfaceIcon.image.width, guiRect.height);
var toolTipRect = new Rect(position, size);
var labelStyle = EditorGUIUtility.isProSkin ? Styles.k_UrlLabelProfessional : Styles.k_UrlLabelPersonal;
if (GUI.Button(toolTipRect, notificationInfo.userInterfaceIcon, labelStyle))
LaunchLink(notificationInfo);
}
static void LaunchLink(PackageNotificationInfo info)
{
if (info.additionalInfoUri.Length > 0)
Application.OpenURL(info.additionalInfoUri);
}
}
}