protected static bool IsFullMedia(TLMessageMediaBase media, bool width = false) { if (media == null) { return(false); } if (media.TypeId == TLType.MessageMediaGeo) { return(true); } else if (media.TypeId == TLType.MessageMediaVenue) { return(true); } else if (media.TypeId == TLType.MessageMediaPhoto) { return(true); } else if (media.TypeId == TLType.MessageMediaDocument) { var documentMedia = media as TLMessageMediaDocument; if (TLMessage.IsGif(documentMedia.Document)) { return(true); } else if (TLMessage.IsVideo(documentMedia.Document)) { return(true); } } else if (media.TypeId == TLType.MessageMediaInvoice && width) { var invoiceMedia = media as TLMessageMediaInvoice; if (invoiceMedia.HasPhoto && invoiceMedia.Photo != null) { return(true); } } else if (media.TypeId == TLType.MessageMediaWebPage && width) { var webPageMedia = media as TLMessageMediaWebPage; var webPage = webPageMedia.WebPage as TLWebPage; if (webPage != null && MediaTemplateSelector.IsWebPagePhotoTemplate(webPage)) { return(true); } } return(false); }
protected override Size MeasureOverride(Size availableSize) { if (ViewModel?.Media == null || !IsFullMedia(ViewModel?.Media, true)) { return(base.MeasureOverride(availableSize)); } var sumWidth = 0.0; object constraint = null; if (ViewModel?.Media is TLMessageMediaPhoto photoMedia) { constraint = photoMedia.Photo; } else if (ViewModel?.Media is TLMessageMediaDocument documentMedia) { constraint = documentMedia.Document; } else if (ViewModel?.Media is TLMessageMediaInvoice invoiceMedia) { constraint = invoiceMedia.Photo; } else if (ViewModel?.Media is TLMessageMediaWebPage webPageMedia) { if (webPageMedia.WebPage is TLWebPage webPage && MediaTemplateSelector.IsWebPagePhotoTemplate(webPage)) { sumWidth = 8 + 10 + 10; constraint = webPage.Photo; } } else if (ViewModel?.Media is TLMessageMediaGeo || ViewModel?.Media is TLMessageMediaVenue) { constraint = ViewModel?.Media; } if (constraint == null) { return(base.MeasureOverride(availableSize)); } var availableWidth = Math.Min(availableSize.Width, Math.Min(double.IsNaN(Width) ? double.PositiveInfinity : Width, 320 + sumWidth)); var availableHeight = Math.Min(availableSize.Height, Math.Min(double.IsNaN(Height) ? double.PositiveInfinity : Height, 420)); var width = 0.0; var height = 0.0; if (constraint is TLMessageMediaGeo || constraint is TLMessageMediaVenue) { width = 320; height = 240; goto Calculate; } var photo = constraint as TLPhoto; if (photo != null) { //var photoSize = photo.Sizes.OrderByDescending(x => x.W).FirstOrDefault(); var photoSize = photo.Sizes.OfType <TLPhotoSize>().OrderByDescending(x => x.W).FirstOrDefault(); if (photoSize != null) { width = photoSize.W; height = photoSize.H; goto Calculate; } } if (constraint is TLDocument document) { constraint = document.Attributes; } if (constraint is TLWebDocument webDocument) { constraint = webDocument.Attributes; } if (constraint is TLVector <TLDocumentAttributeBase> attributes) { var imageSize = attributes.OfType <TLDocumentAttributeImageSize>().FirstOrDefault(); if (imageSize != null) { width = imageSize.W; height = imageSize.H; goto Calculate; } var video = attributes.OfType <TLDocumentAttributeVideo>().FirstOrDefault(); if (video != null) { width = video.W; height = video.H; goto Calculate; } } Calculate: if (_stateControl == null) { _stateControl = FindName("StatusControl") as FrameworkElement; } if (_stateControl.DesiredSize.IsEmpty) { _stateControl.Measure(availableSize); } width = Math.Max(_stateControl.DesiredSize.Width + /*margin left*/ 8 + /*padding right*/ 6 + /*margin right*/ 6, Math.Max(width, 96) + sumWidth); if (width > availableWidth || height > availableHeight) { var ratioX = availableWidth / width; var ratioY = availableHeight / height; var ratio = Math.Min(ratioX, ratioY); return(base.MeasureOverride(new Size(Math.Max(96, width * ratio), availableSize.Height))); } else { return(base.MeasureOverride(new Size(Math.Max(96, width), availableSize.Height))); } }