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;
38 const char* const FOLDER_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_bg.png";
39 const char* const FOLDER_OPEN_ICON_IMAGE = DEMO_IMAGE_DIR "folder_appicon_empty_open_bg.png";
41 const float BORDER_WIDTH = 4.0f;
43 } // unnamed namespace
46 * @brief The main class of the demo.
48 class TextFieldExample : public ConnectionTracker
51 TextFieldExample(Application& application)
52 : mApplication(application)
54 // Connect to the Application's Init signal
55 mApplication.InitSignal().Connect(this, &TextFieldExample::Create);
60 // Nothing to do here.
64 * One-time setup in response to Application InitSignal.
66 void Create(Application& application)
68 Window window = application.GetWindow();
70 window.SetBackgroundColor(Vector4(0.04f, 0.345f, 0.392f, 1.0f));
71 window.KeyEventSignal().Connect(this, &TextFieldExample::OnKeyEvent);
73 mButton = CreateFolderButton();
74 mButton.ClickedSignal().Connect(this, &TextFieldExample::OnButtonClicked);
78 PushButton CreateFolderButton()
80 PushButton button = PushButton::New();
81 button.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, FOLDER_ICON_IMAGE);
82 button.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, FOLDER_OPEN_ICON_IMAGE);
83 button.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
87 bool OnButtonClicked(Toolkit::Button button)
89 Window window = mApplication.GetWindow();
90 Vector2 windowSize = window.GetSize();
92 // Remove previously hidden pop-up
93 UnparentAndReset(mPopup);
95 // Launch a pop-up containing TextField
96 mField = CreateTextField(windowSize, mButtonLabel);
97 mPopup = CreatePopup();
99 mPopup.OutsideTouchedSignal().Connect(this, &TextFieldExample::OnPopupOutsideTouched);
101 mPopup.SetDisplayState(Popup::SHOWN);
106 TextField CreateTextField(const Vector2& windowSize, const std::string& text)
108 TextField field = TextField::New();
109 field.SetProperty(Dali::Actor::Property::NAME, "textField");
110 field.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
111 field.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
112 field.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
113 field.SetProperty(TextField::Property::TEXT, text);
114 field.SetProperty(TextField::Property::TEXT_COLOR, Vector4(0.0f, 1.0f, 1.0f, 1.0f)); // CYAN
115 field.SetProperty(TextField::Property::PLACEHOLDER_TEXT, "Unnamed folder");
116 field.SetProperty(TextField::Property::PLACEHOLDER_TEXT_FOCUSED, "Enter folder name.");
117 field.SetProperty(TextField::Property::DECORATION_BOUNDING_BOX, Rect<int>(BORDER_WIDTH, BORDER_WIDTH, windowSize.width - BORDER_WIDTH * 2, windowSize.height - BORDER_WIDTH * 2));
124 Popup popup = Popup::New();
125 popup.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
126 popup.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
127 popup.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::WIDTH);
128 popup.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT);
129 popup.TouchedSignal().Connect(this, &TextFieldExample::OnPopupTouched);
134 void OnPopupOutsideTouched()
136 // Update the folder text
137 if(mButton && mField)
139 Property::Value text = mField.GetProperty(TextField::Property::TEXT);
140 mButtonLabel = text.Get<std::string>();
141 mButton.SetProperty(Toolkit::Button::Property::LABEL, mButtonLabel);
144 // Hide & discard the pop-up
147 mPopup.SetDisplayState(Popup::HIDDEN);
152 bool OnPopupTouched(Actor actor, const TouchEvent& event)
154 // End edit mode for TextField if parent Popup touched.
155 if((event.GetPointCount() > 0) && (mPopup == event.GetHitActor(0)))
157 switch(event.GetState(0))
159 case PointState::DOWN:
161 // Update the folder text and lose focus for Key events
162 if(mButton && mField)
164 Property::Value text = mField.GetProperty(TextField::Property::TEXT);
165 mButtonLabel = text.Get<std::string>();
166 mButton.SetProperty(Toolkit::Button::Property::LABEL, mButtonLabel);
167 mField.ClearKeyInputFocus();
182 * Main key event handler
184 void OnKeyEvent(const KeyEvent& event)
186 if(event.GetState() == KeyEvent::DOWN)
188 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
196 Application& mApplication;
198 // This button launches a pop-up containing TextField
200 std::string mButtonLabel;
207 int DALI_EXPORT_API main(int argc, char** argv)
209 // DALI_DEMO_THEME_PATH not passed to Application so TextField example uses default Toolkit style sheet.
210 Application application = Application::New(&argc, &argv);
211 TextFieldExample test(application);
212 application.MainLoop();