Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / web-view / web-view-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/integration-api/debug.h>
20 #include "dali-toolkit/devel-api/controls/web-view/web-view.h"
21
22 using namespace Dali;
23
24 namespace
25 {
26 }
27
28 class WebViewController : public ConnectionTracker
29 {
30 public:
31   WebViewController(Application& application)
32   : mApplication(application),
33     mUrlPointer(0)
34   {
35     // Connect to the Application's Init signal
36     mApplication.InitSignal().Connect(this, &WebViewController::Create);
37   }
38
39   ~WebViewController()
40   {
41     // Nothing to do here;
42   }
43
44   const char* GetNextUrl()
45   {
46     static const unsigned int kUrlSize        = 3;
47     static const char*        kUrls[kUrlSize] = {
48       "https://webkit.org/blog-files/3d-transforms/poster-circle.html",
49       "https://www.amazon.com",
50       "https://www.google.com"};
51     mUrlPointer %= kUrlSize;
52     return kUrls[mUrlPointer++];
53   }
54
55   // The Init signal is received once (only) during the Application lifetime
56   void Create(Application& application)
57   {
58     // Get a handle to the window
59     Window window = application.GetWindow();
60     window.SetBackgroundColor(Vector4(0.2, 0.6, 1, 1));
61
62     float width    = window.GetSize().GetWidth();
63     float height   = window.GetSize().GetHeight();
64     float fontSize = width * 0.02f;
65
66     mWebView = Toolkit::WebView::New("ko-KR", "Asia/Seoul");
67     mWebView.SetProperty(Actor::Property::PARENT_ORIGIN, Dali::ParentOrigin::CENTER);
68     mWebView.SetProperty(Actor::Property::ANCHOR_POINT, Dali::AnchorPoint::CENTER);
69     mWebView.SetProperty(Actor::Property::POSITION, Vector2(0, 0));
70     mWebView.SetProperty(Actor::Property::SIZE, Vector2(width, height));
71     mWebView.PageLoadStartedSignal().Connect(this, &WebViewController::OnPageLoadStarted);
72     mWebView.PageLoadFinishedSignal().Connect(this, &WebViewController::OnPageLoadFinished);
73
74     std::string url = GetNextUrl();
75     mWebView.LoadUrl(url);
76     window.Add(mWebView);
77
78     mAddressLabel = Toolkit::TextLabel::New(url);
79     mAddressLabel.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
80     mAddressLabel.SetProperty(Toolkit::TextLabel::Property::POINT_SIZE, fontSize);
81     mAddressLabel.SetProperty(Toolkit::TextLabel::Property::TEXT_COLOR, Color::WHITE);
82     mAddressLabel.SetBackgroundColor(Vector4(0, 0, 0, 0.5f));
83     mAddressLabel.TouchedSignal().Connect(this, &WebViewController::OnTouchText);
84     window.Add(mAddressLabel);
85
86     // Respond to key events
87     window.KeyEventSignal().Connect(this, &WebViewController::OnKeyEvent);
88     Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(mWebView);
89   }
90
91   void OnPageLoadStarted(Toolkit::WebView view, const std::string& url)
92   {
93     mAddressLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, "Loading");
94   }
95
96   void OnPageLoadFinished(Toolkit::WebView view, const std::string& url)
97   {
98     mAddressLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, url.c_str());
99   }
100
101   bool OnTouchText(Actor actor, const TouchEvent& touch)
102   {
103     if(touch.GetState(0) == PointState::UP)
104     {
105       std::string url = GetNextUrl();
106       mAddressLabel.SetProperty(Toolkit::TextLabel::Property::TEXT, "Waiting");
107       mWebView.LoadUrl(url);
108     }
109
110     return true;
111   }
112
113   void OnKeyEvent(const KeyEvent& event)
114   {
115     if(event.GetState() == KeyEvent::DOWN)
116     {
117       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
118       {
119         mApplication.Quit();
120       }
121     }
122   }
123
124 private:
125   Application&       mApplication;
126   Toolkit::WebView   mWebView;
127   Toolkit::TextLabel mAddressLabel;
128   unsigned int       mUrlPointer;
129 };
130
131 int DALI_EXPORT_API main(int argc, char** argv)
132 {
133   Application       application = Application::New(&argc, &argv);
134   WebViewController test(application);
135   application.MainLoop();
136   return 0;
137 }