Skip to content

Commit

Permalink
Separate all light effects from whole keyboard effects
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki committed Apr 27, 2023
1 parent 68f341e commit 048f6fe
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,18 @@ private unsafe void GetFeature(SafeFileHandle handle, out byte[] bytes, int size

private static SpectrumKeyboardBacklightEffect[] Compress(SpectrumKeyboardBacklightEffect[] effects)
{
if (effects.Any(e => e.Keys.All))
return new[] { effects.Last(e => e.Keys.All) };
if (effects.Any(e => e.Type.IsAllLightsEffect()))
return new[] { effects.Last(e => e.Type.IsAllLightsEffect()) };

var usedKeyCodes = new HashSet<ushort>();
var newEffects = new List<SpectrumKeyboardBacklightEffect>();

foreach (var effect in effects.Reverse())
{
var newKeyCodes = effect.Keys.KeyCodes.Except(usedKeyCodes).ToArray();
if (effect.Type.IsWholeKeyboardEffect() && usedKeyCodes.Intersect(effect.Keys).Any())
continue;

var newKeyCodes = effect.Keys.Except(usedKeyCodes).ToArray();

foreach (var keyCode in newKeyCodes)
usedKeyCodes.Add(keyCode);
Expand All @@ -716,11 +719,12 @@ private static SpectrumKeyboardBacklightEffect[] Compress(SpectrumKeyboardBackli
effect.Direction,
effect.ClockwiseDirection,
effect.Colors,
SpectrumKeyboardBacklightKeys.SomeKeys(newKeyCodes));
newKeyCodes);

newEffects.Add(newEffect);
}

newEffects.Reverse();
return newEffects.ToArray();
}

Expand Down Expand Up @@ -777,11 +781,9 @@ private static SpectrumKeyboardBacklightEffect Convert(LENOVO_SPECTRUM_EFFECT ef

var colors = effect.Colors.Select(c => new RGBColor(c.R, c.G, c.B)).ToArray();

SpectrumKeyboardBacklightKeys keys;
var keys = effect.KeyCodes;
if (effect.KeyCodes.Length == 1 && effect.KeyCodes[0] == 0x65)
keys = SpectrumKeyboardBacklightKeys.AllKeys();
else
keys = SpectrumKeyboardBacklightKeys.SomeKeys(effect.KeyCodes);
keys = Array.Empty<ushort>();

return new(effectType, speed, direction, clockwiseDirection, colors, keys);
}
Expand Down Expand Up @@ -860,7 +862,7 @@ private static LENOVO_SPECTRUM_EFFECT Convert(int index, SpectrumKeyboardBacklig

var header = new LENOVO_SPECTRUM_EFFECT_HEADER(effectType, speed, direction, clockwiseDirection, colorMode);
var colors = effect.Colors.Select(c => new LENOVO_SPECTRUM_COLOR(c.R, c.G, c.B)).ToArray();
var keys = effect.Keys.All ? new ushort[] { 0x65 } : effect.Keys.KeyCodes;
var keys = effect.Type.IsAllLightsEffect() ? new ushort[] { 0x65 } : effect.Keys;
var result = new LENOVO_SPECTRUM_EFFECT(header, index + 1, colors, keys);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace LenovoLegionToolkit.Lib.Extensions;

public static class SpectrumKeyboardBacklightEffectTypeExtensions
{

public static bool IsAllLightsEffect(this SpectrumKeyboardBacklightEffectType type) => type switch
{
SpectrumKeyboardBacklightEffectType.AudioBounce => true,
SpectrumKeyboardBacklightEffectType.AudioRipple => true,
SpectrumKeyboardBacklightEffectType.AuroraSync => true,
_ => false
};

public static bool IsWholeKeyboardEffect(this SpectrumKeyboardBacklightEffectType type) => type switch
{
SpectrumKeyboardBacklightEffectType.Type => true,
SpectrumKeyboardBacklightEffectType.Ripple => true,
_ => false
};
}
23 changes: 5 additions & 18 deletions LenovoLegionToolkit.Lib/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,44 +663,31 @@ public int CompareTo(Resolution other)

}

public readonly struct SpectrumKeyboardBacklightKeys
{
public bool All { get; }
public ushort[] KeyCodes { get; }

[JsonConstructor]
private SpectrumKeyboardBacklightKeys(bool all, ushort[] keyCodes)
{
All = all;
KeyCodes = keyCodes;
}

public static SpectrumKeyboardBacklightKeys AllKeys() => new(true, Array.Empty<ushort>());
public static SpectrumKeyboardBacklightKeys SomeKeys(ushort[] keyCodes) => new(false, keyCodes);
}

