| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>hyphens</c> value within the Allyaria theming system. Provides a strongly typed wrapper for |
| | | 5 | | /// defining how words may be hyphenated when text wraps. |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleHyphens : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleHyphens" /> record using the specified <see cref="Kind" /> val |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="kind">The hyphenation behavior kind to represent.</param> |
| | | 13 | | public StyleHyphens(Kind kind) |
| | 24 | 14 | | : base(value: kind.GetDescription()) { } |
| | | 15 | | |
| | | 16 | | /// <summary>Defines the supported CSS <c>hyphens</c> property values.</summary> |
| | | 17 | | public enum Kind |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The browser automatically inserts hyphens where appropriate according to the language rules and available hy |
| | | 21 | | /// dictionary. |
| | | 22 | | /// </summary> |
| | | 23 | | [Description(description: "auto")] |
| | | 24 | | Auto, |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Hyphens are inserted only where the author has explicitly specified soft hyphens (<c>&shy;</c>) in the t |
| | | 28 | | /// </summary> |
| | | 29 | | [Description(description: "manual")] |
| | | 30 | | Manual, |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Hyphenation is disabled entirely; words will not be broken and will overflow or wrap at spaces only. |
| | | 34 | | /// </summary> |
| | | 35 | | [Description(description: "none")] |
| | | 36 | | None |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Parses a string representation of a CSS <c>hyphens</c> value into a <see cref="StyleHyphens" /> instance. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="value">The string representation of the hyphens value.</param> |
| | | 43 | | /// <returns>A new <see cref="StyleHyphens" /> instance representing the parsed value.</returns> |
| | | 44 | | /// <exception cref="AryArgumentException"> |
| | | 45 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 46 | | /// </exception> |
| | | 47 | | public static StyleHyphens Parse(string? value) |
| | 10 | 48 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 10 | 49 | | ? new StyleHyphens(kind: kind) |
| | 10 | 50 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 51 | | |
| | | 52 | | /// <summary>Attempts to parse a string into a <see cref="StyleHyphens" /> instance.</summary> |
| | | 53 | | /// <param name="value">The string representation of the hyphens value to parse.</param> |
| | | 54 | | /// <param name="result"> |
| | | 55 | | /// When this method returns, contains the parsed <see cref="StyleHyphens" /> instance or <see langword="null" /> if |
| | | 56 | | /// parsing failed. |
| | | 57 | | /// </param> |
| | | 58 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 59 | | public static bool TryParse(string? value, out StyleHyphens? result) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 3 | 63 | | result = Parse(value: value); |
| | | 64 | | |
| | 1 | 65 | | return true; |
| | | 66 | | } |
| | 2 | 67 | | catch |
| | | 68 | | { |
| | 2 | 69 | | result = null; |
| | | 70 | | |
| | 2 | 71 | | return false; |
| | | 72 | | } |
| | 3 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary>Implicitly converts a string into a <see cref="StyleHyphens" /> instance.</summary> |
| | | 76 | | /// <param name="value">The string representation of the hyphens value.</param> |
| | | 77 | | /// <returns>A <see cref="StyleHyphens" /> instance representing the provided value.</returns> |
| | | 78 | | /// <exception cref="AryArgumentException"> |
| | | 79 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 80 | | /// <see cref="Kind" />. |
| | | 81 | | /// </exception> |
| | 2 | 82 | | public static implicit operator StyleHyphens(string? value) => Parse(value: value); |
| | | 83 | | |
| | | 84 | | /// <summary>Implicitly converts a <see cref="StyleHyphens" /> instance to its string representation.</summary> |
| | | 85 | | /// <param name="value">The <see cref="StyleHyphens" /> instance to convert.</param> |
| | | 86 | | /// <returns> |
| | | 87 | | /// The underlying CSS <c>hyphens</c> string value, or an empty string if <paramref name="value" /> is |
| | | 88 | | /// <see langword="null" />. |
| | | 89 | | /// </returns> |
| | 2 | 90 | | public static implicit operator string(StyleHyphens? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 91 | | } |