Prechádzať zdrojové kódy

chore; MainForm use DI

Coder 7 mesiacov pred
rodič
commit
53cecee8eb

+ 1 - 1
Bmp.Core.Lite/Playback/Inputs/DsfAudioStream.cs

@@ -138,7 +138,7 @@ public class DsfAudioStream : DsdAudioStream
         var bytes = count / ChannelNum;
         var dstPtr = 0;
 
-        //TODO: optimization Unsafe moving prt
+        //TODO: optimization Unsafe moving ptr
 
         for (var s = 0; s < bytes; s++)
         {

+ 2 - 3
Bmp.Core.Lite/Playback/Inputs/InputSourceProviderLite.cs

@@ -100,7 +100,6 @@ public class InputSourceProviderLite
         }
         else if (uri.IsAbsoluteUri && uri.Scheme.ToLower() == "data")
         {
-
             var base64 = urlOrPath.Split(",").LastOrDefault();
             if (base64 != null)
             {
@@ -112,7 +111,7 @@ public class InputSourceProviderLite
                 }
                 catch (Exception e)
                 {
-                    //TODO: Event Bus?
+                    //TODO: 消息机制 错误 解析 DataURI 失败 无法获取大小
                     Console.WriteLine(e);
                 }
             }
@@ -177,7 +176,7 @@ public class InputSourceProviderLite
                 }
                 catch (Exception e)
                 {
-                    //TODO: Event Bus?
+                    //TODO: 消息机制 错误 解析 DataURI 失败
                     Console.WriteLine(e);
                     throw;
                 }

+ 1 - 7
Bmp.Core/Playback/Inputs/InputSourceProvider.cs

@@ -10,7 +10,6 @@ namespace Bmp.Core.Playback.Inputs
     {
         protected InputSourceProvider()
         {
-
         }
 
         public static AttachedPic[] GetPics(string urlOrPath)
@@ -23,11 +22,10 @@ namespace Bmp.Core.Playback.Inputs
                 var fp = decoder.AttachedPics;
 
                 mappedEmbeddedPics = fp?.Select(p => new AttachedPic { Tags = p.Meta, Data = p.Data.ToArray() }).ToArray();
-
             }
             catch (Exception e)
             {
-                //TODO: EventBus?
+                //TODO: 消息机制 警告 无法从元数据获取图片
                 Console.WriteLine(e);
             }
 
@@ -67,7 +65,6 @@ namespace Bmp.Core.Playback.Inputs
                     Duration = decoder.GetLength(),
                 };
             }
-          
             finally
             {
                 decoder?.Dispose();
@@ -89,13 +86,10 @@ namespace Bmp.Core.Playback.Inputs
 
                 underlyingStream.Position = 0;
                 if (DffAudioStream.IsDffFile(underlyingStream)) return new DffAudioStream(urlOrPath);
-
             }
 
             return new FFMPEGAudioStream(ProcessUrlOrPathForFFMPEG(urlOrPath));
         }
-
-
     }
 
     public class AttachedPic

+ 1 - 1
Bmp.WinForms/MainForm.Designer.cs

@@ -63,7 +63,7 @@
             SeekTrackBar.LargeChange = 1;
             SeekTrackBar.Location = new Point(407, 12);
             SeekTrackBar.Name = "SeekTrackBar";
-            SeekTrackBar.Size = new Size(517, 69);
+            SeekTrackBar.Size = new Size(501, 69);
             SeekTrackBar.TabIndex = 5;
             SeekTrackBar.TickFrequency = 0;
             SeekTrackBar.TickStyle = TickStyle.Both;

+ 47 - 64
Bmp.WinForms/MainForm.cs

@@ -1,5 +1,6 @@
 using System.CodeDom;
 using System.ComponentModel;
+using System.Diagnostics;
 using System.IO;
 using System.Text;
 using System.Threading.Channels;
