From 0a121b7efc2ee9b70bcf6e4921549a898bf38ac7 Mon Sep 17 00:00:00 2001 From: Felix Ruff Date: Sun, 29 Mar 2026 12:00:38 +0000 Subject: [PATCH] Fix device selection not persisting across restarts - Replace DeviceName.GetHashCode() with SHA256 in HidppDevice.cs; GetHashCode() is randomized per-process in .NET Core, causing the saved device ID to never match the newly computed one on restart. - Load previously selected device placeholders before subscribing to device messages to close a startup race condition. - In OnInitMessage, pre-check newly discovered devices that are already in SelectedDevices, as a belt-and-suspenders fix. Co-Authored-By: Claude Sonnet 4.6 --- LGSTrayHID/HidppDevice.cs | 6 ++++-- LGSTrayUI/LogiDeviceCollection.cs | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/LGSTrayHID/HidppDevice.cs b/LGSTrayHID/HidppDevice.cs index 8acca00..cbd3043 100644 --- a/LGSTrayHID/HidppDevice.cs +++ b/LGSTrayHID/HidppDevice.cs @@ -151,8 +151,10 @@ private async Task InitPopulateAsync() } else { - // Device does not have a serial identifier the device name as a hash identifier - Identifier = $"{DeviceName.GetHashCode():X04}"; + // Device does not have a serial identifier, use a stable hash of the device name + var hashBytes = System.Security.Cryptography.SHA256.HashData( + System.Text.Encoding.UTF8.GetBytes(DeviceName)); + Identifier = Convert.ToHexString(hashBytes, 0, 4); } #if DEBUG diff --git a/LGSTrayUI/LogiDeviceCollection.cs b/LGSTrayUI/LogiDeviceCollection.cs index 6c84597..2901d1d 100644 --- a/LGSTrayUI/LogiDeviceCollection.cs +++ b/LGSTrayUI/LogiDeviceCollection.cs @@ -29,6 +29,8 @@ ISubscriber subscriber _logiDeviceViewModelFactory = logiDeviceViewModelFactory; _subscriber = subscriber; + LoadPreviouslySelectedDevices(); + _subscriber.Subscribe(x => { if (x is InitMessage initMessage) @@ -40,8 +42,6 @@ ISubscriber subscriber OnUpdateMessage(updateMessage); } }); - - LoadPreviouslySelectedDevices(); } private void LoadPreviouslySelectedDevices() @@ -81,7 +81,12 @@ public void OnInitMessage(InitMessage initMessage) return; } - dev = _logiDeviceViewModelFactory.CreateViewModel((x) => x.UpdateState(initMessage)); + bool wasSelected = _userSettings.SelectedDevices?.Contains(initMessage.deviceId) ?? false; + dev = _logiDeviceViewModelFactory.CreateViewModel((x) => + { + x.UpdateState(initMessage); + x.IsChecked = wasSelected; + }); Application.Current.Dispatcher.BeginInvoke(() => Devices.Add(dev)); }