From cf40cded1708bb303be4a404b7aceddd196e1d46 Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期三, 29 七月 2026 20:58:14 +0800
Subject: [PATCH] v1.0

---
 src/Moli.Desktop/Management/ManagementWindow.xaml.cs |  127 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 102 insertions(+), 25 deletions(-)

diff --git a/src/Moli.Desktop/Management/ManagementWindow.xaml.cs b/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
index 1bee7d6..19185ed 100644
--- a/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
+++ b/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
@@ -30,6 +30,8 @@
     private readonly DesktopPaths _paths;
     private readonly DispatcherTimer _timer;
     private AppRuntimeState? _state;
+    private string? _selectedWidgetForDisplayLayer;
+    private bool _isRefreshing;
 
     public ManagementWindow(
         IDesktopRuntime runtime,
@@ -43,6 +45,7 @@
         _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 (_, _) =>
@@ -55,33 +58,42 @@
 
     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 ? "瑙i攣鍏ㄩ儴" : "閿佸畾鍏ㄩ儴";
+            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 ? "瑙i攣鍏ㄩ儴" : "閿佸畾鍏ㄩ儴";
 
-        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();
@@ -199,6 +211,12 @@
     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)
@@ -228,6 +246,65 @@
 
     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);

--
Gitblit v1.9.1