6f9dad57b8a58c95b6831d86b6ff0b03daf043ff
[platform/core/uifw/dali-demo.git] / examples / focus-integration / focus-integration.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 "shared/view.h"
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27
28 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-gradient.jpg";
29 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
30 const char* const TOOLBAR_TITLE = "Focus Integration";
31 const Vector4 BACKGROUND_COLOUR( 1.0f, 1.0f, 1.0f, 0.15f );
32
33 // Layout sizes
34 const int MARGIN_SIZE = 10;
35 const int TOP_MARGIN = 85;
36 const std::string ITEMNAME[] = { "TextLabel", "TextField", "TextEditor", "PushButton", "RadioButton", "CheckBoxButton" };
37
38 }  // namespace
39
40 /**
41  * @brief Shows how integrated DALi Focus works.
42  */
43 class FocusIntegrationExample: public ConnectionTracker
44 {
45 public:
46
47   FocusIntegrationExample( Application& application )
48     : mApplication( application )
49   {
50     // Connect to the Application's Init signal
51     mApplication.InitSignal().Connect( this, &FocusIntegrationExample::Create );
52   }
53
54   void Create( Application& application )
55   {
56     mWindow = application.GetWindow();
57     Vector2 windowSize = mWindow.GetSize();
58     mContentLayer = DemoHelper::CreateView( application,
59                                             mView,
60                                             mToolBar,
61                                             BACKGROUND_IMAGE,
62                                             TOOLBAR_IMAGE,
63                                             TOOLBAR_TITLE );
64
65     TableView contentTable = TableView::New(2, 1);
66     contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
67     contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
68     contentTable.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
69     contentTable.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
70     contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
71     contentTable.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE,true);
72
73     for( unsigned int i = 0; i < contentTable.GetRows(); ++i )
74     {
75       contentTable.SetFitHeight( i );
76     }
77     contentTable.SetProperty( Actor::Property::POSITION, Vector2( 0.0f, TOP_MARGIN ));
78     mContentLayer.Add( contentTable );
79
80     // Create label to display which control's KeyEvent callback is called
81     mEventLabel = TextLabel::New("Controls don't get KeyEvent yet");
82     mEventLabel.SetProperty( Actor::Property::SIZE, Vector2( windowSize.width, windowSize.height*0.1f ) );
83     mEventLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
84     mEventLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
85     mEventLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
86     mEventLabel.SetBackgroundColor( Color::WHITE );
87     contentTable.Add( mEventLabel );
88
89     mContainer = TableView::New( 4, 3 );
90     mContainer.SetProperty( Actor::Property::SIZE, Vector2( windowSize.width, windowSize.height*0.4f ) );
91     mContainer.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
92     mContainer.SetBackgroundColor( BACKGROUND_COLOUR );
93     mContainer.SetCellPadding( Size( MARGIN_SIZE, MARGIN_SIZE ) );
94     mContainer.SetRelativeHeight( 0, 0.2f);
95     mContainer.SetRelativeHeight( 1, 0.3f);
96     mContainer.SetRelativeHeight( 2, 0.2f);
97     mContainer.SetRelativeHeight( 3, 0.3f);
98     mContainer.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE,true);
99     contentTable.Add( mContainer );
100
101     // Make name label for each controls
102     for(int i = 0; i < 6; i++)
103     {
104       TextLabel itemLabel = TextLabel::New( ITEMNAME[i] );
105       itemLabel.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
106       itemLabel.SetBackgroundColor( BACKGROUND_COLOUR );
107       itemLabel.SetProperty( TextLabel::Property::POINT_SIZE, 14.0f );
108       itemLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
109       itemLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
110       mContainer.AddChild( itemLabel, TableView::CellPosition( (i/3)*2, i%3 ) );
111     }
112
113     TextLabel textLabel = TextLabel::New("TextLabel");
114     mContainer.AddChild( textLabel, TableView::CellPosition( 1, 0 ) );
115
116     TextField textField = TextField::New();
117     textField.SetBackgroundColor( Color::WHITE );
118     textField.SetProperty( TextField::Property::TEXT, "Text" );
119     mContainer.AddChild( textField, TableView::CellPosition( 1, 1 ) );
120
121     TextEditor textEditor = TextEditor::New();
122     textEditor.SetBackgroundColor( Color::WHITE );
123     textEditor.SetProperty( TextEditor::Property::TEXT, "Text\nText" );
124     mContainer.AddChild( textEditor, TableView::CellPosition( 1, 2 ) );
125
126     PushButton pushButton = PushButton::New();
127     mContainer.AddChild( pushButton, TableView::CellPosition( 3, 0 ) );
128
129     RadioButton radioButton = RadioButton::New();
130     mContainer.AddChild( radioButton, TableView::CellPosition( 3, 1 ) );
131
132     CheckBoxButton checkBoxButton = CheckBoxButton::New();
133     mContainer.AddChild( checkBoxButton, TableView::CellPosition( 3, 2 ) );
134
135     // Set name and keyboard focusable for each controls
136     for(int i = 0; i<6; i++)
137     {
138       Control control = Control::DownCast( mContainer.GetChildAt( TableView::CellPosition( (i/3)*2+1, i%3 ) ) );
139       control.SetProperty( Actor::Property::KEYBOARD_FOCUSABLE,true);
140       control.SetProperty( Dali::Actor::Property::NAME,ITEMNAME[i]);
141       control.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
142       control.KeyEventSignal().Connect( this, &FocusIntegrationExample::OnControlKeyEvent );
143     }
144
145     KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &FocusIntegrationExample::OnPreFocusChange );
146
147     // Respond to key events
148     mWindow.KeyEventSignal().Connect( this, &FocusIntegrationExample::OnKeyEvent );
149   }
150
151   // Callback for KeyboardFocusManager
152   Actor OnPreFocusChange( Actor current, Actor next, Control::KeyboardFocus::Direction direction )
153   {
154     if( !current && !next )
155     {
156       next = mContainer.GetChildAt( TableView::CellPosition( 1, 0 ) );
157     }
158     return next;
159   }
160
161   // Callback for each controls.
162   // Display current control name.
163   bool OnControlKeyEvent( Control control, const KeyEvent& event )
164   {
165     std::string controlName = control.GetProperty< std::string >( Dali::Actor::Property::NAME );
166     mEventLabel.SetProperty( TextLabel::Property::TEXT, controlName+"'s KeyEvent works\n" );
167
168     return false;
169   }
170
171 private:
172
173   /**
174    * Main key event handler
175    */
176   void OnKeyEvent(const KeyEvent& event)
177   {
178     if(event.GetState() == KeyEvent::DOWN)
179     {
180       if( IsKey( event, DALI_KEY_ESCAPE) || IsKey( event, DALI_KEY_BACK ) )
181       {
182         mApplication.Quit();
183       }
184     }
185   }
186
187 private:
188
189   Application&  mApplication;
190   Window mWindow;
191   TableView mContainer;
192   TextLabel mEventLabel;
193   Toolkit::Control  mView;                              ///< The View instance.
194   Toolkit::ToolBar  mToolBar;                           ///< The View's Toolbar.
195   Layer             mContentLayer;                      ///< Content layer.
196 };
197
198 //
199 int DALI_EXPORT_API main( int argc, char **argv )
200 {
201   Application application = Application::New( &argc, &argv );
202
203   FocusIntegrationExample test( application );
204
205   application.MainLoop();
206
207   return 0;
208 }