@@ -20,7 +21,7 @@ using AsioOut = Bmp.Core.Lite.Playback.Outputs.NAudioASIO.AsioOut;
 
 namespace Bmp.WinForms
 {
-    public partial class MainForm : Form
+    internal partial class MainForm : Form
     {
         private const string EMOJI_X = "❌";
         private const string EMOJI_WARN = "⚠";
@@ -31,10 +32,9 @@ namespace Bmp.WinForms
 
         private readonly Channel<string> _pendingAddToList = Channel.CreateUnbounded<string>();
 
-        private IServiceProvider? _serviceProvider;
-        private ILogger<MainForm> _logger = new Logger<MainForm>(NullLoggerFactory.Instance);
-        private IEventBus? _eventBus;
-        private SaveLoadService? _sl;
+        private readonly ILogger<MainForm> _logger;
+        private readonly IEventBus? _eventBus;
+        private readonly SaveLoadService? _saveLoadService;
 
         private bool _isRunning;
 
@@ -50,24 +50,19 @@ namespace Bmp.WinForms
         private UIPlaybackState _playbackState;
         private bool _trackBarHolding;
 
-        public MainForm()
+        public MainForm(IEventBus eventBus, SaveLoadService saveLoadService, ILogger<MainForm> logger)
         {
-            Initialize();
             InitializeComponent();
-        }
 
-        private void Initialize()
-        {
+            _eventBus = eventBus;
+            _saveLoadService = saveLoadService;
+            _logger = logger;
+
             Text = Const.AppTitle;
             var eIcon = IconExtractor.GetMainIcon();
-            if (null != eIcon)
-            {
-                Icon = eIcon;
-            }
+            if (null != eIcon) Icon = eIcon;
         }
 
-        public void SetServiceProvider(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
-
         // -----------------  playback control -----------------
 
         private async Task LoadItemAsync(ListViewItem item)
@@ -189,7 +184,7 @@ namespace Bmp.WinForms
 
         private async Task ReloadSource()
         {
-            _inputSource = await Task.Run(() => InputSourceProvider.CreateWaveStream(_currentListViewItem!.Name, _sl?.State?.DecodeDsdToPcm == true));
+            _inputSource = await Task.Run(() => InputSourceProvider.CreateWaveStream(_currentListViewItem!.Name, _saveLoadService?.State?.DecodeDsdToPcm == true));
         }
 
         private async Task DeInitOutputDeviceAsync()
@@ -228,7 +223,7 @@ namespace Bmp.WinForms
                 if (_inputSource is DsdAudioStream)
                 {
                     _nativeDsd = true;
-                    if (_sl?.State?.SelectedDsdAsioOutputDeviceId == null)
+                    if (_saveLoadService?.State?.SelectedDsdAsioOutputDeviceId == null)
                     {
                         finalState = EMOJI_X;
                         balloonShow = true;
@@ -242,7 +237,7 @@ namespace Bmp.WinForms
                     var allDevices = OutputDeviceProvider.GetAllSupportedDevices();
                     IOutputDeviceInfo? selectedDevice = null;
 
-                    selectedDevice = allDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedDsdAsioOutputDeviceId);
+                    selectedDevice = allDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedDsdAsioOutputDeviceId);
 
                     if (selectedDevice == null)
                     {
@@ -261,7 +256,7 @@ namespace Bmp.WinForms
                 else
                 {
                     _nativeDsd = false;
-                    if (_sl?.State?.SelectedPcmOutputDeviceId == null)
+                    if (_saveLoadService?.State?.SelectedPcmOutputDeviceId == null)
                     {
                         //TODO: 消息机制 错误 未指定输出设备
 
@@ -277,7 +272,7 @@ namespace Bmp.WinForms
                     var allDevices = OutputDeviceProvider.GetAllSupportedDevices();
                     IOutputDeviceInfo? selectedDevice = null;
 
-                    selectedDevice = allDevices.FirstOrDefault(p => p.Id == _sl.State.SelectedPcmOutputDeviceId);
+                    selectedDevice = allDevices.FirstOrDefault(p => p.Id == _saveLoadService.State.SelectedPcmOutputDeviceId);
 
                     if (selectedDevice == null)
                     {
@@ -335,12 +330,6 @@ namespace Bmp.WinForms
                 _outputDevice = od;
                 _outputDevice.PlaybackStopped += _outputDevice_PlaybackStopped;
             }
-            catch (Exception e)
-            {
-                //TODO: 异常处理 ReInitOutputDevice
-                Console.WriteLine(e);
-                throw;
-            }
             finally
             {
                 _currentListViewItem!.SubItems[StateColumnHeader.Index].Text = finalState;
@@ -451,13 +440,13 @@ namespace Bmp.WinForms
 
         private void SaveState()
         {
-            if (_sl == null) return;
-            if (_sl.State != null)
+            if (_saveLoadService == null) return;
+            if (_saveLoadService.State != null)
             {
-                _sl.State.FormPosition = Location;
-                _sl.State.FormSize = Size;
+                _saveLoadService.State.FormPosition = Location;
+                _saveLoadService.State.FormSize = Size;
 
-                _sl.State.Playlist = MainListView.Items.Cast<ListViewItem>().Select(p => new SaveLoadPlaylistItem
+                _saveLoadService.State.Playlist = MainListView.Items.Cast<ListViewItem>().Select(p => new SaveLoadPlaylistItem
                 {
                     Path = p.Name,
                     Title = p.SubItems[TitleColumnHeader.Index].Text,
@@ -466,7 +455,7 @@ namespace Bmp.WinForms
                 }).ToArray();
             }
 
-            _sl.Save();
+            _saveLoadService.Save();
         }
 
         private async void StartProcessPendingAddQueue()
@@ -521,8 +510,8 @@ namespace Bmp.WinForms
             ctx.Items.Add("-");
 
             var allSupportedDevices = OutputDeviceProvider.GetAllSupportedDevices();
-            var selectedPcmDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedPcmOutputDeviceId);
-            var selectedDsdDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _sl?.State?.SelectedDsdAsioOutputDeviceId);
+            var selectedPcmDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedPcmOutputDeviceId);
+            var selectedDsdDevice = allSupportedDevices.FirstOrDefault(p => p.Id == _saveLoadService?.State?.SelectedDsdAsioOutputDeviceId);
 
             var pcmOutputSelect = new ToolStripMenuItem($"PCM 输出{(selectedPcmDevice == null ? "(未选择)" : "")}");
             ctx.Items.Add(pcmOutputSelect);
@@ -531,13 +520,13 @@ namespace Bmp.WinForms
             ctx.Items.Add(dsdOutputSelect);
 
             var dsdToPcm = new ToolStripMenuItem("软解码成PCM");
-            if (_sl?.State?.DecodeDsdToPcm == true) dsdToPcm.CheckState = CheckState.Indeterminate;
+            if (_saveLoadService?.State?.DecodeDsdToPcm == true) dsdToPcm.CheckState = CheckState.Indeterminate;
             dsdOutputSelect.DropDownItems.Add(dsdToPcm);
             dsdToPcm.Click += delegate
             {
-                if (_sl?.State == null) return;
-                _sl.State.DecodeDsdToPcm = true;
-                _sl.State.SelectedDsdAsioOutputDeviceId = null;
+                if (_saveLoadService?.State == null) return;
+                _saveLoadService.State.DecodeDsdToPcm = true;
+                _saveLoadService.State.SelectedDsdAsioOutputDeviceId = null;
                 ShowSettingContextMenu();
             };
 
@@ -549,9 +538,9 @@ namespace Bmp.WinForms
                 if (selectedPcmDevice == deviceInfo) pcmDeviceItem.CheckState = CheckState.Indeterminate;
                 pcmDeviceItem.Click += delegate
                 {
-                    if (_sl?.State == null) return;
-                    _sl.State.SelectedPcmOutputDeviceId = deviceInfo.Id;
-                    _sl.State.OutputDeviceLatency = null;
+                    if (_saveLoadService?.State == null) return;
+                    _saveLoadService.State.SelectedPcmOutputDeviceId = deviceInfo.Id;
+                    _saveLoadService.State.OutputDeviceLatency = null;
                     ShowSettingContextMenu();
                 };
 
@@ -564,9 +553,9 @@ namespace Bmp.WinForms
 
                     dsdDeviceItem.Click += delegate
                     {
-                        if (_sl?.State == null) return;
-                        _sl.State.SelectedDsdAsioOutputDeviceId = deviceInfo.Id;
-                        _sl.State.DecodeDsdToPcm = false;
+                        if (_saveLoadService?.State == null) return;
+                        _saveLoadService.State.SelectedDsdAsioOutputDeviceId = deviceInfo.Id;
+                        _saveLoadService.State.DecodeDsdToPcm = false;
                         ShowSettingContextMenu();
                     };
                 }
@@ -585,15 +574,15 @@ namespace Bmp.WinForms
                     const int mulCount = 4;
 
                     var defaultLatency = selectedPcmDevice.Latency;
-                    var selectedLatency = _sl?.State?.OutputDeviceLatency ?? defaultLatency;
+                    var selectedLatency = _saveLoadService?.State?.OutputDeviceLatency ?? defaultLatency;
 
-                    var latencyMenu = new ToolStripMenuItem("PCM 输出缓冲大小:" + (_sl.State?.OutputDeviceLatency ?? defaultLatency));
+                    var latencyMenu = new ToolStripMenuItem("PCM 输出缓冲大小:" + (_saveLoadService.State?.OutputDeviceLatency ?? defaultLatency));
                     ctx.Items.Add(latencyMenu);
 
                     void SetLatency(int latency)
                     {
-                        if (_sl.State == null) return;
-                        _sl.State.OutputDeviceLatency = latency;
+                        if (_saveLoadService.State == null) return;
+                        _saveLoadService.State.OutputDeviceLatency = latency;
                         ShowSettingContextMenu();
                         //ShowSettingContextMenu(ctx.Items.IndexOf(latencyMenu));
                     }
@@ -633,7 +622,7 @@ namespace Bmp.WinForms
 
             if (selectedDsdDevice == null)
             {
-                if (_sl?.State?.DecodeDsdToPcm == false)
+                if (_saveLoadService?.State?.DecodeDsdToPcm == false)
                 {
                     ctx.Items.Add("(未选择 DSD 输出设备)").Enabled = false;
                 }
@@ -674,8 +663,8 @@ namespace Bmp.WinForms
             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; };
+            dbgItem.DropDownItems.Add("清除选中 PCM 设备").Click += delegate { if (_saveLoadService is { State: { } }) _saveLoadService.State.SelectedPcmOutputDeviceId = null; };
+            dbgItem.DropDownItems.Add("清除选中 DSD 设备").Click += delegate { if (_saveLoadService is { State: { } }) _saveLoadService.State.SelectedDsdAsioOutputDeviceId = null; };
 
 #endif
         }
@@ -684,12 +673,6 @@ namespace Bmp.WinForms
 
         private void MainForm_Shown(object sender, EventArgs e)
         {
-            if (_serviceProvider == null) throw new InvalidOperationException($"Must set {nameof(ServiceProvider)} before show!");
-
-            _eventBus = _serviceProvider.GetRequiredService<IEventBus>();
-            _sl = _serviceProvider.GetRequiredService<SaveLoadService>();
-            _logger = _serviceProvider.GetRequiredService<ILogger<MainForm>>();
-
             //var lvi = new ListViewItem();
 
             //AlbumImageList.Images.Add(this.Icon);
@@ -699,17 +682,17 @@ namespace Bmp.WinForms
 
             MainListView.Items.Clear();
 
-            if (_sl != null)
+            if (_saveLoadService != null)
             {
-                if (_sl.State != null)
+                if (_saveLoadService.State != null)
                 {
-                    if (_sl.State.FormPosition.HasValue) Location = _sl.State.FormPosition.Value;
+                    if (_saveLoadService.State.FormPosition.HasValue) Location = _saveLoadService.State.FormPosition.Value;
                     Application.DoEvents();
-                    if (_sl.State.FormSize.HasValue) Size = _sl.State.FormSize.Value;
+                    if (_saveLoadService.State.FormSize.HasValue) Size = _saveLoadService.State.FormSize.Value;
 
-                    if (_sl.State.Playlist != null)
+                    if (_saveLoadService.State.Playlist != null)
                     {
-                        foreach (var playlistItem in _sl.State.Playlist)
+                        foreach (var playlistItem in _saveLoadService.State.Playlist)
                         {
                             var item = new ListViewItem();
                             for (var i = 0; i < MainListView.Columns.Count - 1; i++) item.SubItems.Add("");

+ 5 - 5
Bmp.WinForms/Program.cs

@@ -18,8 +18,6 @@ internal class Program
 
         var loggingIntoEventBus = new LoggingIntoEventBus();
 
-        var mainForm = new MainForm();
-
         var builder = Host.CreateDefaultBuilder();
 
         builder.ConfigureServices((ctx, services) =>
@@ -42,14 +40,16 @@ internal class Program
             //services.AddWindowsFormsBlazorWebView();
             services.AddAssemblyInject<Program>();
             //services.AddBootstrapBlazor();
-            services.AddSingleton(mainForm);
+            services.AddSingleton<MainForm>();
         });
 
         var host = builder.Build();
         host.Start();
-        mainForm.SetServiceProvider(host.Services);
+
+        var mainForm = host.Services.GetRequiredService<MainForm>();
+
         Application.Run(mainForm);
-        host.StopAsync().Wait();
 
+        host.StopAsync().Wait();
     }
 }