| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a CSS <c>justify-content</c>, <c>justify-items</c>, or <c>justify-self</c> value within the Allyaria them |
| | | 5 | | /// system. Provides a strongly typed wrapper for defining how items are distributed or aligned along the main axis of a |
| | | 6 | | /// flex or grid container. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed record StyleJustify : StyleValueBase |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="StyleJustify" /> record using the specified <see cref="Kind" /> val |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="kind">The justification kind that determines how elements are distributed along the main axis.</par |
| | | 14 | | public StyleJustify(Kind kind) |
| | 78 | 15 | | : base(value: kind.GetDescription()) { } |
| | | 16 | | |
| | | 17 | | /// <summary>Defines the supported CSS justification values for flexbox and grid layouts.</summary> |
| | | 18 | | public enum Kind |
| | | 19 | | { |
| | | 20 | | /// <summary>Centers items along the main axis.</summary> |
| | | 21 | | [Description(description: "center")] |
| | | 22 | | Center, |
| | | 23 | | |
| | | 24 | | /// <summary>Aligns items toward the end of the main axis.</summary> |
| | | 25 | | [Description(description: "end")] |
| | | 26 | | End, |
| | | 27 | | |
| | | 28 | | /// <summary>Aligns flex items to the end of the container’s main axis.</summary> |
| | | 29 | | [Description(description: "flex-end")] |
| | | 30 | | FlexEnd, |
| | | 31 | | |
| | | 32 | | /// <summary>Aligns flex items to the start of the container’s main axis.</summary> |
| | | 33 | | [Description(description: "flex-start")] |
| | | 34 | | FlexStart, |
| | | 35 | | |
| | | 36 | | /// <summary>Uses the default alignment behavior of the container.</summary> |
| | | 37 | | [Description(description: "normal")] |
| | | 38 | | Normal, |
| | | 39 | | |
| | | 40 | | /// <summary>Centers items without causing overflow; items are aligned safely within the container.</summary> |
| | | 41 | | [Description(description: "safe center")] |
| | | 42 | | SafeCenter, |
| | | 43 | | |
| | | 44 | | /// <summary>Distributes items evenly with space around each item.</summary> |
| | | 45 | | [Description(description: "space-around")] |
| | | 46 | | SpaceAround, |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Distributes items evenly with the first item at the start and last item at the end of the container. |
| | | 50 | | /// </summary> |
| | | 51 | | [Description(description: "space-between")] |
| | | 52 | | SpaceBetween, |
| | | 53 | | |
| | | 54 | | /// <summary>Distributes items evenly with equal spacing between and around them.</summary> |
| | | 55 | | [Description(description: "space-evenly")] |
| | | 56 | | SpaceEvenly, |
| | | 57 | | |
| | | 58 | | /// <summary>Aligns items toward the start of the main axis.</summary> |
| | | 59 | | [Description(description: "start")] |
| | | 60 | | Start, |
| | | 61 | | |
| | | 62 | | /// <summary>Stretches items to fill the available space along the main axis.</summary> |
| | | 63 | | [Description(description: "stretch")] |
| | | 64 | | Stretch, |
| | | 65 | | |
| | | 66 | | /// <summary>Centers items even if doing so may cause overflow (unsafe centering).</summary> |
| | | 67 | | [Description(description: "unsafe center")] |
| | | 68 | | UnsafeCenter |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Parses a string representation of a CSS justification value into a <see cref="StyleJustify" /> instance. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="value">The string representation of the justify value.</param> |
| | | 75 | | /// <returns>A new <see cref="StyleJustify" /> instance representing the parsed value.</returns> |
| | | 76 | | /// <exception cref="AryArgumentException"> |
| | | 77 | | /// Thrown when the provided <paramref name="value" /> does not correspond to a valid <see cref="Kind" />. |
| | | 78 | | /// </exception> |
| | | 79 | | public static StyleJustify Parse(string? value) |
| | 19 | 80 | | => value.TryParseEnum<Kind>(result: out var kind) |
| | 19 | 81 | | ? new StyleJustify(kind: kind) |
| | 19 | 82 | | : throw new AryArgumentException(message: $"Invalid style: {value}", argName: nameof(value)); |
| | | 83 | | |
| | | 84 | | /// <summary>Attempts to parse a string into a <see cref="StyleJustify" /> instance.</summary> |
| | | 85 | | /// <param name="value">The string representation of the justification value to parse.</param> |
| | | 86 | | /// <param name="result"> |
| | | 87 | | /// When this method returns, contains the parsed <see cref="StyleJustify" /> instance or <see langword="null" /> if |
| | | 88 | | /// parsing failed. |
| | | 89 | | /// </param> |
| | | 90 | | /// <returns><see langword="true" /> if parsing succeeded; otherwise, <see langword="false" />.</returns> |
| | | 91 | | public static bool TryParse(string? value, out StyleJustify? result) |
| | | 92 | | { |
| | | 93 | | try |
| | | 94 | | { |
| | 3 | 95 | | result = Parse(value: value); |
| | | 96 | | |
| | 1 | 97 | | return true; |
| | | 98 | | } |
| | 2 | 99 | | catch |
| | | 100 | | { |
| | 2 | 101 | | result = null; |
| | | 102 | | |
| | 2 | 103 | | return false; |
| | | 104 | | } |
| | 3 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary>Implicitly converts a string into a <see cref="StyleJustify" /> instance.</summary> |
| | | 108 | | /// <param name="value">The string representation of the justify value.</param> |
| | | 109 | | /// <returns>A <see cref="StyleJustify" /> instance representing the provided value.</returns> |
| | | 110 | | /// <exception cref="AryArgumentException"> |
| | | 111 | | /// Thrown when the provided string cannot be parsed into a valid |
| | | 112 | | /// <see cref="Kind" />. |
| | | 113 | | /// </exception> |
| | 2 | 114 | | public static implicit operator StyleJustify(string? value) => Parse(value: value); |
| | | 115 | | |
| | | 116 | | /// <summary>Implicitly converts a <see cref="StyleJustify" /> instance to its string representation.</summary> |
| | | 117 | | /// <param name="value">The <see cref="StyleJustify" /> instance to convert.</param> |
| | | 118 | | /// <returns> |
| | | 119 | | /// The underlying CSS justification string value, or an empty string if <paramref name="value" /> is |
| | | 120 | | /// <see langword="null" />. |
| | | 121 | | /// </returns> |
| | 2 | 122 | | public static implicit operator string(StyleJustify? value) => value?.Value ?? string.Empty; |
| | | 123 | | } |