Files
LanMountainDesktop/CODE_WIKI.md

875 lines
33 KiB
Markdown
Raw Permalink Normal View History

合并对设置系统的更新 (#11) * Add Windows system chrome patchers (Harmony) Introduce support for toggling the system chrome on Windows using Harmony patchers. Adds Lib.Harmony.Thin to package props and project, new patcher infrastructure (ChromePatchState, PatcherEntrance) and two Harmony patches that disable FluentAvalonia's Windows chrome when configured. Program.cs now loads the chrome setting and installs patchers conditionally on Windows/x86-x64. Settings viewmodel and view updated: expose IsWindowsOs, require restart on appearance changes, migrate SettingsWindow to FAAppWindow and adapt titlebar/layout (include Windows caption placeholder and footer menu items). Also add a .gitkeep and a build log file. * Refactor settings window UI and theming Improve theming and layout for the Settings window and related services. - MaterialSurfaceService: add special material parameters for SettingsWindowBackground (lower alpha, no blur) and avoid hot-switching real backdrops for non-settings windows. - GlassEffectService: add AdaptiveSettingsWindowTintBrush + ResolveSettingsWindowTintAlpha to provide optional content tinting tied to system material mode. - SettingsWindowService: refactor theme application into ApplyThemeVariantAndResources, ensure settings window material is applied at show/activate times, and tidy theme/resource application flow. - SettingsWindow.axaml / .axaml.cs: restructure title bar (separate Grid.Row=0 border) and FANavigationView host, add pane-footer toggle button for :minimal layout, use dynamic corner radius resource, and update toggle/visibility/icon logic and responsive layout code. - SettingsPages: remove some IconText usages and adjust margins; use DesignCornerRadiusLg for update card corner radius. - Add NuGet.Config to set local globalPackagesFolder and ignore .nuget/packages in .gitignore. These changes aim to improve visuals, avoid backdrop overdraw, and make the settings window behavior consistent across themes and layouts. * Add localization and localize settings pages Add many new localization keys (en-US and zh-CN) for notifications, developer tools, about page, status bar, and video wallpaper. Update Notification, Dev, About and StatusBar view models to use LocalizationService, expose localized ObservableProperties, and refresh localized text at construction. Localize selection options and test notification texts, and fix notification severity handling. Wire up XAML to the new localized properties (About/Dev/StatusBar pages) and update the settings page title for notifications. Also adjust copyright line generation and replace hardcoded placeholders with bound Watermark properties. * Redesign settings window with fluent shell & search Rebuild the settings window as a Fluent shell: adds a custom 48-DIP titlebar with Back, pane toggle, icon/title, search box, restart/more menu, and caption-button spacer; moves compact pane toggle into the titlebar and preserves FANavigationView as the primary navigation surface. Introduces a SettingsSearchService (with UI AutoComplete integration, search indexing, navigation-by-result, and search result highlighting) plus focused tests for search filtering and theme material normalization. Adds navigation history/back stack, updates SettingsViewModels for new bindings and localization keys, and updates General/Apearance pages to expose new strings and options. Implements an "auto" system material mode: default in AppSettingsSnapshot, new MaterialAuto constants and normalization/resolution logic in ThemeAppearanceValues, WindowMaterialService and MaterialSurfaceService adjustments to prefer Mica on Win11 and Acrylic on Win10 using TransparencyLevelHint. GlassEffectService and AppearanceThemeService updated to use effective material mode and to track live theme state changes. Adds localization entries (en-US, zh-CN), spec/tasks docs, and other UI/style tweaks to support the redesign. * fix.修折叠与展开按钮 * Add OOBE startup presentation and settings merge Introduce a new OOBE step for "Startup & Presentation" that exposes startup and UI preferences in OobeWindow (toggles for taskbar, slide/fade transitions, fused popup, and autostart). Add HostAppSettingsOobeMerger to read/write Host settings.json (PascalCase fields) and MergeStartupPresentation behavior, plus LauncherWindowsStartupService to sync the current Launcher into the Windows Run key on Windows. Wire UI handlers, persist choices on Next, and load defaults when entering the step. Include unit tests for the merger, adjust SettingsWindow navigation pane/toggle handling, and update docs/LAUNCHER.md to describe the new OOBE step and implementation files. * Move whiteboard persistence to file storage Switch whiteboard note storage from legacy DB rows to per-note JSON files and add migration support. Update WhiteboardNoteSnapshot schema (version bump, viewport, canvas, expires, PathSvgData) and change IWhiteboardNotePersistenceService.SaveNote to return bool to surface write failures (e.g. read-only files). Implement file-based WhiteboardNotePersistenceService with legacy DB migration/cleanup, retention handling, and logging. Add comprehensive unit tests for persistence, stroke path builder, SVG import and viewport helper. Also add ThirdParty/DotNetCampus.InkCanvas project and reference it in the main csproj, and bump PostHog package to 2.6.0. * Introduce render gate and chart caching Replace UI DispatcherTimer polling with a StudySnapshotRenderGate across multiple widgets to queue and apply only the latest analytics snapshot; components updated include StudyDeductionReasonsWidget, StudyEnvironmentWidget, StudyInterruptDensityWidget, StudyNoiseCurveWidget. Add StudySnapshotRenderGate implementation to coordinate rendering and monitoring leases and update subscription/lease lifecycle handling (subscribe/unsubscribe, Acquire/Dispose leases, Clear/Dispose gate). Rewrite chart controls (StudyNoiseCurveChartControl and StudyNoiseDistributionScatterChartControl) to use stable logical-time origins, split series into static vs dynamic tails, add geometry/sample caching, stable jitter/coordinate mapping helpers, and expose internal helpers & counts for testing. Add unit tests (StudyComponentRenderingTests) covering the render gate and chart behaviors (layer counts, logical X mapping, stable jitter, cache rebuild). These changes improve rendering correctness and performance by avoiding redundant renders and enabling deterministic chart layout. * Use MaterialColorSnapshot in appearance flow Introduce unified material/color spec and tests, and refactor appearance plumbing to use MaterialColorSnapshot as the single source of truth. Add .trae material-color-service spec/checklist/tasks and integration/unit tests for plugin mapping and appearance VM behavior. AppearanceChangedEvent extended with new appearance change flags and HasChanged logic. ComponentEditorMaterialThemeAdapter rewritten to accept MaterialColorSnapshot and derive palette from snapshot data. Simplify AppearanceSettingsPageViewModel and related view code: remove legacy preview/custom-seed UI logic, preserve material/color fields when updating theme or corner radius, and update save calls to use with-expressions. Update ComponentEditorWindow to use adapter-provided OnPrimary brush and minor docs updates. * Add material color services, plugin DTOs, and tests Introduce IPC wire-format appearance DTOs (PluginIsolation.Contracts) and clarify they are distinct from the runtime PluginSdk snapshot. Update PluginSdk comments to document the runtime-facing snapshot shape. Change ComponentColorSchemeHelper to use the HostMaterialColorProvider and add an overload that accepts a MaterialColorSnapshot. Add new services and pipelines (MaterialColorService, MaterialSurfaceService, WindowMaterialService, WallpaperColorPipeline) and refactor AppearanceThemeService to depend on MaterialColorService while removing legacy internal implementations. Add multiple unit tests (ComponentColorSchemeHelper, PluginAppearanceBoundary, SettingsCatalogService, WallpaperSettingsPageViewModel) and update localization resources with new material_color and wallpaper keys. * Add CODE_WIKI and update localization Add a comprehensive CODE_WIKI.md documenting project architecture, modules, startup flow, plugin system, testing and developer workflows. Update localization resources (en-US.json, zh-CN.json) with new/translated keys for wallpaper controls (custom color UI), material & color settings (semantic roles, surfaces, refresh/polling state), appearance (corner radius), status bar font size options, privacy policy text, component library labels, clock settings, and new language entry (Korean). Also modify settings-related ViewModels and Settings page views to surface these new features and texts (MaterialColorSettingsPageViewModel.cs, SettingsViewModels.cs, WallpaperSettingsPageViewModel.cs, MainWindow.SettingsHardCut.Stubs.cs, ComponentsSettingsPage.axaml, WallpaperSettingsPage.axaml). * Add Data settings page and storage scanner Introduce a new "Data" settings page to visualize and manage local app storage. Adds DataStorageService (scanning, disk info, clean operations), DataSettingsPageViewModel, XAML view and code-behind, and HexToColor/HexToBrush converters; registers converters in App.axaml. Also update localization strings for the new page and add icon mapping so the settings entry uses the Database icon. Enables per-category and global cleaning workflows and formatted size display. * Add IPC backoff/retries and safer disposal Introduce exponential backoff, jitter and retry logic across IPC components to improve robustness and avoid tight retry loops; make disposal idempotent and add connection guards. Key changes: - LauncherCoordinatorIpcServer / LauncherIpcServer: add backoff constants, ComputeBackoff(), consecutive error tracking and delayed retries with jitter. - LanMountainDesktopIpcClient / LauncherIpcClient: add connect retry loops, timeouts, delayed retries, improved error logging, and use ArrayPool for buffered async writes; ensure proper cleanup on failures. - PublicIpcHostService: add disposed flag, guard OnPeerConnected and Dispose, and clear connected peers on dispose. - Add many auto-generated commit analysis docs under docs/auto_commit_md and new scripts for analyzing/generating commit docs. These changes aim to make IPC connection handling more resilient and resource-safe. * Add preview controls and settings UI tweaks Introduce GridPreviewControl and CornerRadiusPreviewControl for visual previews and wire them into the Components settings (add ScreenAspectRatio, CornerRadiusPreviewValue, and screen aspect init). Refactor ComponentsSettingsPage UI to show live previews. Improve DataSettingsPage layout and storage bar logic (use item percentages directly, include remaining segment, adjust visuals and visibility triggers). Simplify LauncherSettingsPage header/appearance layout. Add SECURITY_AUDIT_REPORT.md, analysis summary, mockup HTML, and a local .claude settings file. * Add install checkpoint/resume and DDSS workflows Introduce install checkpoint support and resume logic for updates, plus related locking and validation. Adds InstallCheckpoint model, AppJsonContext serialization, and UpdatePaths helpers for deployment lock, apply-in-progress lock and install-checkpoint path. UpdateEngineService gains checkpoint load/save/delete, incoming-state validation, resume logic for PLONDS and legacy updates, apply lock handling, and safer cleanup; ApplyPendingPlondsUpdateAsync and ApplyPendingUpdate flow updated accordingly. Add DeploymentLock contract and extend UpdateState with pause/resume/cancel helpers. Tests updated to cover stale/valid checkpoint resume and legacy/PLONDS flows. CI: enhance ddss-publish to detect release channel, validate S3 assets, prepare and atomically publish channel pointer; add ddss-rollback workflow to publish rollbacks; adjust plonds-build concurrency and release events. * changed.更了好多 * fix.消息盒子媒体播放器组件服务修复 * change.重做天气,为回到系统提供自定义功能。 * feat.airapp与融合桌面 * feat.动画优化与更新界面 * feat.数字时钟,白板功能修复 * feat.完善了时钟轻应用,为启动器提供了多语言支持 * feat.发布与打包优化 * changed.天气选项卡更新
2026-05-19 07:55:21 +08:00
# LanMountainDesktop Code Wiki
> 本文档是 LanMountainDesktop阑山桌面项目的结构化 Code Wiki涵盖项目整体架构、主要模块职责、关键类与函数说明、依赖关系以及项目运行方式等关键信息。
>
> 生成日期2026-05-07
> 产品版本1.0.0
> Plugin SDK API 基线5.0.0
---
## 目录
1. [项目概述](#1-项目概述)
2. [整体架构](#2-整体架构)
3. [项目结构与模块职责](#3-项目结构与模块职责)
4. [关键类与函数说明](#4-关键类与函数说明)
5. [依赖关系](#5-依赖关系)
6. [项目运行方式](#6-项目运行方式)
7. [启动流程详解](#7-启动流程详解)
8. [插件系统架构](#8-插件系统架构)
9. [数据流与交互模型](#9-数据流与交互模型)
10. [测试体系](#10-测试体系)
---
## 1. 项目概述
### 1.1 产品定位
**阑山桌面LanMountainDesktop** 是一款跨平台桌面环境增强工具,基于 Avalonia UI 和 .NET 10 构建。
- **产品口号**:你的桌面,不止一面
- **技术基线**Avalonia UI + .NET 10
- **支持平台**Windows、Linux、macOS
- **仓库角色**桌面宿主、插件运行时、Plugin SDK 与共享契约的权威来源
### 1.2 目标用户
- **学生用户**:课程表、自习监测、计时、天气和日常信息聚合
- **办公用户**:日历、资讯、最近文档、常用工具入口
- **效率和美化爱好者**:自由布局、主题切换、插件扩展
- **中文用户**:本地化界面、农历和节假日等本地语境支持
### 1.3 核心能力
- **桌面组件系统**:内置组件与扩展组件统一注册、统一放置约束
- **插件系统**:宿主加载插件、整合设置页、组件与市场安装流
- **外观系统**:主题、玻璃层级、圆角与颜色资源统一管理
- **设置系统**:独立设置窗口、设置页注册与分域持久化
- **跨平台运行**:基于 Avalonia 的桌面宿主运行在 Windows、Linux、macOS
### 1.4 生态边界
| 仓库 | 职责 |
|------|------|
| `LanMountainDesktop`(本仓库) | 宿主代码、插件运行时、SDK、共享契约、主题与设置基础设施 |
| `LanAirApp`(兄弟仓库) | 插件市场元数据、开发者生态材料 |
| `LanMountainDesktop.SamplePlugin` | 官方示例插件实现 |
---
## 2. 整体架构
### 2.1 架构分层
```
┌─────────────────────────────────────────────────────────────┐
│ 用户界面层 (UI Layer) │
│ Views/ │ ViewModels/ │ Theme/ │ Styles/ │ Localization/
├─────────────────────────────────────────────────────────────┤
│ 业务服务层 (Service Layer) │
│ Services/ │ ComponentSystem/ │ DesktopEditing/ │ plugins/
├─────────────────────────────────────────────────────────────┤
│ 基础设施层 (Infrastructure) │
│ DesktopHost/ │ Appearance/ │ Settings.Core/ │ Shared.IPC/
├─────────────────────────────────────────────────────────────┤
│ 抽象与契约层 (Abstractions) │
│ Host.Abstractions/ │ Shared.Contracts/ │ PluginSdk/
├─────────────────────────────────────────────────────────────┤
│ 启动与更新层 (Launcher) │
│ LanMountainDesktop.Launcher/ │
└─────────────────────────────────────────────────────────────┘
```
### 2.2 核心设计原则
1. **插件优先**:核心功能通过插件扩展,宿主提供运行时和基础设施
2. **组件化桌面**:所有桌面元素都是组件,统一注册、统一放置
3. **设置分域**App / Launcher / ComponentInstance / Plugin 四级设置作用域
4. **主题动态化**:支持 Material Design 3 动态配色、系统主题跟随
5. **进程隔离预留**:当前为进程内加载,预留了隔离进程架构
---
## 3. 项目结构与模块职责
### 3.1 解决方案项目列表
| 项目路径 | 输出类型 | 主要职责 |
|---------|---------|---------|
| `LanMountainDesktop/` | WinExe | 主桌面宿主应用,包含 UI、服务、组件系统、插件运行时接入 |
| `LanMountainDesktop.Launcher/` | WinExe | 启动器 - 负责 OOBE、Splash、版本管理、增量更新、插件安装 |
| `LanMountainDesktop.PluginSdk/` | Library | 官方插件 SDK定义插件可依赖的公开接口与打包行为 |
| `LanMountainDesktop.Shared.Contracts/` | Library | 宿主与插件共享的稳定契约类型 |
| `LanMountainDesktop.Shared.IPC/` | Library | 统一 IPC 基础,用于 Host 公共服务、Launcher/OOBE 启动通知、插件贡献的公共服务 |
| `LanMountainDesktop.Appearance/` | Library | 主题、圆角、外观资源相关基础设施 |
| `LanMountainDesktop.Settings.Core/` | Library | 设置域、持久化和设置基础抽象 |
| `LanMountainDesktop.DesktopHost/` | Library | 桌面宿主流程与生命周期相关逻辑 |
| `LanMountainDesktop.DesktopComponents.Runtime/` | Library | 组件运行时支撑能力 |
| `LanMountainDesktop.Host.Abstractions/` | Library | 宿主侧抽象接口 |
| `LanMountainDesktop.PluginIsolation.Contracts/` | Library | 插件隔离机制的传输无关 DTO、路由常量、错误码 |
| `LanMountainDesktop.PluginIsolation.Ipc/` | Library | 插件隔离 IPC 外观,基于 dotnetCampus.Ipc |
| `LanMountainDesktop.PluginTemplate/` | Library | `dotnet new lmd-plugin` 官方模板 |
| `LanMountainDesktop.PluginUpgradeHelper/` | Library | 插件升级帮助程序 |
| `LanMountainDesktop.Tests/` | Test | 宿主与 SDK 的测试项目 |
### 3.2 主宿主工程内部结构
```
LanMountainDesktop/
├── Program.cs # 进程启动主线
├── App.axaml.cs # 应用初始化、主题、语言、托盘、插件运行时
├── Views/ # 界面视图
│ ├── MainWindow.axaml # 主窗口
│ ├── SettingsWindow.axaml # 设置窗口
│ ├── ComponentLibraryWindow.axaml # 组件库窗口
│ ├── FusedDesktopComponentLibraryWindow.axaml # 融合桌面组件库
│ ├── NotificationWindow.axaml # 通知窗口
│ ├── TransparentOverlayWindow.axaml # 透明覆盖层窗口
│ ├── SettingsPages/ # 设置页面
│ ├── Components/ # 桌面组件视图
│ └── ComponentEditors/ # 组件编辑器视图
├── ViewModels/ # 视图模型
│ ├── MainWindowViewModel.cs
│ ├── ViewModelBase.cs
│ └── ...
├── Services/ # 业务服务层
│ ├── AppearanceThemeService.cs # 外观主题服务
│ ├── Settings/ # 设置相关服务
│ ├── MaterialColorService.cs # Material 颜色服务
│ ├── DesktopTrayService.cs # 桌面托盘服务
│ ├── FusedDesktopManagerService.cs # 融合桌面管理
│ └── ...
├── ComponentSystem/ # 组件系统
│ ├── ComponentRegistry.cs # 组件注册表
│ ├── DesktopComponentDefinition.cs # 组件定义
│ └── ...
├── plugins/ # 插件运行时
│ ├── PluginRuntimeService.cs # 插件运行时服务
│ ├── PluginLoader.cs # 插件加载器
│ └── ...
├── Theme/ # 主题资源
├── Styles/ # 样式规则
├── DesktopEditing/ # 桌面布局编辑
├── Localization/ # 本地化资源
└── Models/ # 数据模型
```
### 3.3 Launcher 工程结构
```
LanMountainDesktop.Launcher/
├── Program.cs # 启动器入口
├── App.axaml.cs # 启动器应用初始化
├── Views/ # 启动器视图
│ ├── OobeWindow.axaml # 首次体验窗口
│ └── SplashWindow.axaml # 启动动画窗口
└── Services/ # 启动器服务
├── DeploymentLocator.cs # 版本目录定位
├── UpdateCheckService.cs # 更新检查
├── UpdateEngineService.cs # 更新引擎
├── LauncherFlowCoordinator.cs # 流程协调器
├── OobeStateService.cs # OOBE 状态管理
├── PluginInstallerService.cs # 插件安装
└── PluginUpgradeQueueService.cs # 插件升级队列
```
---
## 4. 关键类与函数说明
### 4.1 应用程序入口与生命周期
#### `Program`LanMountainDesktop/Program.cs
**职责**:应用程序入口点,负责启动初始化、单实例控制、资源加载、渲染模式配置、日志初始化。
**关键属性**
```csharp
internal static string StartupRenderMode { get; private set; } = AppRenderingModeHelper.Default;
```
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `Main` | `public static void Main(string[] args)` | 应用入口,初始化日志、单实例、遥测,构建 Avalonia AppBuilder |
| `BuildAvaloniaApp` | `public static AppBuilder BuildAvaloniaApp(string renderMode)` | 构建 Avalonia 应用,配置 Win32 渲染模式 |
| `AcquireSingleInstance` | `private static SingleInstanceService AcquireSingleInstance(int? restartParentProcessId)` | 获取单实例锁,支持重启场景 |
| `LoadConfiguredRenderMode` | `private static string LoadConfiguredRenderMode()` | 从设置加载配置的渲染模式 |
| `RegisterGlobalExceptionLogging` | `private static void RegisterGlobalExceptionLogging()` | 注册全局未处理异常日志和遥测 |
#### `App`LanMountainDesktop/App.axaml.cs
**职责**:应用启动和生命周期管理,包含应用初始化、主窗口管理、插件运行时初始化、主题设置、设置系统初始化。
**关键属性**
```csharp
internal static SingleInstanceService? CurrentSingleInstanceService { get; set; }
internal static IHostApplicationLifecycle? CurrentHostApplicationLifecycle { get; }
internal static INotificationService? CurrentNotificationService { get; }
public PluginRuntimeService? PluginRuntimeService => _pluginRuntimeService;
public ISettingsFacadeService SettingsFacade => _settingsFacade;
```
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `Initialize` | `public override void Initialize()` | 初始化应用资源、主题、语言、设置服务 |
| `OnFrameworkInitializationCompleted` | `public override void OnFrameworkInitializationCompleted()` | 框架初始化完成后调用,初始化 IPC、桌面壳层 |
| `InitializeDesktopShell` | `private void InitializeDesktopShell()` | 初始化桌面壳层,包括插件运行时、托盘、主窗口 |
| `OpenIndependentSettingsModule` | `internal void OpenIndependentSettingsModule(string source, string? pageTag)` | 打开独立设置窗口 |
| `ActivateMainWindow` | `internal void ActivateMainWindow()` | 激活主窗口 |
### 4.2 插件系统
#### `PluginRuntimeService`LanMountainDesktop/plugins/PluginRuntimeService.cs
**职责**:插件系统的核心运行时类,负责插件的加载、卸载、管理、依赖注入、插件贡献点注册。
**关键属性**
```csharp
public string PluginsDirectory { get; } // 插件目录路径
public IReadOnlyList<LoadedPlugin> LoadedPlugins { get; } // 已加载插件列表
public IReadOnlyList<PluginLoadResult> LoadResults { get; } // 加载结果列表
public IReadOnlyList<PluginCatalogEntry> Catalog { get; } // 插件目录
public IReadOnlyList<PluginSettingsSectionContribution> SettingsSections { get; } // 设置页贡献
public IReadOnlyList<PluginDesktopComponentContribution> DesktopComponents { get; } // 组件贡献
public IReadOnlyList<PluginDesktopComponentEditorContribution> DesktopComponentEditors { get; } // 编辑器贡献
```
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `LoadInstalledPlugins` | `public void LoadInstalledPlugins()` | 加载所有已安装插件 |
| `SetPluginEnabled` | `public bool SetPluginEnabled(string pluginId, bool isEnabled)` | 启用/禁用插件 |
| `InstallPluginPackage` | `public PluginManifest InstallPluginPackage(string packagePath)` | 安装插件包(.laapp |
| `DeleteInstalledPlugin` | `public bool DeleteInstalledPlugin(string pluginId)` | 删除已安装插件 |
#### `IPlugin`LanMountainDesktop.PluginSdk/IPlugin.cs
**职责**:插件接口,定义了插件的基本生命周期和能力。插件必须实现此接口以被宿主识别和加载。
```csharp
public interface IPlugin
{
void Initialize(HostBuilderContext context, IServiceCollection services);
}
```
#### `PluginBase`LanMountainDesktop.PluginSdk/PluginBase.cs
**职责**:插件基类,提供了插件开发的基础实现。
```csharp
public abstract class PluginBase : IPlugin
{
public virtual void Initialize(HostBuilderContext context, IServiceCollection services) { }
}
```
#### `PluginManifest`LanMountainDesktop.PluginSdk/PluginManifest.cs
**职责**:插件清单信息类,包含插件的元数据。
```csharp
public sealed record PluginManifest(
string Id, // 插件唯一标识
string Name, // 插件名称
string EntranceAssembly, // 入口程序集
string? Description = null, // 描述
string? Author = null, // 作者
string? Version = null, // 版本
string? ApiVersion = null, // API 版本
IReadOnlyList<PluginSharedContractReference>? SharedContracts = null,
PluginRuntimeConfiguration? Runtime = null)
```
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `Load` | `public static PluginManifest Load(string manifestPath)` | 从文件加载插件清单 |
| `ResolveEntranceAssemblyPath` | `public string ResolveEntranceAssemblyPath(string manifestPath)` | 解析入口程序集路径 |
### 4.3 设置系统
#### `SettingsService`LanMountainDesktop/Services/Settings/SettingsService.cs
**职责**:设置系统的核心服务,管理应用和插件的设置数据持久化、读取和保存、设置变更监听。
**关键属性**
```csharp
public event EventHandler<SettingsChangedEvent>? Changed; // 设置变更事件
```
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `LoadSnapshot` | `public T LoadSnapshot<T>(SettingsScope scope, string? subjectId = null, string? placementId = null)` | 加载设置快照 |
| `SaveSnapshot` | `public void SaveSnapshot<T>(SettingsScope scope, T snapshot, ...)` | 保存设置快照 |
| `LoadSection` | `public T LoadSection<T>(SettingsScope scope, string subjectId, string sectionId, ...)` | 加载设置节 |
| `SaveSection` | `public void SaveSection<T>(SettingsScope scope, string subjectId, string sectionId, T section, ...)` | 保存设置节 |
| `GetValue` | `public T? GetValue<T>(SettingsScope scope, string key, ...)` | 获取单个值 |
| `SetValue` | `public void SetValue<T>(SettingsScope scope, string key, T value, ...)` | 设置单个值 |
| `GetComponentAccessor` | `public IComponentSettingsAccessor GetComponentAccessor(string componentId, string? placementId)` | 获取组件设置访问器 |
**设置作用域SettingsScope**
| 作用域 | 说明 |
|--------|------|
| `App` | 应用级设置 |
| `Launcher` | 启动器设置 |
| `ComponentInstance` | 组件实例设置 |
| `Plugin` | 插件设置 |
### 4.4 外观主题系统
#### `IAppearanceThemeService`LanMountainDesktop/Services/AppearanceThemeService.cs
**职责**:外观主题服务接口,定义了主题获取、预览构建、资源应用等方法。
```csharp
public interface IAppearanceThemeService
{
AppearanceThemeSnapshot GetCurrent();
AppearanceThemeSnapshot BuildPreview(ThemeAppearanceSettingsState pendingState);
event EventHandler<AppearanceThemeSnapshot>? Changed;
void ApplyThemeResources(IResourceDictionary resources);
AppearanceMaterialSurface GetMaterialSurface(MaterialSurfaceRole role);
void ApplyWindowMaterial(Window window, MaterialSurfaceRole role);
}
```
#### `AppearanceThemeService`
**职责**:外观主题服务的实现,委托给 `MaterialColorService` 处理具体逻辑。
**关键方法**
| 方法 | 签名 | 说明 |
|------|------|------|
| `GetCurrent` | `public AppearanceThemeSnapshot GetCurrent()` | 获取当前主题快照 |
| `BuildPreview` | `public AppearanceThemeSnapshot BuildPreview(ThemeAppearanceSettingsState pendingState)` | 构建主题预览 |
| `ApplyThemeResources` | `public void ApplyThemeResources(IResourceDictionary resources)` | 应用主题资源到资源字典 |
| `GetMaterialSurface` | `public AppearanceMaterialSurface GetMaterialSurface(MaterialSurfaceRole role)` | 获取材质表面配置 |
| `ApplyWindowMaterial` | `public void ApplyWindowMaterial(Window window, MaterialSurfaceRole role)` | 应用窗口材质效果 |
**材质表面角色MaterialSurfaceRole**
| 角色 | 说明 |
|------|------|
| `WindowBackground` | 窗口背景 |
| `SettingsWindowBackground` | 设置窗口背景 |
| `DockBackground` | 停靠栏背景 |
| `StatusBarBackground` | 状态栏背景 |
| `DesktopComponentHost` | 桌面组件宿主 |
| `StatusBarComponentHost` | 状态栏组件宿主 |
| `OverlayPanel` | 覆盖层面板 |
### 4.5 桌面宿主
#### `DesktopBootstrap`LanMountainDesktop.DesktopHost/DesktopBootstrap.cs
**职责**:桌面启动引导,协调启动服务初始化和应用初始化。
```csharp
public static class DesktopBootstrap
{
public static void InitializeStartupServices(
Action initializeTelemetryIdentity,
Action initializeCrashTelemetry,
Action initializeUsageTelemetry,
Action scheduleStartupCleanup);
public static void InitializeApplication(Application application, Action initializeShell);
}
```
### 4.6 Launcher 核心服务
#### `DeploymentLocator`LanMountainDesktop.Launcher/Services/DeploymentLocator.cs
**职责**:扫描和定位 `app-*` 版本目录,选择最佳版本。
**版本选择算法**
1. 扫描所有 `app-*` 目录
2. 过滤掉带 `.destroy``.partial` 标记的目录
3. 优先选择带 `.current` 标记的版本
4. 如果没有 `.current`,选择版本号最高的
#### `UpdateEngineService`
**职责**:下载、验证、应用增量更新,支持原子化更新和回滚。
#### `LauncherFlowCoordinator`
**职责**:协调 OOBE → Splash → 更新 → 插件 → 启动主程序的完整流程。
---
## 5. 依赖关系
### 5.1 项目间依赖图
```
LanMountainDesktop (主程序)
├── LanMountainDesktop.Host.Abstractions
├── LanMountainDesktop.Shared.Contracts
├── LanMountainDesktop.Shared.IPC
├── LanMountainDesktop.Settings.Core
├── LanMountainDesktop.Appearance
├── LanMountainDesktop.DesktopComponents.Runtime
├── LanMountainDesktop.DesktopHost
├── LanMountainDesktop.PluginSdk
└── ThirdParty/DotNetCampus.InkCanvas
LanMountainDesktop.Launcher (启动器)
├── LanMountainDesktop.Shared.Contracts
├── LanMountainDesktop.Shared.IPC
└── LanMountainDesktop.Settings.Core
LanMountainDesktop.PluginSdk (插件SDK)
└── (无项目引用,纯公共接口)
LanMountainDesktop.DesktopHost
├── LanMountainDesktop.Host.Abstractions
└── LanMountainDesktop.Shared.Contracts
LanMountainDesktop.Appearance
├── LanMountainDesktop.Settings.Core
└── LanMountainDesktop.Shared.Contracts
LanMountainDesktop.DesktopComponents.Runtime
├── LanMountainDesktop.Host.Abstractions
└── LanMountainDesktop.Shared.Contracts
LanMountainDesktop.PluginIsolation.Ipc
├── LanMountainDesktop.PluginIsolation.Contracts
└── LanMountainDesktop.Shared.IPC
```
### 5.2 主要 NuGet 依赖
| 包名 | 版本 | 用途 |
|------|------|------|
| Avalonia | 12.0.2 | 跨平台 UI 框架 |
| Avalonia.Controls.WebView | 12.0.0 | WebView 控件 |
| Avalonia.Desktop | 12.0.2 | 桌面平台支持 |
| Avalonia.Themes.Fluent | 12.0.2 | Fluent 主题 |
| FluentAvaloniaUI | 3.0.0-preview2 | Fluent UI 控件库 |
| Material.Avalonia | 3.16.1 | Material Design 控件 |
| MaterialColorUtilities | 0.3.0 | Material Design 3 动态配色 |
| CommunityToolkit.Mvvm | 8.4.2 | MVVM 工具包 |
| Microsoft.Extensions.DependencyInjection | 11.0.0-preview | 依赖注入 |
| Microsoft.Extensions.Hosting.Abstractions | 11.0.0-preview | 宿主抽象 |
| Microsoft.Data.Sqlite | 11.0.0-preview | SQLite 数据库 |
| PostHog | 2.6.0 | 使用遥测 |
| Sentry | 6.4.1 | 崩溃遥测 |
| Downloader | 5.4.0 | 文件下载 |
| Lib.Harmony.Thin | 2.4.2 | 运行时方法拦截 |
| log4net | 3.3.1 | 日志记录 |
---
## 6. 项目运行方式
### 6.1 环境准备
- 安装 **.NET SDK 10**(由 `global.json` 锁定版本 `10.0.103`
- 桌面端建议优先在 Windows 上开发和验证
- 仓库主入口解决方案文件为 `LanMountainDesktop.slnx`
### 6.2 常用命令
#### 还原与构建
```bash
dotnet restore
dotnet build LanMountainDesktop.slnx -c Debug
```
#### 运行桌面宿主(开发模式)
```bash
# 直接运行主程序,跳过 Launcher
dotnet run --project LanMountainDesktop/LanMountainDesktop.csproj
```
#### 运行桌面宿主(生产模式)
```bash
# 先构建 Launcher
dotnet build LanMountainDesktop.Launcher/LanMountainDesktop.Launcher.csproj -c Debug
# 通过 Launcher 启动主程序
dotnet run --project LanMountainDesktop.Launcher/LanMountainDesktop.Launcher.csproj -- launch
```
#### Launcher 其他命令
```bash
# 检查更新
dotnet run --project LanMountainDesktop.Launcher/LanMountainDesktop.Launcher.csproj -- update check
# 安装插件
dotnet run --project LanMountainDesktop.Launcher/LanMountainDesktop.Launcher.csproj -- plugin install <path-to-plugin.laapp>
# 版本回退
dotnet run --project LanMountainDesktop.Launcher/LanMountainDesktop.Launcher.csproj -- update rollback
```
#### 运行测试
```bash
dotnet test LanMountainDesktop.slnx -c Debug
```
#### 插件本地包生成
```powershell
./scripts/Pack-PluginPackages.ps1
```
### 6.3 Linux 录音依赖
如果在 Linux 上使用录音机或自习监测相关能力,需要安装音频库:
```bash
# Debian/Ubuntu
sudo apt install libportaudio2 libasound2
# Fedora/RHEL
sudo dnf install portaudio-libs alsa-lib
# Arch Linux
sudo pacman -S portaudio alsa-lib
# Alpine Linux
sudo apk add portaudio alsa-lib
```
---
## 7. 启动流程详解
### 7.1 生产环境启动流程(通过 Launcher
```
用户启动 LanMountainDesktop.Launcher.exe
Launcher 扫描 app-* 目录,选择最佳版本
(优先 .current 标记,然后按版本号降序)
首次启动?→ 显示 OOBE 引导OobeWindow
显示 Splash 启动动画SplashWindow
检查并应用待处理的更新UpdateEngineService.ApplyPendingUpdate
处理插件升级队列PluginUpgradeQueueService
启动主程序 app-{version}/LanMountainDesktop.exe
清理标记为 .destroy 的旧版本
```
### 7.2 主程序启动流程LanMountainDesktop.exe
```
Program.cs Main()
├── 初始化日志AppLogger.Initialize
├── 初始化应用数据路径AppDataPathProvider.Initialize
├── 解析开发插件选项DevPluginOptions.Parse
├── 注册全局异常日志
└── 获取重启父进程 ID
获取单实例锁SingleInstanceService
├── 非主实例?→ 通知主实例并退出
└── 是主实例?→ 继续
初始化启动服务DesktopBootstrap.InitializeStartupServices
├── 初始化遥测身份TelemetryIdentityService
├── 初始化崩溃遥测SentryCrashTelemetryService
├── 初始化使用遥测PostHogUsageTelemetryService
└── 调度白板笔记启动清理
运行启动诊断StartupDiagnosticsService.Run
加载配置的渲染模式LoadConfiguredRenderMode
构建 Avalonia AppBuilderBuildAvaloniaApp
进入 App.axaml.cs
├── 初始化主题ApplyThemeFromSettings
├── 初始化语言ApplyCurrentCultureFromSettings
├── 初始化设置窗口服务EnsureSettingsWindowService
├── 初始化天气定位刷新EnsureWeatherLocationRefreshService
└── 初始化通知服务EnsureNotificationService
框架初始化完成OnFrameworkInitializationCompleted
├── 初始化公共 IPCInitializePublicIpc
├── 启动单实例激活监听
├── 初始化 Launcher IPCInitializeLauncherIpcAsync
└── 初始化桌面壳层InitializeDesktopShell
桌面壳层初始化
├── 初始化插件运行时InitializePluginRuntime
├── 初始化托盘图标InitializeTrayIcon
├── 创建主窗口CreateAndAssignMainWindow
└── 启动天气定位刷新
```
### 7.3 版本目录结构
```
安装根目录/
├── LanMountainDesktop.Launcher.exe ← 唯一入口
├── app-1.0.0/ ← 版本目录
│ ├── .current ← 当前版本标记
│ ├── LanMountainDesktop.exe
│ └── ...
├── app-1.0.1/ ← 新版本
│ ├── .partial ← 下载中标记
│ └── ...
└── .launcher/ ← Launcher 数据
├── state/ ← OOBE 状态
├── update/incoming/ ← 更新缓存
└── snapshots/ ← 更新快照
```
**版本标记文件**
- `.current` - 标记当前使用的版本
- `.partial` - 标记下载未完成的版本(更新失败时自动清理)
- `.destroy` - 标记待删除的旧版本(下次启动时清理)
---
## 8. 插件系统架构
### 8.1 插件生命周期
```
插件包(.laapp
发现阶段DiscoverCandidates
├── 扫描 PluginsDirectory
├── 解析 plugin.json 清单
└── 验证 API 版本兼容性
加载阶段PluginLoader.LoadFromPackage / LoadFromManifest
├── 注册共享契约
├── 加载入口程序集
├── 调用 IPlugin.Initialize
└── 收集贡献点(设置页、组件、编辑器)
激活阶段
├── 注册设置页到设置窗口
├── 注册组件到组件系统
└── 注册编辑器到编辑器系统
运行阶段
├── 插件服务通过 DI 容器解析
├── 插件通过 IPluginContext 访问宿主功能
└── 插件通过 IPC 与宿主通信
卸载阶段
├── 卸载插件程序集
├── 清理贡献点
└── 释放资源
```
### 8.2 插件运行时模式
| 模式 | 状态 | 说明 |
|------|------|------|
| `in-proc` | 当前默认 | 进程内加载PluginLoadContext 提供程序集隔离 |
| `isolated-background` | 预留 | 后台逻辑移至独立工作进程Host UI 变为薄 IPC 驱动壳 |
| `isolated-window` | 预留 | 插件 UI 离屏渲染Host 嵌入平台窗口句柄 |
### 8.3 插件贡献点
插件可以向宿主贡献以下内容:
1. **设置页Settings Sections**:通过 `IPluginSettingsService` 注册自定义设置页
2. **桌面组件Desktop Components**:通过组件贡献点注册可放置的桌面组件
3. **组件编辑器Component Editors**:为组件提供自定义编辑器界面
4. **公共服务Public Services**:通过 IPC 向外部提供公共服务
### 8.4 插件目录结构
```
PluginsDirectory/
├── PluginA/
│ ├── plugin.json # 插件清单
│ ├── PluginA.dll # 入口程序集
│ └── ... # 其他资源
├── PluginB.laapp # 打包的插件包
└── ...
```
---
## 9. 数据流与交互模型
### 9.1 设置流
```
Settings.Core基础设置能力
├── 宿主通过 SettingsFacade 读取和监听设置变化
├── 插件通过 IPluginSettingsService 访问设置
└── 组件通过 IComponentSettingsAccessor 访问设置
```
### 9.2 外观流
```
Appearance主题和圆角资源
├── 宿主在 App.axaml.cs 中应用到资源字典
├── MaterialColorService 处理动态配色
└── 主题变更通过事件通知所有订阅者
```
### 9.3 组件流
```
ComponentSystem组件定义、注册、扩展接入
├── 内置组件在 ComponentSystem/ 中定义
├── 插件通过贡献点注册扩展组件
└── DesktopEditing/ 处理组件放置和布局
```
### 9.4 插件流
```
plugins/(宿主侧插件运行时)
├── .laapp 插件包的发现、安装、替换
├── 插件激活与共享契约装配
└── 插件设置页注册到宿主设置窗口
```
### 9.5 IPC 流
```
Shared.IPC统一 IPC 基础)
├── Host 公共服务
├── Launcher/OOBE 启动通知
├── 插件贡献的公共服务
└── 外部集成External IPC Public API
```
---
## 10. 测试体系
### 10.1 测试项目
测试项目 `LanMountainDesktop.Tests/` 覆盖以下方面:
| 测试类 | 覆盖内容 |
|--------|---------|
| `CornerRadiusScaleTests.cs` | 圆角和外观缩放 |
| `DesktopPlacementMathTests.cs` | 桌面布局数学计算 |
| `DesktopEditCommitMathTests.cs` | 桌面编辑提交计算 |
| `ComponentSettingsServiceTests.cs` | 组件设置服务 |
| `UiExceptionGuardTests.cs` | UI 异常保护 |
| `WhiteboardNotePersistenceServiceTests.cs` | 白板笔记持久化 |
| `MaterialColorIntegrationTests.cs` | 材质颜色集成 |
| `OobeStateServiceTests.cs` | OOBE 状态服务 |
| `PluginInstallerServiceTests.cs` | 插件安装服务 |
| `PluginUpgradeQueueServiceTests.cs` | 插件升级队列 |
| `LauncherFlowCoordinatorTests.cs` | 启动器流程协调 |
| `LauncherBackgroundServiceTests.cs` | 启动器后台服务 |
| `PluginIpcServerTests.cs` | 插件 IPC 服务端 |
| `PluginIpcClientTests.cs` | 插件 IPC 客户端 |
| `HostShutdownGateTests.cs` | 主机关闭门 |
| `SingleInstanceServiceTests.cs` | 单实例服务 |
### 10.2 测试原则
- 涉及宿主行为、SDK 契约、布局计算或设置持久化的改动,应优先补对应测试
- 优先扩展已有测试而不是新建无关测试入口
---
## 附录 A快速参考
### A.1 关键文件速查
| 需求 | 优先查看文件 |
|------|-------------|
| 启动问题 | `LanMountainDesktop/Program.cs`, `LanMountainDesktop/App.axaml.cs` |
| Launcher 启动问题 | `LanMountainDesktop.Launcher/Program.cs`, `Services/LauncherFlowCoordinator.cs` |
| 版本管理问题 | `LanMountainDesktop.Launcher/Services/DeploymentLocator.cs` |
| 更新系统问题 | `LanMountainDesktop.Launcher/Services/UpdateEngineService.cs`, `UpdateCheckService.cs` |
| 设置窗口和设置页 | `LanMountainDesktop/Views/`, `ViewModels/`, `Services/Settings/` |
| 插件加载与安装 | `LanMountainDesktop/plugins/PluginRuntimeService.cs` |
| 组件元数据或放置规则 | `LanMountainDesktop/ComponentSystem/` |
| 主题、颜色、圆角 | `LanMountainDesktop/Theme/`, `Styles/`, `LanMountainDesktop.Appearance/` |
| 设置持久化 | `LanMountainDesktop.Settings.Core/`, `LanMountainDesktop/Services/Settings/SettingsService.cs` |
| SDK 接口调整 | `LanMountainDesktop.PluginSdk/`, `LanMountainDesktop.Shared.Contracts/` |
| 桌面壳层或生命周期 | `Program.cs`, `App.axaml.cs`, `LanMountainDesktop.DesktopHost/` |
### A.2 文档权威来源
| 主题 | 权威文档 |
|------|---------|
| 产品定位 | `docs/PRODUCT.md` |
| 架构与模块职责 | `docs/ARCHITECTURE.md` |
| 运行、构建、测试、打包 | `docs/DEVELOPMENT.md` |
| 视觉规范 | `docs/VISUAL_SPEC.md` |
| 圆角规范 | `docs/CORNER_RADIUS_SPEC.md` |
| 生态边界 | `docs/ECOSYSTEM_BOUNDARIES.md` |
| SDK v5 迁移 | `docs/PLUGIN_SDK_V5_MIGRATION.md` |
| 代码地图 | `docs/ai/CODEBASE_MAP.md` |
| AI 协作入口 | `AGENTS.md` |
| Feature 规格 | `.trae/specs/` |
---
*本文档基于 LanMountainDesktop 仓库代码和文档自动生成,如有更新请以仓库最新代码为准。*