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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
20 #include "shared/view.h"
23 using namespace Dali::Toolkit;
27 const char* const BACKGROUND_IMAGE = DEMO_IMAGE_DIR "background-gradient.jpg";
28 const char* const TOOLBAR_IMAGE = DEMO_IMAGE_DIR "top-bar.png";
29 const char* const TOOLBAR_TITLE = "Focus Integration";
30 const Vector4 BACKGROUND_COLOUR(1.0f, 1.0f, 1.0f, 0.15f);
33 const int MARGIN_SIZE = 10;
34 const int TOP_MARGIN = 85;
35 const std::string ITEMNAME[] = {"TextLabel", "TextField", "TextEditor", "PushButton", "RadioButton", "CheckBoxButton"};
40 * @brief Shows how integrated DALi Focus works.
42 class FocusIntegrationExample : public ConnectionTracker
45 FocusIntegrationExample(Application& application)
46 : mApplication(application)
48 // Connect to the Application's Init signal
49 mApplication.InitSignal().Connect(this, &FocusIntegrationExample::Create);
52 void Create(Application& application)
54 mWindow = application.GetWindow();
55 Vector2 windowSize = mWindow.GetSize();
56 mContentLayer = DemoHelper::CreateView(application,
63 TableView contentTable = TableView::New(2, 1);
64 contentTable.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
65 contentTable.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
66 contentTable.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
67 contentTable.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
68 contentTable.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE * 0.5f));
69 contentTable.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
71 for(unsigned int i = 0; i < contentTable.GetRows(); ++i)
73 contentTable.SetFitHeight(i);
75 contentTable.SetProperty(Actor::Property::POSITION, Vector2(0.0f, TOP_MARGIN));
76 mContentLayer.Add(contentTable);
78 // Create label to display which control's KeyEvent callback is called
79 mEventLabel = TextLabel::New("Controls don't get KeyEvent yet");
80 mEventLabel.SetProperty(Actor::Property::SIZE, Vector2(windowSize.width, windowSize.height * 0.1f));
81 mEventLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
82 mEventLabel.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
83 mEventLabel.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
84 mEventLabel.SetBackgroundColor(Color::WHITE);
85 contentTable.Add(mEventLabel);
87 mContainer = TableView::New(4, 3);
88 mContainer.SetProperty(Actor::Property::SIZE, Vector2(windowSize.width, windowSize.height * 0.4f));
89 mContainer.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
90 mContainer.SetBackgroundColor(BACKGROUND_COLOUR);
91 mContainer.SetCellPadding(Size(MARGIN_SIZE, MARGIN_SIZE));
92 mContainer.SetRelativeHeight(0, 0.2f);
93 mContainer.SetRelativeHeight(1, 0.3f);
94 mContainer.SetRelativeHeight(2, 0.2f);
95 mContainer.SetRelativeHeight(3, 0.3f);
96 mContainer.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
97 contentTable.Add(mContainer);
99 // Make name label for each controls
100 for(int i = 0; i < 6; i++)
102 TextLabel itemLabel = TextLabel::New(ITEMNAME[i]);
103 itemLabel.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
104 itemLabel.SetBackgroundColor(BACKGROUND_COLOUR);
105 itemLabel.SetProperty(TextLabel::Property::POINT_SIZE, 14.0f);
106 itemLabel.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
107 itemLabel.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
108 mContainer.AddChild(itemLabel, TableView::CellPosition((i / 3) * 2, i % 3));
111 TextLabel textLabel = TextLabel::New("TextLabel");
112 mContainer.AddChild(textLabel, TableView::CellPosition(1, 0));
114 TextField textField = TextField::New();
115 textField.SetBackgroundColor(Color::WHITE);
116 textField.SetProperty(TextField::Property::TEXT, "Text");
117 mContainer.AddChild(textField, TableView::CellPosition(1, 1));
119 TextEditor textEditor = TextEditor::New();
120 textEditor.SetBackgroundColor(Color::WHITE);
121 textEditor.SetProperty(TextEditor::Property::TEXT, "Text\nText");
122 mContainer.AddChild(textEditor, TableView::CellPosition(1, 2));
124 PushButton pushButton = PushButton::New();
125 mContainer.AddChild(pushButton, TableView::CellPosition(3, 0));
127 RadioButton radioButton = RadioButton::New();
128 mContainer.AddChild(radioButton, TableView::CellPosition(3, 1));
130 CheckBoxButton checkBoxButton = CheckBoxButton::New();
131 mContainer.AddChild(checkBoxButton, TableView::CellPosition(3, 2));
133 // Set name and keyboard focusable for each controls
134 for(int i = 0; i < 6; i++)
136 Control control = Control::DownCast(mContainer.GetChildAt(TableView::CellPosition((i / 3) * 2 + 1, i % 3)));
137 control.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
138 control.SetProperty(Dali::Actor::Property::NAME, ITEMNAME[i]);
139 control.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
140 control.KeyEventSignal().Connect(this, &FocusIntegrationExample::OnControlKeyEvent);
143 KeyboardFocusManager::Get().PreFocusChangeSignal().Connect(this, &FocusIntegrationExample::OnPreFocusChange);
145 // Respond to key events
146 mWindow.KeyEventSignal().Connect(this, &FocusIntegrationExample::OnKeyEvent);
149 // Callback for KeyboardFocusManager
150 Actor OnPreFocusChange(Actor current, Actor next, Control::KeyboardFocus::Direction direction)
152 if(!current && !next)
154 next = mContainer.GetChildAt(TableView::CellPosition(1, 0));
159 // Callback for each controls.
160 // Display current control name.
161 bool OnControlKeyEvent(Control control, const KeyEvent& event)
163 std::string controlName = control.GetProperty<std::string>(Dali::Actor::Property::NAME);
164 mEventLabel.SetProperty(TextLabel::Property::TEXT, controlName + "'s KeyEvent works\n");
171 * Main key event handler
173 void OnKeyEvent(const KeyEvent& event)
175 if(event.GetState() == KeyEvent::DOWN)
177 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
185 Application& mApplication;
187 TableView mContainer;
188 TextLabel mEventLabel;
189 Toolkit::Control mView; ///< The View instance.
190 Toolkit::ToolBar mToolBar; ///< The View's Toolbar.
191 Layer mContentLayer; ///< Content layer.
195 int DALI_EXPORT_API main(int argc, char** argv)
197 Application application = Application::New(&argc, &argv);
199 FocusIntegrationExample test(application);
201 application.MainLoop();