| | | 1 | | namespace Allyaria.Theming.Helpers; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides a configurable implementation of <see cref="IThemeConfigurator" /> that allows consumers to override specif |
| | | 5 | | /// theming values through a collection of <see cref="ThemeUpdater" /> entries. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <remarks> |
| | | 8 | | /// <para> |
| | | 9 | | /// The <see cref="ThemeConfigurator" /> is used during application startup to register or customize the Allyaria th |
| | | 10 | | /// by chaining calls to <see cref="Override(ThemeUpdater)" />. |
| | | 11 | | /// </para> |
| | | 12 | | /// <para> |
| | | 13 | | /// It enforces immutability rules by preventing modification of system or high-contrast themes, ensuring consistent |
| | | 14 | | /// accessibility and visual integrity. |
| | | 15 | | /// </para> |
| | | 16 | | /// </remarks> |
| | | 17 | | public sealed class ThemeConfigurator : IThemeConfigurator |
| | | 18 | | { |
| | | 19 | | /// <summary>Internal list of <see cref="ThemeUpdater" /> instances representing theme overrides.</summary> |
| | 15 | 20 | | private readonly List<ThemeUpdater> _internalList = new(); |
| | | 21 | | |
| | | 22 | | /// <summary>Gets the total number of configured <see cref="ThemeUpdater" /> entries.</summary> |
| | 3 | 23 | | public int Count => _internalList.Count; |
| | | 24 | | |
| | | 25 | | /// <summary>Gets the <see cref="ThemeUpdater" /> at the specified index.</summary> |
| | | 26 | | /// <param name="index">The zero-based index of the updater to retrieve.</param> |
| | | 27 | | /// <returns>The <see cref="ThemeUpdater" /> at the specified index.</returns> |
| | 6 | 28 | | public ThemeUpdater this[int index] => _internalList[index: index]; |
| | | 29 | | |
| | | 30 | | /// <summary>Returns an enumerator that iterates through the collection of theme updaters.</summary> |
| | | 31 | | /// <returns>An enumerator for the <see cref="ThemeUpdater" /> collection.</returns> |
| | 2 | 32 | | public IEnumerator<ThemeUpdater> GetEnumerator() => _internalList.GetEnumerator(); |
| | | 33 | | |
| | | 34 | | /// <summary>Returns a non-generic enumerator that iterates through the collection of theme updaters.</summary> |
| | | 35 | | /// <returns>An enumerator for the <see cref="ThemeUpdater" /> collection.</returns> |
| | 1 | 36 | | IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_internalList).GetEnumerator(); |
| | | 37 | | |
| | | 38 | | /// <summary>Adds or replaces a <see cref="ThemeUpdater" /> in the configuration sequence.</summary> |
| | | 39 | | /// <param name="updater">The <see cref="ThemeUpdater" /> that defines a theming update or override to apply.</param |
| | | 40 | | /// <returns>The current <see cref="IThemeConfigurator" /> instance, allowing for fluent chaining.</returns> |
| | | 41 | | /// <exception cref="AryArgumentException"> |
| | | 42 | | /// Thrown when an invalid override is attempted, such as modifying system or high-contrast themes, or restricted co |
| | | 43 | | /// states. |
| | | 44 | | /// </exception> |
| | | 45 | | public IThemeConfigurator Override(ThemeUpdater updater) |
| | | 46 | | { |
| | 19 | 47 | | if (updater.Navigator.ThemeTypes.Contains(value: ThemeType.System)) |
| | | 48 | | { |
| | 1 | 49 | | throw new AryArgumentException( |
| | 1 | 50 | | message: "System theme cannot be set directly.", argName: nameof(updater.Value) |
| | 1 | 51 | | ); |
| | | 52 | | } |
| | | 53 | | |
| | 18 | 54 | | if (updater.Navigator.ComponentStates.Contains(value: ComponentState.Hidden) || |
| | 18 | 55 | | updater.Navigator.ComponentStates.Contains(value: ComponentState.ReadOnly)) |
| | | 56 | | { |
| | 2 | 57 | | throw new AryArgumentException( |
| | 2 | 58 | | message: "Hidden and read-only states cannot be set directly.", argName: nameof(updater.Value) |
| | 2 | 59 | | ); |
| | | 60 | | } |
| | | 61 | | |
| | 16 | 62 | | if (updater.Navigator.ThemeTypes.Contains(value: ThemeType.HighContrastDark) || |
| | 16 | 63 | | updater.Navigator.ThemeTypes.Contains(value: ThemeType.HighContrastLight)) |
| | | 64 | | { |
| | 2 | 65 | | throw new AryArgumentException( |
| | 2 | 66 | | message: "Cannot alter High Contrast themes.", argName: nameof(updater.Value) |
| | 2 | 67 | | ); |
| | | 68 | | } |
| | | 69 | | |
| | 14 | 70 | | if (updater.Navigator.ComponentStates.Contains(value: ComponentState.Focused) && |
| | 14 | 71 | | (updater.Navigator.StyleTypes.Contains(value: StyleType.OutlineOffset) || |
| | 14 | 72 | | updater.Navigator.StyleTypes.Contains(value: StyleType.OutlineStyle) || |
| | 14 | 73 | | updater.Navigator.StyleTypes.Contains(value: StyleType.OutlineWidth))) |
| | | 74 | | { |
| | 3 | 75 | | throw new AryArgumentException( |
| | 3 | 76 | | message: "Cannot change focused outline offset, style or width.", argName: nameof(updater.Value) |
| | 3 | 77 | | ); |
| | | 78 | | } |
| | | 79 | | |
| | 11 | 80 | | _internalList.Add(item: updater); |
| | | 81 | | |
| | 11 | 82 | | return this; |
| | | 83 | | } |
| | | 84 | | } |