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