2 * Copyright (c) 2024 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>
28 #include <dali/public-api/object/property-map.h>
29 #include <dali/public-api/object/type-info.h>
32 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
33 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
34 #include <dali-toolkit/internal/visuals/image/image-visual.h>
35 #include <dali-toolkit/public-api/controls/control-impl.h>
36 #include <dali-toolkit/public-api/controls/control.h>
37 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
38 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
40 namespace Dali::Toolkit::DevelControl
44 constexpr const char* ATTR_IMG_SRC_KEY = "imgSrc";
46 std::string GetLocaleText(std::string string, const char* domain = "dali-toolkit")
48 #ifdef DGETTEXT_ENABLED
49 /*TODO: currently non-localized string is used as a key for translation lookup. In case the lookup key formatting is forced
50 consider calling utility function for converting non-localized string into well-formatted key before lookup. */
51 return dgettext(domain, string.c_str());
57 Dali::Actor CreateHighlightIndicatorActor()
59 std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
60 focusBorderImagePath += "/keyboard_focus.9.png";
62 // Create the default if it hasn't been set and one that's shared by all the
63 // keyboard focusable actors
64 auto actor = Toolkit::ImageView::New(focusBorderImagePath);
65 actor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
67 DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
68 actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
73 std::string FetchImageSrcFromMap(const Dali::Property::Map& imageMap)
75 auto urlVal = imageMap.Find(Toolkit::ImageVisual::Property::URL);
78 if(urlVal->GetType() == Dali::Property::STRING)
80 return urlVal->Get<std::string>();
82 else if(urlVal->GetType() == Dali::Property::ARRAY)
84 auto urlArray = urlVal->GetArray();
85 if(urlArray && !urlArray->Empty())
87 // Returns first element if url is an array
88 return (*urlArray)[0].Get<std::string>();
95 std::string FetchImageSrc(const Toolkit::ImageView& imageView)
97 const auto imageUrl = imageView.GetProperty<std::string>(Toolkit::ImageView::Property::IMAGE);
103 const auto imageMap = imageView.GetProperty<Dali::Property::Map>(Toolkit::ImageView::Property::IMAGE);
104 if(!imageMap.Empty())
106 return FetchImageSrcFromMap(imageMap);
111 } // unnamed namespace
113 ControlAccessible::ControlAccessible(Dali::Actor self)
114 : ActorAccessible(self)
118 std::string ControlAccessible::GetName() const
120 auto control = Dali::Toolkit::Control::DownCast(Self());
122 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
123 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
126 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
128 controlImpl.mAccessibilityGetNameSignal.Emit(name);
130 else if(!controlImpl.mAccessibilityName.empty())
132 name = controlImpl.mAccessibilityName;
134 else if(auto raw = GetNameRaw(); !raw.empty())
140 name = Self().GetProperty<std::string>(Actor::Property::NAME);
143 if(!controlImpl.mAccessibilityTranslationDomain.empty())
145 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
148 return GetLocaleText(name);
151 std::string ControlAccessible::GetNameRaw() const
156 std::string ControlAccessible::GetDescription() const
158 auto control = Dali::Toolkit::Control::DownCast(Self());
160 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
161 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
162 std::string description;
164 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
166 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
168 else if(!controlImpl.mAccessibilityDescription.empty())
170 description = controlImpl.mAccessibilityDescription;
174 description = GetDescriptionRaw();
177 if(!controlImpl.mAccessibilityTranslationDomain.empty())
179 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
182 return GetLocaleText(description);
185 std::string ControlAccessible::GetDescriptionRaw() const
190 Dali::Accessibility::Role ControlAccessible::GetRole() const
192 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
195 std::string ControlAccessible::GetLocalizedRoleName() const
197 return GetLocaleText(GetRoleName());
200 bool ControlAccessible::IsShowing()
202 Dali::Actor self = Self();
203 if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || Dali::EqualsZero(self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a) || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
209 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
217 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
218 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
222 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
228 Dali::Accessibility::States ControlAccessible::CalculateStates()
230 using Dali::Accessibility::State;
232 Dali::Actor self = Self();
233 Dali::Accessibility::States states;
235 states[State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
236 states[State::FOCUSED] = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
237 states[State::HIGHLIGHTABLE] = self.GetProperty<bool>(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE);
238 states[State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
239 states[State::ENABLED] = true;
240 states[State::SENSITIVE] = (Dali::DevelActor::IsHittable(self) && Dali::DevelActor::GetTouchRequired(self));
241 states[State::VISIBLE] = self.GetProperty<bool>(Actor::Property::VISIBLE);
242 states[State::SHOWING] = IsShowing();
243 states[State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
248 Dali::Accessibility::States ControlAccessible::GetStates()
250 return CalculateStates();
253 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
255 static const std::string automationIdKey = "automationId";
256 static const std::string classKey = "class";
258 Accessibility::Attributes result;
259 Toolkit::Control control = Toolkit::Control::DownCast(Self());
260 Dali::Property::Value property = control.GetProperty(DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
261 Dali::Property::Map* attributeMap = property.GetMap();
262 std::size_t attributeCount = attributeMap ? attributeMap->Count() : 0U;
264 for(std::size_t i = 0; i < attributeCount; i++)
266 Dali::Property::Key mapKey = attributeMap->GetKeyAt(i);
267 std::string mapValue;
269 if(mapKey.type == Dali::Property::Key::STRING && attributeMap->GetValue(i).Get(mapValue))
271 result.emplace(std::move(mapKey.stringKey), std::move(mapValue));
275 auto automationId = control.GetProperty<std::string>(DevelControl::Property::AUTOMATION_ID);
276 if(!automationId.empty())
278 result.emplace(automationIdKey, std::move(automationId));
281 if(auto imageView = Toolkit::ImageView::DownCast(Self()))
283 auto imageSrc = FetchImageSrc(imageView);
284 if(!imageSrc.empty())
286 result.emplace(ATTR_IMG_SRC_KEY, std::move(imageSrc));
290 // Add "class" if not present already
291 if(result.find(classKey) == result.end())
293 Dali::TypeInfo typeInfo;
294 Self().GetTypeInfo(typeInfo);
297 const std::string& typeName = typeInfo.GetName();
299 result.emplace(classKey, typeName);
301 // Save the 'typeName' so we don't have to calculate it again
302 DevelControl::AppendAccessibilityAttribute(control, classKey, typeName);
309 bool ControlAccessible::IsHidden() const
311 auto control = Dali::Toolkit::Control::DownCast(Self());
313 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
314 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
316 return controlImpl.mAccessibilityHidden;
319 bool ControlAccessible::GrabFocus()
321 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
324 void ControlAccessible::ScrollToSelf()
327 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
331 if(parent->IsScrollable())
333 parent->ScrollToChild(child->Self());
336 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
340 void ControlAccessible::RegisterPositionPropertyNotification()
342 auto control = Dali::Toolkit::Control::DownCast(Self());
343 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
344 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
345 controlImpl.RegisterAccessibilityPositionPropertyNotification();
348 void ControlAccessible::UnregisterPositionPropertyNotification()
350 auto control = Dali::Toolkit::Control::DownCast(Self());
351 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
352 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
353 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
356 void ControlAccessible::RegisterPropertySetSignal()
358 auto control = Dali::Toolkit::Control::DownCast(Self());
359 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
360 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
361 controlImpl.RegisterAccessibilityPropertySetSignal();
364 void ControlAccessible::UnregisterPropertySetSignal()
366 auto control = Dali::Toolkit::Control::DownCast(Self());
367 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
368 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
369 controlImpl.UnregisterAccessibilityPropertySetSignal();
372 bool ControlAccessible::GrabHighlight()
374 Dali::Actor self = Self();
375 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
377 if(!Dali::Accessibility::IsUp())
382 if(self == oldHighlightedActor)
387 // Clear the old highlight.
388 if(oldHighlightedActor)
390 auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
391 if(oldHighlightedObject)
393 oldHighlightedObject->ClearHighlight();
397 auto highlight = GetHighlightActor();
400 highlight = CreateHighlightIndicatorActor();
401 SetHighlightActor(highlight);
404 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
405 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
406 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
408 // Need to set resize policy again, to update SIZE property which is set by
409 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
410 // ControlAccessible. In this case, highlight has incorrect size.
411 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
413 // Remember the highlight actor, so that when the default is changed with
414 // SetHighlightActor(), the currently displayed highlight can still be cleared.
415 mCurrentHighlightActor = highlight;
418 SetCurrentlyHighlightedActor(self);
419 EmitHighlighted(true);
420 RegisterPositionPropertyNotification();
421 RegisterPropertySetSignal();
426 bool ControlAccessible::ClearHighlight()
428 Dali::Actor self = Self();
430 if(!Dali::Accessibility::IsUp())
435 if(GetCurrentlyHighlightedActor() == self)
437 UnregisterPropertySetSignal();
438 UnregisterPositionPropertyNotification();
439 self.Remove(mCurrentHighlightActor.GetHandle());
440 mCurrentHighlightActor = {};
441 SetCurrentlyHighlightedActor({});
442 EmitHighlighted(false);
448 std::string ControlAccessible::GetActionName(size_t index) const
450 if(index >= GetActionCount())
456 Self().GetTypeInfo(type);
457 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
458 return type.GetActionName(index);
461 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
463 return GetLocaleText(GetActionName(index));
466 std::string ControlAccessible::GetActionDescription(size_t index) const
471 size_t ControlAccessible::GetActionCount() const
474 Self().GetTypeInfo(type);
475 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
476 return type.GetActionCount();
479 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
484 bool ControlAccessible::DoAction(size_t index)
486 std::string actionName = GetActionName(index);
487 return Self().DoAction(actionName, {});
490 bool ControlAccessible::DoAction(const std::string& name)
492 return Self().DoAction(name, {});
495 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
497 auto control = Dali::Toolkit::Control::DownCast(Self());
499 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
500 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
502 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
504 auto ret = std::make_pair(gestureInfo, false);
505 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
512 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
514 auto control = Dali::Toolkit::Control::DownCast(Self());
516 return DevelControl::GetAccessibilityRelations(control);
519 bool ControlAccessible::ScrollToChild(Actor child)
524 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
526 return Actor::Property::NAME;
529 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
531 return Dali::Property::INVALID_INDEX;
534 void ControlAccessible::SetLastPosition(Vector2 position)
536 mLastPosition = position;
539 Vector2 ControlAccessible::GetLastPosition() const
541 return mLastPosition;
544 } // namespace Dali::Toolkit::DevelControl