| | |
| | | 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() |
| | | { |
| | | _isRefreshing = true; |
| | | try |
| | | { |
| | | _state = await _runtime.GetStateAsync(); |
| | | var api = _apiStatus.GetStatus(); |
| | |
| | | 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; |
| | | 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); |