summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/WebViewRenderer.cs
blob: 56579fb70c76cf3d2752ef44505e9ba74b181f99 (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
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
´╗┐using System;
using System.ComponentModel;
using Xamarin.Forms.Internals;
using TChromium = Tizen.WebView.Chromium;
using TWebView = Tizen.WebView.WebView;

namespace Xamarin.Forms.Platform.Tizen
{
	public class WebViewRenderer : VisualElementRenderer<WebView>, IWebViewDelegate
	{
		bool _updating;
		WebNavigationEvent _eventState;
		TWebView _control = null;

		public void LoadHtml(string html, string baseUrl)
		{
			_control.LoadHtml(html, baseUrl);
		}

		public void LoadUrl(string url)
		{
			if (!string.IsNullOrEmpty(url))
			{
				_control.LoadUrl(url);
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (Element != null)
				{
					if (_control != null)
					{
						_control.StopLoading();
						_control.LoadStarted -= OnLoadStarted;
						_control.LoadFinished -= OnLoadFinished;
						_control.LoadError -= OnLoadError;
					}
					Element.EvalRequested -= OnEvalRequested;
					Element.GoBackRequested -= OnGoBackRequested;
					Element.GoForwardRequested -= OnGoForwardRequested;
				}
			}
			base.Dispose(disposing);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
		{
			if (_control == null)
			{
				TChromium.Initialize();
				Forms.Context.Terminated += (sender, arg) => TChromium.Shutdown();
				_control = new TWebView(Forms.Context.MainWindow);
				_control.LoadStarted += OnLoadStarted;
				_control.LoadFinished += OnLoadFinished;
				_control.LoadError += OnLoadError;
				SetNativeControl(_control);
			}

			if (e.OldElement != null)
			{
				e.OldElement.EvalRequested -= OnEvalRequested;
				e.OldElement.GoBackRequested -= OnGoBackRequested;
				e.OldElement.GoForwardRequested -= OnGoForwardRequested;
			}

			if (e.NewElement != null)
			{
				e.NewElement.EvalRequested += OnEvalRequested;
				e.NewElement.GoForwardRequested += OnGoForwardRequested;
				e.NewElement.GoBackRequested += OnGoBackRequested;
				Load();
			}
			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == WebView.SourceProperty.PropertyName)
				Load();

			base.OnElementPropertyChanged(sender, e);
		}

		void OnLoadError(object sender, global::Tizen.WebView.SmartCallbackLoadErrorArgs e)
		{
			string url = e.Url;
			if (!string.IsNullOrEmpty(url))
				SendNavigated(new UrlWebViewSource { Url = url }, _eventState, WebNavigationResult.Failure);
		}

		void OnLoadStarted(object sender, EventArgs e)
		{
			string url = _control.Url;
			if (!string.IsNullOrEmpty(url))
			{
				var args = new WebNavigatingEventArgs(_eventState, new UrlWebViewSource { Url = url }, url);
				Element.SendNavigating(args);

				if (args.Cancel)
				{
					_eventState = WebNavigationEvent.NewPage;
				}
			}
		}

		void OnLoadFinished(object sender, EventArgs e)
		{
			string url = _control.Url;
			if (!string.IsNullOrEmpty(url))
				SendNavigated(new UrlWebViewSource { Url = url }, _eventState, WebNavigationResult.Success);

			_control.SetFocus(true);
			UpdateCanGoBackForward();
		}

		void Load()
		{
			if (_updating)
				return;

			if (Element.Source != null)
			{
				Element.Source.Load(this);
			}

			UpdateCanGoBackForward();
		}

		void OnEvalRequested(object sender, EvalRequested eventArg)
		{
			_control.Eval(eventArg.Script);
		}

		void OnGoBackRequested(object sender, EventArgs eventArgs)
		{
			if (_control.CanGoBack())
			{
				_eventState = WebNavigationEvent.Back;
				_control.GoBack();
			}

			UpdateCanGoBackForward();
		}

		void OnGoForwardRequested(object sender, EventArgs eventArgs)
		{
			if (_control.CanGoForward())
			{
				_eventState = WebNavigationEvent.Forward;
				_control.GoForward();
			}

			UpdateCanGoBackForward();
		}

		void SendNavigated(UrlWebViewSource source, WebNavigationEvent evnt, WebNavigationResult result)
		{
			_updating = true;
			((IElementController)Element).SetValueFromRenderer(WebView.SourceProperty, source);
			_updating = false;

			Element.SendNavigated(new WebNavigatedEventArgs(evnt, source, source.Url, result));

			UpdateCanGoBackForward();
			_eventState = WebNavigationEvent.NewPage;
		}

		void UpdateCanGoBackForward()
		{
			Element.CanGoBack = _control.CanGoBack();
			Element.CanGoForward = _control.CanGoForward();
		}
	}
}