2 * Copyright (c) 2021 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 <dali/devel-api/adaptor-framework/actor-accessible.h>
22 #include <dali/devel-api/actors/actor-devel.h>
25 #include <dali/devel-api/adaptor-framework/window-devel.h>
27 namespace Dali::Accessibility
29 ActorAccessible::ActorAccessible(Actor actor)
31 mChildrenDirty{true} // to trigger the initial UpdateChildren()
33 // Select the right overload manually because Connect(this, &OnChildrenChanged) is ambiguous.
34 void (ActorAccessible::*handler)(Dali::Actor) = &ActorAccessible::OnChildrenChanged;
36 Dali::DevelActor::ChildAddedSignal(actor).Connect(this, handler);
37 Dali::DevelActor::ChildRemovedSignal(actor).Connect(this, handler);
38 Dali::DevelActor::ChildOrderChangedSignal(actor).Connect(this, handler);
41 std::string ActorAccessible::GetName() const
43 return Self().GetProperty<std::string>(Dali::Actor::Property::NAME);
46 std::string ActorAccessible::GetDescription() const
51 Accessible* ActorAccessible::GetParent()
55 auto data = GetBridgeData();
56 return data->mBridge->GetApplication();
59 return Get(Self().GetParent());
62 std::size_t ActorAccessible::GetChildCount() const
64 // ActorAccessible is never stored in const memory and it is an implementation detail that
65 // children are recalculated lazily.
66 const_cast<ActorAccessible*>(this)->UpdateChildren();
68 return mChildren.size();
71 std::vector<Accessible*> ActorAccessible::GetChildren()
78 Accessible* ActorAccessible::GetChildAtIndex(std::size_t index)
80 auto childCount = GetChildCount(); // calls UpdateChildren()
81 if(DALI_UNLIKELY(index >= childCount))
83 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(childCount) + " children"};
86 return mChildren[index];
89 std::size_t ActorAccessible::GetIndexInParent()
91 auto* parent = GetParent();
93 if(DALI_UNLIKELY(!parent))
95 throw std::domain_error{"can't call GetIndexInParent on object without parent"};
98 // Avoid calling parent->GetChildren() in order not to make an unnecessary copy
99 auto childCount = parent->GetChildCount();
100 for(auto i = 0u; i < childCount; ++i)
102 if(parent->GetChildAtIndex(i) == this)
108 throw std::domain_error{"actor is not a child of its parent"};
111 Dali::Actor ActorAccessible::GetInternalActor()
116 ComponentLayer ActorAccessible::GetLayer() const
118 return ComponentLayer::WINDOW;
121 std::int16_t ActorAccessible::GetMdiZOrder() const
126 double ActorAccessible::GetAlpha() const
131 bool ActorAccessible::IsScrollable() const
136 Dali::Rect<> ActorAccessible::GetExtents(CoordinateType type) const
138 Dali::Actor actor = Self();
139 Vector2 screenPosition = actor.GetProperty<Vector2>(Actor::Property::SCREEN_POSITION);
140 Vector3 size = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * actor.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
141 bool positionUsesAnchorPoint = actor.GetProperty<bool>(Actor::Property::POSITION_USES_ANCHOR_POINT);
142 Vector3 anchorPointOffSet = size * (positionUsesAnchorPoint ? actor.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
143 Vector2 position = Vector2(screenPosition.x - anchorPointOffSet.x, screenPosition.y - anchorPointOffSet.y);
145 if(type == CoordinateType::WINDOW)
147 return {position.x, position.y, size.x, size.y};
149 else // CoordinateType::SCREEN
151 auto window = Dali::DevelWindow::Get(actor);
152 auto windowPosition = window.GetPosition();
153 return {position.x + windowPosition.GetX(), position.y + windowPosition.GetY(), size.x, size.y};
157 void ActorAccessible::OnChildrenChanged()
159 mChildrenDirty = true;
162 void ActorAccessible::OnChildrenChanged(Dali::Actor)
167 void ActorAccessible::DoGetChildren(std::vector<Accessible*>& children)
170 auto childCount = self.GetChildCount();
172 children.reserve(childCount);
174 for(auto i = 0u; i < childCount; ++i)
176 children.push_back(Accessible::Get(self.GetChildAt(i)));
180 void ActorAccessible::UpdateChildren()
187 // Set to false before DoGetChildren() to prevent recursion
188 // in case DoGetChildren() does something strange.
189 mChildrenDirty = false;
192 DoGetChildren(mChildren);
194 // Erase-remove idiom
195 // TODO (C++20): Replace with std::erase_if
196 auto it = std::remove_if(mChildren.begin(), mChildren.end(), [](const Accessible* child) {
197 return !child || child->IsHidden();
199 mChildren.erase(it, mChildren.end());
200 mChildren.shrink_to_fit();
203 } // namespace Dali::Accessibility