2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using Tizen.NUI.BaseComponents;
20 namespace Tizen.NUI.WebViewTest
22 public class WebViewApplication : NUIApplication
24 private WebView simpleWebView = null;
25 private TextField addressBar = null;
27 private int index = 0;
28 private const int WEBSITES_COUNT = 93;
29 private string[] websites =
31 "http://www.baidu.com","http://www.Google.com","http://www.Facebook.com","http://www.Youtube.com","http://www.Yahoo.com",
32 "http://www.Amazon.com","http://www.Wikipedia.org","http://www.Google.co.in","http://www.Qq.com","http://www.Twitter.com",
33 "http://www.Live.com","http://www.Taobao.com","http://www.Msn.com","http://www.Yahoo.co.jp","http://www.Linkedin.com",
34 "http://www.Google.co.jp","http://www.Sina.com.cn","http://www.Bing.com","http://www.Weibo.com","http://www.Yandex.ru",
35 "http://www.Vk.com","http://www.Instagram.com","http://www.Hao123.com","http://www.Ebay.com","http://www.Google.de",
36 "http://www.Amazon.co.jp","http://www.Mail.ru","http://www.Google.co.uk","http://www.Google.ru","http://www.Pinterest.com",
37 "http://www.360.cn","http://www.T.co","http://www.Reddit.com","http://www.Google.com.br","http://www.Netflix.com",
38 "http://www.Tmall.com","http://www.Google.fr","http://www.Paypal.com","http://www.Microsoft.com","http://www.Wordpress.com",
39 "http://www.Sohu.com","http://www.Blogspot.com","http://www.Google.it","http://www.Google.es","http://www.Onclickads.net",
40 "http://www.Tumblr.com","http://www.Imgur.com","http://www.Gmw.cn","http://www.Ok.ru","http://www.Aliexpress.com",
41 "http://www.Apple.com","http://www.Imdb.com","http://www.Stackoverflow.com","http://www.Fc2.com","http://www.Google.com.mx",
42 "http://www.Ask.com","http://www.Amazon.de","http://www.Google.com.hk","http://www.Google.com.tr","http://www.Alibaba.com",
43 "http://www.Google.ca","http://www.Office.com","http://www.Rakuten.co.jp","http://www.Google.co.id","http://www.Tianya.cn",
44 "http://www.Xinhuanet.com","http://www.Github.com","http://www.Craigslist.org","http://www.Nicovideo.jp","http://www.Soso.com",
45 "http://www.Amazon.co.uk","http://www.Amazon.in","http://www.Blogger.com","http://www.Kat.cr","http://www.Outbrain.com",
46 "http://www.Pixnet.net","http://www.Cnn.com","http://www.Go.com","http://www.Google.pl","http://www.Dropbox.com",
47 "http://www.Google.com.au","http://www.360.com","http://www.Haosou.com","http://www.Naver.com","http://www.Jd.com",
48 "http://www.Adobe.com","http://www.Flipkart.com","http://www.Whatsapp.com","http://www.Nytimes.com","http://www.Coccoc.com",
49 "http://www.Chase.com","http://www.Chinadaily.com.cn","http://www.bbc.co.uk"
52 private const string USER_AGENT = "Mozilla/5.0 (SMART-TV; Linux; Tizen 6.0) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/4.0 Chrome/76.0.3809.146 TV Safari/537.36";
54 private const int ADDRESSBAR_HEIGHT = 100;
56 private const int SCREEN_WIDTH = 1920;
57 private const int SCREEN_HEIGHT = 1080;
59 private const int MIN_WEBVIEW_WIDTH = 1000;
60 private const int MIN_WEBVIEW_HEIGHT = 600;
62 private const int WEBVIEW_WIDTH = SCREEN_WIDTH;
63 private const int WEBVIEW_HEIGHT = SCREEN_HEIGHT - ADDRESSBAR_HEIGHT;
65 private int blueKeyPressedCount = 0;
66 private int yellowKeyPressedCount = 0;
68 private static long startTime = 0;
70 private TextLabel messageLabel = null;
71 private Timer messageTimer = null;
73 protected override void OnCreate()
77 GetDefaultWindow().BackgroundColor = new Color((float)189 /255, (float)179 /255, (float)204 /255, 1.0f);
78 //EnvironmentVariable.SetEnvironmentVariable("DALI_WEB_ENGINE_NAME", "lwe");
80 addressBar = new TextField()
82 BackgroundColor = Color.White,
83 Size = new Size(SCREEN_WIDTH, ADDRESSBAR_HEIGHT),
84 EnableGrabHandlePopup = false,
85 EnableGrabHandle = false,
86 EnableSelection = true,
88 PlaceholderText = "Please input url here like Www.baidu.com.",
90 addressBar.FocusGained += OnTextEditorFocusGained;
91 addressBar.FocusLost += OnTextEditorFocusLost;
92 addressBar.KeyEvent += OnAddressBarKeyEvent;
93 addressBar.TouchEvent += OnAddressBarTouchEvent;
94 GetDefaultWindow().Add(addressBar);
96 simpleWebView = new WebView()
98 Position = new Position(0, ADDRESSBAR_HEIGHT),
99 Size = new Size(WEBVIEW_WIDTH, WEBVIEW_HEIGHT),
100 UserAgent = USER_AGENT,
103 simpleWebView.FocusGained += OnWebViewFocusGained;
104 simpleWebView.FocusLost += OnWebViewFocusLost;
105 simpleWebView.KeyEvent += OnWebViewKeyEvent;
106 simpleWebView.TouchEvent += OnWebViewTouchEvent;
107 simpleWebView.PageLoadStarted += OnPageLoadStarted;
108 simpleWebView.ScrollEdgeReached += OnScrollEdgeReached;
109 GetDefaultWindow().Add(simpleWebView);
111 GetDefaultWindow().KeyEvent += Instance_KeyEvent;
112 simpleWebView.LoadUrl(websites[index]);
113 FocusManager.Instance.SetCurrentFocusView(simpleWebView);
115 messageTimer = new Timer(10000);
116 messageTimer.Tick += OnTick;
119 protected override void OnTerminate()
121 GetDefaultWindow().Remove(simpleWebView);
122 GetDefaultWindow().Remove(addressBar);
124 messageTimer.Tick -= OnTick;
125 messageTimer.Dispose();
131 private bool OnTick(object sender, EventArgs e)
133 GetDefaultWindow().Remove(messageLabel);
134 messageLabel.Dispose();
139 private bool OnWebViewTouchEvent(object source, View.TouchEventArgs args)
141 if (!simpleWebView.HasFocus())
143 FocusManager.Instance.SetCurrentFocusView(simpleWebView);
148 private void OnScrollEdgeReached(object sender, WebViewScrollEdgeReachedEventArgs e)
150 Log.Info("WebView", $"------------scroll edge reached: {e.ScrollEdge}-------");
153 private void OnPageLoadStarted(object sender, WebViewPageLoadEventArgs e)
155 Log.Info("WebView", $"------------web view start to load time: {DateTime.Now.Ticks - startTime}-------");
158 private void OnWebViewFocusGained(object source, EventArgs args)
160 Log.Info("WebView", $"------------web view focus is gained-------");
163 private void OnWebViewFocusLost(object source, EventArgs args)
165 Log.Info("WebView", $"------------web view focus is lost-------");
168 private bool OnWebViewKeyEvent(object source, View.KeyEventArgs args)
172 Log.Info("WebView", $"----web view key is {args.Key.KeyPressedName}, state is {args.Key.State}-------");
174 if (args.Key.State == Key.StateType.Up)
176 if (args.Key.KeyPressedName == "XF86RaiseChannel")
178 Log.Info("WebView", $"old url is {simpleWebView.Url}.");
181 simpleWebView.Url = websites[--index].ToLowerInvariant();
185 simpleWebView.Url = websites[WEBSITES_COUNT - 1].ToLowerInvariant();
188 Log.Info("WebView", $"new url is {simpleWebView.Url}.");
190 else if (args.Key.KeyPressedName == "XF86LowerChannel")
192 Log.Info("WebView", $"old url is {simpleWebView.Url}.");
193 if (index != WEBSITES_COUNT - 1)
195 simpleWebView.Url = websites[++index].ToLowerInvariant();
199 simpleWebView.Url = websites[0].ToLowerInvariant();
202 Log.Info("WebView", $"new url is {simpleWebView.Url}.");
204 else if (args.Key.KeyPressedName == "XF86Back")
206 simpleWebView.GoBack();
209 else if (args.Key.KeyPressedName == "XF86Red")
211 FocusManager.Instance.SetCurrentFocusView(addressBar);
214 else if (args.Key.KeyPressedName == "XF86Blue")
216 blueKeyPressedCount++;
217 if (blueKeyPressedCount % 6 == 0)
219 simpleWebView.Position = new Position(0, ADDRESSBAR_HEIGHT);
220 simpleWebView.Orientation = new Rotation(new Radian(new Degree(60 * blueKeyPressedCount)), Vector3.ZAxis);
221 blueKeyPressedCount = 0;
225 simpleWebView.Orientation = new Rotation(new Radian(new Degree(60 * blueKeyPressedCount)), Vector3.ZAxis);
229 else if (args.Key.KeyPressedName == "XF86Yellow")
231 yellowKeyPressedCount++;
232 int wdistance = (SCREEN_WIDTH - MIN_WEBVIEW_WIDTH) / 5;
233 int hdistance = (SCREEN_HEIGHT - MIN_WEBVIEW_HEIGHT - ADDRESSBAR_HEIGHT) / 5;
234 simpleWebView.Position = new Position((SCREEN_WIDTH - MIN_WEBVIEW_WIDTH - yellowKeyPressedCount * wdistance) / 2, ADDRESSBAR_HEIGHT);
235 simpleWebView.Size = new Size(MIN_WEBVIEW_WIDTH + yellowKeyPressedCount * wdistance, MIN_WEBVIEW_HEIGHT + hdistance * yellowKeyPressedCount);
236 if (yellowKeyPressedCount % 5 == 0)
238 yellowKeyPressedCount = 0;
242 else if (args.Key.KeyPressedName == "XF86Green")
244 if (messageLabel != null)
247 Log.Info("WebView", $"key XF86Green is pressed.");
249 simpleWebView.ScrollPosition = new Position(0, 200);
250 simpleWebView.ScrollBy(0, 50);
251 Log.Info("WebView", $"scroll position is ({simpleWebView.ScrollPosition.X}, {simpleWebView.ScrollPosition.Y}).");
252 Log.Info("WebView", $"scroll size is ({simpleWebView.ScrollSize.Width}, {simpleWebView.ScrollSize.Height}).");
253 Log.Info("WebView", $"content size is ({simpleWebView.ContentSize.Width}, {simpleWebView.ContentSize.Height}).");
262 private bool OnAddressBarTouchEvent(object source, View.TouchEventArgs args)
264 FocusManager.Instance.SetCurrentFocusView(addressBar);
268 private void OnTextEditorFocusGained(object source, EventArgs args)
270 Log.Info("WebView", $"------------address bar focus is gained-------");
272 addressBar.Text = "";
274 addressBar.GetInputMethodContext().Activate();
275 addressBar.GetInputMethodContext().ShowInputPanel();
278 private void OnTextEditorFocusLost(object source, EventArgs args)
280 Log.Info("WebView", $"------------address bar focus lost-------");
283 private bool OnAddressBarKeyEvent(object source, View.KeyEventArgs args)
285 Log.Info("WebView", $"------------address bar key is {args.Key.KeyPressedName}-------");
287 if (args.Key.State == Key.StateType.Up)
289 if (args.Key.KeyPressedName == "Select")
291 Log.Info("WebView", $"------------address bar text is {addressBar.Text}-------");
293 if (addressBar.Text.Length > 0)
295 addressBar.GetInputMethodContext().HideInputPanel();
296 addressBar.GetInputMethodContext().Deactivate();
298 simpleWebView.Url = $"http://{addressBar.Text.ToLowerInvariant()}";
300 // set focus to webview.
301 FocusManager.Instance.SetCurrentFocusView(simpleWebView);
304 else if (args.Key.KeyPressedName == "BackSpace")
306 if (addressBar.Text.Length > 0)
308 addressBar.Text = addressBar.Text.Substring(0, addressBar.Text.Length - 1);
311 else if (args.Key.KeyPressedName == "XF86Red")
313 FocusManager.Instance.SetCurrentFocusView(simpleWebView);
320 private void Instance_KeyEvent(object sender, Window.KeyEventArgs args)
322 if (args.Key.State == Key.StateType.Up)
324 Log.Info("WebView", $"window key is {args.Key.KeyPressedName}.");
329 static void Main(string[] args)
331 startTime = DateTime.Now.Ticks;
332 Log.Info("WebView", $"------------web view start time: {startTime}-------");
333 new WebViewApplication().Run(args);