1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Added DevicesChanged-event to device providers

This commit is contained in:
Darth Affe 2023-05-08 21:28:55 +02:00
parent 4a0ae1a185
commit 7e72d3199b
4 changed files with 92 additions and 6 deletions

View File

@ -20,8 +20,13 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// <inheritdoc /> /// <inheritdoc />
public bool ThrowsExceptions { get; protected set; } public bool ThrowsExceptions { get; protected set; }
/// <summary>
/// The list of devices managed by this device-provider.
/// </summary>
protected List<IRGBDevice> InternalDevices { get; } = new();
/// <inheritdoc /> /// <inheritdoc />
public virtual IEnumerable<IRGBDevice> Devices { get; protected set; } = Enumerable.Empty<IRGBDevice>(); public virtual IReadOnlyList<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(InternalDevices);
/// <summary> /// <summary>
/// Gets the dictionary containing the registered update triggers. /// Gets the dictionary containing the registered update triggers.
@ -39,6 +44,9 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// <inheritdoc /> /// <inheritdoc />
public event EventHandler<ExceptionEventArgs>? Exception; public event EventHandler<ExceptionEventArgs>? Exception;
/// <inheritdoc />
public event EventHandler<DevicesChangedEventArgs>? DevicesChanged;
#endregion #endregion
#region Constructors #region Constructors
@ -67,7 +75,8 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
InitializeSDK(); InitializeSDK();
Devices = new ReadOnlyCollection<IRGBDevice>(GetLoadedDevices(loadFilter).ToList()); foreach (IRGBDevice device in GetLoadedDevices(loadFilter))
AddDevice(device);
foreach (IDeviceUpdateTrigger updateTrigger in UpdateTriggerMapping.Values) foreach (IDeviceUpdateTrigger updateTrigger in UpdateTriggerMapping.Values)
updateTrigger.Start(); updateTrigger.Start();
@ -168,11 +177,43 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
foreach (IRGBDevice device in Devices) foreach (IRGBDevice device in Devices)
device.Dispose(); device.Dispose();
Devices = Enumerable.Empty<IRGBDevice>(); List<IRGBDevice> devices = new(InternalDevices);
foreach (IRGBDevice device in devices)
RemoveDevice(device);
UpdateTriggerMapping.Clear(); UpdateTriggerMapping.Clear();
IsInitialized = false; IsInitialized = false;
} }
/// <summary>
/// Adds the provided device to the list of managed devices.
/// </summary>
/// <param name="device">The device to add.</param>
/// <returns><c>true</c> if the device was added successfully; otherwise <c>false</c>.</returns>
protected virtual bool AddDevice(IRGBDevice device)
{
if (InternalDevices.Contains(device)) return false;
InternalDevices.Add(device);
try { OnDevicesChanged(DevicesChangedEventArgs.CreateDevicesAddedArgs(device)); } catch { /* we don't want to throw due to bad event handlers */ }
return true;
}
/// <summary>
/// Removes the provided device from the list of managed devices.
/// </summary>
/// <param name="device">The device to remove.</param>
/// <returns><c>true</c> if the device was removed successfully; otherwise <c>false</c>.</returns>
protected virtual bool RemoveDevice(IRGBDevice device)
{
if (!InternalDevices.Remove(device)) return false;
try { OnDevicesChanged(DevicesChangedEventArgs.CreateDevicesRemovedArgs(device)); } catch { /* we don't want to throw due to bad event handlers */ }
return true;
}
/// <summary> /// <summary>
/// Triggers the <see cref="Exception"/>-event and throws the specified exception if <see cref="ThrowsExceptions"/> is true and it is not overriden in the event. /// Triggers the <see cref="Exception"/>-event and throws the specified exception if <see cref="ThrowsExceptions"/> is true and it is not overriden in the event.
/// </summary> /// </summary>
@ -188,11 +229,17 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
} }
/// <summary> /// <summary>
/// Throws the <see cref="Exception"/> event. /// Throws the <see cref="Exception"/>.event.
/// </summary> /// </summary>
/// <param name="args">The parameters passed to the event.</param> /// <param name="args">The parameters passed to the event.</param>
protected virtual void OnException(ExceptionEventArgs args) => Exception?.Invoke(this, args); protected virtual void OnException(ExceptionEventArgs args) => Exception?.Invoke(this, args);
/// <summary>
/// Throws the <see cref="DevicesChanged"/>-event.
/// </summary>
/// <param name="args">The parameters passed to the event.</param>
protected virtual void OnDevicesChanged(DevicesChangedEventArgs args) => DevicesChanged?.Invoke(this, args);
/// <inheritdoc /> /// <inheritdoc />
public virtual void Dispose() public virtual void Dispose()
{ {

View File

@ -29,7 +29,7 @@ public interface IRGBDeviceProvider : IDisposable
/// <summary> /// <summary>
/// Gets a collection of <see cref="IRGBDevice"/> loaded by this <see cref="IRGBDeviceProvider"/>. /// Gets a collection of <see cref="IRGBDevice"/> loaded by this <see cref="IRGBDeviceProvider"/>.
/// </summary> /// </summary>
IEnumerable<IRGBDevice> Devices { get; } IReadOnlyList<IRGBDevice> Devices { get; }
/// <summary> /// <summary>
/// Gets a collection <see cref="IDeviceUpdateTrigger"/> registered to this device provider. /// Gets a collection <see cref="IDeviceUpdateTrigger"/> registered to this device provider.
@ -45,6 +45,11 @@ public interface IRGBDeviceProvider : IDisposable
/// </summary> /// </summary>
event EventHandler<ExceptionEventArgs>? Exception; event EventHandler<ExceptionEventArgs>? Exception;
/// <summary>
/// Occures when the devices provided by this device provider changed.
/// </summary>
event EventHandler<DevicesChangedEventArgs>? DevicesChanged;
#endregion #endregion
#region Methods #region Methods

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
namespace RGB.NET.Core;
public sealed class DevicesChangedEventArgs : EventArgs
{
#region Properties & Fields
public IList<IRGBDevice> Added { get; }
public IList<IRGBDevice> Removed { get; }
#endregion
#region Constructors
private DevicesChangedEventArgs(IList<IRGBDevice> added, IList<IRGBDevice> removed)
{
this.Added = added;
this.Removed = removed;
}
#endregion
#region Methods
public static DevicesChangedEventArgs CreateDevicesAddedArgs(params IRGBDevice[] addedDevices) => CreateDevicesAddedArgs((IEnumerable<IRGBDevice>)addedDevices);
public static DevicesChangedEventArgs CreateDevicesAddedArgs(IEnumerable<IRGBDevice> addedDevices) => new(new List<IRGBDevice>(addedDevices), new List<IRGBDevice>());
public static DevicesChangedEventArgs CreateDevicesRemovedArgs(params IRGBDevice[] removedDevices) => CreateDevicesRemovedArgs((IEnumerable<IRGBDevice>)removedDevices);
public static DevicesChangedEventArgs CreateDevicesRemovedArgs(IEnumerable<IRGBDevice> removedDevices) => new(new List<IRGBDevice>(), new List<IRGBDevice>(removedDevices));
#endregion
}

View File

@ -161,7 +161,7 @@ public sealed class OpenRGBDeviceProvider : AbstractRGBDeviceProvider
_clients.Clear(); _clients.Clear();
DeviceDefinitions.Clear(); DeviceDefinitions.Clear();
Devices = Enumerable.Empty<IRGBDevice>();
} }
#endregion #endregion
} }