-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathImageView.cs
420 lines (372 loc) · 14 KB
/
ImageView.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//
// 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 Telegram.Common;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.ViewModels;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace Telegram.Controls
{
public partial class ImageView : HyperlinkButton
{
protected FrameworkElement Holder;
public ImageView()
{
DefaultStyleKey = typeof(ImageView);
}
protected override void OnApplyTemplate()
{
Holder = (FrameworkElement)GetTemplateChild("Holder");
Holder.Loaded += Holder_Loaded;
if (Holder is Image image)
{
image.ImageFailed += Holder_ImageFailed;
image.ImageOpened += Holder_ImageOpened;
}
}
private void Holder_Loaded(object sender, RoutedEventArgs e)
{
OnSourceChanged(Source, null);
}
private void Holder_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
ImageFailed?.Invoke(this, e);
}
private void Holder_ImageOpened(object sender, RoutedEventArgs e)
{
ImageOpened?.Invoke(this, e);
}
#region Source
public ImageSource Source
{
get => (ImageSource)GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageView), new PropertyMetadata(null, OnSourceChanged));
private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ImageView)d).OnSourceChanged((ImageSource)e.NewValue, (ImageSource)e.OldValue);
}
private void OnSourceChanged(ImageSource newValue, ImageSource oldValue)
{
if (newValue is WriteableBitmap bitmap && bitmap.PixelWidth > 0 && bitmap.PixelHeight > 0)
{
ImageOpened?.Invoke(this, null);
}
}
#endregion
#region Stretch
public Stretch Stretch
{
get => (Stretch)GetValue(StretchProperty);
set => SetValue(StretchProperty, value);
}
public static readonly DependencyProperty StretchProperty =
DependencyProperty.Register("Stretch", typeof(Stretch), typeof(ImageView), new PropertyMetadata(Stretch.Uniform));
#endregion
#region Constraint
public object Constraint
{
get => GetValue(ConstraintProperty);
set => SetValue(ConstraintProperty, value);
}
public static readonly DependencyProperty ConstraintProperty =
DependencyProperty.Register("Constraint", typeof(object), typeof(ImageView), new PropertyMetadata(null, OnConstraintChanged));
private static void OnConstraintChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ImageView)d).InvalidateMeasure();
}
#endregion
protected override Size MeasureOverride(Size availableSize)
{
if (Constraint == null)
{
return base.MeasureOverride(availableSize);
}
var availableWidth = Math.Min(availableSize.Width, Math.Min(double.IsNaN(Width) ? double.PositiveInfinity : Width, MaxWidth));
var availableHeight = Math.Min(availableSize.Height, Math.Min(double.IsNaN(Height) ? double.PositiveInfinity : Height, MaxHeight));
var width = 0.0;
var height = 0.0;
var constraint = Constraint;
if (constraint is MessageViewModel viewModel)
{
//ttl = viewModel.SelfDestructTime > 0;
constraint = viewModel.Content;
}
else if (constraint is Message message)
{
//ttl = message.SelfDestructTime > 0;
constraint = message.Content;
}
if (constraint is MessageAnimation animationMessage)
{
constraint = animationMessage.Animation;
}
else if (constraint is MessageInvoice invoiceMessage)
{
if (invoiceMessage.PaidMedia is PaidMediaPhoto paidMediaPhoto)
{
constraint = paidMediaPhoto.Photo;
}
else if (invoiceMessage.PaidMedia is PaidMediaVideo paidMediaVideo)
{
constraint = paidMediaVideo.Video;
}
else if (invoiceMessage.PaidMedia is PaidMediaPreview paidMediaPreview)
{
width = paidMediaPreview.Width;
height = paidMediaPreview.Height;
}
else
{
constraint = invoiceMessage.ProductInfo.Photo;
}
}
else if (constraint is MessageGame gameMessage)
{
if (gameMessage.Game.Animation != null)
{
constraint = gameMessage.Game.Animation;
}
else if (gameMessage.Game.Photo != null)
{
constraint = gameMessage.Game.Photo;
}
}
else if (constraint is MessageLocation locationMessage)
{
constraint = locationMessage.Location;
}
else if (constraint is MessagePhoto photoMessage)
{
constraint = photoMessage.Photo;
}
else if (constraint is MessageSticker stickerMessage)
{
constraint = stickerMessage.Sticker;
}
else if (constraint is MessageText textMessage)
{
if (textMessage?.LinkPreview?.Type is LinkPreviewTypeBackground)
{
width = 900;
height = 1600;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeAnimation previewAnimation)
{
constraint = previewAnimation.Animation;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeDocument previewDocument)
{
constraint = previewDocument.Document;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeEmbeddedAnimationPlayer previewEmbeddedAnimationPlayer)
{
width = previewEmbeddedAnimationPlayer.Width;
height = previewEmbeddedAnimationPlayer.Height;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeEmbeddedVideoPlayer previewEmbeddedVideoPlayer)
{
width = previewEmbeddedVideoPlayer.Width;
height = previewEmbeddedVideoPlayer.Height;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypePhoto previewPhoto)
{
constraint = previewPhoto.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeSticker previewSticker)
{
constraint = previewSticker.Sticker;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideo previewVideo)
{
constraint = previewVideo.Video;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideoNote videoNote)
{
constraint = videoNote.VideoNote;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeApp app)
{
constraint = app.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeArticle article)
{
constraint = article.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeChannelBoost channelBoost)
{
constraint = channelBoost.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeChat chat)
{
constraint = chat.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeSupergroupBoost supergroupBoost)
{
constraint = supergroupBoost.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeUser user)
{
constraint = user.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeVideoChat videoChat)
{
constraint = videoChat.Photo;
}
else if (textMessage?.LinkPreview?.Type is LinkPreviewTypeWebApp webApp)
{
constraint = webApp.Photo;
}
}
else if (constraint is MessageVenue venueMessage)
{
constraint = venueMessage.Venue;
}
else if (constraint is MessageVideo videoMessage)
{
constraint = videoMessage.Video;
}
else if (constraint is MessageVideoNote videoNoteMessage)
{
constraint = videoNoteMessage.VideoNote;
}
else if (constraint is MessageChatChangePhoto chatChangePhoto)
{
constraint = chatChangePhoto.Photo;
}
else if (constraint is PaidMediaPreview paidMediaPreview)
{
width = paidMediaPreview.Width;
height = paidMediaPreview.Height;
}
if (constraint is Animation animation)
{
width = animation.Width;
height = animation.Height;
}
else if (constraint is Location location)
{
width = 320;
height = 200;
}
else if (constraint is Photo photo)
{
var size = photo.Sizes.Count > 0 ? photo.Sizes[^1] : null;
if (size != null)
{
width = size.Width;
height = size.Height;
}
}
else if (constraint is ChatPhoto chatPhoto)
{
var size = chatPhoto.Sizes.Count > 0 ? chatPhoto.Sizes[^1] : null;
if (size != null)
{
width = size.Width;
height = size.Height;
}
}
else if (constraint is Sticker sticker)
{
width = sticker.Width;
height = sticker.Height;
}
else if (constraint is Venue venue)
{
width = 320;
height = 200;
}
else if (constraint is Video video)
{
width = video.Width;
height = video.Height;
}
else if (constraint is VideoNote videoNote)
{
width = 200;
height = 200;
}
if (constraint is PhotoSize photoSize)
{
width = photoSize.Width;
height = photoSize.Height;
}
if (constraint is PageBlockMap map)
{
width = map.Width;
height = map.Height;
}
if (constraint is Background wallpaper)
{
width = 900;
height = 1600;
}
Calculate:
if (width > availableWidth || height > availableHeight)
{
var ratioX = availableWidth / width;
var ratioY = availableHeight / height;
var ratio = Math.Min(ratioX, ratioY);
//if (Holder != null)
//{
// Holder.Width = width * ratio;
// Holder.Height = height * ratio;
//}
return new Size(width * ratio, height * ratio);
}
else
{
return new Size(width, height);
}
}
public event ExceptionRoutedEventHandler ImageFailed;
public event RoutedEventHandler ImageOpened;
#region Bitmap
private IClientService _clientService;
private File _file;
private int _width;
private int _height;
private long _fileToken;
public void SetSource(IClientService clientService, File file, int width = 0, int height = 0)
{
_clientService = clientService;
_file = file;
_width = width;
_height = height;
Source = GetSource(clientService, file, width, height, true);
}
private ImageSource GetSource(IClientService clientService, File file, int width, int height, bool download)
{
if (file.Local.IsDownloadingCompleted)
{
return UriEx.ToBitmap(file.Local.Path, width, height);
}
else if (download)
{
UpdateManager.Subscribe(this, clientService, file, ref _fileToken, UpdateSource, true);
if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingActive)
{
clientService.DownloadFile(file.Id, 16);
}
}
return null;
}
private void UpdateSource(object target, File file)
{
Source = GetSource(_clientService, _file, _width, _height, false);
}
#endregion
}
}