LenovoLegionToolkit

Форк
0
256 строк · 14.9 Кб
1
using System;
2
using System.Windows;
3
using System.Windows.Controls;
4
using System.Windows.Threading;
5
using LenovoLegionToolkit.Lib;
6
using LenovoLegionToolkit.Lib.Settings;
7
using LenovoLegionToolkit.Lib.Utils;
8
using LenovoLegionToolkit.WPF.Extensions;
9
using LenovoLegionToolkit.WPF.Resources;
10
using LenovoLegionToolkit.WPF.Windows;
11
using LenovoLegionToolkit.WPF.Windows.Utils;
12
using Wpf.Ui.Common;
13
using Wpf.Ui.Controls;
14

15
namespace LenovoLegionToolkit.WPF.Utils;
16

17
public class NotificationsManager
18
{
19
    private static Dispatcher Dispatcher => Application.Current.Dispatcher;
20

21
    private readonly ApplicationSettings _settings;
22

23
    private NotificationWindow? _window;
24

25
    public NotificationsManager(ApplicationSettings settings)
26
    {
27
        _settings = settings;
28

29
        MessagingCenter.Subscribe<Notification>(this, OnNotificationReceived);
30
    }
31

32
    private void OnNotificationReceived(Notification notification)
33
    {
34
        Dispatcher.Invoke(() =>
35
        {
36
            if (Log.Instance.IsTraceEnabled)
37
                Log.Instance.Trace($"Notification {notification} received");
38

39
            if (_settings.Store.DontShowNotifications)
40
            {
41
                if (Log.Instance.IsTraceEnabled)
42
                    Log.Instance.Trace($"Notifications are disabled.");
43

44
                return;
45
            }
46

47
            if (FullscreenHelper.IsAnyApplicationFullscreen())
48
            {
49
                if (Log.Instance.IsTraceEnabled)
50
                    Log.Instance.Trace($"Some application is in fullscreen.");
51

52
                return;
53
            }
54

55
            var allow = notification.Type switch
56
            {
57
                NotificationType.ACAdapterConnected => _settings.Store.Notifications.ACAdapter,
58
                NotificationType.ACAdapterConnectedLowWattage => _settings.Store.Notifications.ACAdapter,
59
                NotificationType.ACAdapterDisconnected => _settings.Store.Notifications.ACAdapter,
60
                NotificationType.AutomationNotification => _settings.Store.Notifications.AutomationNotification,
61
                NotificationType.CapsLockOn => _settings.Store.Notifications.CapsNumLock,
62
                NotificationType.CapsLockOff => _settings.Store.Notifications.CapsNumLock,
63
                NotificationType.CameraOn => _settings.Store.Notifications.CameraLock,
64
                NotificationType.CameraOff => _settings.Store.Notifications.CameraLock,
65
                NotificationType.FnLockOn => _settings.Store.Notifications.FnLock,
66
                NotificationType.FnLockOff => _settings.Store.Notifications.FnLock,
67
                NotificationType.MicrophoneOn => _settings.Store.Notifications.Microphone,
68
                NotificationType.MicrophoneOff => _settings.Store.Notifications.Microphone,
69
                NotificationType.NumLockOn => _settings.Store.Notifications.CapsNumLock,
70
                NotificationType.NumLockOff => _settings.Store.Notifications.CapsNumLock,
71
                NotificationType.PanelLogoLightingOn => _settings.Store.Notifications.KeyboardBacklight,
72
                NotificationType.PanelLogoLightingOff => _settings.Store.Notifications.KeyboardBacklight,
73
                NotificationType.PortLightingOn => _settings.Store.Notifications.KeyboardBacklight,
74
                NotificationType.PortLightingOff => _settings.Store.Notifications.KeyboardBacklight,
75
                NotificationType.PowerModeQuiet => _settings.Store.Notifications.PowerMode,
76
                NotificationType.PowerModeBalance => _settings.Store.Notifications.PowerMode,
77
                NotificationType.PowerModePerformance => _settings.Store.Notifications.PowerMode,
78
                NotificationType.PowerModeGodMode => _settings.Store.Notifications.PowerMode,
79
                NotificationType.RefreshRate => _settings.Store.Notifications.RefreshRate,
80
                NotificationType.RGBKeyboardBacklightOff => _settings.Store.Notifications.KeyboardBacklight,
81
                NotificationType.RGBKeyboardBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,
82
                NotificationType.SmartKeyDoublePress => _settings.Store.Notifications.SmartKey,
83
                NotificationType.SmartKeySinglePress => _settings.Store.Notifications.SmartKey,
84
                NotificationType.SpectrumBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,
85
                NotificationType.SpectrumBacklightOff => _settings.Store.Notifications.KeyboardBacklight,
86
                NotificationType.SpectrumBacklightPresetChanged => _settings.Store.Notifications.KeyboardBacklight,
87
                NotificationType.TouchpadOn => _settings.Store.Notifications.TouchpadLock,
88
                NotificationType.TouchpadOff => _settings.Store.Notifications.TouchpadLock,
89
                NotificationType.UpdateAvailable => _settings.Store.Notifications.UpdateAvailable,
90
                NotificationType.WhiteKeyboardBacklightOff => _settings.Store.Notifications.KeyboardBacklight,
91
                NotificationType.WhiteKeyboardBacklightChanged => _settings.Store.Notifications.KeyboardBacklight,
92
                _ => throw new ArgumentException(nameof(notification.Type))
93
            };
94

95
            if (!allow)
96
            {
97
                if (Log.Instance.IsTraceEnabled)
98
                    Log.Instance.Trace($"Notification type {notification.Type} is disabled.");
99

100
                return;
101
            }
102

103
            var symbol = notification.Type switch
104
            {
105
                NotificationType.ACAdapterConnected => SymbolRegular.BatteryCharge24,
106
                NotificationType.ACAdapterConnectedLowWattage => SymbolRegular.BatteryCharge24,
107
                NotificationType.ACAdapterDisconnected => SymbolRegular.BatteryCharge24,
108
                NotificationType.AutomationNotification => SymbolRegular.Rocket24,
109
                NotificationType.CapsLockOn => SymbolRegular.KeyboardShiftUppercase24,
110
                NotificationType.CapsLockOff => SymbolRegular.KeyboardShiftUppercase24,
111
                NotificationType.CameraOn => SymbolRegular.Camera24,
112
                NotificationType.CameraOff => SymbolRegular.Camera24,
113
                NotificationType.FnLockOn => SymbolRegular.Keyboard24,
114
                NotificationType.FnLockOff => SymbolRegular.Keyboard24,
115
                NotificationType.MicrophoneOn => SymbolRegular.Mic24,
116
                NotificationType.MicrophoneOff => SymbolRegular.Mic24,
117
                NotificationType.NumLockOn => SymbolRegular.Keyboard12324,
118
                NotificationType.NumLockOff => SymbolRegular.Keyboard12324,
119
                NotificationType.PanelLogoLightingOn => SymbolRegular.LightbulbCircle24,
120
                NotificationType.PanelLogoLightingOff => SymbolRegular.LightbulbCircle24,
121
                NotificationType.PortLightingOn => SymbolRegular.UsbPlug24,
122
                NotificationType.PortLightingOff => SymbolRegular.UsbPlug24,
123
                NotificationType.PowerModeQuiet => SymbolRegular.Gauge24,
124
                NotificationType.PowerModeBalance => SymbolRegular.Gauge24,
125
                NotificationType.PowerModePerformance => SymbolRegular.Gauge24,
126
                NotificationType.PowerModeGodMode => SymbolRegular.Gauge24,
127
                NotificationType.RefreshRate => SymbolRegular.DesktopPulse24,
128
                NotificationType.RGBKeyboardBacklightOff => SymbolRegular.Lightbulb24,
129
                NotificationType.RGBKeyboardBacklightChanged => SymbolRegular.Lightbulb24,
130
                NotificationType.SmartKeyDoublePress => SymbolRegular.StarEmphasis24,
131
                NotificationType.SmartKeySinglePress => SymbolRegular.Star24,
132
                NotificationType.SpectrumBacklightChanged => SymbolRegular.Lightbulb24,
133
                NotificationType.SpectrumBacklightOff => SymbolRegular.Lightbulb24,
134
                NotificationType.SpectrumBacklightPresetChanged => SymbolRegular.Lightbulb24,
135
                NotificationType.TouchpadOn => SymbolRegular.Tablet24,
136
                NotificationType.TouchpadOff => SymbolRegular.Tablet24,
137
                NotificationType.UpdateAvailable => SymbolRegular.ArrowSync24,
138
                NotificationType.WhiteKeyboardBacklightOff => SymbolRegular.Lightbulb24,
139
                NotificationType.WhiteKeyboardBacklightChanged => SymbolRegular.Lightbulb24,
140
                _ => throw new ArgumentException(nameof(notification.Type))
141
            };
142

143
            SymbolRegular? overlaySymbol = notification.Type switch
144
            {
145
                NotificationType.ACAdapterDisconnected => SymbolRegular.Line24,
146
                NotificationType.CapsLockOff => SymbolRegular.Line24,
147
                NotificationType.CameraOff => SymbolRegular.Line24,
148
                NotificationType.FnLockOff => SymbolRegular.Line24,
149
                NotificationType.MicrophoneOff => SymbolRegular.Line24,
150
                NotificationType.NumLockOff => SymbolRegular.Line24,
151
                NotificationType.PanelLogoLightingOff => SymbolRegular.Line24,
152
                NotificationType.PortLightingOff => SymbolRegular.Line24,
153
                NotificationType.RGBKeyboardBacklightOff => SymbolRegular.Line24,
154
                NotificationType.SpectrumBacklightOff => SymbolRegular.Line24,
155
                NotificationType.TouchpadOff => SymbolRegular.Line24,
156
                NotificationType.WhiteKeyboardBacklightOff => SymbolRegular.Line24,
157
                _ => null,
158
            };
159

160
            var text = notification.Type switch
161
            {
162
                NotificationType.ACAdapterConnected => Resource.Notification_ACAdapterConnected,
163
                NotificationType.ACAdapterConnectedLowWattage => Resource.Notification_ACAdapterConnectedLowWattage,
164
                NotificationType.ACAdapterDisconnected => Resource.Notification_ACAdapterDisconnected,
165
                NotificationType.AutomationNotification => string.Format("{0}", notification.Args),
166
                NotificationType.CapsLockOn => Resource.Notification_CapsLockOn,
167
                NotificationType.CapsLockOff => Resource.Notification_CapsLockOff,
168
                NotificationType.CameraOn => Resource.Notification_CameraOn,
169
                NotificationType.CameraOff => Resource.Notification_CameraOff,
170
                NotificationType.FnLockOn => Resource.Notification_FnLockOn,
171
                NotificationType.FnLockOff => Resource.Notification_FnLockOff,
172
                NotificationType.MicrophoneOn => Resource.Notification_MicrophoneOn,
173
                NotificationType.MicrophoneOff => Resource.Notification_MicrophoneOff,
174
                NotificationType.NumLockOn => Resource.Notification_NumLockOn,
175
                NotificationType.NumLockOff => Resource.Notification_NumLockOff,
176
                NotificationType.PanelLogoLightingOn => Resource.Notification_PanelLogoLightingOn,
177
                NotificationType.PanelLogoLightingOff => Resource.Notification_PanelLogoLightingOff,
178
                NotificationType.PortLightingOn => Resource.Notification_PortLightingOn,
179
                NotificationType.PortLightingOff => Resource.Notification_PortLightingOff,
180
                NotificationType.PowerModeQuiet => string.Format("{0}", notification.Args),
181
                NotificationType.PowerModeBalance => string.Format("{0}", notification.Args),
182
                NotificationType.PowerModePerformance => string.Format("{0}", notification.Args),
183
                NotificationType.PowerModeGodMode => string.Format("{0}", notification.Args),
184
                NotificationType.RefreshRate => string.Format("{0}", notification.Args),
185
                NotificationType.RGBKeyboardBacklightOff => string.Format("{0}", notification.Args),
186
                NotificationType.RGBKeyboardBacklightChanged => string.Format("{0}", notification.Args),
187
                NotificationType.SmartKeyDoublePress => string.Format("{0}", notification.Args),
188
                NotificationType.SmartKeySinglePress => string.Format("{0}", notification.Args),
189
                NotificationType.SpectrumBacklightChanged => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Brightness, notification.Args),
190
                NotificationType.SpectrumBacklightOff => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Backlight, notification.Args),
191
                NotificationType.SpectrumBacklightPresetChanged => string.Format(Resource.Notification_SpectrumKeyboardBacklight_Profile, notification.Args),
192
                NotificationType.TouchpadOn => Resource.Notification_TouchpadOn,
193
                NotificationType.TouchpadOff => Resource.Notification_TouchpadOff,
194
                NotificationType.UpdateAvailable => string.Format(Resource.Notification_UpdateAvailable, notification.Args),
195
                NotificationType.WhiteKeyboardBacklightOff => string.Format(Resource.Notification_WhiteKeyboardBacklight, notification.Args),
196
                NotificationType.WhiteKeyboardBacklightChanged => string.Format(Resource.Notification_WhiteKeyboardBacklight, notification.Args),
197
                _ => throw new ArgumentException(nameof(notification.Type))
198
            };
199

200
            Action<SymbolIcon>? symbolTransform = notification.Type switch
201
            {
202
                NotificationType.PowerModeQuiet => si => si.Foreground = PowerModeState.Quiet.GetSolidColorBrush(),
203
                NotificationType.PowerModePerformance => si => si.Foreground = PowerModeState.Performance.GetSolidColorBrush(),
204
                NotificationType.PowerModeGodMode => si => si.Foreground = PowerModeState.GodMode.GetSolidColorBrush(),
205
                _ => null
206
            };
207

208
            Action? clickAction = notification.Type switch
209
            {
210
                NotificationType.UpdateAvailable => UpdateAvailableAction,
211
                _ => null
212
            };
213

214
            if (symbolTransform is null && overlaySymbol is not null)
215
                symbolTransform = si =>
216
                    si.SetResourceReference(Control.ForegroundProperty, "TextFillColorSecondaryBrush");
217

218
            ShowNotification(symbol, overlaySymbol, symbolTransform, text, clickAction);
219

220
            if (Log.Instance.IsTraceEnabled)
221
                Log.Instance.Trace($"Notification {notification} shown.");
222
        });
223
    }
224

225
    private void ShowNotification(SymbolRegular symbol, SymbolRegular? overlaySymbol, Action<SymbolIcon>? symbolTransform, string text, Action? clickAction)
226
    {
227
        if (App.Current.MainWindow is not MainWindow mainWindow)
228
            return;
229

230
        if (_window is not null)
231
        {
232
            _window.WindowStyle = WindowStyle.None;
233
            _window.Close();
234
        }
235

236
        var nw = new NotificationWindow(symbol, overlaySymbol, symbolTransform, text, clickAction, _settings.Store.NotificationPosition) { Owner = mainWindow };
237
        nw.Show(_settings.Store.NotificationDuration switch
238
        {
239
            NotificationDuration.Short => 500,
240
            NotificationDuration.Long => 2500,
241
            NotificationDuration.Normal => 1000,
242
            _ => throw new ArgumentException(nameof(_settings.Store.NotificationDuration))
243
        });
244

245
        _window = nw;
246
    }
247

248
    private static void UpdateAvailableAction()
249
    {
250
        if (App.Current.MainWindow is not MainWindow mainWindow)
251
            return;
252

253
        mainWindow.BringToForeground();
254
        mainWindow.ShowUpdateWindow();
255
    }
256
}
257

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.