|
| 1 | +using SharpConsoleUI.Controls; |
| 2 | + |
| 3 | +namespace SharpConsoleUI.Core; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Registration for a single settings page within a group. |
| 7 | +/// </summary> |
| 8 | +public record SettingsPageRegistration( |
| 9 | + string Name, |
| 10 | + string? Icon, |
| 11 | + string? Subtitle, |
| 12 | + Action<ScrollablePanelControl> ContentFactory); |
| 13 | + |
| 14 | +/// <summary> |
| 15 | +/// Registration for a settings group containing one or more pages. |
| 16 | +/// </summary> |
| 17 | +public record SettingsGroupRegistration( |
| 18 | + string Name, |
| 19 | + Color AccentColor, |
| 20 | + List<SettingsPageRegistration> Pages); |
| 21 | + |
| 22 | +/// <summary> |
| 23 | +/// Builder for configuring pages within a settings group during registration. |
| 24 | +/// </summary> |
| 25 | +public sealed class SettingsGroupBuilder |
| 26 | +{ |
| 27 | + internal readonly List<SettingsPageRegistration> Pages = new(); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Adds a page to this settings group. |
| 31 | + /// </summary> |
| 32 | + /// <param name="name">The display name of the settings page.</param> |
| 33 | + /// <param name="icon">Optional icon character or string shown beside the page name.</param> |
| 34 | + /// <param name="subtitle">Optional subtitle shown below the page name.</param> |
| 35 | + /// <param name="content">Factory that populates the page content panel.</param> |
| 36 | + /// <returns>This builder for method chaining.</returns> |
| 37 | + public SettingsGroupBuilder AddPage(string name, string? icon = null, |
| 38 | + string? subtitle = null, Action<ScrollablePanelControl>? content = null) |
| 39 | + { |
| 40 | + Pages.Add(new SettingsPageRegistration(name, icon, subtitle, content ?? (_ => { }))); |
| 41 | + return this; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// <summary> |
| 46 | +/// Stores custom settings group and page registrations for the Settings dialog. |
| 47 | +/// </summary> |
| 48 | +public sealed class SettingsRegistrationService |
| 49 | +{ |
| 50 | + private readonly List<SettingsGroupRegistration> _groups = new(); |
| 51 | + private static readonly Color DefaultExtensionsColor = new Color(100, 180, 100); |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets all registered settings groups. |
| 55 | + /// </summary> |
| 56 | + public IReadOnlyList<SettingsGroupRegistration> Groups => _groups.AsReadOnly(); |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Registers a settings group with multiple pages. |
| 60 | + /// </summary> |
| 61 | + /// <param name="name">The display name of the settings group.</param> |
| 62 | + /// <param name="accentColor">The accent color used for this group in the navigation sidebar.</param> |
| 63 | + /// <param name="configure">Action that configures the group's pages via a builder.</param> |
| 64 | + public void RegisterGroup(string name, Color accentColor, Action<SettingsGroupBuilder> configure) |
| 65 | + { |
| 66 | + var builder = new SettingsGroupBuilder(); |
| 67 | + configure(builder); |
| 68 | + _groups.Add(new SettingsGroupRegistration(name, accentColor, builder.Pages)); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Registers a single settings page under the "Extensions" group. |
| 73 | + /// Creates the Extensions group if it does not already exist. |
| 74 | + /// </summary> |
| 75 | + /// <param name="name">The display name of the settings page.</param> |
| 76 | + /// <param name="icon">Optional icon character or string shown beside the page name.</param> |
| 77 | + /// <param name="subtitle">Optional subtitle shown below the page name.</param> |
| 78 | + /// <param name="content">Factory that populates the page content panel.</param> |
| 79 | + public void RegisterPage(string name, string? icon = null, |
| 80 | + string? subtitle = null, Action<ScrollablePanelControl>? content = null) |
| 81 | + { |
| 82 | + var extGroup = _groups.Find(g => g.Name == "Extensions"); |
| 83 | + if (extGroup == null) |
| 84 | + { |
| 85 | + extGroup = new SettingsGroupRegistration("Extensions", DefaultExtensionsColor, new List<SettingsPageRegistration>()); |
| 86 | + _groups.Add(extGroup); |
| 87 | + } |
| 88 | + extGroup.Pages.Add(new SettingsPageRegistration(name, icon, subtitle, content ?? (_ => { }))); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Removes a registered settings group by name. |
| 93 | + /// </summary> |
| 94 | + /// <param name="name">The name of the group to remove.</param> |
| 95 | + public void UnregisterGroup(string name) |
| 96 | + { |
| 97 | + _groups.RemoveAll(g => g.Name == name); |
| 98 | + } |
| 99 | +} |
0 commit comments