Skip to content

Commit

Permalink
Hide white keyboard backlight control if RGB or Spectrum is detected. (
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki authored Jun 26, 2023
1 parent 307cc07 commit 8217c77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
21 changes: 10 additions & 11 deletions LenovoLegionToolkit.Lib/Features/PanelLogoBacklightFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,43 @@ public class PanelLogoBacklightFeature : IFeature<PanelLogoBacklightState>
private readonly PanelLogoLenovoLightingBacklightFeature _lenovoLightingFeature;
private readonly PanelLogoSpectrumBacklightFeature _spectrumFeature;

private IFeature<PanelLogoBacklightState>? _feature;
private readonly Lazy<Task<IFeature<PanelLogoBacklightState>?>> _lazyAsyncFeature;

public PanelLogoBacklightFeature(PanelLogoLenovoLightingBacklightFeature lenovoLightingFeature, PanelLogoSpectrumBacklightFeature spectrumFeature)
{
_lenovoLightingFeature = lenovoLightingFeature ?? throw new ArgumentNullException(nameof(lenovoLightingFeature));
_spectrumFeature = spectrumFeature ?? throw new ArgumentNullException(nameof(spectrumFeature));

_lazyAsyncFeature = new(GetFeatureLazyAsync);
}

public async Task<bool> IsSupportedAsync() => await GetFeatureAsync().ConfigureAwait(false) != null;
public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;

public async Task<PanelLogoBacklightState[]> GetAllStatesAsync()
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
return await feature.GetAllStatesAsync().ConfigureAwait(false);
}

public async Task<PanelLogoBacklightState> GetStateAsync()
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
return await feature.GetStateAsync().ConfigureAwait(false);
}

public async Task SetStateAsync(PanelLogoBacklightState state)
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
await feature.SetStateAsync(state).ConfigureAwait(false);
}

private async Task<IFeature<PanelLogoBacklightState>?> GetFeatureAsync()
private async Task<IFeature<PanelLogoBacklightState>?> GetFeatureLazyAsync()
{
if (_feature is not null)
return _feature;

if (await _lenovoLightingFeature.IsSupportedAsync().ConfigureAwait(false))
return _feature = _lenovoLightingFeature;
return _lenovoLightingFeature;

if (await _spectrumFeature.IsSupportedAsync().ConfigureAwait(false))
return _feature = _spectrumFeature;
return _spectrumFeature;

return null;
}
Expand Down
37 changes: 24 additions & 13 deletions LenovoLegionToolkit.Lib/Features/WhiteKeyboardBacklightFeature.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
using System;
using System.Threading.Tasks;
using LenovoLegionToolkit.Lib.Controllers;

namespace LenovoLegionToolkit.Lib.Features;

public class WhiteKeyboardBacklightFeature : IFeature<WhiteKeyboardBacklightState>
{
private readonly WhiteKeyboardLenovoLightingBacklightFeature _lenovoLightingFeature;
private readonly WhiteKeyboardDriverBacklightFeature _driverFeature;
private readonly SpectrumKeyboardBacklightController _spectrumController;
private readonly RGBKeyboardBacklightController _rgbController;

private IFeature<WhiteKeyboardBacklightState>? _feature;
private readonly Lazy<Task<IFeature<WhiteKeyboardBacklightState>?>> _lazyAsyncFeature;

public WhiteKeyboardBacklightFeature(WhiteKeyboardLenovoLightingBacklightFeature lenovoLightingFeature, WhiteKeyboardDriverBacklightFeature driverFeature)
public WhiteKeyboardBacklightFeature(WhiteKeyboardLenovoLightingBacklightFeature lenovoLightingFeature,
WhiteKeyboardDriverBacklightFeature driverFeature,
SpectrumKeyboardBacklightController spectrumController,
RGBKeyboardBacklightController rgbController
)
{
_lenovoLightingFeature = lenovoLightingFeature;
_driverFeature = driverFeature;
_lenovoLightingFeature = lenovoLightingFeature ?? throw new ArgumentNullException(nameof(lenovoLightingFeature));
_driverFeature = driverFeature ?? throw new ArgumentNullException(nameof(driverFeature));
_spectrumController = spectrumController ?? throw new ArgumentNullException(nameof(spectrumController));
_rgbController = rgbController ?? throw new ArgumentNullException(nameof(rgbController));

_lazyAsyncFeature = new(GetFeatureLazyAsync);
}

public async Task<bool> IsSupportedAsync() => await GetFeatureAsync().ConfigureAwait(false) != null;
public async Task<bool> IsSupportedAsync() => await _lazyAsyncFeature.Value.ConfigureAwait(false) != null;

public async Task<WhiteKeyboardBacklightState[]> GetAllStatesAsync()
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
return await feature.GetAllStatesAsync().ConfigureAwait(false);
}

public async Task<WhiteKeyboardBacklightState> GetStateAsync()
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
return await feature.GetStateAsync().ConfigureAwait(false);
}

public async Task SetStateAsync(WhiteKeyboardBacklightState state)
{
var feature = await GetFeatureAsync().ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
var feature = await _lazyAsyncFeature.Value.ConfigureAwait(false) ?? throw new InvalidOperationException("No supported feature found.");
await feature.SetStateAsync(state).ConfigureAwait(false);
}

private async Task<IFeature<WhiteKeyboardBacklightState>?> GetFeatureAsync()
private async Task<IFeature<WhiteKeyboardBacklightState>?> GetFeatureLazyAsync()
{
if (_feature is not null)
return _feature;
if (await _spectrumController.IsSupportedAsync().ConfigureAwait(false) || await _rgbController.IsSupportedAsync().ConfigureAwait(false))
return null;

if (await _lenovoLightingFeature.IsSupportedAsync().ConfigureAwait(false))
return _feature = _lenovoLightingFeature;
return _lenovoLightingFeature;

if (await _driverFeature.IsSupportedAsync().ConfigureAwait(false))
return _feature = _driverFeature;
return _driverFeature;

return null;
}
Expand Down

0 comments on commit 8217c77

Please sign in to comment.