| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>box-sizing</c> value used within the Allyaria theming system. Provides a strongly typed wrapper |
| | | 5 | | /// defining how element dimensions are calculated (content-box or border-box). |
| | | 6 | | /// </summary> |
| | | 7 | | public sealed record StyleBoxSizing : StyleValueBase |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Initializes a new instance of the <see cref="StyleBoxSizing" /> record using the specified <see cref="Kind" /> v |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="kind">The box-sizing kind that determines how width and height are calculated.</param> |
| | | 13 | | public StyleBoxSizing(Kind kind) |
| | 56 | 14 | | : base(value: kind.GetDescription()) { } |
| | | 15 | | |
| | | 16 | | /// <summary>Defines the supported CSS <c>box-sizing</c> property values.</summary> |
| | | 17 | | public enum Kind |
| | | 18 | | { |
| | | 19 | | /// <summary>Includes padding and border within the total width and height of the element.</summary> |
| | | 20 | | [Description(description: "border-box")] |
| | | 21 | | BorderBox, |
| | | 22 | | |
| | | 23 | | /// <summary>Calculates width and height based only on the content box, excluding padding and border.</summary> |
| | | 24 | | [Description(description: "content-box")] |
| | | 25 | | ContentBox, |
| | | 26 | | |
| | | 27 | | /// <summary>Inherits the box-sizing value from the parent element.</summary> |
| | | 28 | | [Description(description: "inherit")] |
| | | 29 | | Inherit |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Parses a string representation of a CSS <c>box-sizing</c> value into a <see cref="StyleBoxSizing" /> instance. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="value">The string representation of the box-sizing value to parse.</param> |
| | | 36 | | /// <returns>A new <see cref="StyleBoxSizing" /> instance representing the parsed value.</returns> |
| | | 37 | | /// <exception cref="AryArgumentException"> |
| | | 38 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 39 | | /// </exception> |
| | | 40 | | public static StyleBoxSizing Parse(string? value) |
| | 10 | 41 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 10 | 42 | | ? new StyleBoxSizing(kind: kind) |
| | 10 | 43 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 44 | | |
| | | 45 | | /// <summary>Attempts to parse a string into a <see cref="StyleBoxSizing" /> instance.</summary> |
| | | 46 | | /// <param name="value">The string value to parse.</param> |
| | | 47 | | /// <param name="result"> |
| | | 48 | | /// When this method returns, contains the parsed <see cref="StyleBoxSizing" /> instance or <see langword="null" /> |
| | | 49 | | /// parsing failed. |
| | | 50 | | /// </param> |
| | | 51 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 52 | | public static bool TryParse(string? value, out StyleBoxSizing? result) |
| | | 53 | | { |
| | | 54 | | try |
| | | 55 | | { |
| | 3 | 56 | | result = Parse(value: value); |
| | | 57 | | |
| | 1 | 58 | | return true; |
| | | 59 | | } |
| | 2 | 60 | | catch |
| | | 61 | | { |
| | 2 | 62 | | result = null; |
| | | 63 | | |
| | 2 | 64 | | return false; |
| | | 65 | | } |
| | 3 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary>Implicitly converts a string into a <see cref="StyleBoxSizing" /> instance.</summary> |
| | | 69 | | /// <param name="value">The string representation of the box-sizing value.</param> |
| | | 70 | | /// <returns>A <see cref="StyleBoxSizing" /> instance representing the provided value.</returns> |
| | | 71 | | /// <exception cref="AryArgumentException"> |
| | | 72 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 73 | | /// <see cref="Kind" />. |
| | | 74 | | /// </exception> |
| | 2 | 75 | | public static implicit operator StyleBoxSizing(string? value) => Parse(value: value); |
| | | 76 | | |
| | | 77 | | /// <summary>Implicitly converts a <see cref="StyleBoxSizing" /> instance to its string representation.</summary> |
| | | 78 | | /// <param name="value">The <see cref="StyleBoxSizing" /> instance to convert.</param> |
| | | 79 | | /// <returns> |
| | | 80 | | /// The underlying CSS <c>box-sizing</c> string, or an empty string if <paramref name="value" /> is <see langword="n |
| | | 81 | | /// . |
| | | 82 | | /// </returns> |
| | 2 | 83 | | public static implicit operator string(StyleBoxSizing? value) => (value?.Value).OrDefaultIfEmpty(); |
| | | 84 | | } |