1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Improved device-loading in the visualizer

This commit is contained in:
Darth Affe 2017-08-03 19:01:13 +02:00
parent 59e0344c68
commit 9a59791721
4 changed files with 76 additions and 18 deletions

View File

@ -1,4 +1,7 @@
using System; // ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization; using System.Globalization;
@ -16,10 +19,11 @@ namespace RGB.NET.Devices.CoolerMaster
{ {
#region Properties & Fields #region Properties & Fields
private static CoolerMasterDeviceProvider _instance;
/// <summary> /// <summary>
/// Gets the singleton <see cref="CoolerMasterDeviceProvider"/> instance. /// Gets the singleton <see cref="CoolerMasterDeviceProvider"/> instance.
/// </summary> /// </summary>
public static CoolerMasterDeviceProvider Instance { get; } = new CoolerMasterDeviceProvider(); public static CoolerMasterDeviceProvider Instance => _instance ?? new CoolerMasterDeviceProvider();
/// <summary> /// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications. /// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
@ -60,8 +64,15 @@ namespace RGB.NET.Devices.CoolerMaster
#region Constructors #region Constructors
private CoolerMasterDeviceProvider() /// <summary>
{ } /// Initializes a new instance of the <see cref="CoolerMasterDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public CoolerMasterDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instanc of type {nameof(CoolerMasterDeviceProvider)}");
_instance = this;
}
#endregion #endregion

View File

@ -1,6 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global // ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global // ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -17,10 +18,11 @@ namespace RGB.NET.Devices.Corsair
{ {
#region Properties & Fields #region Properties & Fields
private static CorsairDeviceProvider _instance;
/// <summary> /// <summary>
/// Gets the singleton <see cref="CorsairDeviceProvider"/> instance. /// Gets the singleton <see cref="CorsairDeviceProvider"/> instance.
/// </summary> /// </summary>
public static CorsairDeviceProvider Instance { get; } = new CorsairDeviceProvider(); public static CorsairDeviceProvider Instance => _instance ?? new CorsairDeviceProvider();
/// <summary> /// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications. /// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
@ -66,8 +68,15 @@ namespace RGB.NET.Devices.Corsair
#region Constructors #region Constructors
private CorsairDeviceProvider() /// <summary>
{ } /// Initializes a new instance of the <see cref="CorsairDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public CorsairDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instanc of type {nameof(CorsairDeviceProvider)}");
_instance = this;
}
#endregion #endregion

View File

@ -1,4 +1,7 @@
using System; // ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Globalization; using System.Globalization;
@ -15,10 +18,11 @@ namespace RGB.NET.Devices.Logitech
{ {
#region Properties & Fields #region Properties & Fields
private static LogitechDeviceProvider _instance;
/// <summary> /// <summary>
/// Gets the singleton <see cref="LogitechDeviceProvider"/> instance. /// Gets the singleton <see cref="LogitechDeviceProvider"/> instance.
/// </summary> /// </summary>
public static LogitechDeviceProvider Instance { get; } = new LogitechDeviceProvider(); public static LogitechDeviceProvider Instance => _instance ?? new LogitechDeviceProvider();
/// <summary> /// <summary>
/// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications. /// Gets a modifiable list of paths used to find the native SDK-dlls for x86 applications.
@ -55,8 +59,15 @@ namespace RGB.NET.Devices.Logitech
#region Constructors #region Constructors
private LogitechDeviceProvider() /// <summary>
{ } /// Initializes a new instance of the <see cref="LogitechDeviceProvider"/> class.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if this constructor is called even if there is already an instance of this class.</exception>
public LogitechDeviceProvider()
{
if (_instance != null) throw new InvalidOperationException($"There can be only one instanc of type {nameof(LogitechDeviceProvider)}");
_instance = this;
}
#endregion #endregion

View File

@ -1,4 +1,5 @@
using System.Windows; using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using RGB.NET.Core; using RGB.NET.Core;
@ -21,6 +22,9 @@ namespace RGB.NET.WPF.Controls
private RGBSurface _surface; private RGBSurface _surface;
private Canvas _canvas; private Canvas _canvas;
//TODO DarthAffe 17.06.2017: This is ugly - redesign how device connect/disconnect is generally handled!
private readonly List<IRGBDevice> _newDevices = new List<IRGBDevice>();
#endregion #endregion
#region Constructors #region Constructors
@ -29,22 +33,36 @@ namespace RGB.NET.WPF.Controls
/// Initializes a new instance of the <see cref="RGBSurfaceVisualizer"/> class. /// Initializes a new instance of the <see cref="RGBSurfaceVisualizer"/> class.
/// </summary> /// </summary>
public RGBSurfaceVisualizer() public RGBSurfaceVisualizer()
{
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{ {
_surface = RGBSurface.Instance; _surface = RGBSurface.Instance;
_surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; _surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
foreach (IRGBDevice device in _surface.Devices) foreach (IRGBDevice device in _surface.Devices)
AddDevice(device); _newDevices.Add(device);
UpdateSurface();
}
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
{
_surface.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged;
_canvas?.Children.Clear();
_newDevices.Clear();
} }
private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args)
{ {
if (args.DeviceAdded) if (args.DeviceAdded)
foreach (IRGBDevice device in args.Devices) foreach (IRGBDevice device in args.Devices)
AddDevice(device); _newDevices.Add(device);
_canvas.Width = _surface.SurfaceRectangle.Size.Width; UpdateSurface();
_canvas.Height = _surface.SurfaceRectangle.Size.Height;
} }
#endregion #endregion
@ -54,12 +72,21 @@ namespace RGB.NET.WPF.Controls
/// <inheritdoc /> /// <inheritdoc />
public override void OnApplyTemplate() public override void OnApplyTemplate()
{ {
_canvas?.Children.Clear();
_canvas = (Canvas)GetTemplateChild(PART_CANVAS); _canvas = (Canvas)GetTemplateChild(PART_CANVAS);
UpdateSurface();
} }
private void AddDevice(IRGBDevice device) private void UpdateSurface()
{ {
_canvas.Children.Add(new RGBDeviceVisualizer { Device = device }); if ((_canvas == null) || (_newDevices.Count == 0)) return;
foreach (IRGBDevice device in _newDevices)
_canvas.Children.Add(new RGBDeviceVisualizer { Device = device });
_newDevices.Clear();
_canvas.Width = _surface.SurfaceRectangle.Size.Width;
_canvas.Height = _surface.SurfaceRectangle.Size.Height;
} }
#endregion #endregion