Files
LanMountainDesktop/airapp/LanMountainDesktop.AirAppSdk/AirAppSettingsSectionBuilder.cs

190 lines
6.0 KiB
C#
Raw Normal View History

2026-04-13 01:23:11 +08:00
using System;
2026-03-13 00:33:00 +08:00
using System.Collections.Generic;
2026-04-13 01:23:11 +08:00
using Avalonia.Controls;
2026-03-13 00:33:00 +08:00
2026-08-11 23:03:55 +08:00
namespace LanMountainDesktop.AirAppSdk;
2026-03-13 00:33:00 +08:00
2026-08-11 23:03:55 +08:00
public sealed class AirAppSettingsSectionBuilder
2026-03-13 00:33:00 +08:00
{
2026-08-11 23:03:55 +08:00
private readonly List<AirAppSettingOptionDefinition> _options = [];
2026-04-13 01:23:11 +08:00
private Type? _customViewType;
2026-03-13 00:33:00 +08:00
2026-08-11 23:03:55 +08:00
internal AirAppSettingsSectionBuilder(
2026-03-13 00:33:00 +08:00
string id,
string titleLocalizationKey,
string? descriptionLocalizationKey,
string iconKey,
int sortOrder)
{
Id = id;
TitleLocalizationKey = titleLocalizationKey;
DescriptionLocalizationKey = descriptionLocalizationKey;
IconKey = iconKey;
SortOrder = sortOrder;
}
public string Id { get; }
public string TitleLocalizationKey { get; }
public string? DescriptionLocalizationKey { get; }
public string IconKey { get; }
public int SortOrder { get; }
2026-04-13 01:23:11 +08:00
public Type? CustomViewType => _customViewType;
2026-08-11 23:03:55 +08:00
public IReadOnlyList<AirAppSettingOptionDefinition> Options => _options;
2026-03-13 00:33:00 +08:00
2026-04-13 01:23:11 +08:00
/// <summary>
/// Sets a custom AXAML view for this settings section.
2026-08-11 23:03:55 +08:00
/// The view type must be a subclass of <see cref="AirAppSettingsPageBase"/>.
2026-04-13 01:23:11 +08:00
/// When a custom view is provided, the host application will use it directly
/// instead of generating a page from the declared options, allowing the plugin
/// to use any Fluent Avalonia controls and custom layouts.
/// </summary>
2026-08-11 23:03:55 +08:00
/// <typeparam name="TView">A <see cref="AirAppSettingsPageBase"/> subclass that defines the settings UI.</typeparam>
public AirAppSettingsSectionBuilder SetCustomView<TView>() where TView : AirAppSettingsPageBase
2026-04-13 01:23:11 +08:00
{
_customViewType = typeof(TView);
return this;
}
/// <summary>
/// Sets a custom AXAML view for this settings section.
2026-08-11 23:03:55 +08:00
/// The view type must be a subclass of <see cref="AirAppSettingsPageBase"/>.
2026-04-13 01:23:11 +08:00
/// When a custom view is provided, the host application will use it directly
/// instead of generating a page from the declared options.
/// </summary>
2026-08-11 23:03:55 +08:00
/// <param name="viewType">A <see cref="AirAppSettingsPageBase"/> subclass type that defines the settings UI.</param>
public AirAppSettingsSectionBuilder SetCustomView(Type viewType)
2026-04-13 01:23:11 +08:00
{
ArgumentNullException.ThrowIfNull(viewType);
2026-08-11 23:03:55 +08:00
if (!typeof(AirAppSettingsPageBase).IsAssignableFrom(viewType))
2026-04-13 01:23:11 +08:00
{
throw new ArgumentException(
2026-08-11 23:03:55 +08:00
$"Custom view type must be a subclass of {nameof(AirAppSettingsPageBase)}.",
2026-04-13 01:23:11 +08:00
nameof(viewType));
}
_customViewType = viewType;
return this;
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddOption(AirAppSettingOptionDefinition option)
2026-03-13 00:33:00 +08:00
{
ArgumentNullException.ThrowIfNull(option);
_options.Add(option);
return this;
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddToggle(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
bool defaultValue = false)
{
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.Toggle,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue));
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddText(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
string defaultValue = "",
string? validationPattern = null)
{
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.Text,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue,
validationPattern: validationPattern));
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddNumber(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
double defaultValue = 0,
double? minimum = null,
double? maximum = null)
{
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.Number,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue,
minimum: minimum,
maximum: maximum));
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddSelect(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
2026-08-11 23:03:55 +08:00
IEnumerable<AirAppSettingOptionChoice> choices,
2026-03-13 00:33:00 +08:00
string? descriptionLocalizationKey = null,
string? defaultValue = null)
{
ArgumentNullException.ThrowIfNull(choices);
var normalizedChoices = choices.ToArray();
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.Select,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue,
normalizedChoices));
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddPath(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
string defaultValue = "")
{
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.Path,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue));
}
2026-08-11 23:03:55 +08:00
public AirAppSettingsSectionBuilder AddList(
2026-03-13 00:33:00 +08:00
string key,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
IReadOnlyList<string>? defaultValue = null)
{
2026-08-11 23:03:55 +08:00
return AddOption(new AirAppSettingOptionDefinition(
2026-03-13 00:33:00 +08:00
key,
2026-08-11 23:03:55 +08:00
AirAppSettingOptionType.List,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
defaultValue ?? Array.Empty<string>()));
}
2026-08-11 23:03:55 +08:00
internal AirAppSettingsSectionRegistration Build()
2026-03-13 00:33:00 +08:00
{
2026-08-11 23:03:55 +08:00
return new AirAppSettingsSectionRegistration(
2026-03-13 00:33:00 +08:00
Id,
TitleLocalizationKey,
_options.ToArray(),
DescriptionLocalizationKey,
IconKey,
2026-04-13 01:23:11 +08:00
SortOrder,
_customViewType);
2026-03-13 00:33:00 +08:00
}
}