Revert "[Tizen] Revert "Changes after touch consumed behaviour change"
[platform/core/uifw/dali-demo.git] / examples / text-field / text-field-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 /**
19  * @file text-field-example.cpp
20  * @brief Basic usage of TextField control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/popup/popup.h>
26 #include <iostream>
27
28 // INTERNAL INCLUDES
29 #include "shared/multi-language-strings.h"
30 #include "shared/view.h"
31
32 using namespace Dali;
33 using namespace Dali::Toolkit;
34 using namespace MultiLanguageStrings;
35
36 namespace
37 {
38
39   const char* const FOLDER_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_bg.png";
40   const char* const FOLDER_OPEN_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_open_bg.png";
41
42   const float BORDER_WIDTH = 4.0f;
43
44 } // unnamed namespace
45
46 /**
47  * @brief The main class of the demo.
48  */
49 class TextFieldExample : public ConnectionTracker
50 {
51 public:
52
53   TextFieldExample( Application& application )
54   : mApplication( application )
55   {
56     // Connect to the Application's Init signal
57     mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
58   }
59
60   ~TextFieldExample()
61   {
62     // Nothing to do here.
63   }
64
65   /**
66    * One-time setup in response to Application InitSignal.
67    */
68   void Create( Application& application )
69   {
70     Window window = application.GetWindow();
71
72     window.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
73     window.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
74
75     mButton = CreateFolderButton();
76     mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
77     window.Add( mButton );
78   }
79
80   PushButton CreateFolderButton()
81   {
82     PushButton button = PushButton::New();
83     button.SetProperty( Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FOLDER_ICON_IMAGE );
84     button.SetProperty( Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FOLDER_OPEN_ICON_IMAGE );
85     button.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
86     return button;
87   }
88
89   bool OnButtonClicked( Toolkit::Button button )
90   {
91     Window window = mApplication.GetWindow();
92     Vector2 windowSize = window.GetSize();
93
94     // Remove previously hidden pop-up
95     UnparentAndReset(mPopup);
96
97     // Launch a pop-up containing TextField
98     mField = CreateTextField( windowSize, mButtonLabel );
99     mPopup = CreatePopup();
100     mPopup.Add( mField );
101     mPopup.OutsideTouchedSignal().Connect( this, &TextFieldExample::OnPopupOutsideTouched );
102     window.Add( mPopup );
103     mPopup.SetDisplayState( Popup::SHOWN );
104
105     return true;
106   }
107
108   TextField CreateTextField( const Vector2& windowSize, const std::string& text )
109   {
110     TextField field = TextField::New();
111     field.SetProperty( Dali::Actor::Property::NAME,"textField");
112     field.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
113     field.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
114     field.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
115     field.SetProperty( TextField::Property::TEXT, text );
116     field.SetProperty( TextField::Property::TEXT_COLOR, Vector4( 0.0f, 1.0f, 1.0f, 1.0f ) ); // CYAN
117     field.SetProperty( TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder" );
118     field.SetProperty( TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name." );
119     field.SetProperty( TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>( BORDER_WIDTH, BORDER_WIDTH, windowSize.width - BORDER_WIDTH*2, windowSize.height - BORDER_WIDTH*2 ) );
120
121     return field;
122   }
123
124   Popup CreatePopup()
125   {
126     Popup popup = Popup::New();
127     popup.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
128     popup.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
129     popup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH );
130     popup.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
131     popup.TouchSignal().Connect( this, &TextFieldExample::OnPopupTouched );
132
133     return popup;
134   }
135
136   void OnPopupOutsideTouched()
137   {
138     // Update the folder text
139     if( mButton && mField )
140     {
141       Property::Value text = mField.GetProperty( TextField::Property::TEXT );
142       mButtonLabel = text.Get< std::string >();
143       mButton.SetProperty( Toolkit::Button::Property::LABEL, mButtonLabel );
144     }
145
146     // Hide & discard the pop-up
147     if( mPopup )
148     {
149       mPopup.SetDisplayState( Popup::HIDDEN );
150     }
151     mField.Reset();
152   }
153
154   bool OnPopupTouched( Actor actor, const TouchEvent& event )
155   {
156     // End edit mode for TextField if parent Popup touched.
157     if((event.GetPointCount() > 0) && (mPopup == event.GetHitActor(0)))
158     {
159       switch( event.GetState( 0 ) )
160       {
161         case PointState::DOWN:
162         {
163           // Update the folder text and lose focus for Key events
164           if( mButton && mField )
165           {
166             Property::Value text = mField.GetProperty( TextField::Property::TEXT );
167             mButtonLabel = text.Get< std::string >();
168             mButton.SetProperty( Toolkit::Button::Property::LABEL, mButtonLabel );
169             mField.ClearKeyInputFocus();
170           }
171           break;
172         }
173         default:
174         {
175           break;
176         }
177       } // end switch
178     }
179
180     return false;
181   }
182
183   /**
184    * Main key event handler
185    */
186   void OnKeyEvent(const KeyEvent& event)
187   {
188     if(event.state == KeyEvent::Down)
189     {
190       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
191       {
192         mApplication.Quit();
193       }
194     }
195   }
196
197 private:
198
199   Application& mApplication;
200
201   // This button launches a pop-up containing TextField
202   PushButton mButton;
203   std::string mButtonLabel;
204
205   // Pop-up contents
206   TextField mField;
207   Popup mPopup;
208 };
209
210 int DALI_EXPORT_API main( int argc, char **argv )
211 {
212   // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
213   Application application = Application::New( &argc, &argv );
214   TextFieldExample test( application );
215   application.MainLoop();
216   return 0;
217 }