-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathNotifyIcon.cs
209 lines (179 loc) · 6.4 KB
/
NotifyIcon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Telegram.Controls;
using Telegram.Navigation;
using Telegram.Services;
using Windows.ApplicationModel;
using Windows.ApplicationModel.AppService;
using Windows.ApplicationModel.Background;
using Windows.Foundation.Collections;
using Windows.Foundation.Metadata;
using Windows.UI.Xaml;
namespace Telegram.Common
{
public partial class NotifyIcon
{
private static AppServiceConnection _connection;
private static BackgroundTaskDeferral _deferral;
private static readonly DisposableMutex _lock = new();
public static async Task LaunchAsync()
{
if (ApiInformation.IsTypePresent("Windows.ApplicationModel.FullTrustProcessLauncher"))
{
try
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
}
catch
{
// The app has been compiled without desktop bridge
}
}
}
public static async void Connect(AppServiceConnection connection, IBackgroundTaskInstance task)
{
Logger.Info();
task.Canceled += OnCanceled;
using (_lock.Wait())
{
_connection = connection;
_connection.RequestReceived += OnRequestReceived;
_connection.ServiceClosed += OnServiceClosed;
_deferral = task.GetDeferral();
}
var values = new ValueSet
{
{ "ProcessId", Process.GetCurrentProcess().Id },
{ "OpenText", Strings.NotifyIconOpen },
{ "ExitText", Strings.NotifyIconExit }
};
await SendMessageAsync(values);
}
public static Task ExitAsync()
{
return SendMessageAsync("Exit");
}
public static async void Debug(string value)
{
var response = await SendMessageAsync("Debug", value);
if (response?.Status == AppServiceResponseStatus.Success)
{
if (response.Message.TryGet<string>("Debug", out var message))
{
// TODO: XamlRoot here is not needed, WinUI 3 will not have this code
await MessagePopup.ShowAsync(null as XamlRoot, message);
}
}
}
public static void LoopbackExempt(bool enabled)
{
_ = SendMessageAsync("LoopbackExempt", enabled);
}
public static void SendUnreadCount(int unreadCount, int unreadMutedCount)
{
_ = SendMessageAsync(new ValueSet { { "UnreadCount", unreadCount }, { "UnreadUnmutedCount", unreadMutedCount } });
}
private static Task<AppServiceResponse> SendMessageAsync(string message, object parameter = null, bool reconnect = true)
{
return SendMessageAsync(new ValueSet { { message, parameter ?? true } }, reconnect);
}
private static async Task<AppServiceResponse> SendMessageAsync(ValueSet message, bool reconnect = true)
{
try
{
AppServiceResponse response = null;
using (_lock.Wait())
{
var connection = _connection;
if (connection == null)
{
return null;
}
if (SettingsService.Current.Diagnostics.BridgeDebug)
{
foreach (var item in message)
{
Logger.Info(item.Key);
break;
}
}
var task = connection.SendMessageAsync(message).AsTask();
var completed = await Task.WhenAny(task, Task.Delay(500));
if (task == completed)
{
response = task.Result;
}
}
if (response?.Status != AppServiceResponseStatus.Success)
{
Logger.Error(response == null ? "Timeout" : response.Status);
if (reconnect)
{
Cancel();
}
}
else if (SettingsService.Current.Diagnostics.BridgeDebug)
{
Logger.Info("Succeeded");
}
return response;
}
catch (Exception ex)
{
// All the remote procedure calls must be wrapped in a try-catch block
// ToString not to send to AppCenter
Logger.Error(ex.ToString());
if (reconnect)
{
Cancel();
}
}
return null;
}
private static async void OnRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
if (args.Request.Message.ContainsKey("Exit"))
{
await BootStrapper.ConsolidateAsync();
}
}
private static void OnServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
{
Logger.Debug(args.Status);
Cancel();
}
private static void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
Logger.Debug(reason);
Cancel();
}
private static void Cancel()
{
if (SettingsService.Current.Diagnostics.BridgeDebug)
{
Logger.Info();
}
using (_lock.Wait())
{
if (_connection != null)
{
_connection.RequestReceived -= OnRequestReceived;
_connection.ServiceClosed -= OnServiceClosed;
_connection = null;
}
if (_deferral != null)
{
_deferral.Complete();
_deferral = null;
}
}
}
}
}