Skip to content

Commit

Permalink
Fix #1533
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki committed Jan 1, 2025
1 parent 60c33d8 commit 9913fda
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Media;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace LenovoLegionToolkit.Lib.Automation.Steps;

[method: JsonConstructor]
public class PlaySoundAutomationStep(string? path) : IAutomationStep
{
public string? Path { get; } = path;

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

public Task RunAsync(AutomationContext context, AutomationEnvironment environment, CancellationToken token)
{
if (Path is null)
return Task.CompletedTask;

return Task.Run(() =>
{
var player = new SoundPlayer(Path);
player.PlaySync();
}, token);
}

public IAutomationStep DeepCopy() => new PlaySoundAutomationStep(Path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ private async Task RunAsync()
var pipeline = CreateAutomationPipeline();
await _automationProcessor.RunNowAsync(pipeline);

_runNowButton.Content = Resource.AutomationPipelineControl_RunNow;
_runNowButton.IsEnabled = true;

await SnackbarHelper.ShowAsync(Resource.AutomationPipelineControl_RunNow_Success_Title, Resource.AutomationPipelineControl_RunNow_Success_Message);
}
catch (Exception ex)
{
if (Log.Instance.IsTraceEnabled)
Log.Instance.Trace($"Run now completed with errors", ex);

await SnackbarHelper.ShowAsync(Resource.AutomationPipelineControl_RunNow_Error_Title, Resource.AutomationPipelineControl_RunNow_Error_Message);
}
finally
{
_runNowButton.Content = Resource.AutomationPipelineControl_RunNow;
_runNowButton.IsEnabled = true;

await SnackbarHelper.ShowAsync(Resource.AutomationPipelineControl_RunNow_Error_Title, Resource.AutomationPipelineControl_RunNow_Error_Message);
}
}

Expand Down Expand Up @@ -353,6 +354,7 @@ private async Task<AbstractAutomationStepControl> GenerateStepControlAsync(IAuto
OverDriveAutomationStep s => new OverDriveAutomationStepControl(s),
OverclockDiscreteGPUAutomationStep s => new OverclockDiscreteGPUAutomationStepControl(s),
PanelLogoBacklightAutomationStep s => new PanelLogoBacklightAutomationStepControl(s),
PlaySoundAutomationStep s => new PlaySoundAutomationStepControl(s),
PortsBacklightAutomationStep s => new PortsBacklightAutomationStepControl(s),
PowerModeAutomationStep s => new PowerModeAutomationStepControl(s),
RefreshRateAutomationStep s => new RefreshRateAutomationStepControl(s),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using LenovoLegionToolkit.Lib.Automation.Steps;
using LenovoLegionToolkit.WPF.Resources;
using Microsoft.Win32;
using Wpf.Ui.Common;
using Button = Wpf.Ui.Controls.Button;
using Orientation = System.Windows.Controls.Orientation;

namespace LenovoLegionToolkit.WPF.Controls.Automation.Steps;

public class PlaySoundAutomationStepControl : AbstractAutomationStepControl<PlaySoundAutomationStep>
{
private string? _path;

private readonly TextBlock _titleTextBlock = new()
{
VerticalAlignment = VerticalAlignment.Center,
};

private readonly Button _openButton = new()
{
Icon = SymbolRegular.Folder20,
MinWidth = 34,
Height = 34,
Margin = new(8, 0, 0, 0)
};

private readonly StackPanel _stackPanel = new()
{
Orientation = Orientation.Horizontal
};

public PlaySoundAutomationStepControl(PlaySoundAutomationStep automationStep) : base(automationStep)
{
Icon = SymbolRegular.MusicNote2Play20;
Title = Resource.PlaySoundAutomationStepControl_Title;
Subtitle = Resource.PlaySoundAutomationStepControl_Message;
}

public override IAutomationStep CreateAutomationStep()
{
return new PlaySoundAutomationStep(_path);
}

protected override UIElement? GetCustomControl()
{

_openButton.Click += (_, _) =>
{
var ofd = new OpenFileDialog
{
Title = Resource.Import,
InitialDirectory = @"C:\Windows\Media",
CheckFileExists = true,
};

var result = ofd.ShowDialog() ?? false;
if (!result)
return;

_path = ofd.FileName;
_titleTextBlock.Text = Path.GetFileName(_path);

RaiseChanged();
};

_stackPanel.Children.Add(_titleTextBlock);
_stackPanel.Children.Add(_openButton);

return _stackPanel;
}

protected override void OnFinishedLoading() { }

protected override Task RefreshAsync()
{
_path = AutomationStep.Path;
_titleTextBlock.Text = Path.GetFileName(_path) ?? "-";
return Task.CompletedTask;
}
}
1 change: 1 addition & 0 deletions LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ private static async Task<IAutomationStep[]> GetSupportedAutomationStepsAsync()
new OverclockDiscreteGPUAutomationStep(default),
new OverDriveAutomationStep(default),
new PanelLogoBacklightAutomationStep(default),
new PlaySoundAutomationStep(default),
new PortsBacklightAutomationStep(default),
new PowerModeAutomationStep(default),
new RefreshRateAutomationStep(default),
Expand Down
18 changes: 18 additions & 0 deletions LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs

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

6 changes: 6 additions & 0 deletions LenovoLegionToolkit.WPF/Resources/Resource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2227,4 +2227,10 @@ Supported formats are: {1}.</value>
<data name="PackagesPage_Error_CheckInternet_Message" xml:space="preserve">
<value>Check if your internet connection is up and running.</value>
</data>
<data name="PlaySoundAutomationStepControl_Title" xml:space="preserve">
<value>Play sound</value>
</data>
<data name="PlaySoundAutomationStepControl_Message" xml:space="preserve">
<value>Common music formats like wav or mp3 are supported.</value>
</data>
</root>

0 comments on commit 9913fda

Please sign in to comment.