瀏覽代碼

ui state bug fix

Coder 8 月之前
父節點
當前提交
52450b370e
共有 1 個文件被更改,包括 68 次插入44 次删除
  1. 68 44
      Bmp.WinForms/MainForm.cs

+ 68 - 44
Bmp.WinForms/MainForm.cs

@@ -1,3 +1,4 @@
+using System.CodeDom;
 using System.ComponentModel;
 using System.Text;
 using System.Threading.Channels;
@@ -83,6 +84,8 @@ namespace Bmp.WinForms
             string? balloonTitle = null;
             string? balloonContent = null;
 
+            bool? flagPlayOk = null;
+
             try
             {
                 var meta = await Task.Run(() => InputSourceProvider.GetMeta(item.Name));
@@ -140,7 +143,7 @@ namespace Bmp.WinForms
                     return;
                 }
 
-                await PlayAsync();
+                flagPlayOk = await PlayAsync();
             }
             catch (Exception ex)
             {
@@ -151,21 +154,23 @@ namespace Bmp.WinForms
             finally
             {
                 MainPanel.Enabled = true;
-
-                item.SubItems[StateColumnHeader.Index].Text = finalState;
-                if (balloonShow)
+                if (flagPlayOk == null)
                 {
-                    SettingButtonToolTip.ToolTipIcon = balloonIcon ?? ToolTipIcon.None;
-                    SettingButtonToolTip.ToolTipTitle = balloonTitle;
-                    if (balloonContent != null)
+                    item.SubItems[StateColumnHeader.Index].Text = finalState;
+                    if (balloonShow)
                     {
-                        SettingButtonToolTip.Show(balloonContent, SettingButton);
-                        SettingButtonToolTip.Show(balloonContent, SettingButton);
+                        SettingButtonToolTip.ToolTipIcon = balloonIcon ?? ToolTipIcon.None;
+                        SettingButtonToolTip.ToolTipTitle = balloonTitle;
+                        if (balloonContent != null)
+                        {
+                            SettingButtonToolTip.Show(balloonContent, SettingButton);
+                            SettingButtonToolTip.Show(balloonContent, SettingButton);
+                        }
+                    }
+                    else
+                    {
+                        SettingButtonToolTip.Hide(SettingButton);
                     }
-                }
-                else
-                {
-                    SettingButtonToolTip.Hide(SettingButton);
                 }
             }
         }
@@ -177,7 +182,7 @@ namespace Bmp.WinForms
             if (local != null)
             {
                 local.PlaybackStopped -= _outputDevice_PlaybackStopped;
-                Invoke(() => local.Stop());
+                local.Stop();
                 local.Dispose();
             }
         }
@@ -205,7 +210,7 @@ namespace Bmp.WinForms
                         balloonShow = true;
                         balloonIcon = ToolTipIcon.Error;
                         balloonTitle = "未指定 DSD 输出设备";
-                        balloonContent = "在设置菜单选择输出设备";
+                        balloonContent = "在设置菜单选择 DSD 输出设备";
                         _playbackState = UIPlaybackState.Error;
                         return false;
                     }
@@ -222,7 +227,7 @@ namespace Bmp.WinForms
                         balloonShow = true;
                         balloonIcon = ToolTipIcon.Warning;
                         balloonTitle = "找不到指定的 DSD 输出设备";
-                        balloonContent = "在设置菜单选择输出设备";
+                        balloonContent = "在设置菜单选择 DSD 输出设备";
                         _playbackState = UIPlaybackState.Error;
                         return false;
                     }
@@ -240,7 +245,7 @@ namespace Bmp.WinForms
                         balloonShow = true;
                         balloonIcon = ToolTipIcon.Error;
                         balloonTitle = "未指定 PCM 输出设备";
-                        balloonContent = "在设置菜单选择输出设备";
+                        balloonContent = "在设置菜单选择 PCM 输出设备";
                         _playbackState = UIPlaybackState.Error;
                         return false;
                     }
@@ -257,7 +262,7 @@ namespace Bmp.WinForms
                         balloonShow = true;
                         balloonIcon = ToolTipIcon.Warning;
                         balloonTitle = "找不到指定的 PCM 输出设备";
-                        balloonContent = "在设置菜单选择输出设备";
+                        balloonContent = "在设置菜单选择 PCM 输出设备";
                         _playbackState = UIPlaybackState.Error;
                         return false;
                     }
@@ -270,7 +275,7 @@ namespace Bmp.WinForms
 
                 try
                 {
-                    Invoke(() => _outputDevice = OutputDeviceProvider.CreateWavePlayer(_selectedOutputDevice, _nativeDsd));
+                    _outputDevice = OutputDeviceProvider.CreateWavePlayer(_selectedOutputDevice, _nativeDsd);
                 }
                 catch (Exception e)
                 {
@@ -306,6 +311,12 @@ namespace Bmp.WinForms
             }
             catch (Exception e)
             {
+                //TODO: 异常处理
+                Console.WriteLine(e);
+                throw;
+            }
+            finally
+            {
                 _currentListViewItem!.SubItems[StateColumnHeader.Index].Text = finalState;
                 if (balloonShow)
                 {
@@ -342,14 +353,14 @@ namespace Bmp.WinForms
             }
         }
 
