From 9913fdab5770b6afa155320ef9c85015283ff09d Mon Sep 17 00:00:00 2001 From: Bartosz Cichecki Date: Wed, 1 Jan 2025 23:43:26 +0100 Subject: [PATCH] Fix #1533 --- .../Steps/PlaySoundAutomationStep.cs | 28 +++++++ .../Automation/AutomationPipelineControl.cs | 10 ++- .../Steps/PlaySoundAutomationStepControl.cs | 84 +++++++++++++++++++ .../Pages/AutomationPage.xaml.cs | 1 + .../Resources/Resource.Designer.cs | 18 ++++ .../Resources/Resource.resx | 6 ++ 6 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 LenovoLegionToolkit.Lib.Automation/Steps/PlaySoundAutomationStep.cs create mode 100644 LenovoLegionToolkit.WPF/Controls/Automation/Steps/PlaySoundAutomationStepControl.cs diff --git a/LenovoLegionToolkit.Lib.Automation/Steps/PlaySoundAutomationStep.cs b/LenovoLegionToolkit.Lib.Automation/Steps/PlaySoundAutomationStep.cs new file mode 100644 index 0000000000..14661b30c3 --- /dev/null +++ b/LenovoLegionToolkit.Lib.Automation/Steps/PlaySoundAutomationStep.cs @@ -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 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); +} diff --git a/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs b/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs index c0e577198e..375d72d68b 100644 --- a/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs +++ b/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs @@ -202,6 +202,9 @@ 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) @@ -209,12 +212,10 @@ private async Task RunAsync() 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); } } @@ -353,6 +354,7 @@ private async Task 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), diff --git a/LenovoLegionToolkit.WPF/Controls/Automation/Steps/PlaySoundAutomationStepControl.cs b/LenovoLegionToolkit.WPF/Controls/Automation/Steps/PlaySoundAutomationStepControl.cs new file mode 100644 index 0000000000..5e77d231a3 --- /dev/null +++ b/LenovoLegionToolkit.WPF/Controls/Automation/Steps/PlaySoundAutomationStepControl.cs @@ -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 +{ + 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; + } +} diff --git a/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs b/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs index a265d93f81..588a2e07b3 100644 --- a/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs +++ b/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs @@ -178,6 +178,7 @@ private static async Task GetSupportedAutomationStepsAsync() new OverclockDiscreteGPUAutomationStep(default), new OverDriveAutomationStep(default), new PanelLogoBacklightAutomationStep(default), + new PlaySoundAutomationStep(default), new PortsBacklightAutomationStep(default), new PowerModeAutomationStep(default), new RefreshRateAutomationStep(default), diff --git a/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs b/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs index b40001a91d..f4e27c9375 100644 --- a/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs +++ b/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs @@ -4157,6 +4157,24 @@ public static string PeriodicActionPipelineTriggerTabItemContent_PeriodMinutes { } } + /// + /// Looks up a localized string similar to Common music formats like wav or mp3 are supported.. + /// + public static string PlaySoundAutomationStepControl_Message { + get { + return ResourceManager.GetString("PlaySoundAutomationStepControl_Message", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Play sound. + /// + public static string PlaySoundAutomationStepControl_Title { + get { + return ResourceManager.GetString("PlaySoundAutomationStepControl_Title", resourceCulture); + } + } + /// /// Looks up a localized string similar to Turn on or off the backlight of the ports on the back of the laptop.. /// diff --git a/LenovoLegionToolkit.WPF/Resources/Resource.resx b/LenovoLegionToolkit.WPF/Resources/Resource.resx index 8afaff3acb..657a87e143 100644 --- a/LenovoLegionToolkit.WPF/Resources/Resource.resx +++ b/LenovoLegionToolkit.WPF/Resources/Resource.resx @@ -2227,4 +2227,10 @@ Supported formats are: {1}. Check if your internet connection is up and running. + + Play sound + + + Common music formats like wav or mp3 are supported. + \ No newline at end of file