StreamDeckSharp/OpenMacroBoard.SDK/DisconnectReplayAdapter.cs
2026-08-03 22:34:16 +02:00

55 lines
1.7 KiB
C#

using System.Collections.Generic;
namespace OpenMacroBoard.SDK
{
/// <summary>
/// A <see cref="IMacroBoard"/> adapter that replays brightness and key bitmaps if a device is disconnected.
/// </summary>
public class DisconnectReplayAdapter : MacroBoardAdapter
{
private readonly Dictionary<int, KeyBitmap> mostRecentKeyBitmaps = new();
private byte? mostRecentBrightness = null;
/// <summary>
/// Initializes a new instance of the <see cref="DisconnectReplayAdapter"/> class.
/// </summary>
public DisconnectReplayAdapter(IMacroBoard macroBoard)
: base(macroBoard)
{
ConnectionStateChanged += ReplayEventsForConnectionStateChange;
}
/// <inheritdoc/>
public override void SetBrightness(byte percent)
{
mostRecentBrightness = percent;
base.SetBrightness(percent);
}
/// <inheritdoc/>
public override void SetButtonImage(int keyId, KeyBitmap bitmapData)
{
mostRecentKeyBitmaps[keyId] = bitmapData;
base.SetButtonImage(keyId, bitmapData);
}
private void ReplayEventsForConnectionStateChange(object sender, ConnectionEventArgs e)
{
if (e.NewConnectionState)
{
// devices connected again: replay last known values
if (mostRecentBrightness is not null)
{
base.SetBrightness(mostRecentBrightness.Value);
}
foreach (var bmp in mostRecentKeyBitmaps)
{
base.SetButtonImage(bmp.Key, bmp.Value);
}
}
}
}
}