Skip to content

Commit

Permalink
Added Spectrum layout for Legion 9 (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki authored Feb 14, 2024
1 parent 45e9a1d commit b2036f2
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ or SpecialKey.SpectrumPreset5
Log.Instance.Trace($"Checking keyboard layout...");

var (width, height, keys) = await ReadAllKeyCodesAsync().ConfigureAwait(false);
var mi = await Compatibility.GetMachineInformationAsync().ConfigureAwait(false);

var spectrumLayout = (width, height) switch
{
(22, 9) when mi.Properties.HasAlternativeFullSpectrumLayout => SpectrumLayout.FullAlternative,
(22, 9) => SpectrumLayout.Full,
(20, 8) => SpectrumLayout.KeyboardAndFront,
_ => SpectrumLayout.KeyboardOnly // (20, 7)
Expand Down
3 changes: 2 additions & 1 deletion LenovoLegionToolkit.Lib/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ public enum SpectrumLayout
{
KeyboardOnly,
KeyboardAndFront,
Full
Full,
FullAlternative
}

public enum Theme
Expand Down
1 change: 1 addition & 0 deletions LenovoLegionToolkit.Lib/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ public readonly struct PropertyData
public bool HasGodModeToOtherModeSwitchingBug { get; init; }
public bool IsExcludedFromLenovoLighting { get; init; }
public bool IsExcludedFromPanelLogoLenovoLighting { get; init; }
public bool HasAlternativeFullSpectrumLayout { get; init; }
}

public string Vendor { get; init; }
Expand Down
14 changes: 13 additions & 1 deletion LenovoLegionToolkit.Lib/Utils/Compatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public static async Task<MachineInformation> GetMachineInformationAsync()
HasQuietToPerformanceModeSwitchingBug = GetHasQuietToPerformanceModeSwitchingBug(biosVersion),
HasGodModeToOtherModeSwitchingBug = GetHasGodModeToOtherModeSwitchingBug(biosVersion),
IsExcludedFromLenovoLighting = GetIsExcludedFromLenovoLighting(biosVersion),
IsExcludedFromPanelLogoLenovoLighting = GetIsExcludedFromPanelLenovoLighting(machineType, model)
IsExcludedFromPanelLogoLenovoLighting = GetIsExcludedFromPanelLenovoLighting(machineType, model),
HasAlternativeFullSpectrumLayout = GetHasAlternativeFullSpectrumLayout(machineType),
}
};

Expand Down Expand Up @@ -156,6 +157,7 @@ public static async Task<MachineInformation> GetMachineInformationAsync()
Log.Instance.Trace($" * HasGodModeToOtherModeSwitchingBug: '{machineInformation.Properties.HasGodModeToOtherModeSwitchingBug}'");
Log.Instance.Trace($" * IsExcludedFromLenovoLighting: '{machineInformation.Properties.IsExcludedFromLenovoLighting}'");
Log.Instance.Trace($" * IsExcludedFromPanelLogoLenovoLighting: '{machineInformation.Properties.IsExcludedFromPanelLogoLenovoLighting}'");
Log.Instance.Trace($" * HasAlternativeFullSpectrumLayout: '{machineInformation.Properties.HasAlternativeFullSpectrumLayout}'");
}

return (_machineInformation = machineInformation).Value;
Expand Down Expand Up @@ -430,4 +432,14 @@ private static bool GetIsExcludedFromPanelLenovoLighting(string machineType, str
return result;
}).Any();
}

