summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs
blob: f402178befc04712c2706b1db8b7506eddab3950 (plain)
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
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;

using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.Image;

namespace Xamarin.Forms.Platform.Tizen
{
	public class ImageRenderer : ViewRenderer<Image, Native.Image>
	{
		public ImageRenderer()
		{
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
		{
			if (Control == null)
			{
				var image = new Native.Image(Forms.Context.MainWindow);
				SetNativeControl(image);
			}

			if (e.NewElement != null)
			{
				UpdateAll();
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);
			if (e.PropertyName == Image.SourceProperty.PropertyName)
			{
				UpdateSource();
			}
			else if (e.PropertyName == Image.AspectProperty.PropertyName)
			{
				UpdateAspect();
			}
			else if (e.PropertyName == Image.IsOpaqueProperty.PropertyName)
			{
				UpdateIsOpaque();
			}
			else if (e.PropertyName == Specific.BlendColorProperty.PropertyName)
			{
				UpdateBlendColor();
			}
		}

		async void UpdateSource()
		{
			ImageSource source = Element.Source;

			((IImageController)Element).SetIsLoading(true);

			if (Control != null)
			{
				bool success = await Control.LoadFromImageSourceAsync(source);
				if (!IsDisposed && success)
				{
					((IVisualElementController)Element).NativeSizeChanged();
					UpdateAfterLoading();
				}
			}

			if (!IsDisposed)
				((IImageController)Element).SetIsLoading(false);
		}

		protected virtual void UpdateAfterLoading()
		{
			UpdateIsOpaque();
			UpdateBlendColor();
		}

		void UpdateAspect()
		{
			Control.Aspect = Element.Aspect;
		}

		void UpdateIsOpaque()
		{
			Control.IsOpaque = Element.IsOpaque;
		}

		void UpdateBlendColor()
		{
			Control.Color = Specific.GetBlendColor(Element).ToNative();
		}

		void UpdateAll()
		{
			UpdateSource();
			UpdateAspect();
		}
	}

	public interface IImageSourceHandler : IRegisterable
	{
		Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken));
	}

	public sealed class FileImageSourceHandler : IImageSourceHandler
	{
		public Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken))
		{
			var filesource = imageSource as FileImageSource;
			if (filesource != null)
			{
				string file = filesource.File;
				if (!string.IsNullOrEmpty(file))
					return image.LoadAsync(ResourcePath.GetPath(file), cancelationToken);
			}
			return Task.FromResult<bool>(false);
		}
	}

	public sealed class StreamImageSourceHandler : IImageSourceHandler
	{
		public async Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken))
		{
			var streamsource = imageSource as StreamImageSource;
			if (streamsource != null && streamsource.Stream != null)
			{
				using (var streamImage = await ((IStreamImageSource)streamsource).GetStreamAsync(cancelationToken))
				{
					if (streamImage != null)
						return await image.LoadAsync(streamImage, cancelationToken);
				}
			}
			return false;
		}
	}

	public sealed class UriImageSourceHandler : IImageSourceHandler
	{
		public Task<bool> LoadImageAsync(Native.Image image, ImageSource imageSource, CancellationToken cancelationToken = default(CancellationToken))
		{
			var urisource = imageSource as UriImageSource;
			if (urisource != null && urisource.Uri != null)
			{
				return image.LoadAsync(urisource.Uri, cancelationToken);
			}

			return Task.FromResult<bool>(false);
		}
	}
}