public readonly struct SpectrumKeyboardBacklightEffect
{
public SpectrumKeyboardBacklightEffectType Type { get; }
public SpectrumKeyboardBacklightSpeed Speed { get; }
public SpectrumKeyboardBacklightDirection Direction { get; }
public SpectrumKeyboardBacklightClockwiseDirection ClockwiseDirection { get; }
public RGBColor[] Colors { get; }
public SpectrumKeyboardBacklightKeys Keys { get; }
public ushort[] Keys { get; }

public SpectrumKeyboardBacklightEffect(SpectrumKeyboardBacklightEffectType type,
SpectrumKeyboardBacklightSpeed speed,
SpectrumKeyboardBacklightDirection direction,
SpectrumKeyboardBacklightClockwiseDirection clockwiseDirection,
RGBColor[] colors,
SpectrumKeyboardBacklightKeys keys)
ushort[] keys)
{
Type = type;
Speed = speed;
Direction = direction;
ClockwiseDirection = clockwiseDirection;
Colors = colors;
Keys = keys;

if (Type.IsAllLightsEffect())
Keys = Array.Empty<ushort>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,13 @@ public void SetLayout(SpectrumLayout spectrumLayout, KeyboardLayout keyboardLayo
GetButtons().Where(b => b.Visibility == Visibility.Visible);

private IEnumerable<SpectrumZoneControl> GetButtons() =>
this.GetVisibleChildrenOfType<SpectrumZoneControl>().Where(b => b.KeyCode > 0);
this.GetVisibleChildrenOfType<SpectrumZoneControl>()
.Where(b => b.KeyCode > 0);

public IEnumerable<SpectrumZoneControl> GetVisibleKeyboardButtons() =>
GetKeyboardButtons().Where(b => b.Visibility == Visibility.Visible);

private IEnumerable<SpectrumZoneControl> GetKeyboardButtons() =>
this.GetVisibleChildrenOfType<SpectrumZoneControl>()
.Where(b => b.KeyCode is > 0 and < 0x100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ private void AddEffectButton_Click(object sender, RoutedEventArgs e)

var keyCodes = checkedButtons.Select(b => b.KeyCode).ToArray();

CreateEffect(keyCodes);
var allKeyboardKeyCodes = _device.GetVisibleKeyboardButtons()
.Select(b => b.KeyCode)
.ToArray();

CreateEffect(keyCodes, allKeyboardKeyCodes);
}

private async void ResetToDefaultButton_Click(object sender, RoutedEventArgs e) => await ResetToDefaultAsync();
Expand Down Expand Up @@ -292,9 +296,9 @@ protected override async Task OnRefreshAsync()

protected override void OnFinishedLoading() { }

private void SelectButtons(SpectrumKeyboardBacklightKeys keys)
private void SelectButtons(SpectrumKeyboardBacklightEffect effect)
{
if (keys.All)
if (effect.Type.IsAllLightsEffect())
{
SelectAllButtons();
return;
Expand All @@ -304,7 +308,7 @@ private void SelectButtons(SpectrumKeyboardBacklightKeys keys)

foreach (var button in _device.GetVisibleButtons())
{
if (!keys.KeyCodes.Contains(button.KeyCode))
if (!effect.Keys.Contains(button.KeyCode))
continue;

button.IsChecked = true;
Expand Down Expand Up @@ -455,17 +459,17 @@ private async Task ResetToDefaultAsync()
await RefreshProfileDescriptionAsync();
}

private void CreateEffect(ushort[] keyCodes)
private void CreateEffect(ushort[] keyCodes, ushort[] allKeyboardKeyCodes)
{
var window = new SpectrumKeyboardBacklightEditEffectWindow(keyCodes) { Owner = Window.GetWindow(this) };
var window = new SpectrumKeyboardBacklightEditEffectWindow(keyCodes, allKeyboardKeyCodes) { Owner = Window.GetWindow(this) };
window.Apply += async (_, e) => await AddEffect(e);
window.ShowDialog();
}

private SpectrumKeyboardEffectControl CreateEffectControl(SpectrumKeyboardBacklightEffect effect)
{
var control = new SpectrumKeyboardEffectControl(effect);
control.Click += (_, _) => SelectButtons(effect.Keys);
control.Click += (_, _) => SelectButtons(effect);
control.Edit += (_, _) => EditEffect(control);
control.Delete += async (_, _) => await DeleteEffectAsync(control);
return control;
Expand All @@ -475,11 +479,6 @@ private async Task AddEffect(SpectrumKeyboardBacklightEffect effect)
{
DeselectAllButtons();

if (effect.Keys.All)
DeleteAllEffects();
else if (_effects.Children.OfType<SpectrumKeyboardEffectControl>().Any(c => c.Effect.Keys.All))
DeleteAllEffects();

var control = CreateEffectControl(effect);
_effects.Children.Add(control);

Expand All @@ -488,8 +487,14 @@ private async Task AddEffect(SpectrumKeyboardBacklightEffect effect)

private void EditEffect(SpectrumKeyboardEffectControl effectControl)
{
var keyCodes = _device.GetVisibleButtons().Select(b => b.KeyCode).ToArray();
var window = new SpectrumKeyboardBacklightEditEffectWindow(effectControl.Effect, keyCodes) { Owner = Window.GetWindow(this) };
var keyCodes = _device.GetVisibleButtons()
.Select(b => b.KeyCode)
.ToArray();
var allKeyboardKeyCodes = _device.GetVisibleKeyboardButtons()
.Select(b => b.KeyCode)
.ToArray();

var window = new SpectrumKeyboardBacklightEditEffectWindow(effectControl.Effect, keyCodes, allKeyboardKeyCodes) { Owner = Window.GetWindow(this) };
window.Apply += async (_, e) => await ReplaceEffectAsync(effectControl, e);
window.ShowDialog();
}
Expand All @@ -499,7 +504,7 @@ private async Task ReplaceEffectAsync(SpectrumKeyboardEffectControl effectContro
DeselectAllButtons();

var control = new SpectrumKeyboardEffectControl(effect);
control.Click += (_, _) => SelectButtons(effect.Keys);
control.Click += (_, _) => SelectButtons(effect);
control.Edit += (_, _) => EditEffect(control);
control.Delete += async (_, _) => await DeleteEffectAsync(control);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public SpectrumKeyboardEffectControl(SpectrumKeyboardBacklightEffect effect)
_cardHeaderControl.Title = effect.Type.GetDisplayName();

var subtitle = string.Empty;
if (effect.Keys.All)
if (effect.Type.IsAllLightsEffect())
subtitle += Resource.SpectrumKeyboardEffectControl_Description_AllZones;
else
subtitle += string.Format(Resource.SpectrumKeyboardEffectControl_Description_Zones, effect.Keys.KeyCodes.Length);
subtitle += string.Format(Resource.SpectrumKeyboardEffectControl_Description_Zones, effect.Keys.Length);
_cardHeaderControl.Subtitle = subtitle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace LenovoLegionToolkit.WPF.Windows.KeyboardBacklight.Spectrum;
public partial class SpectrumKeyboardBacklightEditEffectWindow
{
private readonly ushort[] _keyCodes;
private readonly ushort[] _allKeyboardKeyCodes;

public event EventHandler<SpectrumKeyboardBacklightEffect>? Apply;

public SpectrumKeyboardBacklightEditEffectWindow(ushort[] keyCodes)
public SpectrumKeyboardBacklightEditEffectWindow(ushort[] keyCodes, ushort[] allKeyboardKeyCodes)
{
_keyCodes = keyCodes;
_allKeyboardKeyCodes = allKeyboardKeyCodes;

InitializeComponent();

Expand All @@ -28,9 +30,10 @@ public SpectrumKeyboardBacklightEditEffectWindow(ushort[] keyCodes)
RefreshVisibility();
}

public SpectrumKeyboardBacklightEditEffectWindow(SpectrumKeyboardBacklightEffect effect, ushort[] keyCodes)
public SpectrumKeyboardBacklightEditEffectWindow(SpectrumKeyboardBacklightEffect effect, ushort[] keyCodes, ushort[] allKeyboardKeyCodes)
{
_keyCodes = effect.Keys.All ? keyCodes : effect.Keys.KeyCodes;
_keyCodes = effect.Type.IsAllLightsEffect() ? keyCodes : effect.Keys;
_allKeyboardKeyCodes = allKeyboardKeyCodes;

InitializeComponent();

Expand Down Expand Up @@ -78,9 +81,12 @@ private void Apply_Click(object sender, RoutedEventArgs e)
if (_multiColors.Visibility == Visibility.Visible)
colors = _multiColorPicker.SelectedColors.Select(c => c.ToRGBColor()).ToArray();

var keys = IsWholeKeyboardEffect(effectType)
? SpectrumKeyboardBacklightKeys.AllKeys()
: SpectrumKeyboardBacklightKeys.SomeKeys(_keyCodes);
var keys = _keyCodes;

if (effectType.IsAllLightsEffect())
keys = Array.Empty<ushort>();
if (effectType.IsWholeKeyboardEffect())
keys = _allKeyboardKeyCodes;

var effect = new SpectrumKeyboardBacklightEffect(effectType,
speed,
Expand Down Expand Up @@ -172,7 +178,7 @@ private void RefreshVisibility()
if (!_effectTypeComboBox.TryGetSelectedItem(out SpectrumKeyboardBacklightEffectType effect))
return;

_effectTypeCardHeader.Warning = IsWholeKeyboardEffect(effect)
_effectTypeCardHeader.Warning = effect.IsAllLightsEffect() || effect.IsWholeKeyboardEffect()
? Resource.SpectrumKeyboardBacklightEditEffectWindow_Effect_Warning
: string.Empty;

Expand Down Expand Up @@ -221,13 +227,4 @@ private void RefreshVisibility()
_ => Visibility.Collapsed
};
}

private static bool IsWholeKeyboardEffect(SpectrumKeyboardBacklightEffectType effectType) => effectType switch
{
SpectrumKeyboardBacklightEffectType.AudioBounce => true,
SpectrumKeyboardBacklightEffectType.AudioRipple => true,
SpectrumKeyboardBacklightEffectType.Ripple => true,
SpectrumKeyboardBacklightEffectType.AuroraSync => true,
_ => false
};
}

0 comments on commit 048f6fe

Please sign in to comment.