1 #ifndef DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H
2 #define DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/object/weak-handle.h>
25 #include <dali-toolkit/public-api/controls/control-impl.h>
26 #include <dali-toolkit/public-api/controls/flex-container/flex-container.h>
27 #include <dali-toolkit/third-party/yoga/Yoga.h>
36 * FlexContainer is a custom control for laying out actors in a Flexbox layout
37 * @see Dali::Toolkit:FlexContainer for more details
39 class FlexContainer : public Control
43 * The structure to store the style properties and layout information of flex item
47 WeakHandle<Dali::Actor> actor; ///< Actor handle of the flex item
48 YGNodeRef node; ///< The style properties and layout information
51 typedef std::vector<FlexItemNode> FlexItemNodeContainer;
55 * Construct a new FlexContainer.
60 * Create a new FlexContainer.
61 * @return A smart-pointer to the newly allocated FlexContainer.
63 static Toolkit::FlexContainer New();
66 * @brief Set the primary direction in which content is ordered.
67 * @param[in] contentDirection The direction of the content.
69 void SetContentDirection(Toolkit::FlexContainer::ContentDirection contentDirection);
72 * @brief Get the direction of the content.
73 * @return The direction of the content.
75 Toolkit::FlexContainer::ContentDirection GetContentDirection();
78 * @brief Set the direction flex items are laid out.
79 * @param[in] flexDirection The direction flex items are laid out.
81 void SetFlexDirection(Toolkit::FlexContainer::FlexDirection flexDirection);
84 * @brief Get the direction flex items are laid out.
85 * @return The direction flex items are laid out.
87 Toolkit::FlexContainer::FlexDirection GetFlexDirection();
90 * @brief Set whether the flex items should wrap or not, if there
91 * is no enough room for them on one flex line.
92 * @param[in] flexWrap The wrap type.
94 void SetFlexWrap(Toolkit::FlexContainer::WrapType flexWrap);
97 * @brief Get whether the flex items should wrap or not, if there
98 * is no enough room for them on one flex line.
99 * @return The wrap type.
101 Toolkit::FlexContainer::WrapType GetFlexWrap();
104 * @brief Set the horizontal alignment of the flex items when the items
105 * do not use all available space on the main-axis.
106 * @param[in] justifyContent The horizontal alignment of flex items.
108 void SetJustifyContent(Toolkit::FlexContainer::Justification justifyContent);
111 * @brief Get the horizontal alignment of the flex items when the items
112 * do not use all available space on the main-axis.
113 * @return The horizontal alignment of flex items.
115 Toolkit::FlexContainer::Justification GetJustifyContent();
118 * @brief Set the vertical alignment of the flex items when the items
119 * do not use all available space on the cross-axis.
120 * @param[in] alignItems The vertical alignment of flex items.
122 void SetAlignItems(Toolkit::FlexContainer::Alignment alignItems);
125 * @brief Get the vertical alignment of the flex items when the items
126 * do not use all available space on the cross-axis.
127 * @return The vertical alignment of flex items.
129 Toolkit::FlexContainer::Alignment GetAlignItems();
132 * @brief Set the vertical alignment of the flex lines when the lines
133 * do not use all available space on the cross-axis.
134 * @param[in] alignItems The vertical alignment of flex lines.
136 void SetAlignContent(Toolkit::FlexContainer::Alignment alignContent);
139 * @brief Get the vertical alignment of the flex lines when the lines
140 * do not use all available space on the cross-axis.
141 * @return The vertical alignment of flex lines.
143 Toolkit::FlexContainer::Alignment GetAlignContent();
148 * Called when a property of an object of this type is set.
149 * @param[in] object The object whose property is set.
150 * @param[in] index The property index.
151 * @param[in] value The new property value.
153 static void SetProperty(BaseObject* object, Property::Index index, const Property::Value& value);
156 * Called to retrieve a property of an object of this type.
157 * @param[in] object The object whose property is to be retrieved.
158 * @param[in] index The property index.
159 * @return The current value of the property.
161 static Property::Value GetProperty(BaseObject* object, Property::Index index);
163 private: // From Control
165 * @copydoc Control::OnInitialize()
167 void OnInitialize() override;
170 * @copydoc Control::OnChildAdd(Actor& child)
172 void OnChildAdd(Actor& child) override;
175 * @copydoc Control::OnChildRemove(Actor& child)
177 void OnChildRemove(Actor& child) override;
180 * @copydoc Control::OnRelayout
182 void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
185 * @copydoc Control::RelayoutDependentOnChildren()
187 bool RelayoutDependentOnChildren(Dimension::Type dimension = Dimension::ALL_DIMENSIONS) override;
190 * @copydoc Control::GetNextKeyboardFocusableActor
192 Actor GetNextKeyboardFocusableActor(Actor currentFocusedActor, Toolkit::Control::KeyboardFocus::Direction direction, bool loopEnabled) override;
195 * @copydoc CustomActorImpl::OnSizeSet( const Vector3& size )
197 void OnSizeSet(const Vector3& size) override;
200 * @copydoc OnLayoutDirectionChanged( Dali::Actor actor, Dali::LayoutDirection::Type type )
201 * @param[in] actor The actor whose layoutDirection is changed.
202 * @param[in] type The layoutDirection.
204 void OnLayoutDirectionChanged(Dali::Actor actor, Dali::LayoutDirection::Type type);
206 private: // Implementation
208 * Calculate the layout properties of all the children
210 void ComputeLayout();
213 * Calculate the layout of the children and relayout them with their new size and position
215 void RelayoutChildren();
218 * A reference counted object may only be deleted by calling Unreference()
220 virtual ~FlexContainer();
223 // Undefined copy constructor and assignment operators
224 FlexContainer(const FlexContainer&);
225 FlexContainer& operator=(const FlexContainer& rhs);
228 FlexItemNode mRootNode; ///< Style properties and layout information of flex container
229 FlexItemNodeContainer mChildrenNodes; ///< Style properties and layout information of flex items in the container
231 Toolkit::FlexContainer::ContentDirection mContentDirection; ///< The content direction of the container
232 Toolkit::FlexContainer::FlexDirection mFlexDirection; ///< The flex direction of the container
233 Toolkit::FlexContainer::WrapType mFlexWrap; ///< The wrap type of the container
234 Toolkit::FlexContainer::Justification mJustifyContent; ///< The alignment of flex items in the container on the main-axis
235 Toolkit::FlexContainer::Alignment mAlignItems; ///< The alignment of flex items in the container on the cross-axis
236 Toolkit::FlexContainer::Alignment mAlignContent; ///< The alignment of flex lines in the container on the cross-axis
239 } // namespace Internal
241 // Helpers for public-api forwarding methods
243 inline Toolkit::Internal::FlexContainer& GetImpl(Toolkit::FlexContainer& flexContainer)
245 DALI_ASSERT_ALWAYS(flexContainer);
247 Dali::RefObject& handle = flexContainer.GetImplementation();
249 return static_cast<Toolkit::Internal::FlexContainer&>(handle);
252 inline const Toolkit::Internal::FlexContainer& GetImpl(const Toolkit::FlexContainer& flexContainer)
254 DALI_ASSERT_ALWAYS(flexContainer);
256 const Dali::RefObject& handle = flexContainer.GetImplementation();
258 return static_cast<const Toolkit::Internal::FlexContainer&>(handle);
261 } // namespace Toolkit
265 #endif // DALI_TOOLKIT_INTERNAL_FLEX_CONTAINER_H