private static bool GetHasAlternativeFullSpectrumLayout(string machineType)
{
var machineTypes = new[]
{
"83G0", // Gen 9
"83AG" // Gen 8
};
return machineTypes.Contains(machineType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace LenovoLegionToolkit.WPF.Controls.KeyboardBacklight.Spectrum.Device;

public class SpectrumDeviceControl : UserControl
{
private readonly SpectrumDeviceFullAlternativeControl _fullAlternative = new();
private readonly SpectrumDeviceFullControl _full = new();
private readonly SpectrumDeviceKeyboardAndFrontControl _keyboardAndFront = new();
private readonly SpectrumDeviceKeyboardOnlyControl _keyboardOnly = new();
Expand All @@ -22,12 +23,17 @@ public SpectrumDeviceControl()

public void SetLayout(SpectrumLayout spectrumLayout, KeyboardLayout keyboardLayout, HashSet<ushort> keys)
{
_stackPanel.Children.Remove(_fullAlternative);
_stackPanel.Children.Remove(_full);
_stackPanel.Children.Remove(_keyboardAndFront);
_stackPanel.Children.Remove(_keyboardOnly);

switch (spectrumLayout)
{
case SpectrumLayout.FullAlternative:
_stackPanel.Children.Add(_fullAlternative);
_fullAlternative.SetLayout(keyboardLayout);
break;
case SpectrumLayout.Full:
_stackPanel.Children.Add(_full);
_full.SetLayout(keyboardLayout);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<UserControl
x:Class="LenovoLegionToolkit.WPF.Controls.KeyboardBacklight.Spectrum.Device.SpectrumDeviceFullAlternativeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:LenovoLegionToolkit.WPF.Controls.KeyboardBacklight.Spectrum.Device"
FlowDirection="LeftToRight">

<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="24" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="24" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<!-- Panel Logo -->
<controls:SpectrumZoneControl
Grid.Row="0"
Grid.Column="6"
Height="32"
Margin="0,0,0,24"
KeyCode="0x05DD" />

<!-- Keyboard -->
<controls:SpectrumKeyboardControl
x:Name="_keyboard"
Grid.Row="2"
Grid.RowSpan="5"
Grid.Column="1"
Grid.ColumnSpan="6"
Padding="16" />

<!-- Rear vents -->
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="1"
Margin="2"
KeyCode="0x03EA" />
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="2"
Margin="2"
KeyCode="0x03EB" />
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="3"
Margin="2"
KeyCode="0x03EC" />
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="4"
Margin="2"
KeyCode="0x03ED" />
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="5"
Margin="2"
KeyCode="0x03EE" />
<controls:SpectrumZoneControl
Grid.Row="1"
Grid.Column="6"
Margin="2"
KeyCode="0x03EF" />

<!-- Left side -->
<controls:SpectrumZoneControl
Grid.Row="2"
Grid.Column="0"
Height="24"
Margin="2"
VerticalAlignment="Top"
KeyCode="0x03E9" />
<controls:SpectrumZoneControl
Grid.Row="5"
Grid.Column="0"
Margin="2"
KeyCode="0x01F5" />
<controls:SpectrumZoneControl
Grid.Row="6"
Grid.Column="0"
Margin="2"
KeyCode="0x01F6" />

<!-- Right side -->
<controls:SpectrumZoneControl
Grid.Row="2"
Grid.Column="7"
Height="24"
Margin="2"
VerticalAlignment="Top"
KeyCode="0x03F0" />
<controls:SpectrumZoneControl
Grid.Row="5"
Grid.Column="7"
Margin="2"
KeyCode="0x01FE" />
<controls:SpectrumZoneControl
Grid.Row="6"
Grid.Column="7"
Margin="2"
KeyCode="0x01FD" />

<!-- Front -->
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="1"
Margin="2"
KeyCode="0x01F7" />
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="2"
Margin="2"
KeyCode="0x01F8" />
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="3"
Margin="2"
KeyCode="0x01F9" />
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="4"
Margin="2"
KeyCode="0x01FA" />
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="5"
Margin="2"
KeyCode="0x01FB" />
<controls:SpectrumZoneControl
Grid.Row="7"
Grid.Column="6"
Margin="2"
KeyCode="0x01FC" />

</Grid>

</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using LenovoLegionToolkit.Lib;

namespace LenovoLegionToolkit.WPF.Controls.KeyboardBacklight.Spectrum.Device;

public partial class SpectrumDeviceFullAlternativeControl
{
public SpectrumDeviceFullAlternativeControl()
{
InitializeComponent();
}

public void SetLayout(KeyboardLayout keyboardLayout)
{
_keyboard.SetLayout(keyboardLayout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ namespace LenovoLegionToolkit.WPF.Controls.KeyboardBacklight.Spectrum.Device;

public partial class SpectrumZoneControl
{
public ushort KeyCode { get; set; }
private ushort _keyCode;

public ushort KeyCode
{
get => _keyCode;
set
{
_keyCode = value;

#if DEBUG
_button.ToolTip = $"0x{value:X2}";
#endif
}
}

public Color? Color
{
Expand Down

0 comments on commit b2036f2

Please sign in to comment.