LenovoLegionToolkit
256 строк · 14.9 Кб
1using System;2using System.Windows;3using System.Windows.Controls;4using System.Windows.Threading;5using LenovoLegionToolkit.Lib;6using LenovoLegionToolkit.Lib.Settings;7using LenovoLegionToolkit.Lib.Utils;8using LenovoLegionToolkit.WPF.Extensions;9using LenovoLegionToolkit.WPF.Resources;10using LenovoLegionToolkit.WPF.Windows;11using LenovoLegionToolkit.WPF.Windows.Utils;12using Wpf.Ui.Common;13using Wpf.Ui.Controls;14
15namespace LenovoLegionToolkit.WPF.Utils;16
17public class NotificationsManager18{
19private static Dispatcher Dispatcher => Application.Current.Dispatcher;20
21private readonly ApplicationSettings _settings;22
23private NotificationWindow? _window;24
25public NotificationsManager(ApplicationSettings settings)26{27_settings = settings;28
29MessagingCenter.Subscribe<Notification>(this, OnNotificationReceived);30}31
32private void OnNotificationReceived(Notification notification)33{34Dispatcher.Invoke(() =>35{36if (Log.Instance.IsTraceEnabled)37Log.Instance.Trace($"Notification {notification} received");38
39if (_settings.Store.DontShowNotifications)40{41if (Log.Instance.IsTraceEnabled)42Log.Instance.Trace($"Notifications are disabled.");43
44return;45}46
47if (FullscreenHelper.IsAnyApplicationFullscreen())48{49if (Log.Instance.IsTraceEnabled)50Log.Instance.Trace($"Some application is in fullscreen.");51
52return;53}54
55var allow = notification.Type switch56{57NotificationType.ACAdapterConnected => _settings.Store.Notifications.ACAdapter,58NotificationType.ACAdapterConnectedLowWattage => _settings.Store.Notifications.ACAdapter,59NotificationType.ACAdapterDisconnected => _settings.Store.Notifications.ACAdapter,60NotificationType.AutomationNotification => _settings.Store.Notifications.AutomationNotification,61NotificationType.CapsLockOn => _settings.Store.Notifications.CapsNumLock,62NotificationType.CapsLockOff => _settings.Store.Notifications.CapsNumLock,63NotificationType.CameraOn => _settings.Store.Notifications.CameraLock,64NotificationType.CameraOff => _settings.Store.Notifications.CameraLock,65NotificationType.FnLockOn => _settings.Store.Notifications.FnLock,66NotificationType.FnLockOff => _settings.Store.Notifications.FnLock,67NotificationType.MicrophoneOn => _settings.Store.Notifications.Microphone,68NotificationType.MicrophoneOff => _settings.Store.Notifications.Microphone,69NotificationType.NumLockOn => _settings.Store.Notifications.CapsNumLock,70NotificationType.NumLockOff => _settings.Store.Notifications.CapsNumLock,71NotificationType.PanelLogoLightingOn => _settings.Store.Notifications.KeyboardBacklight,72NotificationType.PanelLogoLightingOff => _settings.Store.Notifications.KeyboardBacklight,73NotificationType.PortLightingOn => _settings.Store.Notifications.KeyboardBacklight,74NotificationType.PortLightingOff => _settings.Store.Notifications.KeyboardBacklight,75NotificationType.PowerModeQuiet => _settings.Store.Notifications.PowerMode,76NotificationType.PowerModeBalance => _settings.Store.Notifications.PowerMode,77NotificationType.PowerModePerformance => _settings.Store.Notifications.PowerMode,78NotificationType.PowerModeGodMode => _settings.Store.Notifications.PowerMode,79NotificationType.RefreshRate => _settings.Store.Notifications.RefreshRate,80NotificationType.RGBKeyboardBacklightOff => _settings.Store.Notifications.KeyboardBacklight,81NotificationType.RGBKeyboardBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,82NotificationType.SmartKeyDoublePress => _settings.Store.Notifications.SmartKey,83NotificationType.SmartKeySinglePress => _settings.Store.Notifications.SmartKey,84NotificationType.SpectrumBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,85NotificationType.SpectrumBacklightOff => _settings.Store.Notifications.KeyboardBacklight,86NotificationType.SpectrumBacklightPresetChanged => _settings.Store.Notifications.KeyboardBacklight,87NotificationType.TouchpadOn => _settings.Store.Notifications.TouchpadLock,88NotificationType.TouchpadOff => _settings.Store.Notifications.TouchpadLock,89NotificationType.UpdateAvailable => _settings.Store.Notifications.UpdateAvailable,90NotificationType.WhiteKeyboardBacklightOff => _settings.Store.Notifications.KeyboardBacklight,91NotificationType.WhiteKeyboardBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,92_ => throw new ArgumentException(nameof(notification.Type))93};94
95if (!allow)96{97if (Log.Instance.IsTraceEnabled)98Log.Instance.Trace($"Notification type {notification.Type} is disabled.");99
100return;101}102
103var symbol = notification.Type switch104{105NotificationType.ACAdapterConnected => SymbolRegular.BatteryCharge24,106NotificationType.ACAdapterConnectedLowWattage => SymbolRegular.BatteryCharge24,107NotificationType.ACAdapterDisconnected => SymbolRegular.BatteryCharge24,108NotificationType.AutomationNotification => SymbolRegular.Rocket24,109NotificationType.CapsLockOn => SymbolRegular.KeyboardShiftUppercase24,110NotificationType.CapsLockOff => SymbolRegular.KeyboardShiftUppercase24,111NotificationType.CameraOn => SymbolRegular.Camera24,112NotificationType.CameraOff => SymbolRegular.Camera24,113NotificationType.FnLockOn => SymbolRegular.Keyboard24,114NotificationType.FnLockOff => SymbolRegular.Keyboard24,115NotificationType.MicrophoneOn => SymbolRegular.Mic24,116NotificationType.MicrophoneOff => SymbolRegular.Mic24,117NotificationType.NumLockOn => SymbolRegular.Keyboard12324,118NotificationType.NumLockOff => SymbolRegular.Keyboard12324,119NotificationType.PanelLogoLightingOn => SymbolRegular.LightbulbCircle24,120NotificationType.PanelLogoLightingOff => SymbolRegular.LightbulbCircle24,121NotificationType.PortLightingOn => SymbolRegular.UsbPlug24,122NotificationType.PortLightingOff => SymbolRegular.UsbPlug24,123NotificationType.PowerModeQuiet => SymbolRegular.Gauge24,124NotificationType.PowerModeBalance => SymbolRegular.Gauge24,125NotificationType.PowerModePerformance => SymbolRegular.Gauge24,126NotificationType.PowerModeGodMode => SymbolRegular.Gauge24,127NotificationType.RefreshRate => SymbolRegular.DesktopPulse24,128NotificationType.RGBKeyboardBacklightOff => SymbolRegular.Lightbulb24,129NotificationType.RGBKeyboardBacklightChanged => SymbolRegular.Lightbulb24,130NotificationType.SmartKeyDoublePress => SymbolRegular.StarEmphasis24,131NotificationType.SmartKeySinglePress => SymbolRegular.Star24,132NotificationType.SpectrumBacklightChanged => SymbolRegular.Lightbulb24,133NotificationType.SpectrumBacklightOff => SymbolRegular.Lightbulb24,134NotificationType.SpectrumBacklightPresetChanged => SymbolRegular.Lightbulb24,135NotificationType.TouchpadOn => SymbolRegular.Tablet24,136NotificationType.TouchpadOff => SymbolRegular.Tablet24,137NotificationType.UpdateAvailable => SymbolRegular.ArrowSync24,138NotificationType.WhiteKeyboardBacklightOff => SymbolRegular.Lightbulb24,139NotificationType.WhiteKeyboardBacklightChanged => SymbolRegular.Lightbulb24,140_ => throw new ArgumentException(nameof(notification.Type))141};142
143SymbolRegular? overlaySymbol = notification.Type switch144{145NotificationType.ACAdapterDisconnected => SymbolRegular.Line24,146NotificationType.CapsLockOff => SymbolRegular.Line24,147NotificationType.CameraOff => SymbolRegular.Line24,148NotificationType.FnLockOff => SymbolRegular.Line24,149NotificationType.MicrophoneOff => SymbolRegular.Line24,150NotificationType.NumLockOff => SymbolRegular.Line24,151NotificationType.PanelLogoLightingOff => SymbolRegular.Line24,152NotificationType.PortLightingOff => SymbolRegular.Line24,153NotificationType.RGBKeyboardBacklightOff => SymbolRegular.Line24,154NotificationType.SpectrumBacklightOff => SymbolRegular.Line24,155NotificationType.TouchpadOff => SymbolRegular.Line24,156NotificationType.WhiteKeyboardBacklightOff => SymbolRegular.Line24,157_ => null,158};159
160var text = notification.Type switch161{162NotificationType.ACAdapterConnected => Resource.Notification_ACAdapterConnected,163NotificationType.ACAdapterConnectedLowWattage => Resource.Notification_ACAdapterConnectedLowWattage,164NotificationType.ACAdapterDisconnected => Resource.Notification_ACAdapterDisconnected,165NotificationType.AutomationNotification => string.Format("{0}", notification.Args),166NotificationType.CapsLockOn => Resource.Notification_CapsLockOn,167NotificationType.CapsLockOff => Resource.Notification_CapsLockOff,168NotificationType.CameraOn => Resource.Notification_CameraOn,169NotificationType.CameraOff => Resource.Notification_CameraOff,170NotificationType.FnLockOn => Resource.Notification_FnLockOn,171NotificationType.FnLockOff => Resource.Notification_FnLockOff,172NotificationType.MicrophoneOn => Resource.Notification_MicrophoneOn,173NotificationType.MicrophoneOff => Resource.Notification_MicrophoneOff,174NotificationType.NumLockOn => Resource.Notification_NumLockOn,175NotificationType.NumLockOff => Resource.Notification_NumLockOff,176NotificationType.PanelLogoLightingOn => Resource.Notification_PanelLogoLightingOn,177NotificationType.PanelLogoLightingOff => Resource.Notification_PanelLogoLightingOff,178NotificationType.PortLightingOn => Resource.Notification_PortLightingOn,179NotificationType.PortLightingOff => Resource.Notification_PortLightingOff,180NotificationType.PowerModeQuiet => string.Format("{0}", notification.Args),181NotificationType.PowerModeBalance => string.Format("{0}", notification.Args),182NotificationType.PowerModePerformance => string.Format("{0}", notification.Args),183NotificationType.PowerModeGodMode => string.Format("{0}", notification.Args),184NotificationType.RefreshRate => string.Format("{0}", notification.Args),185NotificationType.RGBKeyboardBacklightOff => string.Format("{0}", notification.Args),186NotificationType.RGBKeyboardBacklightChanged => string.Format("{0}", notification.Args),187NotificationType.SmartKeyDoublePress => string.Format("{0}", notification.Args),188NotificationType.SmartKeySinglePress => string.Format("{0}", notification.Args),189NotificationType.SpectrumBacklightChanged => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Brightness, notification.Args),190NotificationType.SpectrumBacklightOff => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Backlight, notification.Args),191NotificationType.SpectrumBacklightPresetChanged => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Profile, notification.Args),192NotificationType.TouchpadOn => Resource.Notification_TouchpadOn,193NotificationType.TouchpadOff => Resource.Notification_TouchpadOff,194NotificationType.UpdateAvailable => string.Format(Resource.Notification_UpdateAvailable, notification.Args),195NotificationType.WhiteKeyboardBacklightOff => string.Format(Resource.Notification_WhiteKeyboardBacklight, notification.Args),196NotificationType.WhiteKeyboardBacklightChanged => string.Format(Resource.Notification_WhiteKeyboardBacklight, notification.Args),197_ => throw new ArgumentException(nameof(notification.Type))198};199
200Action<SymbolIcon>? symbolTransform = notification.Type switch201{202NotificationType.PowerModeQuiet => si => si.Foreground = PowerModeState.Quiet.GetSolidColorBrush(),203NotificationType.PowerModePerformance => si => si.Foreground = PowerModeState.Performance.GetSolidColorBrush(),204NotificationType.PowerModeGodMode => si => si.Foreground = PowerModeState.GodMode.GetSolidColorBrush(),205_ => null206};207
208Action? clickAction = notification.Type switch209{210NotificationType.UpdateAvailable => UpdateAvailableAction,211_ => null212};213
214if (symbolTransform is null && overlaySymbol is not null)215symbolTransform = si =>216si.SetResourceReference(Control.ForegroundProperty, "TextFillColorSecondaryBrush");217
218ShowNotification(symbol, overlaySymbol, symbolTransform, text, clickAction);219
220if (Log.Instance.IsTraceEnabled)221Log.Instance.Trace($"Notification {notification} shown.");222});223}224
225private void ShowNotification(SymbolRegular symbol, SymbolRegular? overlaySymbol, Action<SymbolIcon>? symbolTransform, string text, Action? clickAction)226{227if (App.Current.MainWindow is not MainWindow mainWindow)228return;229
230if (_window is not null)231{232_window.WindowStyle = WindowStyle.None;233_window.Close();234}235
236var nw = new NotificationWindow(symbol, overlaySymbol, symbolTransform, text, clickAction, _settings.Store.NotificationPosition) { Owner = mainWindow };237nw.Show(_settings.Store.NotificationDuration switch238{239NotificationDuration.Short => 500,240NotificationDuration.Long => 2500,241NotificationDuration.Normal => 1000,242_ => throw new ArgumentException(nameof(_settings.Store.NotificationDuration))243});244
245_window = nw;246}247
248private static void UpdateAvailableAction()249{250if (App.Current.MainWindow is not MainWindow mainWindow)251return;252
253mainWindow.BringToForeground();254mainWindow.ShowUpdateWindow();255}256}
257