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 "control-accessible.h"
22 #ifdef DGETTEXT_ENABLED
26 #include <dali/devel-api/actors/actor-devel.h>
27 #include <dali/devel-api/adaptor-framework/window-devel.h>
30 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
31 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
32 #include <dali-toolkit/public-api/controls/control-impl.h>
33 #include <dali-toolkit/public-api/controls/control.h>
34 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
35 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
37 namespace Dali::Toolkit::DevelControl
41 static std::string GetLocaleText(std::string string, const char *domain = "dali-toolkit")
43 #ifdef DGETTEXT_ENABLED
44 /*TODO: currently non-localized string is used as a key for translation lookup. In case the lookup key formatting is forced
45 consider calling utility function for converting non-localized string into well-formatted key before lookup. */
46 return dgettext(domain, string.c_str());
52 static Dali::Actor CreateHighlightIndicatorActor()
54 std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
55 focusBorderImagePath += "/keyboard_focus.9.png";
57 // Create the default if it hasn't been set and one that's shared by all the
58 // keyboard focusable actors
59 auto actor = Toolkit::ImageView::New(focusBorderImagePath);
60 actor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
62 DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
63 actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
67 } // unnamed namespace
69 ControlAccessible::ControlAccessible(Dali::Actor self, Dali::Accessibility::Role role, bool modal)
73 auto control = Dali::Toolkit::Control::DownCast(Self());
75 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
76 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
77 if(controlImpl.mAccessibilityRole == Dali::Accessibility::Role::UNKNOWN)
79 controlImpl.mAccessibilityRole = role;
82 Self().PropertySetSignal().Connect(&controlImpl, [this, &controlImpl](Dali::Handle& handle, Dali::Property::Index index, Dali::Property::Value value) {
83 if(this->Self() != Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor())
88 if(index == DevelControl::Property::ACCESSIBILITY_NAME || (index == GetNamePropertyIndex() && !controlImpl.mAccessibilityNameSet))
90 if(controlImpl.mAccessibilityGetNameSignal.Empty())
92 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::NAME);
96 if(index == DevelControl::Property::ACCESSIBILITY_DESCRIPTION || (index == GetDescriptionPropertyIndex() && !controlImpl.mAccessibilityDescriptionSet))
98 if(controlImpl.mAccessibilityGetDescriptionSignal.Empty())
100 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
106 std::string ControlAccessible::GetName() const
108 auto control = Dali::Toolkit::Control::DownCast(Self());
110 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
111 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
114 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
116 controlImpl.mAccessibilityGetNameSignal.Emit(name);
118 else if(controlImpl.mAccessibilityNameSet)
120 name = controlImpl.mAccessibilityName;
122 else if(auto raw = GetNameRaw(); !raw.empty())
128 name = Self().GetProperty<std::string>(Actor::Property::NAME);
131 if(controlImpl.mAccessibilityTranslationDomainSet)
133 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
136 return GetLocaleText(name);
139 std::string ControlAccessible::GetNameRaw() const
144 std::string ControlAccessible::GetDescription() const
146 auto control = Dali::Toolkit::Control::DownCast(Self());
148 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
149 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
150 std::string description;
152 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
154 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
156 else if(controlImpl.mAccessibilityDescriptionSet)
158 description = controlImpl.mAccessibilityDescription;
162 description = GetDescriptionRaw();
164 if(controlImpl.mAccessibilityTranslationDomainSet)
166 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
169 return GetLocaleText(description);
172 std::string ControlAccessible::GetDescriptionRaw() const
177 Dali::Accessibility::Accessible* ControlAccessible::GetParent()
179 return Dali::Accessibility::Accessible::Get(Self().GetParent());
182 size_t ControlAccessible::GetChildCount() const
184 return Self().GetChildCount();
187 Dali::Accessibility::Accessible* ControlAccessible::GetChildAtIndex(size_t index)
189 return Dali::Accessibility::Accessible::Get(Self().GetChildAt(static_cast<unsigned int>(index)));
192 size_t ControlAccessible::GetIndexInParent()
195 auto parent = self.GetParent();
196 DALI_ASSERT_ALWAYS(parent && "can't call GetIndexInParent on object without parent");
198 auto count = parent.GetChildCount();
199 for(auto i = 0u; i < count; ++i)
201 auto child = parent.GetChildAt(i);
207 DALI_ASSERT_ALWAYS(false && "object isn't child of it's parent");
208 return static_cast<size_t>(-1);
211 Dali::Accessibility::Role ControlAccessible::GetRole() const
213 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
216 std::string ControlAccessible::GetLocalizedRoleName() const
218 return GetLocaleText(GetRoleName());
221 bool ControlAccessible::IsShowing()
223 Dali::Actor self = Self();
224 if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a == 0 || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
230 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
236 auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
239 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
240 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
244 auto clipMode = control.GetProperty(Actor::Property::CLIPPING_MODE).Get<bool>();
245 auto parentExtent = parent->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
246 if ((clipMode != ClippingMode::DISABLED) && !parentExtent.Intersects(childExtent))
250 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
256 Dali::Accessibility::States ControlAccessible::CalculateStates()
258 Dali::Actor self = Self();
259 Dali::Accessibility::States state;
260 state[Dali::Accessibility::State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
261 state[Dali::Accessibility::State::FOCUSED] = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
263 if(self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).GetType() == Dali::Property::NONE)
265 state[Dali::Accessibility::State::HIGHLIGHTABLE] = false;
269 state[Dali::Accessibility::State::HIGHLIGHTABLE] = self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).Get<bool>();
272 state[Dali::Accessibility::State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
273 state[Dali::Accessibility::State::ENABLED] = true;
274 state[Dali::Accessibility::State::SENSITIVE] = true;
275 state[Dali::Accessibility::State::VISIBLE] = self.GetProperty<bool>(Actor::Property::VISIBLE);
279 state[Dali::Accessibility::State::MODAL] = true;
281 state[Dali::Accessibility::State::SHOWING] = IsShowing();
282 state[Dali::Accessibility::State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
286 Dali::Accessibility::States ControlAccessible::GetStates()
288 return CalculateStates();
291 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
293 std::unordered_map<std::string, std::string> attributeMap;
294 auto control = Dali::Toolkit::Control::DownCast(Self());
295 auto attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
296 auto map = attribute.GetMap();
300 auto mapSize = map->Count();
302 for(unsigned int i = 0; i < mapSize; i++)
304 auto mapKey = map->GetKeyAt(i);
305 if(mapKey.type == Dali::Property::Key::STRING)
307 std::string mapValue;
308 if(map->GetValue(i).Get(mapValue))
310 attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
319 Dali::Accessibility::ComponentLayer ControlAccessible::GetLayer() const
321 return Dali::Accessibility::ComponentLayer::WINDOW;
324 Dali::Rect<> ControlAccessible::GetExtents(Dali::Accessibility::CoordinateType type) const
326 Dali::Actor self = Self();
328 Vector2 screenPosition = self.GetProperty(Dali::DevelActor::Property::SCREEN_POSITION).Get<Vector2>();
329 auto size = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
330 bool positionUsesAnchorPoint = self.GetProperty(Dali::DevelActor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
331 Vector3 anchorPointOffSet = size * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
332 Vector2 position = Vector2((screenPosition.x - anchorPointOffSet.x), (screenPosition.y - anchorPointOffSet.y));
334 if(type == Dali::Accessibility::CoordinateType::WINDOW)
336 return {position.x, position.y, size.x, size.y};
338 else // Dali::Accessibility::CoordinateType::SCREEN
340 auto window = Dali::DevelWindow::Get(self);
341 auto windowPosition = window.GetPosition();
342 return {position.x + windowPosition.GetX(), position.y + windowPosition.GetY(), size.x, size.y};
346 int16_t ControlAccessible::GetMdiZOrder() const
351 double ControlAccessible::GetAlpha() const
356 bool ControlAccessible::GrabFocus()
358 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
361 void ControlAccessible::ScrollToSelf()
364 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
368 if (parent->IsScrollable())
370 parent->ScrollToChild(child->Self());
373 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
377 void ControlAccessible::RegisterPositionPropertyNotification()
379 auto control = Dali::Toolkit::Control::DownCast(Self());
380 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
381 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
382 controlImpl.RegisterAccessibilityPositionPropertyNotification();
385 void ControlAccessible::UnregisterPositionPropertyNotification()
387 auto control = Dali::Toolkit::Control::DownCast(Self());
388 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
389 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
390 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
393 bool ControlAccessible::GrabHighlight()
395 Dali::Actor self = Self();
396 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
398 if(!Dali::Accessibility::IsUp())
403 if(self == oldHighlightedActor)
408 // Clear the old highlight.
409 if(oldHighlightedActor)
411 auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
412 if(oldHighlightObject)
414 oldHighlightObject->ClearHighlight();
418 auto highlight = GetHighlightActor();
421 highlight = CreateHighlightIndicatorActor();
422 SetHighlightActor(highlight);
425 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
426 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
427 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
429 // Need to set resize policy again, to update SIZE property which is set by
430 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
431 // ControlAccessible. In this case, highlight has incorrect size.
432 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
434 // Remember the highlight actor, so that when the default is changed with
435 // SetHighlightActor(), the currently displayed highlight can still be cleared.
436 mCurrentHighlightActor = highlight;
439 SetCurrentlyHighlightedActor(self);
440 EmitHighlighted(true);
441 RegisterPositionPropertyNotification();
446 bool ControlAccessible::ClearHighlight()
448 Dali::Actor self = Self();
450 if(!Dali::Accessibility::IsUp())
455 if(GetCurrentlyHighlightedActor() == self)
457 UnregisterPositionPropertyNotification();
458 self.Remove(mCurrentHighlightActor.GetHandle());
459 mCurrentHighlightActor = {};
460 SetCurrentlyHighlightedActor({});
461 EmitHighlighted(false);
467 std::string ControlAccessible::GetActionName(size_t index) const
469 if(index >= GetActionCount())
475 Self().GetTypeInfo(type);
476 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
477 return type.GetActionName(index);
480 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
482 return GetLocaleText(GetActionName(index));
485 std::string ControlAccessible::GetActionDescription(size_t index) const
490 size_t ControlAccessible::GetActionCount() const
493 Self().GetTypeInfo(type);
494 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
495 return type.GetActionCount();
498 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
503 bool ControlAccessible::DoAction(size_t index)
505 std::string actionName = GetActionName(index);
506 return Self().DoAction(actionName, {});
509 bool ControlAccessible::DoAction(const std::string& name)
511 return Self().DoAction(name, {});
514 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
516 auto control = Dali::Toolkit::Control::DownCast(Self());
518 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
519 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
521 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
523 auto ret = std::make_pair(gestureInfo, false);
524 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
531 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
533 auto control = Dali::Toolkit::Control::DownCast(Self());
535 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
536 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
538 std::vector<Dali::Accessibility::Relation> ret;
540 for(auto& relation : controlImpl.mAccessibilityRelations)
542 auto& targets = relation.second;
544 ret.emplace_back(Accessibility::Relation{relation.first, {}});
546 // Map every Accessible* to its Address
547 std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
548 return x->GetAddress();
555 Dali::Actor ControlAccessible::GetInternalActor()
557 return Dali::Actor{};
560 bool ControlAccessible::ScrollToChild(Actor child)
565 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
567 return Actor::Property::NAME;
570 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
572 return Dali::Property::INVALID_INDEX;
575 void ControlAccessible::SetLastPosition(Vector2 position)
577 mLastPosition = position;
580 Vector2 ControlAccessible::GetLastPosition() const
582 return mLastPosition;
585 } // namespace Dali::Toolkit::DevelControl