using System.Threading.Tasks; using Xamarin.Forms.Internals; using ElmSharp; using EImage = ElmSharp.Image; using ESize = ElmSharp.Size; using EColor = ElmSharp.Color; namespace Xamarin.Forms.Platform.Tizen.Native { /// /// Extends the ElmSharp.Image class with functionality useful to renderer. /// public class Image : EImage, IMeasurable { Aspect _aspect; bool _imageLoadCompleted; /// /// Initializes a new instance of the class. /// /// The parent EvasObject. public Image(EvasObject parent) : base(parent) { IsScaling = true; CanScaleUp = true; CanScaleDown = true; ApplyAspect(Aspect.AspectFit); } /// /// Gets or sets the image aspect ratio preserving option. /// /// The aspect option. public Aspect Aspect { get { return _aspect; } set { if (_aspect != value) { ApplyAspect(value); } } } /// /// Gets or sets the image color. /// public override EColor Color { get { return base.Color; } set { if (_imageLoadCompleted) { base.Color = value; } } } /// /// Loads image data from the given asynchronously. /// /// A task which will be completed when image data is loaded. /// Image source specifying from where the image data has to be loaded. public async Task LoadFromImageSourceAsync(ImageSource source) { _imageLoadCompleted = false; IImageSourceHandler handler; if (source != null && (handler = Registrar.Registered.GetHandler(source.GetType())) != null) { _imageLoadCompleted = await handler.LoadImageAsync(this, source); } return _imageLoadCompleted; } /// /// Implements the interface. /// /// Available width. /// Available height. public ESize Measure(int availableWidth, int availableHeight) { var imageSize = ObjectSize; var size = new ESize() { Width = imageSize.Width, Height = imageSize.Height, }; if (0 != availableWidth && 0 != availableHeight && (imageSize.Width > availableWidth || imageSize.Height > availableHeight)) { // when available size is limited and insufficient for the image ... double imageRatio = (double)imageSize.Width / (double)imageSize.Height; double availableRatio = (double)availableWidth / (double)availableHeight; // depending on the relation between availableRatio and imageRatio, copy the availableWidth or availableHeight // and calculate the size which preserves the image ratio, but does not exceed the available size size.Width = availableRatio > imageRatio ? imageSize.Width * availableHeight / imageSize.Height : availableWidth; size.Height = availableRatio > imageRatio ? availableHeight : imageSize.Height * availableWidth / imageSize.Width; } return size; } /// /// Sets the IsFixedAspect and CanFillOutside properties according to the given . /// /// The aspect setting to be applied to the image. void ApplyAspect(Aspect aspect) { _aspect = aspect; switch (_aspect) { case Aspect.AspectFit: IsFixedAspect = true; CanFillOutside = false; break; case Aspect.AspectFill: IsFixedAspect = true; CanFillOutside = true; break; case Aspect.Fill: IsFixedAspect = false; CanFillOutside = false; break; default: Log.Warn("Invalid Aspect value: {0}", _aspect); break; } } } }