2 * Copyright (c) 2023 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)
70 : ActorAccessible(self)
74 std::string ControlAccessible::GetName() const
76 auto control = Dali::Toolkit::Control::DownCast(Self());
78 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
79 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
82 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
84 controlImpl.mAccessibilityGetNameSignal.Emit(name);
86 else if(!controlImpl.mAccessibilityName.empty())
88 name = controlImpl.mAccessibilityName;
90 else if(auto raw = GetNameRaw(); !raw.empty())
96 name = Self().GetProperty<std::string>(Actor::Property::NAME);
99 if(!controlImpl.mAccessibilityTranslationDomain.empty())
101 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
104 return GetLocaleText(name);
107 std::string ControlAccessible::GetNameRaw() const
112 std::string ControlAccessible::GetDescription() const
114 auto control = Dali::Toolkit::Control::DownCast(Self());
116 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
117 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
118 std::string description;
120 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
122 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
124 else if(!controlImpl.mAccessibilityDescription.empty())
126 description = controlImpl.mAccessibilityDescription;
130 description = GetDescriptionRaw();
133 if(!controlImpl.mAccessibilityTranslationDomain.empty())
135 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
138 return GetLocaleText(description);
141 std::string ControlAccessible::GetDescriptionRaw() const
146 Dali::Accessibility::Role ControlAccessible::GetRole() const
148 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
151 std::string ControlAccessible::GetLocalizedRoleName() const
153 return GetLocaleText(GetRoleName());
156 bool ControlAccessible::IsShowing()
158 Dali::Actor self = Self();
159 if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || Dali::EqualsZero(self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a) || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
165 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
173 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
174 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
178 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
184 Dali::Accessibility::States ControlAccessible::CalculateStates()
186 using Dali::Accessibility::State;
188 Dali::Actor self = Self();
189 Dali::Accessibility::States states;
191 states[State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
192 states[State::FOCUSED] = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
193 states[State::HIGHLIGHTABLE] = self.GetProperty<bool>(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE);
194 states[State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
195 states[State::ENABLED] = true;
196 states[State::SENSITIVE] = (Dali::DevelActor::IsHittable(self) && Dali::DevelActor::GetTouchRequired(self));
197 states[State::VISIBLE] = self.GetProperty<bool>(Actor::Property::VISIBLE);
198 states[State::SHOWING] = IsShowing();
199 states[State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
204 Dali::Accessibility::States ControlAccessible::GetStates()
206 return CalculateStates();
209 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
211 std::unordered_map<std::string, std::string> attributeMap;
212 auto control = Dali::Toolkit::Control::DownCast(Self());
213 auto attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
214 auto map = attribute.GetMap();
218 auto mapSize = map->Count();
220 for(unsigned int i = 0; i < mapSize; i++)
222 auto mapKey = map->GetKeyAt(i);
223 if(mapKey.type == Dali::Property::Key::STRING)
225 std::string mapValue;
226 if(map->GetValue(i).Get(mapValue))
228 attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
234 auto automationId = control.GetProperty<std::string>(Dali::Toolkit::DevelControl::Property::AUTOMATION_ID);
235 if(!automationId.empty())
237 attributeMap.emplace("automationId", std::move(automationId));
243 bool ControlAccessible::IsHidden() const
245 auto control = Dali::Toolkit::Control::DownCast(Self());
247 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
248 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
250 return controlImpl.mAccessibilityHidden;
253 bool ControlAccessible::GrabFocus()
255 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
258 void ControlAccessible::ScrollToSelf()
261 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
265 if(parent->IsScrollable())
267 parent->ScrollToChild(child->Self());
270 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
274 void ControlAccessible::RegisterPositionPropertyNotification()
276 auto control = Dali::Toolkit::Control::DownCast(Self());
277 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
278 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
279 controlImpl.RegisterAccessibilityPositionPropertyNotification();
282 void ControlAccessible::UnregisterPositionPropertyNotification()
284 auto control = Dali::Toolkit::Control::DownCast(Self());
285 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
286 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
287 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
290 void ControlAccessible::RegisterPropertySetSignal()
292 auto control = Dali::Toolkit::Control::DownCast(Self());
293 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
294 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
295 controlImpl.RegisterAccessibilityPropertySetSignal();
298 void ControlAccessible::UnregisterPropertySetSignal()
300 auto control = Dali::Toolkit::Control::DownCast(Self());
301 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
302 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
303 controlImpl.UnregisterAccessibilityPropertySetSignal();
306 bool ControlAccessible::GrabHighlight()
308 Dali::Actor self = Self();
309 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
311 if(!Dali::Accessibility::IsUp())
316 if(self == oldHighlightedActor)
321 // Clear the old highlight.
322 if(oldHighlightedActor)
324 auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
325 if(oldHighlightedObject)
327 oldHighlightedObject->ClearHighlight();
331 auto highlight = GetHighlightActor();
334 highlight = CreateHighlightIndicatorActor();
335 SetHighlightActor(highlight);
338 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
339 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
340 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
342 // Need to set resize policy again, to update SIZE property which is set by
343 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
344 // ControlAccessible. In this case, highlight has incorrect size.
345 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
347 // Remember the highlight actor, so that when the default is changed with
348 // SetHighlightActor(), the currently displayed highlight can still be cleared.
349 mCurrentHighlightActor = highlight;
352 SetCurrentlyHighlightedActor(self);
353 EmitHighlighted(true);
354 RegisterPositionPropertyNotification();
355 RegisterPropertySetSignal();
360 bool ControlAccessible::ClearHighlight()
362 Dali::Actor self = Self();
364 if(!Dali::Accessibility::IsUp())
369 if(GetCurrentlyHighlightedActor() == self)
371 UnregisterPropertySetSignal();
372 UnregisterPositionPropertyNotification();
373 self.Remove(mCurrentHighlightActor.GetHandle());
374 mCurrentHighlightActor = {};
375 SetCurrentlyHighlightedActor({});
376 EmitHighlighted(false);
382 std::string ControlAccessible::GetActionName(size_t index) const
384 if(index >= GetActionCount())
390 Self().GetTypeInfo(type);
391 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
392 return type.GetActionName(index);
395 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
397 return GetLocaleText(GetActionName(index));
400 std::string ControlAccessible::GetActionDescription(size_t index) const
405 size_t ControlAccessible::GetActionCount() const
408 Self().GetTypeInfo(type);
409 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
410 return type.GetActionCount();
413 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
418 bool ControlAccessible::DoAction(size_t index)
420 std::string actionName = GetActionName(index);
421 return Self().DoAction(actionName, {});
424 bool ControlAccessible::DoAction(const std::string& name)
426 return Self().DoAction(name, {});
429 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
431 auto control = Dali::Toolkit::Control::DownCast(Self());
433 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
434 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
436 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
438 auto ret = std::make_pair(gestureInfo, false);
439 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
446 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
448 auto control = Dali::Toolkit::Control::DownCast(Self());
450 return DevelControl::GetAccessibilityRelations(control);
453 bool ControlAccessible::ScrollToChild(Actor child)
458 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
460 return Actor::Property::NAME;
463 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
465 return Dali::Property::INVALID_INDEX;
468 void ControlAccessible::SetLastPosition(Vector2 position)
470 mLastPosition = position;
473 Vector2 ControlAccessible::GetLastPosition() const
475 return mLastPosition;
478 } // namespace Dali::Toolkit::DevelControl