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

Merge pull request #321 from DarthAffe/Core/DevicesChangedEvent

Core/devices changed event
This commit is contained in:
DarthAffe 2023-06-03 21:29:56 +02:00 committed by GitHub
commit 83bdfea2f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 6 deletions

View File

@ -20,8 +20,13 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// <inheritdoc />
public bool ThrowsExceptions { get; protected set; }
/// <summary>
/// The list of devices managed by this device-provider.
/// </summary>
protected List<IRGBDevice> InternalDevices { get; } = new();
/// <inheritdoc />
public virtual IEnumerable<IRGBDevice> Devices { get; protected set; } = Enumerable.Empty<IRGBDevice>();
public virtual IReadOnlyList<IRGBDevice> Devices => new ReadOnlyCollection<IRGBDevice>(InternalDevices);
/// <summary>
/// Gets the dictionary containing the registered update triggers.
@ -39,6 +44,9 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
/// <inheritdoc />
public event EventHandler<ExceptionEventArgs>? Exception;
/// <inheritdoc />
public event EventHandler<DevicesChangedEventArgs>? DevicesChanged;
#endregion
#region Constructors
@ -67,7 +75,8 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
InitializeSDK();
Devices = new ReadOnlyCollection<IRGBDevice>(GetLoadedDevices(loadFilter).ToList());
foreach (IRGBDevice device in GetLoadedDevices(loadFilter))
AddDevice(device);
foreach (IDeviceUpdateTrigger updateTrigger in UpdateTriggerMapping.Values)
updateTrigger.Start();
@ -168,11 +177,43 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
foreach (IRGBDevice device in Devices)
device.Dispose();
Devices = Enumerable.Empty<IRGBDevice>();
List<IRGBDevice> devices = new(InternalDevices);
foreach (IRGBDevice device in devices)
RemoveDevice(device);
UpdateTriggerMapping.Clear();
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>
/// 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>
@ -188,11 +229,17 @@ public abstract class AbstractRGBDeviceProvider : IRGBDeviceProvider
}
/// <summary>
/// Throws the <see cref="Exception"/> event.
/// Throws the <see cref="Exception"/>.event.
/// </summary>
/// <param name="args">The parameters passed to the event.</param>
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 />
public virtual void Dispose()
{

View File

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

View File

@ -0,0 +1,36 @@
using System;
namespace RGB.NET.Core;
public sealed class DevicesChangedEventArgs : EventArgs
{
#region Properties & Fields
public IRGBDevice Device { get; }
public DevicesChangedAction Action { get; }
#endregion
#region Constructors
public DevicesChangedEventArgs(IRGBDevice device, DevicesChangedAction action)
{
this.Device = device;
this.Action = action;
}
#endregion
#region Methods
public static DevicesChangedEventArgs CreateDevicesAddedArgs(IRGBDevice addedDevice) => new(addedDevice, DevicesChangedAction.Added);
public static DevicesChangedEventArgs CreateDevicesRemovedArgs(IRGBDevice removedDevice) => new(removedDevice, DevicesChangedAction.Removed);
#endregion
public enum DevicesChangedAction
{
Added,
Removed
}
}

View File

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