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