| | |
| | | private readonly DesktopPaths _paths; |
| | | private readonly DispatcherTimer _timer; |
| | | private AppRuntimeState? _state; |
| | | private string? _selectedWidgetForDisplayLayer; |
| | | private bool _isRefreshing; |
| | | |
| | | public ManagementWindow( |
| | | IDesktopRuntime runtime, |
| | |
| | | _apiStatus = apiStatus; |
| | | _paths = paths; |
| | | InitializeComponent(); |
| | | DisplayPrioritySelector.ItemsSource = Enum.GetValues<DisplayPriority>(); |
| | | 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; |
| | | DisplayPrioritySelector.SelectedItem = (WidgetSelector.SelectedItem as WidgetRow)?.DisplayPriority ?? DisplayPriority.IconLayer; |
| | | 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 async void ToggleSafeInput_Click(object sender, RoutedEventArgs e) => await ExecuteAsync(() => _runtime.SetSafeInputModeAsync(!(_state?.IsSafeInputMode ?? false))); |
| | | private async void ToggleGlobalLock_Click(object sender, RoutedEventArgs e) => await ExecuteAsync(() => _runtime.SetGlobalLockedAsync(!(_state?.IsGlobalLocked ?? false))); |
| | | private async void ReloadPlugins_Click(object sender, RoutedEventArgs e) => await ExecuteAsync(() => _runtime.ReloadPluginsAsync()); |
| | | private async void ApplyDisplayPriority_Click(object sender, RoutedEventArgs e) |
| | | private async void ApplyDisplayLayer_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (SelectedWidget() is not { } widget || DisplayPrioritySelector.SelectedItem is not DisplayPriority priority) |
| | | if (SelectedWidget() is not { } widget || DisplayLayerSelector.SelectedItem is not DisplayLayer layer) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { DisplayPriority = priority }); |
| | | if (layer == DisplayLayer.GlobalTop) |
| | | { |
| | | var choice = MessageBox.Show( |
| | | this, |
| | | "全局置顶会让该组件覆盖其他应用窗口。确认继续?", |
| | | "全局置顶", |
| | | MessageBoxButton.YesNo, |
| | | MessageBoxImage.Warning); |
| | | if (choice != MessageBoxResult.Yes) |
| | | { |
| | | return; |
| | | } |
| | | } |
| | | |
| | | await _runtime.CancelPluginRequestsAsync(new PluginId(widget.PluginId)); |
| | | await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { DisplayLayer = layer }); |
| | | } |
| | | |
| | | private async void ToggleWidgetVisible_Click(object sender, RoutedEventArgs e) |
| | |
| | | { |
| | | var choice = MessageBox.Show( |
| | | this, |
| | | "确认恢复默认布局和交互配置?此操作会显示并解锁组件,恢复透明度、点击穿透和默认显示优先级。", |
| | | "确认恢复默认布局和交互配置?此操作会显示并解锁组件,恢复透明度、点击穿透和默认显示层级。", |
| | | "恢复默认", |
| | | MessageBoxButton.YesNo, |
| | | MessageBoxImage.Warning); |
| | |
| | | IsLocked = false, |
| | | Opacity = 1, |
| | | ClickThrough = true, |
| | | DisplayPriority = DisplayPriority.IconLayer |
| | | DisplayLayer = DisplayLayer.Desktop |
| | | }); |
| | | } |
| | | |
| | |
| | | |
| | | 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) |
| | | { |
| | |
| | | } |
| | | |
| | | 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) |
| | | { |
| | |
| | | description = $"Loaded from {Path.GetFileName(fileName)}.", |
| | | author = "Moli.Desktop", |
| | | permissions = Array.Empty<string>(), |
| | | defaultLayout = new { width = 1920, height = 1080, x = 0, y = 0, locked = true, clickThrough = true, opacity = 1, scale = 1 }, |
| | | defaultLayout = new { width = 1920, height = 1080, x = 0, y = 0, locked = true, clickThrough = true, opacity = 1, scale = 1, displayLayer = "desktop" }, |
| | | config = new { sourceFile = fileName, sourceType = extension.TrimStart('.') } |
| | | }; |
| | | await File.WriteAllTextAsync( |
| | |
| | | |
| | | private static string Html(string value) => System.Net.WebUtility.HtmlEncode(value); |
| | | |
| | | private sealed record WidgetRow(string InstanceId, string PluginId, string Bounds, string MonitorId, bool IsVisible, bool IsLocked, double Opacity, bool ClickThrough, DisplayPriority DisplayPriority) |
| | | private sealed record WidgetRow(string InstanceId, string PluginId, string Bounds, string MonitorId, bool IsVisible, bool IsLocked, double Opacity, bool ClickThrough, DisplayLayer DisplayLayer) |
| | | { |
| | | public static WidgetRow From(WidgetRuntimeState state) |
| | | => new( |
| | |
| | | state.IsLocked, |
| | | state.Opacity, |
| | | state.ClickThrough, |
| | | state.DisplayPriority); |
| | | state.DisplayLayer); |
| | | } |
| | | |
| | | private sealed record PluginRow(string Id, string Name, string Type, string State, string? ErrorCode, string Entry) |