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 #include "shared/view.h"
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali/dali.h>
25 using namespace Dali::Toolkit;
29 const int NUM_FLEX_ITEMS = 8;
31 const char* BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-default.png");
32 const char* TOOLBAR_IMAGE(DEMO_IMAGE_DIR "top-bar.png");
34 const DemoHelper::ViewStyle VIEW_STYLE(0.08f, 0.45f, 80.f, 4.f);
36 const std::string FLEX_DIRECTION[] = {
42 const unsigned int NUM_FLEX_DIRECTION = sizeof(FLEX_DIRECTION) / sizeof(std::string);
44 const std::string FLEX_WRAP[] = {
48 const unsigned int NUM_FLEX_WRAP = sizeof(FLEX_WRAP) / sizeof(std::string);
50 const std::string CONTENT_DIRECTION[] = {
55 const unsigned int NUM_CONTENT_DIRECTION = sizeof(CONTENT_DIRECTION) / sizeof(std::string);
57 const std::string JUSTIFY_CONTENT[] = {
64 const unsigned int NUM_JUSTIFY_CONTENT = sizeof(JUSTIFY_CONTENT) / sizeof(std::string);
66 const std::string ALIGN_ITEMS[] = {
72 const unsigned int NUM_ALIGN_ITEMS = sizeof(ALIGN_ITEMS) / sizeof(std::string);
74 const std::string ALIGN_CONTENT[] = {
80 const unsigned int NUM_ALIGN_CONTENT = sizeof(ALIGN_CONTENT) / sizeof(std::string);
82 } // unnamed namespace
85 * This example demonstrates a proof of concept for FlexContainer UI control.
86 * The flexbox properties can be changed by pressing different buttons in the
89 class FlexContainerExample : public ConnectionTracker
94 * @param application class, stored as reference
96 FlexContainerExample(Application& application)
97 : mApplication(application),
98 mCurrentFlexDirection(FlexContainer::ROW),
99 mCurrentFlexWrap(FlexContainer::WRAP),
100 mCurrentContentDirection(FlexContainer::INHERIT),
101 mCurrentJustifyContent(FlexContainer::JUSTIFY_FLEX_START),
102 mCurrentAlignItems(FlexContainer::ALIGN_FLEX_START),
103 mCurrentAlignContent(FlexContainer::ALIGN_FLEX_START)
105 // Connect to the Application's Init signal
106 mApplication.InitSignal().Connect(this, &FlexContainerExample::OnInit);
110 * This method gets called once the main loop of application is up and running
112 void OnInit(Application& app)
114 auto window = app.GetWindow();
115 window.KeyEventSignal().Connect(this, &FlexContainerExample::OnKeyEvent);
116 window.GetRootLayer().SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
118 Vector2 windowSize = window.GetSize();
120 // Creates a default view with a default tool bar.
121 // The view is added to the window.
122 Layer contents = DemoHelper::CreateView(mApplication,
129 // Create a flex direction toggle button. (left of toolbar)
130 mFlexDirectionButton = Toolkit::PushButton::New();
131 mFlexDirectionButton.SetProperty(Dali::Actor::Property::NAME, "mFlexDirectionButton");
132 mFlexDirectionButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-change.png");
133 mFlexDirectionButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-change-selected.png");
134 mFlexDirectionButton.ClickedSignal().Connect(this, &FlexContainerExample::OnFlexDirectionButtonClicked);
135 mFlexDirectionButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
136 mToolBar.AddControl(mFlexDirectionButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_LEFT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
138 // Create a flex wrap toggle button. (left of toolbar)
139 mFlexWrapButton = Toolkit::PushButton::New();
140 mFlexWrapButton.SetProperty(Dali::Actor::Property::NAME, "mFlexWrapButton");
141 mFlexWrapButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-edit.png");
142 mFlexWrapButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-edit-selected.png");
143 mFlexWrapButton.ClickedSignal().Connect(this, &FlexContainerExample::OnFlexWrapButtonClicked);
144 mFlexWrapButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
145 mToolBar.AddControl(mFlexWrapButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_LEFT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
147 // Create a content direction toggle button. (left of toolbar)
148 mContentDirectionButton = Toolkit::PushButton::New();
149 mContentDirectionButton.SetProperty(Dali::Actor::Property::NAME, "mContentDirectionButton");
150 mContentDirectionButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-replace.png");
151 mContentDirectionButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-replace-selected.png");
152 mContentDirectionButton.ClickedSignal().Connect(this, &FlexContainerExample::OnContentDirectionButtonClicked);
153 mContentDirectionButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
154 mToolBar.AddControl(mContentDirectionButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_LEFT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
156 // Create a justify content toggle button. (right of toolbar)
157 mJustifyContentButton = Toolkit::PushButton::New();
158 mJustifyContentButton.SetProperty(Dali::Actor::Property::NAME, "mJustifyContentButton");
159 mJustifyContentButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-reset.png");
160 mJustifyContentButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-reset-selected.png");
161 mJustifyContentButton.ClickedSignal().Connect(this, &FlexContainerExample::OnJustifyContentButtonClicked);
162 mJustifyContentButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
163 mToolBar.AddControl(mJustifyContentButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
165 // Create a align items toggle button. (right of toolbar)
166 mAlignItemsButton = Toolkit::PushButton::New();
167 mAlignItemsButton.SetProperty(Dali::Actor::Property::NAME, "mAlignItemsButton");
168 mAlignItemsButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-highp.png");
169 mAlignItemsButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-highp-selected.png");
170 mAlignItemsButton.ClickedSignal().Connect(this, &FlexContainerExample::OnAlignItemsButtonClicked);
171 mAlignItemsButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
172 mToolBar.AddControl(mAlignItemsButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
174 // Create a align content toggle button. (right of toolbar)
175 mAlignContentButton = Toolkit::PushButton::New();
176 mAlignContentButton.SetProperty(Dali::Actor::Property::NAME, "mAlignContentButton");
177 mAlignContentButton.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-effect-cross.png");
178 mAlignContentButton.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, DEMO_IMAGE_DIR "icon-effect-cross-selected.png");
179 mAlignContentButton.ClickedSignal().Connect(this, &FlexContainerExample::OnAlignContentButtonClicked);
180 mAlignContentButton.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
181 mToolBar.AddControl(mAlignContentButton, VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HORIZONTAL_RIGHT, DemoHelper::DEFAULT_MODE_SWITCH_PADDING);
183 // Create the base flex container
184 mFlexContainer = FlexContainer::New();
185 mFlexContainer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
186 mFlexContainer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
187 mFlexContainer.SetProperty(Actor::Property::SIZE, Vector2(windowSize.width, windowSize.height - VIEW_STYLE.mToolBarHeight));
188 mFlexContainer.SetProperty(Actor::Property::POSITION_Y, VIEW_STYLE.mToolBarHeight);
189 mFlexContainer.SetProperty(FlexContainer::Property::FLEX_DIRECTION, FlexContainer::COLUMN); // column as main axis
190 contents.Add(mFlexContainer);
192 // Add a text label to the container for showing the recently updated flexbox property value
193 mFlexPropertyLabel = TextLabel::New(FLEX_DIRECTION[mCurrentFlexDirection]);
194 mFlexPropertyLabel.SetProperty(FlexContainer::ChildProperty::FLEX_MARGIN, Vector4(10.0f, 10.0f, 10.0f, 10.0f));
195 mFlexPropertyLabel.SetProperty(FlexContainer::ChildProperty::FLEX, 0.05f); // 5 pecent of the container size in the main axis
196 mFlexPropertyLabel.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
197 mFlexPropertyLabel.SetProperty(TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER");
198 mFlexContainer.Add(mFlexPropertyLabel);
200 // Create the flex container for the flex items and add it to the base flex container
201 mFlexItemContainer = FlexContainer::New();
202 mFlexItemContainer.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
203 mFlexItemContainer.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
204 mFlexItemContainer.SetBackgroundColor(Color::YELLOW);
205 mFlexItemContainer.SetProperty(FlexContainer::Property::FLEX_DIRECTION, mCurrentFlexDirection);
206 mFlexItemContainer.SetProperty(FlexContainer::Property::FLEX_WRAP, mCurrentFlexWrap);
207 mFlexItemContainer.SetProperty(FlexContainer::ChildProperty::FLEX, 0.95f); // 95 pecent of the container size in the main axis
208 mFlexContainer.Add(mFlexItemContainer);
210 // Create flex items and add them to the container
211 for(int i = 0; i < NUM_FLEX_ITEMS; i++)
213 PushButton flexItem = PushButton::New();
214 flexItem.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
215 flexItem.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
217 // Set different background colour to help to identify different items
218 flexItem.SetBackgroundColor(Vector4(static_cast<float>(i) / NUM_FLEX_ITEMS, static_cast<float>(NUM_FLEX_ITEMS - i) / NUM_FLEX_ITEMS, 1.0f, 1.0f));
219 flexItem.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, "");
220 flexItem.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, "");
222 // Add a label to the button so that we can identify each item more easily
223 std::ostringstream index;
225 flexItem.SetProperty(Toolkit::Button::Property::LABEL, index.str());
226 flexItem.SetProperty(Dali::Actor::Property::NAME, "FlexItem " + index.str());
228 // Set a fixed size to the items so that we can wrap the line and test these
229 // flex properties that only work when there are multiple lines in the layout
230 flexItem.SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
231 // Make sure there are still extra space in the line after wrapping
232 flexItem.SetProperty(Actor::Property::SIZE, Vector2(windowSize.width / NUM_FLEX_ITEMS * 1.25f, (windowSize.height - VIEW_STYLE.mToolBarHeight) * 0.95f / NUM_FLEX_ITEMS * 1.25f));
234 mFlexItemContainer.Add(flexItem);
238 SetTitle("Flex direction", FLEX_DIRECTION[mCurrentFlexDirection]);
241 bool OnFlexDirectionButtonClicked(Toolkit::Button button)
243 mCurrentFlexDirection = static_cast<FlexContainer::FlexDirection>((mCurrentFlexDirection + 1) % NUM_FLEX_DIRECTION);
244 mFlexItemContainer.SetProperty(FlexContainer::Property::FLEX_DIRECTION, mCurrentFlexDirection);
245 SetTitle("Flex direction", FLEX_DIRECTION[mCurrentFlexDirection]);
250 bool OnFlexWrapButtonClicked(Toolkit::Button button)
252 mCurrentFlexWrap = static_cast<FlexContainer::WrapType>((mCurrentFlexWrap + 1) % NUM_FLEX_WRAP);
253 mFlexItemContainer.SetProperty(FlexContainer::Property::FLEX_WRAP, mCurrentFlexWrap);
254 SetTitle("Flex wrap", FLEX_WRAP[mCurrentFlexWrap]);
259 bool OnContentDirectionButtonClicked(Toolkit::Button button)
261 mCurrentContentDirection = static_cast<FlexContainer::ContentDirection>((mCurrentContentDirection + 1) % NUM_CONTENT_DIRECTION);
262 mFlexItemContainer.SetProperty(FlexContainer::Property::CONTENT_DIRECTION, mCurrentContentDirection);
263 SetTitle("Content direction", CONTENT_DIRECTION[mCurrentContentDirection]);
268 bool OnJustifyContentButtonClicked(Toolkit::Button button)
270 mCurrentJustifyContent = static_cast<FlexContainer::Justification>((mCurrentJustifyContent + 1) % NUM_JUSTIFY_CONTENT);
271 mFlexItemContainer.SetProperty(FlexContainer::Property::JUSTIFY_CONTENT, mCurrentJustifyContent);
272 SetTitle("Justify content", JUSTIFY_CONTENT[mCurrentJustifyContent]);
277 bool OnAlignItemsButtonClicked(Toolkit::Button button)
279 mCurrentAlignItems = static_cast<FlexContainer::Alignment>((mCurrentAlignItems + 1) % (NUM_ALIGN_ITEMS + 1));
280 mCurrentAlignItems = mCurrentAlignItems < FlexContainer::ALIGN_FLEX_START ? FlexContainer::ALIGN_FLEX_START : mCurrentAlignItems; // skip auto as it is invalid for alignItems property
281 mFlexItemContainer.SetProperty(FlexContainer::Property::ALIGN_ITEMS, mCurrentAlignItems);
282 SetTitle("Align Items", ALIGN_ITEMS[mCurrentAlignItems - 1]);
287 bool OnAlignContentButtonClicked(Toolkit::Button button)
289 mCurrentAlignContent = static_cast<FlexContainer::Alignment>((mCurrentAlignContent + 1) % (NUM_ALIGN_CONTENT + 1));
290 mCurrentAlignContent = mCurrentAlignContent < FlexContainer::ALIGN_FLEX_START ? FlexContainer::ALIGN_FLEX_START : mCurrentAlignContent; // skip auto as it is invalid for alignContent property
291 mFlexItemContainer.SetProperty(FlexContainer::Property::ALIGN_CONTENT, mCurrentAlignContent);
292 SetTitle("Align content", ALIGN_CONTENT[mCurrentAlignContent - 1]);
299 * Sets/Updates the title of the View and the value of the recently updated
302 * @param[in] title The new title for the view.
303 * @param[in] propertyValue The value of the flexbox property.
305 void SetTitle(const std::string& title, const std::string& propertyValue)
309 mTitleActor = DemoHelper::CreateToolBarLabel("");
310 // Add title to the tool bar.
311 mToolBar.AddControl(mTitleActor, VIEW_STYLE.mToolBarTitlePercentage, Alignment::HORIZONTAL_CENTER);
314 // Update the title and property value
315 mTitleActor.SetProperty(TextLabel::Property::TEXT, title);
316 mFlexPropertyLabel.SetProperty(TextLabel::Property::TEXT, propertyValue);
320 * Main key event handler
322 void OnKeyEvent(const KeyEvent& event)
324 if(event.GetState() == KeyEvent::DOWN)
326 if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
334 Application& mApplication;
336 Toolkit::Control mView;
337 Toolkit::ToolBar mToolBar;
338 TextLabel mTitleActor; ///< The Toolbar's Title.
340 FlexContainer mFlexContainer;
341 FlexContainer mFlexItemContainer;
342 TextLabel mFlexPropertyLabel;
344 FlexContainer::FlexDirection mCurrentFlexDirection;
345 FlexContainer::WrapType mCurrentFlexWrap;
346 FlexContainer::ContentDirection mCurrentContentDirection;
347 FlexContainer::Justification mCurrentJustifyContent;
348 FlexContainer::Alignment mCurrentAlignItems;
349 FlexContainer::Alignment mCurrentAlignContent;
351 Toolkit::PushButton mFlexDirectionButton;
352 Toolkit::PushButton mFlexWrapButton;
353 Toolkit::PushButton mContentDirectionButton;
354 Toolkit::PushButton mJustifyContentButton;
355 Toolkit::PushButton mAlignItemsButton;
356 Toolkit::PushButton mAlignContentButton;
359 int DALI_EXPORT_API main(int argc, char** argv)
361 Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
362 FlexContainerExample test(app);