diff --git a/MultiTerm.Protocols/UsbHid/UsbHidDeviceInfo.cs b/MultiTerm.Protocols/UsbHid/UsbHidDeviceInfo.cs index fe13112..3ddc6fc 100644 --- a/MultiTerm.Protocols/UsbHid/UsbHidDeviceInfo.cs +++ b/MultiTerm.Protocols/UsbHid/UsbHidDeviceInfo.cs @@ -1,6 +1,6 @@ namespace MultiTerm.Protocols.UsbHid; -public class UsbHidDeviceInfo +public class UsbHidDeviceInfo : ICloneable, IEquatable { public string VendorId { get; internal set; } = string.Empty; @@ -9,4 +9,30 @@ public class UsbHidDeviceInfo public string Manufacturer { get; internal set; } = string.Empty; public string SerialNumber { get; internal set; } = string.Empty; + + #region ICloneable Implementation + public object Clone() + { + return base.MemberwiseClone(); + } + #endregion + + #region IEquatable Implementation + public bool Equals(UsbHidDeviceInfo? other) + { + // null cases + if (other == null && this == null) + { return true; } + if (other == null && this != null) + { return false; } + + // otherwise if vendor id, product id and serial number match => they are equal + if (this.VendorId == other!.VendorId && + this.ProductId == other!.ProductId && + this.SerialNumber == other!.SerialNumber) + { return true; } + else + { return false; } + } + #endregion } diff --git a/MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewModel.cs b/MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewModel.cs index d5b28fc..2d79714 100644 --- a/MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewModel.cs +++ b/MultiTerm.Protocols/UsbHid/UsbHidProtocolSettingsViewModel.cs @@ -33,9 +33,30 @@ public partial class UsbHidProtocolSettingsViewModel : ProtocolSettingsViewModel [RelayCommand] private void ReloadDevices() { + UsbHidDeviceInfo? previouslySelectedDevice = null; + + // if any device is selected => temporarily store it + if (this.SelectedDevice != null) + { + // shallow copy of the device + previouslySelectedDevice = (UsbHidDeviceInfo)this.SelectedDevice.Clone(); + } + + // reload collection this.Devices = new ObservableCollection(UsbHidProtocol.GetDevices()); - // select first deivce, if none is selected yet and there are some available + // select previously selected device again if it still exists + if (previouslySelectedDevice != null) + { + var equalDevices = this.Devices.Where(d => d.Equals(previouslySelectedDevice)); + // exactly one match found => take as selected device and quit method + if(equalDevices.Count() == 1) + { + this.SelectedDevice = equalDevices.First(); + } + } + + // otherwise select first device, if none is selected yet and there are some available if (this.SelectedDevice == null && this.Devices.Any()) { this.SelectedDevice = this.Devices.First(); diff --git a/MultiTerm.Wpf/View/SettingsView/SerialSettingsView.xaml b/MultiTerm.Wpf/View/SettingsView/SerialSettingsView.xaml index a89d301..e9e62d3 100644 --- a/MultiTerm.Wpf/View/SettingsView/SerialSettingsView.xaml +++ b/MultiTerm.Wpf/View/SettingsView/SerialSettingsView.xaml @@ -7,6 +7,7 @@ xmlns:local="clr-namespace:MultiTerm.Wpf.View.SettingsView" xmlns:conv="clr-namespace:MultiTerm.Wpf.ValueConverters" xmlns:serial_protocol="clr-namespace:MultiTerm.Protocols.Serial;assembly=MultiTerm.Protocols" + xmlns:XamlBehavioursWpf="http://schemas.microsoft.com/xaml/behaviors" mc:Ignorable="d" d:DesignHeight="50" d:DesignWidth="800"> @@ -43,16 +44,39 @@ - + + + + + + + +