| | |
| | | private readonly DesktopPaths _paths; |
| | | private readonly DispatcherTimer _timer; |
| | | private AppRuntimeState? _state; |
| | | private string? _selectedWidgetForDisplayLayer; |
| | | private bool _isRefreshing; |
| | | |
| | | public ManagementWindow( |
| | | IDesktopRuntime runtime, |
| | |
| | | _paths = paths; |
| | | InitializeComponent(); |
| | | DisplayLayerSelector.ItemsSource = Enum.GetValues<DisplayLayer>(); |
| | | WidgetSelector.SelectionChanged += WidgetSelector_SelectionChanged; |
| | | _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; |
| | | _timer.Tick += async (_, _) => await RefreshAsync(); |
| | | Loaded += async (_, _) => |
| | |
| | | |
| | | private async Task RefreshAsync() |
| | | { |
| | | _state = await _runtime.GetStateAsync(); |
| | | var api = _apiStatus.GetStatus(); |
| | | var diagnostics = _diagnostics.Snapshot().OrderByDescending(entry => entry.Timestamp).ToArray(); |
| | | var webViewStatus = diagnostics.FirstOrDefault(entry => entry.Code == DiagnosticCodes.WebViewHostInitialized) is null |
| | | ? "未检测到初始化记录" |
| | | : "已初始化"; |
| | | _isRefreshing = true; |
| | | try |
| | | { |
| | | _state = await _runtime.GetStateAsync(); |
| | | var api = _apiStatus.GetStatus(); |
| | | var diagnostics = _diagnostics.Snapshot().OrderByDescending(entry => entry.Timestamp).ToArray(); |
| | | var webViewStatus = diagnostics.FirstOrDefault(entry => entry.Code == DiagnosticCodes.WebViewHostInitialized) is null |
| | | ? "未检测到初始化记录" |
| | | : "已初始化"; |
| | | |
| | | StatusSummaryText.Text = $"插件 {_state.Plugins.Count} 个,组件 {_state.Widgets.Count} 个,诊断 {diagnostics.Length} 条"; |
| | | RuntimeText.Text = $"运行时:编辑={_state.IsEditMode},安全输入={_state.IsSafeInputMode},全局锁定={_state.IsGlobalLocked}"; |
| | | ApiText.Text = api.IsListening ? $"API:127.0.0.1:{api.Port}" : $"API:未监听 {api.ErrorCode ?? ""}"; |
| | | WallpaperText.Text = $"壁纸:启用={_state.Wallpaper.IsEnabled},插件={_state.Wallpaper.PluginId?.Value ?? "无"}"; |
| | | WebViewText.Text = $"WebView2:{webViewStatus}"; |
| | | WallpaperDetailText.Text = $"启用:{_state.Wallpaper.IsEnabled}\n暂停:{_state.Wallpaper.IsPaused}\n静音:{_state.Wallpaper.IsMuted}\n透明度:{_state.Wallpaper.Opacity}\n点击穿透:{_state.Wallpaper.ClickThrough}"; |
| | | EditModeButton.Content = _state.IsEditMode ? "退出编辑" : "进入编辑"; |
| | | SafeInputButton.Content = _state.IsSafeInputMode ? "退出安全" : "安全输入"; |
| | | GlobalLockButton.Content = _state.IsGlobalLocked ? "解锁全部" : "锁定全部"; |
| | | StatusSummaryText.Text = $"插件 {_state.Plugins.Count} 个,组件 {_state.Widgets.Count} 个,诊断 {diagnostics.Length} 条"; |
| | | RuntimeText.Text = $"运行时:编辑={_state.IsEditMode},安全输入={_state.IsSafeInputMode},全局锁定={_state.IsGlobalLocked}"; |
| | | ApiText.Text = api.IsListening ? $"API:127.0.0.1:{api.Port}" : $"API:未监听 {api.ErrorCode ?? ""}"; |
| | | WallpaperText.Text = $"壁纸:启用={_state.Wallpaper.IsEnabled},插件={_state.Wallpaper.PluginId?.Value ?? "无"}"; |
| | | WebViewText.Text = $"WebView2:{webViewStatus}"; |
| | | WallpaperDetailText.Text = $"启用:{_state.Wallpaper.IsEnabled}\n暂停:{_state.Wallpaper.IsPaused}\n静音:{_state.Wallpaper.IsMuted}\n透明度:{_state.Wallpaper.Opacity}\n点击穿透:{_state.Wallpaper.ClickThrough}"; |
| | | EditModeButton.Content = _state.IsEditMode ? "退出编辑" : "进入编辑"; |
| | | SafeInputButton.Content = _state.IsSafeInputMode ? "退出安全" : "安全输入"; |
| | | GlobalLockButton.Content = _state.IsGlobalLocked ? "解锁全部" : "锁定全部"; |
| | | |
| | | var widgets = _state.Widgets.Select(WidgetRow.From).ToArray(); |
| | | var selected = (WidgetSelector.SelectedItem as WidgetRow)?.InstanceId; |
| | | WidgetSelector.ItemsSource = widgets; |
| | | WidgetSelector.SelectedItem = widgets.FirstOrDefault(widget => widget.InstanceId == selected) ?? widgets.FirstOrDefault(); |
| | | WidgetsGrid.ItemsSource = widgets; |
| | | DisplayLayerSelector.SelectedItem = (WidgetSelector.SelectedItem as WidgetRow)?.DisplayLayer ?? DisplayLayer.Desktop; |
| | | PluginsGrid.ItemsSource = _state.Plugins.Select(PluginRow.From).ToArray(); |
| | | DiagnosticsGrid.ItemsSource = diagnostics.Select(DiagnosticRow.From).ToArray(); |
| | | RecentDiagnosticsGrid.ItemsSource = diagnostics.Take(8).Select(DiagnosticRow.From).ToArray(); |
| | | ManifestErrorsText.Text = BuildManifestErrorText(_state); |
| | | var widgets = _state.Widgets.Select(WidgetRow.From).ToArray(); |
| | | var selected = (WidgetSelector.SelectedItem as WidgetRow)?.InstanceId; |
| | | WidgetSelector.ItemsSource = widgets; |
| | | WidgetSelector.SelectedItem = widgets.FirstOrDefault(widget => widget.InstanceId == selected) ?? widgets.FirstOrDefault(); |
| | | WidgetsGrid.ItemsSource = widgets; |
| | | SyncDisplayLayerSelector(force: _selectedWidgetForDisplayLayer != (WidgetSelector.SelectedItem as WidgetRow)?.InstanceId); |
| | | PluginsGrid.ItemsSource = _state.Plugins.Select(PluginRow.From).ToArray(); |
| | | DiagnosticsGrid.ItemsSource = diagnostics.Select(DiagnosticRow.From).ToArray(); |
| | | RecentDiagnosticsGrid.ItemsSource = diagnostics.Take(8).Select(DiagnosticRow.From).ToArray(); |
| | | ManifestErrorsText.Text = BuildManifestErrorText(_state); |
| | | LoadTodayLog(); |
| | | } |
| | | finally |
| | | { |
| | | _isRefreshing = false; |
| | | } |
| | | } |
| | | |
| | | private async void RefreshButton_Click(object sender, RoutedEventArgs e) => await RefreshAsync(); |
| | |
| | | private void OpenPluginsJson_Click(object sender, RoutedEventArgs e) |
| | | => OpenJson(Path.Combine(_paths.ConfigDirectory, "plugins.json")); |
| | | |
| | | private void OpenLogDirectory_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | Directory.CreateDirectory(_paths.LogDirectory); |
| | | Process.Start(new ProcessStartInfo("explorer.exe", $"\"{_paths.LogDirectory}\"") { UseShellExecute = true }); |
| | | } |
| | | |
| | | private void OpenPluginJson_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if ((PluginsGrid.SelectedItem as PluginRow) is not { } plugin) |
| | |
| | | |
| | | private WidgetRow? SelectedWidget() => WidgetSelector.SelectedItem as WidgetRow; |
| | | |
| | | private void WidgetSelector_SelectionChanged(object sender, SelectionChangedEventArgs e) |
| | | { |
| | | if (!_isRefreshing) |
| | | { |
| | | SyncDisplayLayerSelector(force: true); |
| | | } |
| | | } |
| | | |
| | | private void SyncDisplayLayerSelector(bool force) |
| | | { |
| | | if (SelectedWidget() is not { } widget) |
| | | { |
| | | DisplayLayerSelector.SelectedItem = DisplayLayer.Desktop; |
| | | _selectedWidgetForDisplayLayer = null; |
| | | return; |
| | | } |
| | | |
| | | if (!force) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | DisplayLayerSelector.SelectedItem = widget.DisplayLayer; |
| | | _selectedWidgetForDisplayLayer = widget.InstanceId; |
| | | } |
| | | |
| | | private void LoadTodayLog() |
| | | { |
| | | var logPath = Path.Combine(_paths.LogDirectory, $"moli-desktop-{DateTimeOffset.Now:yyyyMMdd}.log"); |
| | | LogPathText.Text = logPath; |
| | | if (!File.Exists(logPath)) |
| | | { |
| | | LogTextBox.Text = "当天暂无日志。"; |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | var text = File.ReadAllText(logPath, Encoding.UTF8); |
| | | if (LogTextBox.Text == text) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var wasNearBottom = LogTextBox.VerticalOffset >= LogTextBox.ExtentHeight - LogTextBox.ViewportHeight - 24; |
| | | LogTextBox.Text = text; |
| | | if (wasNearBottom) |
| | | { |
| | | LogTextBox.ScrollToEnd(); |
| | | } |
| | | } |
| | | catch (IOException) |
| | | { |
| | | } |
| | | catch (UnauthorizedAccessException) |
| | | { |
| | | } |
| | | } |
| | | |
| | | private async Task InstallWallpaperFilePluginAsync(string fileName) |
| | | { |
| | | Directory.CreateDirectory(_paths.PluginDirectory); |