From 06a37081adc2d5e402187b0d1e1ee6fd6c30a24f Mon Sep 17 00:00:00 2001 From: Bartosz Cichecki Date: Sat, 21 Oct 2023 21:50:55 +0200 Subject: [PATCH] Fix #999 --- .../Steps/NotificationAutomationStep.cs | 26 ++++++++ LenovoLegionToolkit.Lib/Enums.cs | 1 + .../Settings/ApplicationSettings.cs | 1 + .../Automation/AutomationPipelineControl.cs | 1 + .../NotificationAutomationStepControl.cs | 60 +++++++++++++++++++ .../Pages/AutomationPage.xaml.cs | 1 + .../Resources/Resource.Designer.cs | 27 +++++++++ .../Resources/Resource.resx | 9 +++ .../Utils/NotificationsManager.cs | 3 + .../Settings/NotificationsSettingsWindow.xaml | 12 +++- .../NotificationsSettingsWindow.xaml.cs | 14 ++++- 11 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 LenovoLegionToolkit.Lib.Automation/Steps/NotificationAutomationStep.cs create mode 100644 LenovoLegionToolkit.WPF/Controls/Automation/Steps/NotificationAutomationStepControl.cs diff --git a/LenovoLegionToolkit.Lib.Automation/Steps/NotificationAutomationStep.cs b/LenovoLegionToolkit.Lib.Automation/Steps/NotificationAutomationStep.cs new file mode 100644 index 0000000000..50b84e7cd9 --- /dev/null +++ b/LenovoLegionToolkit.Lib.Automation/Steps/NotificationAutomationStep.cs @@ -0,0 +1,26 @@ +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace LenovoLegionToolkit.Lib.Automation.Steps; + +public class NotificationAutomationStep : IAutomationStep +{ + public string? Text { get; } + + [JsonConstructor] + public NotificationAutomationStep(string? text) + { + Text = text; + } + + public Task IsSupportedAsync() => Task.FromResult(true); + + public Task RunAsync(AutomationEnvironment environment) + { + if (!string.IsNullOrWhiteSpace(Text)) + MessagingCenter.Publish(new Notification(NotificationType.AutomationNotification, Text)); + return Task.CompletedTask; + } + + IAutomationStep IAutomationStep.DeepCopy() => new NotificationAutomationStep(Text); +} diff --git a/LenovoLegionToolkit.Lib/Enums.cs b/LenovoLegionToolkit.Lib/Enums.cs index 5a72554797..84b2838ff9 100644 --- a/LenovoLegionToolkit.Lib/Enums.cs +++ b/LenovoLegionToolkit.Lib/Enums.cs @@ -211,6 +211,7 @@ public enum NotificationType ACAdapterConnected, ACAdapterConnectedLowWattage, ACAdapterDisconnected, + AutomationNotification, CameraOn, CameraOff, CapsLockOn, diff --git a/LenovoLegionToolkit.Lib/Settings/ApplicationSettings.cs b/LenovoLegionToolkit.Lib/Settings/ApplicationSettings.cs index bd74874845..fb41281676 100644 --- a/LenovoLegionToolkit.Lib/Settings/ApplicationSettings.cs +++ b/LenovoLegionToolkit.Lib/Settings/ApplicationSettings.cs @@ -19,6 +19,7 @@ public class Notifications public bool RefreshRate { get; set; } = true; public bool ACAdapter { get; set; } public bool SmartKey { get; set; } + public bool AutomationNotification { get; set; } = true; } public class ApplicationSettingsStore diff --git a/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs b/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs index c41dce735f..81956c7131 100644 --- a/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs +++ b/LenovoLegionToolkit.WPF/Controls/Automation/AutomationPipelineControl.cs @@ -326,6 +326,7 @@ private async Task GenerateStepControlAsync(IAuto HybridModeAutomationStep s => await HybridModeAutomationStepControlFactory.GetControlAsync(s), InstantBootAutomationStep s => new InstantBootAutomationStepControl(s), MicrophoneAutomationStep s => new MicrophoneAutomationStepControl(s), + NotificationAutomationStep s => new NotificationAutomationStepControl(s), OneLevelWhiteKeyboardBacklightAutomationStep s => new OneLevelWhiteKeyboardBacklightAutomationStepControl(s), OverDriveAutomationStep s => new OverDriveAutomationStepControl(s), OverclockDiscreteGPUAutomationStep s => new OverclockDiscreteGPUAutomationStepControl(s), diff --git a/LenovoLegionToolkit.WPF/Controls/Automation/Steps/NotificationAutomationStepControl.cs b/LenovoLegionToolkit.WPF/Controls/Automation/Steps/NotificationAutomationStepControl.cs new file mode 100644 index 0000000000..f785a1b639 --- /dev/null +++ b/LenovoLegionToolkit.WPF/Controls/Automation/Steps/NotificationAutomationStepControl.cs @@ -0,0 +1,60 @@ +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using LenovoLegionToolkit.Lib.Automation.Steps; +using LenovoLegionToolkit.WPF.Resources; +using Wpf.Ui.Common; +using TextBox = Wpf.Ui.Controls.TextBox; + +namespace LenovoLegionToolkit.WPF.Controls.Automation.Steps; + +public class NotificationAutomationStepControl : AbstractAutomationStepControl +{ + private readonly TextBox _scriptPath = new() + { + PlaceholderText = Resource.NotificationAutomationStepControl_NotificationText, + Width = 300 + }; + + private readonly StackPanel _stackPanel = new(); + + public NotificationAutomationStepControl(NotificationAutomationStep step) : base(step) + { + Icon = SymbolRegular.Rocket24; + Title = Resource.NotificationAutomationStepControl_Title; + + SizeChanged += RunAutomationStepControl_SizeChanged; + } + + private void RunAutomationStepControl_SizeChanged(object sender, SizeChangedEventArgs e) + { + if (!e.WidthChanged) + return; + + var newWidth = e.NewSize.Width / 3; + _scriptPath.Width = newWidth; + } + + public override IAutomationStep CreateAutomationStep() => new NotificationAutomationStep(_scriptPath.Text); + + protected override UIElement GetCustomControl() + { + _scriptPath.TextChanged += (_, _) => + { + if (_scriptPath.Text != AutomationStep.Text) + RaiseChanged(); + }; + + _stackPanel.Children.Add(_scriptPath); + + return _stackPanel; + } + + protected override void OnFinishedLoading() { } + + protected override Task RefreshAsync() + { + _scriptPath.Text = AutomationStep.Text; + return Task.CompletedTask; + } +} diff --git a/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs b/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs index 01945ee7b2..519ed0338b 100644 --- a/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs +++ b/LenovoLegionToolkit.WPF/Pages/AutomationPage.xaml.cs @@ -172,6 +172,7 @@ private static async Task GetSupportedAutomationStepsAsync() new HDRAutomationStep(default), new InstantBootAutomationStep(default), new MicrophoneAutomationStep(default), + new NotificationAutomationStep(default), new OneLevelWhiteKeyboardBacklightAutomationStep(default), new OverclockDiscreteGPUAutomationStep(default), new OverDriveAutomationStep(default), diff --git a/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs b/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs index 7ce8119548..ab301923cb 100644 --- a/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs +++ b/LenovoLegionToolkit.WPF/Resources/Resource.Designer.cs @@ -2929,6 +2929,24 @@ public static string Notification_WhiteKeyboardBacklight { } } + /// + /// Looks up a localized string similar to Notification Text. + /// + public static string NotificationAutomationStepControl_NotificationText { + get { + return ResourceManager.GetString("NotificationAutomationStepControl_NotificationText", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Show notification. + /// + public static string NotificationAutomationStepControl_Title { + get { + return ResourceManager.GetString("NotificationAutomationStepControl_Title", resourceCulture); + } + } + /// /// Looks up a localized string similar to AC Adapter. /// @@ -2938,6 +2956,15 @@ public static string NotificationsSettingsWindow_ACAdapter { } } + /// + /// Looks up a localized string similar to Actions. + /// + public static string NotificationsSettingsWindow_Automation { + get { + return ResourceManager.GetString("NotificationsSettingsWindow_Automation", resourceCulture); + } + } + /// /// Looks up a localized string similar to Camera. /// diff --git a/LenovoLegionToolkit.WPF/Resources/Resource.resx b/LenovoLegionToolkit.WPF/Resources/Resource.resx index be64a7da7d..a15539c796 100644 --- a/LenovoLegionToolkit.WPF/Resources/Resource.resx +++ b/LenovoLegionToolkit.WPF/Resources/Resource.resx @@ -1919,4 +1919,13 @@ Requires restart. Customize + + Actions + + + Notification Text + + + Show notification + \ No newline at end of file diff --git a/LenovoLegionToolkit.WPF/Utils/NotificationsManager.cs b/LenovoLegionToolkit.WPF/Utils/NotificationsManager.cs index f8336a032c..e2fee41bc5 100644 --- a/LenovoLegionToolkit.WPF/Utils/NotificationsManager.cs +++ b/LenovoLegionToolkit.WPF/Utils/NotificationsManager.cs @@ -57,6 +57,7 @@ private void OnNotificationReceived(Notification notification) NotificationType.ACAdapterConnected => _settings.Store.Notifications.ACAdapter, NotificationType.ACAdapterConnectedLowWattage => _settings.Store.Notifications.ACAdapter, NotificationType.ACAdapterDisconnected => _settings.Store.Notifications.ACAdapter, + NotificationType.AutomationNotification => _settings.Store.Notifications.AutomationNotification, NotificationType.CapsLockOn => _settings.Store.Notifications.CapsNumLock, NotificationType.CapsLockOff => _settings.Store.Notifications.CapsNumLock, NotificationType.CameraOn => _settings.Store.Notifications.CameraLock, @@ -104,6 +105,7 @@ private void OnNotificationReceived(Notification notification) NotificationType.ACAdapterConnected => SymbolRegular.BatteryCharge24, NotificationType.ACAdapterConnectedLowWattage => SymbolRegular.BatteryCharge24, NotificationType.ACAdapterDisconnected => SymbolRegular.BatteryCharge24, + NotificationType.AutomationNotification => SymbolRegular.Rocket24, NotificationType.CapsLockOn => SymbolRegular.KeyboardShiftUppercase24, NotificationType.CapsLockOff => SymbolRegular.KeyboardShiftUppercase24, NotificationType.CameraOn => SymbolRegular.Camera24, @@ -160,6 +162,7 @@ private void OnNotificationReceived(Notification notification) NotificationType.ACAdapterConnected => Resource.Notification_ACAdapterConnected, NotificationType.ACAdapterConnectedLowWattage => Resource.Notification_ACAdapterConnectedLowWattage, NotificationType.ACAdapterDisconnected => Resource.Notification_ACAdapterDisconnected, + NotificationType.AutomationNotification => string.Format("{0}", notification.Args), NotificationType.CapsLockOn => Resource.Notification_CapsLockOn, NotificationType.CapsLockOff => Resource.Notification_CapsLockOff, NotificationType.CameraOn => Resource.Notification_CameraOn, diff --git a/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml b/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml index 75e0ffd163..7e109b0057 100644 --- a/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml +++ b/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml @@ -84,7 +84,7 @@ SelectionChanged="NotificationDurationComboBox_SelectionChanged" /> - + @@ -94,6 +94,16 @@ Click="UpdateAvailableToggle_Click" /> + + + + + + + diff --git a/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml.cs b/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml.cs index e6794fea1e..630f37a973 100644 --- a/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml.cs +++ b/LenovoLegionToolkit.WPF/Windows/Settings/NotificationsSettingsWindow.xaml.cs @@ -28,7 +28,8 @@ public partial class NotificationsSettingsWindow _powerModeCard, _refreshRateCard, _acAdapterCard, - _smartKeyCard + _smartKeyCard, + _automationCard }; public NotificationsSettingsWindow() @@ -51,6 +52,7 @@ public NotificationsSettingsWindow() _refreshRateToggle.IsChecked = _settings.Store.Notifications.RefreshRate; _acAdapterToggle.IsChecked = _settings.Store.Notifications.ACAdapter; _smartKeyToggle.IsChecked = _settings.Store.Notifications.SmartKey; + _automationToggle.IsChecked = _settings.Store.Notifications.AutomationNotification; RefreshCards(); } @@ -202,4 +204,14 @@ private void UpdateAvailableToggle_Click(object sender, RoutedEventArgs e) _settings.Store.Notifications.UpdateAvailable = state.Value; _settings.SynchronizeStore(); } + + private void AutomationToggle_Click(object sender, RoutedEventArgs e) + { + var state = _automationToggle.IsChecked; + if (state is null) + return; + + _settings.Store.Notifications.AutomationNotification = state.Value; + _settings.SynchronizeStore(); + } }