-        private async Task PlayAsync()
+        private async Task<bool> PlayAsync()
         {
             _playbackState = UIPlaybackState.Playing;
             if (await ReInitOutputDevice())
             {
                 try
                 {
-                    Invoke(() => _outputDevice?.Play());
+                    _outputDevice!.Play();
                 }
                 catch (Exception e)
                 {
@@ -358,6 +369,8 @@ namespace Bmp.WinForms
                     _playbackState = UIPlaybackState.Error;
                 }
             }
+
+            return _playbackState == UIPlaybackState.Playing;
         }
 
         private async Task PauseAsync()
@@ -469,7 +482,7 @@ namespace Bmp.WinForms
             var pcmOutputSelect = new ToolStripMenuItem($"PCM 输出{(selectedPcmDevice == null ? "(未选择)" : "")}");
             ctx.Items.Add(pcmOutputSelect);
 
-            var dsdOutputSelect = new ToolStripMenuItem($"DSD 输出{(selectedPcmDevice == null ? "(未选择)" : "")}");
+            var dsdOutputSelect = new ToolStripMenuItem($"DSD 输出{(selectedDsdDevice == null ? "(未选择)" : "")}");
             ctx.Items.Add(dsdOutputSelect);
 
             var dsdToPcm = new ToolStripMenuItem("软解码成PCM");
@@ -529,7 +542,7 @@ namespace Bmp.WinForms
                     var defaultLatency = selectedPcmDevice.Latency;
                     var selectedLatency = _sl?.State?.OutputDeviceLatency ?? defaultLatency;
 
-                    var latencyMenu = new ToolStripMenuItem("缓冲大小:" + (_sl.State?.OutputDeviceLatency ?? defaultLatency));
+                    var latencyMenu = new ToolStripMenuItem("PCM 输出缓冲大小:" + (_sl.State?.OutputDeviceLatency ?? defaultLatency));
                     ctx.Items.Add(latencyMenu);
 
                     void SetLatency(int latency)
@@ -552,7 +565,7 @@ namespace Bmp.WinForms
 
                 if (selectedPcmDevice.HasControlPanel)
                 {
-                    var toolStripItem = ctx.Items.Add("调出 PCM ASIO 设置面板");
+                    var toolStripItem = ctx.Items.Add("调出 PCM 设置面板");
                     if (_playbackState == UIPlaybackState.Playing)
                     {
                         toolStripItem.Enabled = false;
@@ -567,36 +580,36 @@ namespace Bmp.WinForms
                         catch (Exception e)
                         {
                             //TODO: 消息机制 错误 调出ASIO设置面板
-                            MessageBox.Show("无法调出 PCM ASIO 设置面板" + Environment.NewLine + e.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, false);
+                            MessageBox.Show("无法调出 PCM 设置面板" + Environment.NewLine + e.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, false);
                         }
                     };
                 }
+            }
 
-                if (selectedDsdDevice == null)
+            if (selectedDsdDevice == null)
+            {
+                ctx.Items.Add("(未选择 DSD 输出设备)").Enabled = false;
+            }
+            else if (selectedDsdDevice.HasControlPanel)
+            {
+                var toolStripItem = ctx.Items.Add("调出 DSD 设置面板");
+                if (_playbackState == UIPlaybackState.Playing)
                 {
-                    ctx.Items.Add("(未选择 DSD 输出设备)").Enabled = false;
+                    toolStripItem.Enabled = false;
+                    toolStripItem.Text += "(播放时不可用)";
                 }
-                else if (selectedDsdDevice.HasControlPanel)
+                toolStripItem.Click += delegate
                 {
-                    var toolStripItem = ctx.Items.Add("调出 DSD ASIO 设置面板");
-                    if (_playbackState == UIPlaybackState.Playing)
+                    try
                     {
-                        toolStripItem.Enabled = false;
-                        toolStripItem.Text += "(播放时不可用)";
+                        selectedDsdDevice.ShowControlPanel();
                     }
-                    toolStripItem.Click += delegate
+                    catch (Exception e)
                     {
-                        try
-                        {
-                            selectedDsdDevice.ShowControlPanel();
-                        }
-                        catch (Exception e)
-                        {
-                            //TODO: 消息机制 错误 调出ASIO设置面板
-                            MessageBox.Show("无法调出 DSD ASIO 设置面板" + Environment.NewLine + e.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, false);
-                        }
-                    };
-                }
+                        //TODO: 消息机制 错误 调出ASIO设置面板
+                        MessageBox.Show("无法调出 DSD 设置面板" + Environment.NewLine + e.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0, false);
+                    }
+                };
             }
 
             ctx.Items.Add("-");
@@ -607,6 +620,17 @@ namespace Bmp.WinForms
                 if (ctx.Items[expandIndex.Value] is ToolStripMenuItem menuItem) menuItem.ShowDropDown();
             };
             ctx.Show(SettingButton, Point.Empty);
+
+
+#if DEBUG
+            ctx.Items.Add("-");
+            var dbgItem = new ToolStripMenuItem("调试");
+            ctx.Items.Add(dbgItem);
+
+            dbgItem.DropDownItems.Add("清除选中 PCM 设备").Click += delegate { if (_sl is { State: { } }) _sl.State.SelectedPcmOutputDeviceId = null; };
+            dbgItem.DropDownItems.Add("清除选中 DSD 设备").Click += delegate { if (_sl is { State: { } }) _sl.State.SelectedDsdAsioOutputDeviceId = null; };
+
+#endif
         }
 
         // -----------------  UI event: Form -----------------