1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +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.ObjectModel;
using System.Globalization;
@ -16,10 +19,11 @@ namespace RGB.NET.Devices.CoolerMaster
{
#region Properties & Fields
private static CoolerMasterDeviceProvider _instance;
/// <summary>
/// Gets the singleton <see cref="CoolerMasterDeviceProvider"/> instance.
/// </summary>
public static CoolerMasterDeviceProvider Instance { get; } = new CoolerMasterDeviceProvider();
public static CoolerMasterDeviceProvider Instance => _instance ?? new CoolerMasterDeviceProvider();
/// <summary>
/// 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
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

View File

@ -1,6 +1,7 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
@ -17,10 +18,11 @@ namespace RGB.NET.Devices.Corsair
{
#region Properties & Fields
private static CorsairDeviceProvider _instance;
/// <summary>
/// Gets the singleton <see cref="CorsairDeviceProvider"/> instance.
/// </summary>
public static CorsairDeviceProvider Instance { get; } = new CorsairDeviceProvider();
public static CorsairDeviceProvider Instance => _instance ?? new CorsairDeviceProvider();
/// <summary>
/// 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
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

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.ObjectModel;
using System.Globalization;
@ -15,10 +18,11 @@ namespace RGB.NET.Devices.Logitech
{
#region Properties & Fields
private static LogitechDeviceProvider _instance;
/// <summary>
/// Gets the singleton <see cref="LogitechDeviceProvider"/> instance.
/// </summary>
public static LogitechDeviceProvider Instance { get; } = new LogitechDeviceProvider();
public static LogitechDeviceProvider Instance => _instance ?? new LogitechDeviceProvider();
/// <summary>
/// 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
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

View File

@ -1,4 +1,5 @@
using System.Windows;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using RGB.NET.Core;
@ -21,6 +22,9 @@ namespace RGB.NET.WPF.Controls
private RGBSurface _surface;
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
#region Constructors
@ -29,22 +33,36 @@ namespace RGB.NET.WPF.Controls
/// Initializes a new instance of the <see cref="RGBSurfaceVisualizer"/> class.
/// </summary>
public RGBSurfaceVisualizer()
{
this.Loaded += OnLoaded;
this.Unloaded += OnUnloaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_surface = RGBSurface.Instance;
_surface.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged;
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)
{
if (args.DeviceAdded)
foreach (IRGBDevice device in args.Devices)
AddDevice(device);
_newDevices.Add(device);
_canvas.Width = _surface.SurfaceRectangle.Size.Width;
_canvas.Height = _surface.SurfaceRectangle.Size.Height;
UpdateSurface();
}
#endregion
@ -54,12 +72,21 @@ namespace RGB.NET.WPF.Controls
/// <inheritdoc />
public override void OnApplyTemplate()
{
_canvas?.Children.Clear();
_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