2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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.
19 * @file text-field-example.cpp
20 * @brief Basic usage of TextField control
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/popup/popup.h>
29 #include "shared/multi-language-strings.h"
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
34 using namespace MultiLanguageStrings;
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";
42 const float BORDER_WIDTH = 4.0f;
44 } // unnamed namespace
47 * @brief The main class of the demo.
49 class TextFieldExample : public ConnectionTracker
53 TextFieldExample( Application& application )
54 : mApplication( application )
56 // Connect to the Application's Init signal
57 mApplication.InitSignal().Connect( this, &TextFieldExample::Create );
62 // Nothing to do here.
66 * One-time setup in response to Application InitSignal.
68 void Create( Application& application )
70 Window window = application.GetWindow();
72 window.SetBackgroundColor( Vector4( 0.04f, 0.345f, 0.392f, 1.0f ) );
73 window.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
75 mButton = CreateFolderButton();
76 mButton.ClickedSignal().Connect( this, &TextFieldExample::OnButtonClicked );
77 window.Add( mButton );
80 PushButton CreateFolderButton()
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 );
89 bool OnButtonClicked( Toolkit::Button button )
91 Window window = mApplication.GetWindow();
92 Vector2 windowSize = window.GetSize();
94 // Remove previously hidden pop-up
95 UnparentAndReset(mPopup);
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 );
108 TextField CreateTextField( const Vector2& windowSize, const std::string& text )
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 ) );
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 );
136 void OnPopupOutsideTouched()
138 // Update the folder text
139 if( mButton && mField )
141 Property::Value text = mField.GetProperty( TextField::Property::TEXT );
142 mButtonLabel = text.Get< std::string >();
143 mButton.SetProperty( Toolkit::Button::Property::LABEL, mButtonLabel );
146 // Hide & discard the pop-up
149 mPopup.SetDisplayState( Popup::HIDDEN );
154 bool OnPopupTouched( Actor actor, const TouchData& event )
156 // End edit mode for TextField if parent Popup touched.
157 if(event.GetPointCount() > 0)
159 switch( event.GetState( 0 ) )
161 case PointState::DOWN:
163 // Update the folder text and lose focus for Key events
164 if( mButton && mField )
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();
184 * Main key event handler
186 void OnKeyEvent(const KeyEvent& event)
188 if(event.state == KeyEvent::Down)
190 if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
199 Application& mApplication;
201 // This button launches a pop-up containing TextField
203 std::string mButtonLabel;
210 int DALI_EXPORT_API main( int argc, char **argv )
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();