Files
LanMountainDesktop/LanMountainDesktop.PluginSdk/PluginDesktopComponentContext.cs

90 lines
2.8 KiB
C#
Raw Normal View History

2026-03-09 12:27:33 +08:00
namespace LanMountainDesktop.PluginSdk;
public sealed class PluginDesktopComponentContext
{
public PluginDesktopComponentContext(
PluginManifest manifest,
string pluginDirectory,
string dataDirectory,
IServiceProvider services,
IReadOnlyDictionary<string, object?> properties,
string componentId,
string? placementId,
2026-03-13 09:10:00 +08:00
double cellSize,
2026-03-20 22:37:37 +08:00
IPluginAppearanceContext appearance,
2026-03-13 09:10:00 +08:00
IPluginSettingsService? pluginSettings = null)
2026-03-09 12:27:33 +08:00
{
ArgumentNullException.ThrowIfNull(manifest);
ArgumentException.ThrowIfNullOrWhiteSpace(pluginDirectory);
ArgumentException.ThrowIfNullOrWhiteSpace(dataDirectory);
ArgumentException.ThrowIfNullOrWhiteSpace(componentId);
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(properties);
2026-03-20 22:37:37 +08:00
ArgumentNullException.ThrowIfNull(appearance);
2026-03-09 12:27:33 +08:00
Manifest = manifest;
PluginDirectory = pluginDirectory;
DataDirectory = dataDirectory;
Services = services;
Properties = properties;
ComponentId = componentId.Trim();
PlacementId = string.IsNullOrWhiteSpace(placementId) ? null : placementId.Trim();
CellSize = Math.Max(1, cellSize);
2026-03-20 22:37:37 +08:00
Appearance = appearance;
2026-03-13 09:10:00 +08:00
PluginSettings = pluginSettings;
2026-03-09 12:27:33 +08:00
}
public PluginManifest Manifest { get; }
public string PluginDirectory { get; }
public string DataDirectory { get; }
public IServiceProvider Services { get; }
public IReadOnlyDictionary<string, object?> Properties { get; }
public string ComponentId { get; }
public string? PlacementId { get; }
public double CellSize { get; }
2026-03-20 22:37:37 +08:00
public IPluginAppearanceContext Appearance { get; }
public double GlobalCornerRadiusScale => Appearance.Snapshot.GlobalCornerRadiusScale;
2026-03-20 00:41:14 +08:00
2026-03-20 22:37:37 +08:00
public PluginCornerRadiusTokens CornerRadiusTokens => Appearance.Snapshot.CornerRadiusTokens;
2026-03-20 00:41:14 +08:00
2026-03-13 09:10:00 +08:00
public IPluginSettingsService? PluginSettings { get; }
2026-03-20 00:41:14 +08:00
public double ResolveScaledCornerRadius(double baseRadius, double? minimum = null, double? maximum = null)
{
2026-03-20 22:37:37 +08:00
return Appearance.ResolveScaledCornerRadius(baseRadius, minimum, maximum);
}
public double ResolveCornerRadius(PluginCornerRadiusPreset preset, double? minimum = null, double? maximum = null)
{
return Appearance.ResolveCornerRadius(preset, minimum, maximum);
2026-03-20 00:41:14 +08:00
}
2026-03-09 12:27:33 +08:00
public T? GetService<T>()
{
return (T?)Services.GetService(typeof(T));
}
public bool TryGetProperty<T>(string key, out T? value)
{
ArgumentException.ThrowIfNullOrWhiteSpace(key);
if (Properties.TryGetValue(key, out var rawValue) && rawValue is T typedValue)
{
value = typedValue;
return true;
}
value = default;
return false;
}
}