1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Device settings - Don't add/remove enabled/disabled devices

This commit is contained in:
Robert 2021-11-15 22:37:02 +01:00
parent 09d202cf24
commit 291d71edcb
4 changed files with 43 additions and 34 deletions

View File

@ -92,6 +92,13 @@ namespace Artemis.UI.Avalonia.Shared.Controls
{ {
_deviceImage?.Dispose(); _deviceImage?.Dispose();
_deviceImage = null; _deviceImage = null;
if (Device != null)
{
Device.RgbDevice.PropertyChanged -= DevicePropertyChanged;
Device.DeviceUpdated -= DeviceUpdated;
}
base.OnDetachedFromVisualTree(e); base.OnDetachedFromVisualTree(e);
} }
@ -109,12 +116,6 @@ namespace Artemis.UI.Avalonia.Shared.Controls
InvalidateVisual(); InvalidateVisual();
} }
private void UpdateTransform()
{
InvalidateVisual();
InvalidateMeasure();
}
private Rect MeasureDevice() private Rect MeasureDevice()
{ {
if (Device == null) if (Device == null)
@ -150,12 +151,12 @@ namespace Artemis.UI.Avalonia.Shared.Controls
private void DevicePropertyChanged(object? sender, PropertyChangedEventArgs e) private void DevicePropertyChanged(object? sender, PropertyChangedEventArgs e)
{ {
Dispatcher.UIThread.Post(SetupForDevice); Dispatcher.UIThread.Post(SetupForDevice, DispatcherPriority.Background);
} }
private void DeviceUpdated(object? sender, EventArgs e) private void DeviceUpdated(object? sender, EventArgs e)
{ {
Dispatcher.UIThread.Post(SetupForDevice); Dispatcher.UIThread.Post(SetupForDevice, DispatcherPriority.Background);
} }
#region Properties #region Properties
@ -239,21 +240,20 @@ namespace Artemis.UI.Avalonia.Shared.Controls
_highlightedLeds = new List<DeviceVisualizerLed>(); _highlightedLeds = new List<DeviceVisualizerLed>();
_dimmedLeds = new List<DeviceVisualizerLed>(); _dimmedLeds = new List<DeviceVisualizerLed>();
if (Device == null)
return;
if (_oldDevice != null) if (_oldDevice != null)
{ {
Device.RgbDevice.PropertyChanged -= DevicePropertyChanged; _oldDevice.RgbDevice.PropertyChanged -= DevicePropertyChanged;
Device.DeviceUpdated -= DeviceUpdated; _oldDevice.DeviceUpdated -= DeviceUpdated;
} }
_oldDevice = Device; _oldDevice = Device;
if (Device == null)
return;
_deviceBounds = MeasureDevice(); _deviceBounds = MeasureDevice();
Device.RgbDevice.PropertyChanged += DevicePropertyChanged; Device.RgbDevice.PropertyChanged += DevicePropertyChanged;
Device.DeviceUpdated += DeviceUpdated; Device.DeviceUpdated += DeviceUpdated;
UpdateTransform();
// Create all the LEDs // Create all the LEDs
foreach (ArtemisLed artemisLed in Device.Leds) foreach (ArtemisLed artemisLed in Device.Leds)
@ -263,27 +263,27 @@ namespace Artemis.UI.Avalonia.Shared.Controls
ArtemisDevice? device = Device; ArtemisDevice? device = Device;
Task.Run(() => 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 IDrawingContextImpl context = renderTargetBitmap.CreateDrawingContext(new ImmediateRenderer(this));
using Bitmap bitmap = new(device.Layout.Image.LocalPath); using Bitmap bitmap = new(device.Layout.Image.LocalPath);
context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality); context.DrawBitmap(bitmap.PlatformImpl, 1, new Rect(bitmap.Size), new Rect(renderTargetBitmap.Size), BitmapInterpolationMode.HighQuality);
foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds) foreach (DeviceVisualizerLed deviceVisualizerLed in _deviceVisualizerLeds)
deviceVisualizerLed.DrawBitmap(context); deviceVisualizerLed.DrawBitmap(context);
_deviceImage = renderTargetBitmap; _deviceImage = renderTargetBitmap;
Dispatcher.UIThread.Post(InvalidateMeasure); Dispatcher.UIThread.Post(InvalidateMeasure);
} }
catch catch
{ {
// ignored // ignored
}
} }
}); });
} }

View File

@ -5,6 +5,7 @@ using Artemis.UI.Avalonia.Ninject.Factories;
using Artemis.UI.Avalonia.Screens.Settings.Tabs.ViewModels; using Artemis.UI.Avalonia.Screens.Settings.Tabs.ViewModels;
using Artemis.UI.Avalonia.Shared; using Artemis.UI.Avalonia.Shared;
using Artemis.UI.Avalonia.Shared.Services.Interfaces; using Artemis.UI.Avalonia.Shared.Services.Interfaces;
using Avalonia.Threading;
using Humanizer; using Humanizer;
using ReactiveUI; using ReactiveUI;
using RGB.NET.Core; using RGB.NET.Core;
@ -45,7 +46,7 @@ namespace Artemis.UI.Avalonia.Screens.Device.ViewModels
public bool IsDeviceEnabled public bool IsDeviceEnabled
{ {
get => Device.IsEnabled; get => Device.IsEnabled;
set => Task.Run(() => UpdateIsDeviceEnabled(value)); set => Dispatcher.UIThread.InvokeAsync(async () => await UpdateIsDeviceEnabled(value));
} }
public void IdentifyDevice() public void IdentifyDevice()

View File

@ -11,9 +11,9 @@
<Grid RowDefinitions="140,*,Auto"> <Grid RowDefinitions="140,*,Auto">
<Rectangle Grid.Row="0"> <Rectangle Grid.Row="0">
<Rectangle.Fill> <Rectangle.Fill>
<VisualBrush TileMode="Tile" Stretch="Uniform" DestinationRect="0,0,25,25"> <VisualBrush TileMode="Tile" Stretch="Uniform" DestinationRect="0,0,15,15">
<VisualBrush.Visual> <VisualBrush.Visual>
<Grid Width="25" Height="25" RowDefinitions="*,*" ColumnDefinitions="*,*"> <Grid Width="15" Height="15" RowDefinitions="*,*" ColumnDefinitions="*,*">
<Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" Opacity="0.15" /> <Rectangle Grid.Row="0" Grid.Column="0" Fill="Black" Opacity="0.15" />
<Rectangle Grid.Row="0" Grid.Column="1" /> <Rectangle Grid.Row="0" Grid.Column="1" />
<Rectangle Grid.Row="1" Grid.Column="0" /> <Rectangle Grid.Row="1" Grid.Column="0" />

View File

@ -74,11 +74,19 @@ namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.ViewModels
private void AddDevice(ArtemisDevice device) 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)); Devices.Add(_deviceVmFactory.DeviceSettingsViewModel(device, this));
} }
private void RemoveDevice(ArtemisDevice device) 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); DeviceSettingsViewModel? viewModel = Devices.FirstOrDefault(i => i.Device == device);
if (viewModel != null) if (viewModel != null)
Devices.Remove(viewModel); Devices.Remove(viewModel);