| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>font-weight</c> value within the Allyaria theming system. Provides a strongly typed wrapper for |
| | | 5 | | /// defining typographic weight (thickness or boldness) of text. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleFontWeight : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleFontWeight" /> record using the specified <see cref="Kind" /> |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="kind">The font weight kind that determines how bold or light the text is rendered.</param> |
| | | 13 | | public StyleFontWeight(Kind kind) |
| | 278 | 14 | | : base(value: kind.GetDescription()) { } |
| | | 15 | | |
| | | 16 | | /// <summary>Defines the supported CSS <c>font-weight</c> property values.</summary> |
| | | 17 | | public enum Kind |
| | | 18 | | { |
| | | 19 | | /// <summary>Specifies bold text, typically equivalent to a numeric weight of 700.</summary> |
| | | 20 | | [Description(description: "bold")] |
| | | 21 | | Bold, |
| | | 22 | | |
| | | 23 | | /// <summary>Specifies a font weight bolder than the parent element’s weight.</summary> |
| | | 24 | | [Description(description: "bolder")] |
| | | 25 | | Bolder, |
| | | 26 | | |
| | | 27 | | /// <summary>Specifies a font weight lighter than the parent element’s weight.</summary> |
| | | 28 | | [Description(description: "lighter")] |
| | | 29 | | Lighter, |
| | | 30 | | |
| | | 31 | | /// <summary>Specifies a normal font weight, typically equivalent to a numeric weight of 400.</summary> |
| | | 32 | | [Description(description: "normal")] |
| | | 33 | | Normal, |
| | | 34 | | |
| | | 35 | | /// <summary>Specifies a thin font weight (100).</summary> |
| | | 36 | | [Description(description: "100")] |
| | | 37 | | Weight100, |
| | | 38 | | |
| | | 39 | | /// <summary>Specifies an extra-light font weight (200).</summary> |
| | | 40 | | [Description(description: "200")] |
| | | 41 | | Weight200, |
| | | 42 | | |
| | | 43 | | /// <summary>Specifies a light font weight (300).</summary> |
| | | 44 | | [Description(description: "300")] |
| | | 45 | | Weight300, |
| | | 46 | | |
| | | 47 | | /// <summary>Specifies a normal font weight (400).</summary> |
| | | 48 | | [Description(description: "400")] |
| | | 49 | | Weight400, |
| | | 50 | | |
| | | 51 | | /// <summary>Specifies a medium font weight (500).</summary> |
| | | 52 | | [Description(description: "500")] |
| | | 53 | | Weight500, |
| | | 54 | | |
| | | 55 | | /// <summary>Specifies a semi-bold font weight (600).</summary> |
| | | 56 | | [Description(description: "600")] |
| | | 57 | | Weight600, |
| | | 58 | | |
| | | 59 | | /// <summary>Specifies a bold font weight (700).</summary> |
| | | 60 | | [Description(description: "700")] |
| | | 61 | | Weight700, |
| | | 62 | | |
| | | 63 | | /// <summary>Specifies an extra-bold font weight (800).</summary> |
| | | 64 | | [Description(description: "800")] |
| | | 65 | | Weight800, |
| | | 66 | | |
| | | 67 | | /// <summary>Specifies a black (heaviest) font weight (900).</summary> |
| | | 68 | | [Description(description: "900")] |
| | | 69 | | Weight900 |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Parses a string representation of a CSS <c>font-weight</c> value into a <see cref="StyleFontWeight" /> instance. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="value">The string representation of the font-weight value.</param> |
| | | 76 | | /// <returns>A new <see cref="StyleFontWeight" /> instance representing the parsed value.</returns> |
| | | 77 | | /// <exception cref="AryArgumentException"> |
| | | 78 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 79 | | /// </exception> |
| | | 80 | | public static StyleFontWeight Parse(string? value) |
| | | 81 | | { |
| | 20 | 82 | | var valueString = value is "100" or "200" or "300" or "400" or "500" or "600" or "700" or "800" or "900" |
| | 20 | 83 | | ? $"Weight{value}" |
| | 20 | 84 | | : value?.Trim(); |
| | | 85 | | |
| | 20 | 86 | | return valueString.TryParseEnum<Kind>(result: out var kind) |
| | 20 | 87 | | ? new StyleFontWeight(kind: kind) |
| | 20 | 88 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary>Attempts to parse a string into a <see cref="StyleFontWeight" /> instance.</summary> |
| | | 92 | | /// <param name="value">The string representation of the font-weight value to parse.</param> |
| | | 93 | | /// <param name="result"> |
| | | 94 | | /// When this method returns, contains the parsed <see cref="StyleFontWeight" /> instance or <see langword="null" /> |
| | | 95 | | /// parsing failed. |
| | | 96 | | /// </param> |
| | | 97 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 98 | | public static bool TryParse(string? value, out StyleFontWeight? result) |
| | | 99 | | { |
| | | 100 | | try |
| | | 101 | | { |
| | 3 | 102 | | result = Parse(value: value); |
| | | 103 | | |
| | 1 | 104 | | return true; |
| | | 105 | | } |
| | 2 | 106 | | catch |
| | | 107 | | { |
| | 2 | 108 | | result = null; |
| | | 109 | | |
| | 2 | 110 | | return false; |
| | | 111 | | } |
| | 3 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary>Implicitly converts a string into a <see cref="StyleFontWeight" /> instance.</summary> |
| | | 115 | | /// <param name="value">The string representation of the font-weight value.</param> |
| | | 116 | | /// <returns>A <see cref="StyleFontWeight" /> instance representing the provided value.</returns> |
| | | 117 | | /// <exception cref="AryArgumentException"> |
| | | 118 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 119 | | /// <see cref="Kind" />. |
| | | 120 | | /// </exception> |
| | 2 | 121 | | public static implicit operator StyleFontWeight(string? value) => Parse(value: value); |
| | | 122 | | |
| | | 123 | | /// <summary>Implicitly converts a <see cref="StyleFontWeight" /> instance to its string representation.</summary> |
| | | 124 | | /// <param name="value">The <see cref="StyleFontWeight" /> instance to convert.</param> |
| | | 125 | | /// <returns> |
| | | 126 | | /// The underlying CSS <c>font-weight</c> string value, or an empty string if <paramref name="value" /> is |
| | | 127 | | /// <see langword="null" />. |
| | | 128 | | /// </returns> |
| | 2 | 129 | | public static implicit operator string(StyleFontWeight? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 130 | | } |