From 291d71edcb54a91c990724b0d544c4200dfdcc89 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 15 Nov 2021 22:37:02 +0100 Subject: [PATCH] Device settings - Don't add/remove enabled/disabled devices --- .../Controls/DeviceVisualizer.cs | 62 +++++++++---------- .../ViewModels/DeviceSettingsViewModel.cs | 3 +- .../Device/Views/DeviceSettingsView.axaml | 4 +- .../Tabs/ViewModels/DevicesTabViewModel.cs | 8 +++ 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/Artemis.UI.Avalonia.Shared/Controls/DeviceVisualizer.cs b/src/Artemis.UI.Avalonia.Shared/Controls/DeviceVisualizer.cs index 543d83156..9c6a592e1 100644 --- a/src/Artemis.UI.Avalonia.Shared/Controls/DeviceVisualizer.cs +++ b/src/Artemis.UI.Avalonia.Shared/Controls/DeviceVisualizer.cs @@ -92,6 +92,13 @@ namespace Artemis.UI.Avalonia.Shared.Controls { _deviceImage?.Dispose(); _deviceImage = null; + + if (Device != null) + { + Device.RgbDevice.PropertyChanged -= DevicePropertyChanged; + Device.DeviceUpdated -= DeviceUpdated; + } + base.OnDetachedFromVisualTree(e); } @@ -109,12 +116,6 @@ namespace Artemis.UI.Avalonia.Shared.Controls InvalidateVisual(); } - private void UpdateTransform() - { - InvalidateVisual(); - InvalidateMeasure(); - } - private Rect MeasureDevice() { if (Device == null) @@ -150,12 +151,12 @@ namespace Artemis.UI.Avalonia.Shared.Controls private void DevicePropertyChanged(object? sender, PropertyChangedEventArgs e) { - Dispatcher.UIThread.Post(SetupForDevice); + Dispatcher.UIThread.Post(SetupForDevice, DispatcherPriority.Background); } private void DeviceUpdated(object? sender, EventArgs e) { - Dispatcher.UIThread.Post(SetupForDevice); + Dispatcher.UIThread.Post(SetupForDevice, DispatcherPriority.Background); } #region Properties @@ -239,21 +240,20 @@ namespace Artemis.UI.Avalonia.Shared.Controls _highlightedLeds = new List(); _dimmedLeds = new List(); - if (Device == null) - return; - if (_oldDevice != null) { - Device.RgbDevice.PropertyChanged -= DevicePropertyChanged; - Device.DeviceUpdated -= DeviceUpdated; + _oldDevice.RgbDevice.PropertyChanged -= DevicePropertyChanged; + _oldDevice.DeviceUpdated -= DeviceUpdated; } _oldDevice = Device; + if (Device == null) + return; + _deviceBounds = MeasureDevice(); Device.RgbDevice.PropertyChanged += DevicePropertyChanged; Device.DeviceUpdated += DeviceUpdated; - UpdateTransform(); // Create all the LEDs foreach (ArtemisLed artemisLed in Device.Leds) @@ -263,27 +263,27 @@ namespace Artemis.UI.Avalonia.Shared.Controls ArtemisDevice? device = Device; Task.Run(() => { - if (device.Layout?.Image != null && File.Exists(device.Layout.Image.LocalPath)) + if (device.Layout?.Image == null || !File.Exists(device.Layout.Image.LocalPath)) + return; + + try { - try - { - // Create a bitmap that'll be used to render the device and LED images just once - RenderTargetBitmap renderTargetBitmap = new(new PixelSize((int) device.RgbDevice.Size.Width * 4, (int) device.RgbDevice.Size.Height * 4)); + // Create a bitmap that'll be used to render the device and LED images just once + RenderTargetBitmap renderTargetBitmap = new(new PixelSize((int) device.RgbDevice.Size.Width * 4, (int) device.RgbDevice.Size.Height * 4)); - using IDrawingContextImpl context = renderTargetBitmap.CreateDrawingContext(new ImmediateRenderer(this)); - using Bitmap bitmap = new(device.Layout.Image.LocalPath); - context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality); - foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds) - deviceVisualizerLed.DrawBitmap(context); + using IDrawingContextImpl context = renderTargetBitmap.CreateDrawingContext(new ImmediateRenderer(this)); + using Bitmap bitmap = new(device.Layout.Image.LocalPath); + context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality); + foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds) + deviceVisualizerLed.DrawBitmap(context); - _deviceImage = renderTargetBitmap; + _deviceImage = renderTargetBitmap; - Dispatcher.UIThread.Post(InvalidateMeasure); - } - catch - { - // ignored - } + Dispatcher.UIThread.Post(InvalidateMeasure); + } + catch + { + // ignored } }); } diff --git a/src/Artemis.UI.Avalonia/Screens/Device/ViewModels/DeviceSettingsViewModel.cs b/src/Artemis.UI.Avalonia/Screens/Device/ViewModels/DeviceSettingsViewModel.cs index 12fbe647a..a5347253c 100644 --- a/src/Artemis.UI.Avalonia/Screens/Device/ViewModels/DeviceSettingsViewModel.cs +++ b/src/Artemis.UI.Avalonia/Screens/Device/ViewModels/DeviceSettingsViewModel.cs @@ -5,6 +5,7 @@ using Artemis.UI.Avalonia.Ninject.Factories; using Artemis.UI.Avalonia.Screens.Settings.Tabs.ViewModels; using Artemis.UI.Avalonia.Shared; using Artemis.UI.Avalonia.Shared.Services.Interfaces; +using Avalonia.Threading; using Humanizer; using ReactiveUI; using RGB.NET.Core; @@ -45,7 +46,7 @@ namespace Artemis.UI.Avalonia.Screens.Device.ViewModels public bool IsDeviceEnabled { get => Device.IsEnabled; - set => Task.Run(() => UpdateIsDeviceEnabled(value)); + set => Dispatcher.UIThread.InvokeAsync(async () => await UpdateIsDeviceEnabled(value)); } public void IdentifyDevice() diff --git a/src/Artemis.UI.Avalonia/Screens/Device/Views/DeviceSettingsView.axaml b/src/Artemis.UI.Avalonia/Screens/Device/Views/DeviceSettingsView.axaml index 8bbc47641..338de63ce 100644 --- a/src/Artemis.UI.Avalonia/Screens/Device/Views/DeviceSettingsView.axaml +++ b/src/Artemis.UI.Avalonia/Screens/Device/Views/DeviceSettingsView.axaml @@ -11,9 +11,9 @@ - + - + diff --git a/src/Artemis.UI.Avalonia/Screens/Settings/Tabs/ViewModels/DevicesTabViewModel.cs b/src/Artemis.UI.Avalonia/Screens/Settings/Tabs/ViewModels/DevicesTabViewModel.cs index 39fdafccc..4f53e7142 100644 --- a/src/Artemis.UI.Avalonia/Screens/Settings/Tabs/ViewModels/DevicesTabViewModel.cs +++ b/src/Artemis.UI.Avalonia/Screens/Settings/Tabs/ViewModels/DevicesTabViewModel.cs @@ -74,11 +74,19 @@ namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.ViewModels private void AddDevice(ArtemisDevice device) { + // If the device was only enabled, don't add it + if (Devices.Any(d => d.Device == device)) + return; + Devices.Add(_deviceVmFactory.DeviceSettingsViewModel(device, this)); } private void RemoveDevice(ArtemisDevice device) { + // If the device was only disabled don't remove it + if (_rgbService.Devices.Contains(device)) + return; + DeviceSettingsViewModel? viewModel = Devices.FirstOrDefault(i => i.Device == device); if (viewModel != null) Devices.Remove(viewModel);