Files
LanMountainDesktop/LanAirApp/samples/LanMountainDesktop.SamplePlugin/SamplePlugin.cs

93 lines
3.6 KiB
C#
Raw Normal View History

2026-03-09 12:27:33 +08:00
using LanMountainDesktop.PluginSdk;
namespace LanMountainDesktop.SamplePlugin;
[PluginEntrance]
public sealed class SamplePlugin : PluginBase, IDisposable
{
2026-03-09 14:14:50 +08:00
private SamplePluginRuntimeStateService? _stateService;
private SamplePluginClockService? _clockService;
2026-03-09 12:27:33 +08:00
public override void Initialize(IPluginContext context)
{
Directory.CreateDirectory(context.DataDirectory);
2026-03-10 00:40:26 +08:00
var localizer = PluginLocalizer.Create(context);
2026-03-09 12:27:33 +08:00
2026-03-10 00:40:26 +08:00
var hostName = GetHostProperty(context, PluginHostPropertyKeys.HostApplicationName, "UnknownHost");
var hostVersion = GetHostProperty(context, PluginHostPropertyKeys.HostVersion, "UnknownVersion");
var sdkApiVersion = GetHostProperty(context, PluginHostPropertyKeys.PluginSdkApiVersion, "UnknownApiVersion");
2026-03-09 14:14:50 +08:00
var messageBus = context.GetService<IPluginMessageBus>()
?? throw new InvalidOperationException("Plugin message bus is not available.");
2026-03-09 12:27:33 +08:00
2026-03-09 14:14:50 +08:00
_stateService = new SamplePluginRuntimeStateService(
context.Manifest,
context.PluginDirectory,
context.DataDirectory,
hostName,
hostVersion,
sdkApiVersion,
2026-03-10 00:40:26 +08:00
messageBus,
localizer);
2026-03-09 14:14:50 +08:00
context.RegisterService(_stateService);
2026-03-09 12:27:33 +08:00
2026-03-10 00:40:26 +08:00
_clockService = new SamplePluginClockService(context.DataDirectory, _stateService, messageBus, localizer);
2026-03-09 14:14:50 +08:00
context.RegisterService(_clockService);
_stateService.AttachClockService(_clockService);
var logPath = Path.Combine(context.DataDirectory, "sample-plugin.log");
var initMessage =
$"[{DateTimeOffset.UtcNow:O}] {context.Manifest.Name} initialized in {hostName} (plugin version {context.Manifest.Version ?? "dev"}).";
2026-03-09 12:27:33 +08:00
try
{
2026-03-09 14:14:50 +08:00
File.AppendAllText(logPath, initMessage + Environment.NewLine);
2026-03-10 00:40:26 +08:00
_stateService.MarkBackendReady(localizer.Format(
"status.backend.detail.log_written",
"初始化日志已写入:{0}",
logPath));
2026-03-09 12:27:33 +08:00
}
catch (Exception ex)
{
2026-03-10 00:40:26 +08:00
_stateService.MarkBackendFaulted(localizer.Format(
"status.backend.detail.log_write_failed",
"初始化日志写入失败:{0}",
ex.Message));
2026-03-09 12:27:33 +08:00
throw;
}
2026-03-09 14:14:50 +08:00
_clockService.Start();
2026-03-09 12:27:33 +08:00
context.RegisterSettingsPage(new PluginSettingsPageRegistration(
"status",
2026-03-10 00:40:26 +08:00
localizer.GetString("settings.page_title", "插件状态"),
2026-03-09 12:27:33 +08:00
() => new SamplePluginSettingsView(context)));
context.RegisterDesktopComponent(new PluginDesktopComponentRegistration(
"LanMountainDesktop.SamplePlugin.StatusClock",
2026-03-10 00:40:26 +08:00
localizer.GetString("widget.display_name", "示例插件状态时钟"),
2026-03-09 12:27:33 +08:00
widgetContext => new SamplePluginStatusClockWidget(widgetContext),
iconKey: "PuzzlePiece",
2026-03-10 00:40:26 +08:00
category: localizer.GetString("widget.category", "插件"),
2026-03-09 12:27:33 +08:00
minWidthCells: 4,
minHeightCells: 4,
allowDesktopPlacement: true,
allowStatusBarPlacement: false,
resizeMode: PluginDesktopComponentResizeMode.Proportional,
cornerRadiusResolver: cellSize => Math.Clamp(cellSize * 0.34, 18, 34)));
}
public void Dispose()
{
2026-03-09 14:14:50 +08:00
_clockService?.Dispose();
_clockService = null;
_stateService = null;
}
private static string GetHostProperty(IPluginContext context, string key, string fallback)
{
return context.TryGetProperty<string>(key, out var value) && !string.IsNullOrWhiteSpace(value)
? value
: fallback;
2026-03-09 12:27:33 +08:00
}
}