| | | 1 | | namespace Allyaria.Theming.StyleTypes; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a grouped set of logical CSS values for block and inline directions, implementing <see cref="IStyleValue" |
| | | 5 | | /// so that it can participate in the Allyaria theming pipeline. A <see cref="StyleGroup" /> computes its final CSS usin |
| | | 6 | | /// the associated <see cref="StyleGroupType" />. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed record StyleGroup : IStyleValue |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Initializes a new instance of the <see cref="StyleGroup" /> record with a single <see cref="IStyleValue" /> appl |
| | | 12 | | /// all logical directions (block-start, block-end, inline-start, inline-end). |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="type"> |
| | | 15 | | /// The <see cref="StyleGroupType" /> that describes which CSS properties will be generated for this group. |
| | | 16 | | /// </param> |
| | | 17 | | /// <param name="value">The style value applied uniformly to all logical directions in the group.</param> |
| | 2 | 18 | | public StyleGroup(StyleGroupType type, IStyleValue value) |
| | | 19 | | { |
| | 2 | 20 | | BlockEnd = value; |
| | 2 | 21 | | BlockStart = value; |
| | 2 | 22 | | InlineEnd = value; |
| | 2 | 23 | | InlineStart = value; |
| | 2 | 24 | | Type = type; |
| | 2 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="StyleGroup" /> record with separate <see cref="IStyleValue" /> valu |
| | | 29 | | /// block and inline dimensions. The block value is used for both block-start and block-end, and the inline value is |
| | | 30 | | /// for both inline-start and inline-end. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="type"> |
| | | 33 | | /// The <see cref="StyleGroupType" /> that describes which CSS properties will be generated for this group. |
| | | 34 | | /// </param> |
| | | 35 | | /// <param name="block">The style value applied to the block-start and block-end logical directions.</param> |
| | | 36 | | /// <param name="inline">The style value applied to the inline-start and inline-end logical directions.</param> |
| | 1 | 37 | | public StyleGroup(StyleGroupType type, IStyleValue block, IStyleValue inline) |
| | | 38 | | { |
| | 1 | 39 | | BlockEnd = block; |
| | 1 | 40 | | BlockStart = block; |
| | 1 | 41 | | InlineEnd = inline; |
| | 1 | 42 | | InlineStart = inline; |
| | 1 | 43 | | Type = type; |
| | 1 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Initializes a new instance of the <see cref="StyleGroup" /> record with explicit values for each logical directi |
| | | 48 | | /// (block-start, block-end, inline-start, inline-end). |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="type"> |
| | | 51 | | /// The <see cref="StyleGroupType" /> that describes which CSS properties will be generated for this group. |
| | | 52 | | /// </param> |
| | | 53 | | /// <param name="blockStart">The style value applied to the block-start logical direction.</param> |
| | | 54 | | /// <param name="blockEnd">The style value applied to the block-end logical direction.</param> |
| | | 55 | | /// <param name="inlineStart">The style value applied to the inline-start logical direction.</param> |
| | | 56 | | /// <param name="inlineEnd">The style value applied to the inline-end logical direction.</param> |
| | 116 | 57 | | public StyleGroup(StyleGroupType type, |
| | 116 | 58 | | IStyleValue blockStart, |
| | 116 | 59 | | IStyleValue blockEnd, |
| | 116 | 60 | | IStyleValue inlineStart, |
| | 116 | 61 | | IStyleValue inlineEnd) |
| | | 62 | | { |
| | 116 | 63 | | BlockEnd = blockEnd; |
| | 116 | 64 | | BlockStart = blockStart; |
| | 116 | 65 | | InlineEnd = inlineEnd; |
| | 116 | 66 | | InlineStart = inlineStart; |
| | 116 | 67 | | Type = type; |
| | 116 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary>Gets the style value associated with the block-end logical direction.</summary> |
| | 1696 | 71 | | public IStyleValue BlockEnd { get; init; } |
| | | 72 | | |
| | | 73 | | /// <summary>Gets the style value associated with the block-start logical direction.</summary> |
| | 1696 | 74 | | public IStyleValue BlockStart { get; init; } |
| | | 75 | | |
| | | 76 | | /// <summary>Gets the style value associated with the inline-end logical direction.</summary> |
| | 1696 | 77 | | public IStyleValue InlineEnd { get; init; } |
| | | 78 | | |
| | | 79 | | /// <summary>Gets the style value associated with the inline-start logical direction.</summary> |
| | 1696 | 80 | | public IStyleValue InlineStart { get; init; } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the <see cref="StyleGroupType" /> that determines which underlying CSS properties are produced when this gr |
| | | 84 | | /// rendered. |
| | | 85 | | /// </summary> |
| | 125 | 86 | | public StyleGroupType Type { get; init; } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets a composite string value representing the grouped styles in the order: block-end, block-start, inline-end, |
| | | 90 | | /// inline-start. This is the logical value exposed via <see cref="IStyleValue.Value" />. |
| | | 91 | | /// </summary> |
| | 1571 | 92 | | public string Value => $"{BlockEnd.Value} {BlockStart.Value} {InlineEnd.Value} {InlineStart.Value}"; |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Adds the grouped style values to the provided <see cref="CssBuilder" /> instance using the CSS property names re |
| | | 96 | | /// from <see cref="Type" />. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="builder">The <see cref="CssBuilder" /> to which the CSS variables/properties are added.</param> |
| | | 99 | | /// <param name="varPrefix"> |
| | | 100 | | /// An optional prefix used when emitting CSS custom properties or variables. If <see langword="null" /> or empty, n |
| | | 101 | | /// prefix is applied. |
| | | 102 | | /// </param> |
| | | 103 | | /// <returns>The same <see cref="CssBuilder" /> instance passed in, allowing fluent chaining of method calls.</retur |
| | | 104 | | internal CssBuilder BuildCss(CssBuilder builder, string? varPrefix = "") |
| | | 105 | | { |
| | 3 | 106 | | var typeDescription = Type.GetDescription(); |
| | 3 | 107 | | var typeList = typeDescription.Split(separator: ','); |
| | | 108 | | |
| | 3 | 109 | | builder |
| | 3 | 110 | | .Add(name: typeList[0], value: BlockEnd.Value, varPrefix: varPrefix) |
| | 3 | 111 | | .Add(name: typeList[1], value: BlockStart.Value, varPrefix: varPrefix) |
| | 3 | 112 | | .Add(name: typeList[2], value: InlineEnd.Value, varPrefix: varPrefix) |
| | 3 | 113 | | .Add(name: typeList[3], value: InlineStart.Value, varPrefix: varPrefix); |
| | | 114 | | |
| | 3 | 115 | | return builder; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary>Builds the CSS representation for this <see cref="StyleGroup" /> and returns it as a string.</summary> |
| | | 119 | | /// <param name="varPrefix"> |
| | | 120 | | /// An optional prefix used when emitting CSS custom properties or variables. If <see langword="null" />, the defaul |
| | | 121 | | /// prefixing behavior for <see cref="CssBuilder" /> is used. |
| | | 122 | | /// </param> |
| | | 123 | | /// <returns>A string containing the serialized CSS generated from this <see cref="StyleGroup" />.</returns> |
| | | 124 | | public string ToCss(string? varPrefix = null) |
| | 2 | 125 | | => BuildCss(builder: new CssBuilder(), varPrefix: varPrefix).ToString(); |
| | | 126 | | } |