// ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global using System.Linq; using CUE.NET.Devices.Keyboard.Enums; using CUE.NET.Devices.Keyboard.Keys; namespace CUE.NET.Devices.Keyboard.Extensions { /// /// Offers some extensions and helper-methods for keygroup related things. /// public static class KeyGroupExtension { /// /// Converts the given to a . /// /// The to convert. /// The converted . public static ListKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup) { ListKeyGroup simpleKeyGroup = keyGroup as ListKeyGroup; if (simpleKeyGroup == null) { bool wasAttached = keyGroup.Detach(); simpleKeyGroup = new ListKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Brush = keyGroup.Brush }; } return simpleKeyGroup; } /// /// Returns a new which contains all keys from the given keygroup excluding the specified ones. /// /// The base keygroup. /// The ids of the keys to exclude. /// The new . public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds) { ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup(); foreach (CorsairKeyboardKeyId keyId in keyIds) simpleKeyGroup.RemoveKey(keyId); return simpleKeyGroup; } /// /// Returns a new which contains all keys from the given keygroup excluding the specified ones. /// /// The base keygroup. /// The keys to exclude. /// The new . public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds) { ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup(); foreach (CorsairKey key in keyIds) simpleKeyGroup.RemoveKey(key); return simpleKeyGroup; } // ReSharper disable once UnusedMethodReturnValue.Global /// /// Attaches the given keygroup to the keyboard. /// /// The keygroup to attach. /// true if the keygroup could be attached; otherwise, false. public static bool Attach(this BaseKeyGroup keyGroup) { return keyGroup.Keyboard?.AttachKeyGroup(keyGroup) ?? false; } /// /// Detaches the given keygroup from the keyboard. /// /// The keygroup to attach. /// true if the keygroup could be detached; otherwise, false. public static bool Detach(this BaseKeyGroup keyGroup) { return keyGroup.Keyboard?.DetachKeyGroup(keyGroup) ?? false; } } }