| | | 1 | | using PropertyGridHelpers.Enums; |
| | | 2 | | using System; |
| | | 3 | | using System.ComponentModel; |
| | | 4 | | using System.Linq; |
| | | 5 | | |
| | | 6 | | namespace PropertyGridHelpers.Attributes |
| | | 7 | | { |
| | | 8 | | #if NET8_0_OR_GREATER |
| | | 9 | | /// <summary> |
| | | 10 | | /// Specifies that the resource path used to localize or visualize this property should be |
| | | 11 | | /// dynamically retrieved from another property on the same object. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="pathPropertyName"> |
| | | 14 | | /// The name of the property (on the same class) that returns a string resource path. |
| | | 15 | | /// </param> |
| | | 16 | | /// <param name="resourceUsage"> |
| | | 17 | | /// The kind of resource this path will be used for. Defaults to <see cref="ResourceUsage.All"/>. |
| | | 18 | | /// </param> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// This attribute is useful for making the resource path configurable at runtime or design time, |
| | | 21 | | /// especially when dealing with localized enum values, icons, or other resource-driven behaviors. |
| | | 22 | | /// |
| | | 23 | | /// You can apply multiple <see cref="DynamicPathSourceAttribute"/> instances to a single property, |
| | | 24 | | /// each with a different <see cref="ResourceUsage"/> value to target specific resource types (e.g. strings vs. imag |
| | | 25 | | /// |
| | | 26 | | /// Resolution logic will select the first matching path based on the requested <paramref name="resourceUsage"/>. |
| | | 27 | | /// </remarks> |
| | | 28 | | /// <example> |
| | | 29 | | /// Example 1: Dynamic string resource binding for enum display |
| | | 30 | | /// <code> |
| | | 31 | | /// [DynamicPathSource(nameof(MyResourcePath), ResourceUsage.Strings)] |
| | | 32 | | /// public MyEnum DisplayOption { get; set; } |
| | | 33 | | /// |
| | | 34 | | /// public string MyResourcePath => "MyApp.Strings.EnumResources"; |
| | | 35 | | /// </code> |
| | | 36 | | /// |
| | | 37 | | /// Example 2: Separate image path for visual enum dropdown |
| | | 38 | | /// <code> |
| | | 39 | | /// [DynamicPathSource(nameof(ImagePath), ResourceUsage.Images)] |
| | | 40 | | /// public MyEnum IconChoice { get; set; } |
| | | 41 | | /// |
| | | 42 | | /// public string ImagePath => "MyApp.Images.Icons"; |
| | | 43 | | /// </code> |
| | | 44 | | /// </example> |
| | | 45 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] |
| | 40 | 46 | | public class DynamicPathSourceAttribute(string pathPropertyName, ResourceUsage resourceUsage = ResourceUsage.All) : |
| | | 47 | | #else |
| | | 48 | | /// <summary> |
| | | 49 | | /// Specifies that the resource path used to localize or visualize this property should be |
| | | 50 | | /// dynamically retrieved from another property on the same object. |
| | | 51 | | /// </summary> |
| | | 52 | | /// <remarks> |
| | | 53 | | /// This attribute is useful for making the resource path configurable at runtime or design time, |
| | | 54 | | /// especially when dealing with localized enum values, icons, or other resource-driven behaviors. |
| | | 55 | | /// |
| | | 56 | | /// You can apply multiple <see cref="DynamicPathSourceAttribute"/> instances to a single property, |
| | | 57 | | /// each with a different <see cref="ResourceUsage"/> value to target specific resource types (e.g. strings vs. imag |
| | | 58 | | /// |
| | | 59 | | /// Resolution logic will select the first matching path based on the requested resourceUsage. |
| | | 60 | | /// </remarks> |
| | | 61 | | /// <example> |
| | | 62 | | /// Example 1: Dynamic string resource binding for enum display |
| | | 63 | | /// <code> |
| | | 64 | | /// [DynamicPathSource(nameof(MyResourcePath), ResourceUsage.Strings)] |
| | | 65 | | /// public MyEnum DisplayOption { get; set; } |
| | | 66 | | /// |
| | | 67 | | /// public string MyResourcePath => "MyApp.Strings.EnumResources"; |
| | | 68 | | /// </code> |
| | | 69 | | /// |
| | | 70 | | /// Example 2: Separate image path for visual enum dropdown |
| | | 71 | | /// <code> |
| | | 72 | | /// [DynamicPathSource(nameof(ImagePath), ResourceUsage.Images)] |
| | | 73 | | /// public MyEnum IconChoice { get; set; } |
| | | 74 | | /// |
| | | 75 | | /// public string ImagePath => "MyApp.Images.Icons"; |
| | | 76 | | /// </code> |
| | | 77 | | /// </example> |
| | | 78 | | [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] |
| | | 79 | | public class DynamicPathSourceAttribute : Attribute |
| | | 80 | | #endif |
| | | 81 | | { |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the name of the property that contains the resource path to use for this context. |
| | | 84 | | /// The referenced property should return a string representing a fully qualified resource class name |
| | | 85 | | /// (e.g., <c>"MyApp.Resources.MyStrings"</c>). |
| | | 86 | | /// </summary> |
| | | 87 | | public string PathPropertyName |
| | | 88 | | { |
| | 36 | 89 | | get; |
| | | 90 | | #if NET8_0_OR_GREATER |
| | 40 | 91 | | } = pathPropertyName; |
| | | 92 | | #else |
| | | 93 | | } |
| | | 94 | | #endif |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Indicates what type of resource this path is intended for (e.g., <see cref="ResourceUsage.Strings"/>, |
| | | 98 | | /// <see cref="ResourceUsage.Images"/>). Used to filter when multiple attributes are present. |
| | | 99 | | /// </summary> |
| | | 100 | | public ResourceUsage ResourceUsage |
| | | 101 | | { |
| | 28 | 102 | | get; |
| | | 103 | | #if NET8_0_OR_GREATER |
| | 40 | 104 | | } = resourceUsage; |
| | | 105 | | #else |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Initializes a new instance of the <see cref="DynamicPathSourceAttribute"/> class. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="pathPropertyName"> |
| | | 112 | | /// The name of the property (on the same class) that returns a string resource path. |
| | | 113 | | /// </param> |
| | | 114 | | /// <param name="resourceUsage"> |
| | | 115 | | /// The kind of resource this path will be used for. Defaults to <see cref="ResourceUsage.All"/>. |
| | | 116 | | /// </param> |
| | 16 | 117 | | public DynamicPathSourceAttribute(string pathPropertyName, ResourceUsage resourceUsage = ResourceUsage.All) |
| | 8 | 118 | | { |
| | 16 | 119 | | PathPropertyName = pathPropertyName; |
| | 16 | 120 | | ResourceUsage = resourceUsage; |
| | 16 | 121 | | } |
| | | 122 | | #endif |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Resolves the most appropriate <see cref="DynamicPathSourceAttribute"/> for the given context and usage. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="context">The context containing property and instance metadata.</param> |
| | | 128 | | /// <param name="resourceUsage"> |
| | | 129 | | /// The intended type of resource (e.g., strings, images) to retrieve the matching attribute. |
| | | 130 | | /// </param> |
| | | 131 | | /// <returns> |
| | | 132 | | /// The best-matching <see cref="DynamicPathSourceAttribute"/>, or <c>null</c> if none found. |
| | | 133 | | /// </returns> |
| | | 134 | | /// <exception cref="ArgumentException"> |
| | | 135 | | /// Thrown if <paramref name="resourceUsage"/> is <see cref="ResourceUsage.None"/>. |
| | | 136 | | /// </exception> |
| | | 137 | | public static DynamicPathSourceAttribute Get(ITypeDescriptorContext context, ResourceUsage resourceUsage = Resou |
| | 72 | 138 | | { |
| | 80 | 139 | | if (context?.Instance == null || context.PropertyDescriptor == null) |
| | 20 | 140 | | return null; |
| | | 141 | | |
| | 76 | 142 | | if (resourceUsage == ResourceUsage.None) |
| | 20 | 143 | | throw new ArgumentException("resourceUsage must not be None", nameof(resourceUsage)); |
| | | 144 | | |
| | 72 | 145 | | var property = Support.Support.GetPropertyInfo(context); |
| | 72 | 146 | | var all = GetCustomAttributes(property, typeof(DynamicPathSourceAttribute)) |
| | 72 | 147 | | .OfType<DynamicPathSourceAttribute>(); |
| | | 148 | | |
| | | 149 | | #if NET35 |
| | | 150 | | return all.FirstOrDefault(a => (a.ResourceUsage & resourceUsage) != ResourceUsage.None); |
| | | 151 | | #else |
| | 100 | 152 | | return all.FirstOrDefault(a => a.ResourceUsage.HasFlag(resourceUsage)); |
| | | 153 | | #endif |
| | 68 | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |