| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>vertical-align</c> value within the Allyaria theming system. Provides a strongly typed wrapper f |
| | | 5 | | /// controlling the vertical alignment of inline or table-cell elements. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleVerticalAlign : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleVerticalAlign" /> record using the specified <see cref="Kind" |
| | | 11 | | /// value. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="kind">The vertical alignment kind to represent.</param> |
| | | 14 | | public StyleVerticalAlign(Kind kind) |
| | 54 | 15 | | : base(value: kind.GetDescription()) { } |
| | | 16 | | |
| | | 17 | | /// <summary>Defines the supported CSS <c>vertical-align</c> property values.</summary> |
| | | 18 | | public enum Kind |
| | | 19 | | { |
| | | 20 | | /// <summary>Aligns the baseline of the element with the baseline of its parent.</summary> |
| | | 21 | | [Description(description: "baseline")] |
| | | 22 | | Baseline, |
| | | 23 | | |
| | | 24 | | /// <summary>Aligns the bottom of the element with the lowest element on the line.</summary> |
| | | 25 | | [Description(description: "bottom")] |
| | | 26 | | Bottom, |
| | | 27 | | |
| | | 28 | | /// <summary>Aligns the middle of the element with the baseline plus half the x-height of the parent.</summary> |
| | | 29 | | [Description(description: "middle")] |
| | | 30 | | Middle, |
| | | 31 | | |
| | | 32 | | /// <summary>Aligns the element as subscript text, lowering it below the baseline.</summary> |
| | | 33 | | [Description(description: "sub")] |
| | | 34 | | Sub, |
| | | 35 | | |
| | | 36 | | /// <summary>Aligns the element as superscript text, raising it above the baseline.</summary> |
| | | 37 | | [Description(description: "super")] |
| | | 38 | | Super, |
| | | 39 | | |
| | | 40 | | /// <summary>Aligns the bottom of the element with the bottom of the parent element’s text.</summary> |
| | | 41 | | [Description(description: "text-bottom")] |
| | | 42 | | TextBottom, |
| | | 43 | | |
| | | 44 | | /// <summary>Aligns the top of the element with the top of the parent element’s text.</summary> |
| | | 45 | | [Description(description: "text-top")] |
| | | 46 | | TextTop, |
| | | 47 | | |
| | | 48 | | /// <summary>Aligns the top of the element with the highest element on the line.</summary> |
| | | 49 | | [Description(description: "top")] |
| | | 50 | | Top |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Parses a string representation of a CSS <c>vertical-align</c> value into a <see cref="StyleVerticalAlign" /> ins |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="value">The string representation of the vertical-align value.</param> |
| | | 57 | | /// <returns>A new <see cref="StyleVerticalAlign" /> instance representing the parsed value.</returns> |
| | | 58 | | /// <exception cref="AryArgumentException"> |
| | | 59 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 60 | | /// </exception> |
| | | 61 | | public static StyleVerticalAlign Parse(string? value) |
| | 15 | 62 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 15 | 63 | | ? new StyleVerticalAlign(kind: kind) |
| | 15 | 64 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 65 | | |
| | | 66 | | /// <summary>Attempts to parse a string into a <see cref="StyleVerticalAlign" /> instance.</summary> |
| | | 67 | | /// <param name="value">The string representation of the vertical-align value to parse.</param> |
| | | 68 | | /// <param name="result"> |
| | | 69 | | /// When this method returns, contains the parsed <see cref="StyleVerticalAlign" /> instance or <see langword="null" |
| | | 70 | | /// parsing failed. |
| | | 71 | | /// </param> |
| | | 72 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 73 | | public static bool TryParse(string? value, out StyleVerticalAlign? result) |
| | | 74 | | { |
| | | 75 | | try |
| | | 76 | | { |
| | 3 | 77 | | result = Parse(value: value); |
| | | 78 | | |
| | 1 | 79 | | return true; |
| | | 80 | | } |
| | 2 | 81 | | catch |
| | | 82 | | { |
| | 2 | 83 | | result = null; |
| | | 84 | | |
| | 2 | 85 | | return false; |
| | | 86 | | } |
| | 3 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary>Implicitly converts a string into a <see cref="StyleVerticalAlign" /> instance.</summary> |
| | | 90 | | /// <param name="value">The string representation of the vertical-align value.</param> |
| | | 91 | | /// <returns>A <see cref="StyleVerticalAlign" /> instance representing the provided value.</returns> |
| | | 92 | | /// <exception cref="AryArgumentException"> |
| | | 93 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 94 | | /// <see cref="Kind" />. |
| | | 95 | | /// </exception> |
| | 2 | 96 | | public static implicit operator StyleVerticalAlign(string? value) => Parse(value: value); |
| | | 97 | | |
| | | 98 | | /// <summary>Implicitly converts a <see cref="StyleVerticalAlign" /> instance to its string representation.</summary |
| | | 99 | | /// <param name="value">The <see cref="StyleVerticalAlign" /> instance to convert.</param> |
| | | 100 | | /// <returns> |
| | | 101 | | /// The underlying CSS <c>vertical-align</c> string value, or an empty string if <paramref name="value" /> is |
| | | 102 | | /// <see langword="null" />. |
| | | 103 | | /// </returns> |
| | 2 | 104 | | public static implicit operator string(StyleVerticalAlign? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 105 | | } |