2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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 System.Collections.Generic;
23 using Tizen.Applications;
27 namespace Tizen.WebView.Test
29 public class SimpleWebviewApp : CoreUIApplication
31 private const string LogTag = "WebViewApp";
33 private const string _windowName = "Simple WebView App";
34 private const string _defaultUrl = "http://www.google.com";
35 private WebView _webview;
36 private Entry _addressEntry;
37 private Button _reloadButton;
39 public SimpleWebviewApp()
44 protected override void OnCreate()
47 Chromium.Initialize();
48 // Create webview and toolbox
52 protected override void OnTerminate()
58 private void CreateUI()
61 Window window = new Window(_windowName);
64 // Create Box for main window
65 Box mainBox = CreateBaseUI(window);
68 Box topBar = CreateTopBar(window);
71 _webview = new WebView(window)
81 Box bottomBar = CreateBottomBar(window);
83 mainBox.PackEnd(topBar);
84 mainBox.PackEnd(_webview);
85 mainBox.PackEnd(bottomBar);
90 _webview.LoadUrl(_defaultUrl);
93 private Box CreateBaseUI(Window window)
96 Background background = new Background(window)
105 window.AddResizeObject(background);
108 Conformant conformant = new Conformant(window);
111 // Create Box for all contents
112 Box box = new Box(window)
120 conformant.SetContent(box);
125 private Box CreateTopBar(Window window)
127 // Create Box for address bar
128 Box topBar = new Box(window)
138 // Create address entry
139 _addressEntry = new Entry(window)
149 _addressEntry.SetInputPanelLayout(InputPanelLayout.Url);
150 _addressEntry.Activated += (s, e) =>
152 _webview.LoadUrl(((Entry)s).Text);
154 _addressEntry.Show();
156 // Create reload button
157 _reloadButton = new Button(window)
165 _reloadButton.Clicked += (s, e) =>
167 if (_reloadButton.Text.Equals("Reload"))
171 else if (_reloadButton.Text.Equals("Stop"))
173 _webview.StopLoading();
176 _reloadButton.Show();
178 topBar.PackEnd(_addressEntry);
179 topBar.PackEnd(_reloadButton);
184 private Box CreateBottomBar(Window window)
186 // Create Box for bottom bar
187 Box bottomBar = new Box(window)
197 // Create back/forward buttons
198 Button backButton = new Button(window)
207 backButton.Clicked += (s, e) =>
209 if (_webview.CanGoBack())
214 Button forwardButton = new Button(window)
223 forwardButton.Clicked += (s, e) =>
225 if (_webview.CanGoForward())
226 _webview.GoForward();
228 forwardButton.Show();
230 bottomBar.PackEnd(backButton);
231 bottomBar.PackEnd(forwardButton);
236 private void InitializeWebView()
238 _webview.LoadStarted += (s, e) =>
240 Log.Info(LogTag, "Load started");
241 _reloadButton.Text = "Stop";
244 _webview.LoadFinished += (s, e) =>
246 Log.Info(LogTag, "Load finished");
247 _reloadButton.Text = "Reload";
250 _webview.LoadError += (s, e) =>
252 Log.Info(LogTag, "Load error(" + e.Code + "): " + e.Description);
255 _webview.UrlChanged += (s, e) =>
257 Log.Info(LogTag, "Url changed: " + e.GetAsString());
258 _addressEntry.Text = e.GetAsString();
261 CookieManager cookieManager = _webview.GetContext().GetCookieManager();
262 if (cookieManager != null)
264 cookieManager.SetCookieAcceptPolicy(CookieAcceptPolicy.Always);
265 cookieManager.SetPersistentStorage(DirectoryInfo.Data, CookiePersistentStorage.SqlLite);
269 static void Main(string[] args)
271 Elementary.Initialize();
272 Elementary.ThemeOverlay();
274 SimpleWebviewApp app = new Test.SimpleWebviewApp();