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/type-info.h>
31 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
32 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34 #include <dali-toolkit/public-api/controls/control.h>
35 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
36 #include <dali-toolkit/public-api/focus-manager/keyboard-focus-manager.h>
38 namespace Dali::Toolkit::DevelControl
42 static std::string GetLocaleText(std::string string, const char* domain = "dali-toolkit")
44 #ifdef DGETTEXT_ENABLED
45 /*TODO: currently non-localized string is used as a key for translation lookup. In case the lookup key formatting is forced
46 consider calling utility function for converting non-localized string into well-formatted key before lookup. */
47 return dgettext(domain, string.c_str());
53 static Dali::Actor CreateHighlightIndicatorActor()
55 std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
56 focusBorderImagePath += "/keyboard_focus.9.png";
58 // Create the default if it hasn't been set and one that's shared by all the
59 // keyboard focusable actors
60 auto actor = Toolkit::ImageView::New(focusBorderImagePath);
61 actor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
63 DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
64 actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
68 } // unnamed namespace
70 ControlAccessible::ControlAccessible(Dali::Actor self)
71 : ActorAccessible(self)
75 std::string ControlAccessible::GetName() const
77 auto control = Dali::Toolkit::Control::DownCast(Self());
79 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
80 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
83 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
85 controlImpl.mAccessibilityGetNameSignal.Emit(name);
87 else if(!controlImpl.mAccessibilityName.empty())
89 name = controlImpl.mAccessibilityName;
91 else if(auto raw = GetNameRaw(); !raw.empty())
97 name = Self().GetProperty<std::string>(Actor::Property::NAME);
100 if(!controlImpl.mAccessibilityTranslationDomain.empty())
102 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
105 return GetLocaleText(name);
108 std::string ControlAccessible::GetNameRaw() const
113 std::string ControlAccessible::GetDescription() const
115 auto control = Dali::Toolkit::Control::DownCast(Self());
117 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
118 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
119 std::string description;
121 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
123 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
125 else if(!controlImpl.mAccessibilityDescription.empty())
127 description = controlImpl.mAccessibilityDescription;
131 description = GetDescriptionRaw();
134 if(!controlImpl.mAccessibilityTranslationDomain.empty())
136 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
139 return GetLocaleText(description);
142 std::string ControlAccessible::GetDescriptionRaw() const
147 Dali::Accessibility::Role ControlAccessible::GetRole() const
149 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
152 std::string ControlAccessible::GetLocalizedRoleName() const
154 return GetLocaleText(GetRoleName());
157 bool ControlAccessible::IsShowing()
159 Dali::Actor self = Self();
160 if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || Dali::EqualsZero(self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a) || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
166 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
174 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
175 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
179 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
185 Dali::Accessibility::States ControlAccessible::CalculateStates()
187 using Dali::Accessibility::State;
189 Dali::Actor self = Self();
190 Dali::Accessibility::States states;
192 states[State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
193 states[State::FOCUSED] = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
194 states[State::HIGHLIGHTABLE] = self.GetProperty<bool>(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE);
195 states[State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
196 states[State::ENABLED] = true;
197 states[State::SENSITIVE] = (Dali::DevelActor::IsHittable(self) && Dali::DevelActor::GetTouchRequired(self));
198 states[State::VISIBLE] = self.GetProperty<bool>(Actor::Property::VISIBLE);
199 states[State::SHOWING] = IsShowing();
200 states[State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
205 Dali::Accessibility::States ControlAccessible::GetStates()
207 return CalculateStates();
210 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
212 static const std::string automationIdKey = "automationId";
213 static const std::string classKey = "class";
215 Accessibility::Attributes result;
216 Toolkit::Control control = Toolkit::Control::DownCast(Self());
217 Dali::Property::Value property = control.GetProperty(DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
218 Dali::Property::Map* attributeMap = property.GetMap();
219 std::size_t attributeCount = attributeMap ? attributeMap->Count() : 0U;
221 for(std::size_t i = 0; i < attributeCount; i++)
223 Dali::Property::Key mapKey = attributeMap->GetKeyAt(i);
224 std::string mapValue;
226 if(mapKey.type == Dali::Property::Key::STRING && attributeMap->GetValue(i).Get(mapValue))
228 result.emplace(std::move(mapKey.stringKey), std::move(mapValue));
232 auto automationId = control.GetProperty<std::string>(DevelControl::Property::AUTOMATION_ID);
233 if(!automationId.empty())
235 result.emplace(automationIdKey, std::move(automationId));
238 // Add "class" if not present already
239 if(result.find(classKey) == result.end())
241 Dali::TypeInfo typeInfo;
242 Self().GetTypeInfo(typeInfo);
245 const std::string& typeName = typeInfo.GetName();
247 result.emplace(classKey, typeName);
249 // Save the 'typeName' so we don't have to calculate it again
250 DevelControl::AppendAccessibilityAttribute(control, classKey, typeName);
257 bool ControlAccessible::IsHidden() const
259 auto control = Dali::Toolkit::Control::DownCast(Self());
261 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
262 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
264 return controlImpl.mAccessibilityHidden;
267 bool ControlAccessible::GrabFocus()
269 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
272 void ControlAccessible::ScrollToSelf()
275 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
279 if(parent->IsScrollable())
281 parent->ScrollToChild(child->Self());
284 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
288 void ControlAccessible::RegisterPositionPropertyNotification()
290 auto control = Dali::Toolkit::Control::DownCast(Self());
291 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
292 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
293 controlImpl.RegisterAccessibilityPositionPropertyNotification();
296 void ControlAccessible::UnregisterPositionPropertyNotification()
298 auto control = Dali::Toolkit::Control::DownCast(Self());
299 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
300 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
301 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
304 void ControlAccessible::RegisterPropertySetSignal()
306 auto control = Dali::Toolkit::Control::DownCast(Self());
307 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
308 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
309 controlImpl.RegisterAccessibilityPropertySetSignal();
312 void ControlAccessible::UnregisterPropertySetSignal()
314 auto control = Dali::Toolkit::Control::DownCast(Self());
315 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
316 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
317 controlImpl.UnregisterAccessibilityPropertySetSignal();
320 bool ControlAccessible::GrabHighlight()
322 Dali::Actor self = Self();
323 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
325 if(!Dali::Accessibility::IsUp())
330 if(self == oldHighlightedActor)
335 // Clear the old highlight.
336 if(oldHighlightedActor)
338 auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
339 if(oldHighlightedObject)
341 oldHighlightedObject->ClearHighlight();
345 auto highlight = GetHighlightActor();
348 highlight = CreateHighlightIndicatorActor();
349 SetHighlightActor(highlight);
352 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
353 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
354 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
356 // Need to set resize policy again, to update SIZE property which is set by
357 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
358 // ControlAccessible. In this case, highlight has incorrect size.
359 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
361 // Remember the highlight actor, so that when the default is changed with
362 // SetHighlightActor(), the currently displayed highlight can still be cleared.
363 mCurrentHighlightActor = highlight;
366 SetCurrentlyHighlightedActor(self);
367 EmitHighlighted(true);
368 RegisterPositionPropertyNotification();
369 RegisterPropertySetSignal();
374 bool ControlAccessible::ClearHighlight()
376 Dali::Actor self = Self();
378 if(!Dali::Accessibility::IsUp())
383 if(GetCurrentlyHighlightedActor() == self)
385 UnregisterPropertySetSignal();
386 UnregisterPositionPropertyNotification();
387 self.Remove(mCurrentHighlightActor.GetHandle());
388 mCurrentHighlightActor = {};
389 SetCurrentlyHighlightedActor({});
390 EmitHighlighted(false);
396 std::string ControlAccessible::GetActionName(size_t index) const
398 if(index >= GetActionCount())
404 Self().GetTypeInfo(type);
405 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
406 return type.GetActionName(index);
409 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
411 return GetLocaleText(GetActionName(index));
414 std::string ControlAccessible::GetActionDescription(size_t index) const
419 size_t ControlAccessible::GetActionCount() const
422 Self().GetTypeInfo(type);
423 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
424 return type.GetActionCount();
427 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
432 bool ControlAccessible::DoAction(size_t index)
434 std::string actionName = GetActionName(index);
435 return Self().DoAction(actionName, {});
438 bool ControlAccessible::DoAction(const std::string& name)
440 return Self().DoAction(name, {});
443 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
445 auto control = Dali::Toolkit::Control::DownCast(Self());
447 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
448 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
450 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
452 auto ret = std::make_pair(gestureInfo, false);
453 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
460 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
462 auto control = Dali::Toolkit::Control::DownCast(Self());
464 return DevelControl::GetAccessibilityRelations(control);
467 bool ControlAccessible::ScrollToChild(Actor child)
472 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
474 return Actor::Property::NAME;
477 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
479 return Dali::Property::INVALID_INDEX;
482 void ControlAccessible::SetLastPosition(Vector2 position)
484 mLastPosition = position;
487 Vector2 ControlAccessible::GetLastPosition() const
489 return mLastPosition;
492 } // namespace Dali::Toolkit::DevelControl