// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Collections.Frozen;
using System.Runtime.InteropServices;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.ApplicationModel.Resources.Core;
namespace AccountDialog.Assets;
///
/// Provides access to application image assets using the ResourceManager.
/// Works in both direct launch (TestApp) and shell-hosted (Settings Host) contexts
/// by resolving images through the resource map keyed by AccountDialogAlias.
///
internal sealed class AssetProvider : IAssetProvider
{
// Must match XamlResourceMapName in AccountDialog.csproj and PackageAlias in appxfragment
private const string ResourceMapName = "AccountDialogAlias";
private const string ImageResourcePath = "Files/AccountDialog/Assets/Images/";
private const string DarkImageResourcePath = "Files/AccountDialog/Assets/Images/Dark/";
// Image file names mapped to asset keys
private static readonly FrozenDictionary ImageFileNames = new Dictionary
{
[AssetKey.XboxSubscriptionBanner] = "xbox-purchase-dialogue-banner-lotus.jpg",
[AssetKey.XboxEssentialVertical] = "xbox-essential-vertical.png",
[AssetKey.XboxPcGamePassVertical] = "xbox-pc-game-pass-vertical-logo.png",
[AssetKey.XboxPremiumVertical] = "xbox-premium-vertical-logo.png",
[AssetKey.XboxUltimateVertical] = "xbox-ultimate-vertical-logo.png",
[AssetKey.XboxEssentialHorizontal] = "xbox-essential-horizontal.png",
[AssetKey.XboxPcGamePassHorizontal] = "xbox-pc-game-pass-horizontal.png",
[AssetKey.XboxPremiumHorizontal] = "xbox-premium-horizontal.png",
[AssetKey.XboxUltimateHorizontal] = "xbox-ultimate-horizontal.png",
}.ToFrozenDictionary();
// Dark theme image file names mapped to asset keys
private static readonly FrozenDictionary DarkImageFileNames = new Dictionary
{
}.ToFrozenDictionary();
// String key mappings for backward compatibility
private static readonly FrozenDictionary StringKeyMappings = new Dictionary(StringComparer.OrdinalIgnoreCase)
{
["XboxSubscriptionBanner"] = AssetKey.XboxSubscriptionBanner,
["XboxEssentialVertical"] = AssetKey.XboxEssentialVertical,
["XboxPcGamePassVertical"] = AssetKey.XboxPcGamePassVertical,
["XboxPremiumVertical"] = AssetKey.XboxPremiumVertical,
["XboxUltimateVertical"] = AssetKey.XboxUltimateVertical,
["XboxEssentialHorizontal"] = AssetKey.XboxEssentialHorizontal,
["XboxPcGamePassHorizontal"] = AssetKey.XboxPcGamePassHorizontal,
["XboxPremiumHorizontal"] = AssetKey.XboxPremiumHorizontal,
["XboxUltimateHorizontal"] = AssetKey.XboxUltimateHorizontal,
}.ToFrozenDictionary();
private readonly Dictionary _imageCache = new();
private static ResourceMap? _resourceMap;
///
public ImageSource? GetImage(AssetKey key)
{
string resourcePath;
string fileName;
if (IsDarkTheme() && DarkImageFileNames.TryGetValue(key, out var darkFileName))
{
resourcePath = DarkImageResourcePath;
fileName = darkFileName;
}
else if (ImageFileNames.TryGetValue(key, out var lightFileName))
{
resourcePath = ImageResourcePath;
fileName = lightFileName;
}
else
{
return null;
}
var cacheKey = resourcePath + fileName;
if (_imageCache.TryGetValue(cacheKey, out var cached))
{
return cached;
}
var image = TryLoadViaResourceManager(resourcePath, fileName);
if (image != null)
{
_imageCache[cacheKey] = image;
}
return image;
}
///
public ImageSource? GetImage(string key)
{
if (StringKeyMappings.TryGetValue(key, out var assetKey))
{
return GetImage(assetKey);
}
return null;
}
private static ImageSource? TryLoadViaResourceManager(string resourcePath, string fileName)
{
try
{
_resourceMap ??= TryGetResourceMap();
if (_resourceMap == null)
{
return null;
}
var resourceCandidate = _resourceMap.GetValue(resourcePath + fileName);
if (resourceCandidate != null)
{
return new BitmapImage(new Uri(resourceCandidate.ValueAsString));
}
return null;
}
catch (COMException)
{
return null;
}
}
private static bool IsDarkTheme()
{
try
{
return Application.Current.RequestedTheme == ApplicationTheme.Dark;
}
catch (COMException)
{
return false;
}
}
private static ResourceMap? TryGetResourceMap()
{
try
{
var allMaps = ResourceManager.Current.AllResourceMaps;
if (allMaps.ContainsKey(ResourceMapName))
{
return allMaps[ResourceMapName];
}
return null;
}
catch (COMException)
{
return null;
}
}
}