Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Action as Automation Step #1578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace LenovoLegionToolkit.Lib.Automation.Steps;

public class QuickActionAutomationStep(Guid? pipelineId)
: IAutomationStep
{
public Guid? PipelineId { get; } = pipelineId;

public Task<bool> IsSupportedAsync() => Task.FromResult(true);

public async Task RunAsync(AutomationContext context, AutomationEnvironment environment, CancellationToken token)
{
if (PipelineId is not null)
{
await IoCContainer.Resolve<AutomationProcessor>().RunNowAsync(PipelineId ?? Guid.Empty).ConfigureAwait(false);
}

return;
}

IAutomationStep IAutomationStep.DeepCopy() => new QuickActionAutomationStep(PipelineId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ private async Task<AbstractAutomationStepControl> GenerateStepControlAsync(IAuto
PlaySoundAutomationStep s => new PlaySoundAutomationStepControl(s),
PortsBacklightAutomationStep s => new PortsBacklightAutomationStepControl(s),
PowerModeAutomationStep s => new PowerModeAutomationStepControl(s),
QuickActionAutomationStep s => new QuickActionAutomationStepControl(s),
RefreshRateAutomationStep s => new RefreshRateAutomationStepControl(s),
ResolutionAutomationStep s => new ResolutionAutomationStepControl(s),
RGBKeyboardBacklightAutomationStep s => new RGBKeyboardBacklightAutomationStepControl(s),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using LenovoLegionToolkit.Lib;
using LenovoLegionToolkit.Lib.Automation;
using LenovoLegionToolkit.Lib.Automation.Steps;
using LenovoLegionToolkit.WPF.Resources;
using Wpf.Ui.Common;

namespace LenovoLegionToolkit.WPF.Controls.Automation.Steps;

public class QuickActionAutomationStepControl : AbstractAutomationStepControl<QuickActionAutomationStep>
{
private readonly AutomationProcessor _processor = IoCContainer.Resolve<AutomationProcessor>();

private readonly ComboBox _comboBox = new()
{
MinWidth = 150
};

private readonly StackPanel _stackPanel = new();

private bool _isRefreshing = false;

public QuickActionAutomationStepControl(QuickActionAutomationStep step) : base(step)
{
Icon = SymbolRegular.Play24;
Title = Resource.QuickActionAutomationStepControl_Title;
Subtitle = Resource.QuickActionAutomationStepControl_Message;
}

public override IAutomationStep CreateAutomationStep() => new QuickActionAutomationStep(GetSelectedPipelineIdAsync().Result);

protected override UIElement? GetCustomControl()
{
_comboBox.SelectionChanged += async (_, _) =>
{
if (_isRefreshing)
{
return;
}

var selectedPipelineId = await GetSelectedPipelineIdAsync();
if (selectedPipelineId != AutomationStep.PipelineId)
{
RaiseChanged();
}
};

_stackPanel.Children.Add(_comboBox);

return _stackPanel;
}

protected override void OnFinishedLoading() { }

protected override async Task RefreshAsync()
{
_isRefreshing = true;

_comboBox.Items.Clear();

var index = 0;
var selectedIndex = -1;
var pipelines = await _processor.GetPipelinesAsync();
foreach (var pipeline in pipelines.Where(p => p.Trigger is null))
{
_comboBox.Items.Add(pipeline.Name);
if (pipeline.Id == AutomationStep.PipelineId)
{
selectedIndex = index;
}
index++;
}
_comboBox.SelectedIndex = selectedIndex;

_isRefreshing = false;
return;
}

private async Task<Guid?> GetSelectedPipelineIdAsync()
{
var value = (string)_comboBox.SelectedItem;
var pipelines = await _processor.GetPipelinesAsync();
var selectedPipeline = pipelines.Where(p => p.Trigger is null).FirstOrDefault(p => p.Name == value);
if (selectedPipeline is not null)
{
return selectedPipeline.Id;
}
return null;
}
}
15 changes: 11 additions & 4 deletions LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private async Task RefreshAsync()

foreach (var pipeline in pipelines.Where(p => p.Trigger is null))
{
var control = GenerateControl(pipeline, _manualPipelinesStackPanel);
var control = GenerateControl(pipeline, _manualPipelinesStackPanel, false);
_manualPipelinesStackPanel.Children.Add(control);
initializedTasks.Add(control.InitializedTask);
}
Expand Down Expand Up @@ -181,6 +181,7 @@ private static async Task<IAutomationStep[]> GetSupportedAutomationStepsAsync()
new PlaySoundAutomationStep(default),
new PortsBacklightAutomationStep(default),
new PowerModeAutomationStep(default),
new QuickActionAutomationStep(default),
new RefreshRateAutomationStep(default),
new ResolutionAutomationStep(default),
new RGBKeyboardBacklightAutomationStep(default),
Expand Down Expand Up @@ -208,9 +209,15 @@ private static async Task<IAutomationStep[]> GetSupportedAutomationStepsAsync()
return [.. steps];
}

private AutomationPipelineControl GenerateControl(AutomationPipeline pipeline, Panel stackPanel)
private AutomationPipelineControl GenerateControl(AutomationPipeline pipeline, Panel stackPanel, bool allowQuickActionAutomationStep = true)
{
var control = new AutomationPipelineControl(pipeline, _supportedAutomationSteps);
var supportedSteps = _supportedAutomationSteps;
if (!allowQuickActionAutomationStep)
{
supportedSteps = Array.FindAll(supportedSteps, s => s is not QuickActionAutomationStep);
}

var control = new AutomationPipelineControl(pipeline, supportedSteps);
control.MouseRightButtonUp += (_, e) =>
{
ShowPipelineContextMenu(control, stackPanel);
Expand Down Expand Up @@ -301,7 +308,7 @@ private async Task AddManualPipelineAsync()
return;

var pipeline = new AutomationPipeline(newName);
var control = GenerateControl(pipeline, _manualPipelinesStackPanel);
var control = GenerateControl(pipeline, _manualPipelinesStackPanel, false);
_manualPipelinesStackPanel.Children.Insert(0, control);

_noManualActionsText.Visibility = _manualPipelinesStackPanel.Children.Count < 1
Expand Down
22 changes: 20 additions & 2 deletions LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 13 additions & 7 deletions LenovoLegionToolkit.WPF/Resources/Resource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2216,21 +2216,27 @@ Supported formats are: {1}.</value>
<value>Automatically check for updates</value>
</data>
<data name="PackagesPage_UpdateCatalogNotFound_Title" xml:space="preserve">
<value>Update catalog not found</value>
<value>Update catalog not found</value>
</data>
<data name="PackagesPage_UpdateCatalogNotFound_Message" xml:space="preserve">
<value>Try getting updates from the other source.</value>
<value>Try getting updates from the other source.</value>
</data>
<data name="PackagesPage_Error_Title" xml:space="preserve">
<value>Something went wrong</value>
<value>Something went wrong</value>
</data>
<data name="PackagesPage_Error_CheckInternet_Message" xml:space="preserve">
<value>Check if your internet connection is up and running.</value>
<value>Check if your internet connection is up and running.</value>
</data>
<data name="PlaySoundAutomationStepControl_Title" xml:space="preserve">
<value>Play sound</value>
<value>Play sound</value>
</data>
<data name="PlaySoundAutomationStepControl_Message" xml:space="preserve">
<value>Common music formats like wav or mp3 are supported.</value>
<value>Common music formats like wav or mp3 are supported.</value>
</data>
<data name="QuickActionAutomationStepControl_Title" xml:space="preserve">
<value>Quick Action</value>
</data>
<data name="QuickActionAutomationStepControl_Message" xml:space="preserve">
<value>Run a saved quick action.</value>
</data>
</root>
</root>