Merge "[ATSPI] Update accessibility-test-utils files" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / accessible-impl.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "accessible-impl.h"
20
21 // EXTERNAL INCLUDES
22 #ifdef DGETTEXT_ENABLED
23 #include <libintl.h>
24 #endif
25
26 #include <dali/devel-api/actors/actor-devel.h>
27 #include <dali/devel-api/adaptor-framework/window-devel.h>
28
29 // INTERNAL INCLUDES
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>
36
37 namespace Dali::Toolkit::DevelControl
38 {
39
40 static std::string GetLocaleText(std::string string, const char *domain = "dali-toolkit")
41 {
42 #ifdef DGETTEXT_ENABLED
43     /*TODO: currently non-localized string is used as a key for translation lookup. In case the lookup key formatting is forced
44           consider calling utility function for converting non-localized string into well-formatted key before lookup. */
45     return dgettext(domain, string.c_str());
46 #else
47     return string;
48 #endif
49 }
50
51 AccessibleImpl::AccessibleImpl(Dali::Actor self, Dali::Accessibility::Role role, bool modal)
52 : mSelf(self),
53   mIsModal(modal)
54 {
55   auto control = Dali::Toolkit::Control::DownCast(Self());
56
57   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
58   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
59   if(controlImpl.mAccessibilityRole == Dali::Accessibility::Role::UNKNOWN)
60   {
61     controlImpl.mAccessibilityRole = role;
62   }
63
64   Self().PropertySetSignal().Connect(&controlImpl, [this, &controlImpl](Dali::Handle& handle, Dali::Property::Index index, Dali::Property::Value value) {
65     if(this->Self() != Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor())
66     {
67       return;
68     }
69
70     if(index == DevelControl::Property::ACCESSIBILITY_NAME || (index == GetNamePropertyIndex() && !controlImpl.mAccessibilityNameSet))
71     {
72       if(controlImpl.mAccessibilityGetNameSignal.Empty())
73       {
74         Emit(Dali::Accessibility::ObjectPropertyChangeEvent::NAME);
75       }
76     }
77
78     if(index == DevelControl::Property::ACCESSIBILITY_DESCRIPTION || (index == GetDescriptionPropertyIndex() && !controlImpl.mAccessibilityDescriptionSet))
79     {
80       if(controlImpl.mAccessibilityGetDescriptionSignal.Empty())
81       {
82         Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
83       }
84     }
85   });
86 }
87
88 std::string AccessibleImpl::GetName()
89 {
90   auto control = Dali::Toolkit::Control::DownCast(Self());
91
92   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
93   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
94   std::string name;
95
96   if(!controlImpl.mAccessibilityGetNameSignal.Empty())
97   {
98     controlImpl.mAccessibilityGetNameSignal.Emit(name);
99   }
100   else if(controlImpl.mAccessibilityNameSet)
101   {
102     name = controlImpl.mAccessibilityName;
103   }
104   else if(auto raw = GetNameRaw(); !raw.empty())
105   {
106     name = raw;
107   }
108   else
109   {
110     name = Self().GetProperty<std::string>(Actor::Property::NAME);
111   }
112
113   if(controlImpl.mAccessibilityTranslationDomainSet)
114   {
115     return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
116   }
117
118   return GetLocaleText(name);
119 }
120
121 std::string AccessibleImpl::GetNameRaw()
122 {
123   return {};
124 }
125
126 std::string AccessibleImpl::GetDescription()
127 {
128   auto control = Dali::Toolkit::Control::DownCast(Self());
129
130   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
131   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
132   std::string description;
133
134   if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
135   {
136     controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
137   }
138   else if(controlImpl.mAccessibilityDescriptionSet)
139   {
140     description = controlImpl.mAccessibilityDescription;
141   }
142   else
143   {
144     description = GetDescriptionRaw();
145   }
146   if(controlImpl.mAccessibilityTranslationDomainSet)
147   {
148     return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
149   }
150
151   return GetLocaleText(description);
152 }
153
154 std::string AccessibleImpl::GetDescriptionRaw()
155 {
156   return {};
157 }
158
159 Dali::Accessibility::Accessible* AccessibleImpl::GetParent()
160 {
161   return Dali::Accessibility::Accessible::Get(Self().GetParent());
162 }
163
164 size_t AccessibleImpl::GetChildCount()
165 {
166   return Self().GetChildCount();
167 }
168
169 Dali::Accessibility::Accessible* AccessibleImpl::GetChildAtIndex(size_t index)
170 {
171   return Dali::Accessibility::Accessible::Get(Self().GetChildAt(static_cast<unsigned int>(index)));
172 }
173
174 size_t AccessibleImpl::GetIndexInParent()
175 {
176   auto self = Self();
177   auto parent = self.GetParent();
178   DALI_ASSERT_ALWAYS(parent && "can't call GetIndexInParent on object without parent");
179
180   auto count = parent.GetChildCount();
181   for(auto i = 0u; i < count; ++i)
182   {
183     auto child = parent.GetChildAt(i);
184     if(child == self)
185     {
186       return i;
187     }
188   }
189   DALI_ASSERT_ALWAYS(false && "object isn't child of it's parent");
190   return static_cast<size_t>(-1);
191 }
192
193 Dali::Accessibility::Role AccessibleImpl::GetRole()
194 {
195   return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
196 }
197
198 std::string AccessibleImpl::GetLocalizedRoleName()
199 {
200   return GetLocaleText(GetRoleName());
201 }
202
203 bool AccessibleImpl::IsShowing()
204 {
205   Dali::Actor self = Self();
206   if(self.GetProperty(Dali::DevelActor::Property::CULLED).Get<bool>() || !self.GetCurrentProperty<bool>(Actor::Property::VISIBLE))
207   {
208     return false;
209   }
210
211   auto* child  = this;
212   auto* parent = dynamic_cast<Toolkit::DevelControl::AccessibleImpl*>(child->GetParent());
213   if(!parent)
214   {
215     return true;
216   }
217
218   auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
219   while(parent)
220   {
221     auto control      = Dali::Toolkit::Control::DownCast(parent->Self());
222     auto clipMode     = control.GetProperty(Actor::Property::CLIPPING_MODE).Get<bool>();
223     auto parentExtent = parent->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
224     if ((clipMode != ClippingMode::DISABLED) && !parentExtent.Intersects(childExtent))
225     {
226       return false;
227     }
228     parent = dynamic_cast<Toolkit::DevelControl::AccessibleImpl*>(parent->GetParent());
229   }
230
231   return true;
232 }
233
234 Dali::Accessibility::States AccessibleImpl::CalculateStates()
235 {
236   Dali::Actor self = Self();
237   Dali::Accessibility::States state;
238   state[Dali::Accessibility::State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
239   state[Dali::Accessibility::State::FOCUSED]   = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
240
241   if(self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).GetType() == Dali::Property::NONE)
242   {
243     state[Dali::Accessibility::State::HIGHLIGHTABLE] = false;
244   }
245   else
246   {
247     state[Dali::Accessibility::State::HIGHLIGHTABLE] = self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).Get<bool>();
248   }
249
250   state[Dali::Accessibility::State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
251   state[Dali::Accessibility::State::ENABLED]     = true;
252   state[Dali::Accessibility::State::SENSITIVE]   = true;
253   state[Dali::Accessibility::State::VISIBLE]     = true;
254
255   if(mIsModal)
256   {
257     state[Dali::Accessibility::State::MODAL] = true;
258   }
259   state[Dali::Accessibility::State::SHOWING] = IsShowing();
260   state[Dali::Accessibility::State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
261   return state;
262 }
263
264 Dali::Accessibility::States AccessibleImpl::GetStates()
265 {
266   return CalculateStates();
267 }
268
269 Dali::Accessibility::Attributes AccessibleImpl::GetAttributes()
270 {
271   std::unordered_map<std::string, std::string> attributeMap;
272   auto control = Dali::Toolkit::Control::DownCast(Self());
273   auto attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
274   auto map = attribute.GetMap();
275
276   if(map)
277   {
278     auto mapSize = map->Count();
279
280     for(unsigned int i = 0; i < mapSize; i++)
281     {
282       auto mapKey = map->GetKeyAt(i);
283       if(mapKey.type == Dali::Property::Key::STRING)
284       {
285         std::string mapValue;
286         if(map->GetValue(i).Get(mapValue))
287         {
288           attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
289         }
290       }
291     }
292   }
293
294   return attributeMap;
295 }
296
297 Dali::Accessibility::ComponentLayer AccessibleImpl::GetLayer()
298 {
299   return Dali::Accessibility::ComponentLayer::WINDOW;
300 }
301
302 Dali::Rect<> AccessibleImpl::GetExtents(Dali::Accessibility::CoordinateType type)
303 {
304   Dali::Actor self = Self();
305
306   Vector2 screenPosition = self.GetProperty(Dali::DevelActor::Property::SCREEN_POSITION).Get<Vector2>();
307   auto size = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
308   bool positionUsesAnchorPoint = self.GetProperty(Dali::DevelActor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
309   Vector3 anchorPointOffSet = size * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
310   Vector2 position = Vector2((screenPosition.x - anchorPointOffSet.x), (screenPosition.y - anchorPointOffSet.y));
311
312   if(type == Dali::Accessibility::CoordinateType::WINDOW)
313   {
314     return {position.x, position.y, size.x, size.y};
315   }
316   else // Dali::Accessibility::CoordinateType::SCREEN
317   {
318     auto window = Dali::DevelWindow::Get(self);
319     auto windowPosition = window.GetPosition();
320     return {position.x + windowPosition.GetX(), position.y + windowPosition.GetY(), size.x, size.y};
321   }
322 }
323
324 int16_t AccessibleImpl::GetMdiZOrder()
325 {
326   return 0;
327 }
328 double AccessibleImpl::GetAlpha()
329 {
330   return 0;
331 }
332
333 bool AccessibleImpl::GrabFocus()
334 {
335   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
336 }
337
338 static Dali::Actor CreateHighlightIndicatorActor()
339 {
340   std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
341   focusBorderImagePath += "/keyboard_focus.9.png";
342
343   // Create the default if it hasn't been set and one that's shared by all the
344   // keyboard focusable actors
345   auto actor = Toolkit::ImageView::New(focusBorderImagePath);
346   actor.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
347
348   DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
349   actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
350
351   return actor;
352 }
353
354 void AccessibleImpl::ScrollToSelf()
355 {
356   auto* child = this;
357   auto* parent = dynamic_cast<Toolkit::DevelControl::AccessibleImpl*>(child->GetParent());
358
359   while (parent)
360   {
361     if (parent->IsScrollable())
362     {
363       parent->ScrollToChild(child->Self());
364     }
365
366     child = parent;
367     parent = dynamic_cast<Toolkit::DevelControl::AccessibleImpl*>(parent->GetParent());
368   }
369 }
370
371 void AccessibleImpl::RegisterPositionPropertyNotification()
372 {
373   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
374   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
375   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
376   controlImpl.RegisterAccessibilityPositionPropertyNotification();
377 }
378
379 void AccessibleImpl::UnregisterPositionPropertyNotification()
380 {
381   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
382   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
383   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
384   controlImpl.UnregisterAccessibilityPositionPropertyNotification();
385 }
386
387 bool AccessibleImpl::GrabHighlight()
388 {
389   Dali::Actor self = Self();
390   auto oldHighlightedActor = GetCurrentlyHighlightedActor();
391
392   if(!Dali::Accessibility::IsUp())
393   {
394     return false;
395   }
396
397   if(self == oldHighlightedActor)
398   {
399     return true;
400   }
401
402   // Clear the old highlight.
403   if(oldHighlightedActor)
404   {
405     auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
406     if(oldHighlightObject)
407     {
408       oldHighlightObject->ClearHighlight();
409     }
410   }
411
412   auto highlight = GetHighlightActor();
413   if(!highlight)
414   {
415     highlight = CreateHighlightIndicatorActor();
416     SetHighlightActor(highlight);
417   }
418
419   highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
420   highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
421   highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
422
423   // Need to set resize policy again, to update SIZE property which is set by
424   // AccessibleImpl_NUI. The highlight could move from AccessibleImpl_NUI to
425   // AccessibleImpl. In this case, highlight has incorrect size.
426   highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
427
428   // Remember the highlight actor, so that when the default is changed with
429   // SetHighlightActor(), the currently displayed highlight can still be cleared.
430   mCurrentHighlightActor = highlight;
431   ScrollToSelf();
432   self.Add(highlight);
433   SetCurrentlyHighlightedActor(self);
434   EmitHighlighted(true);
435   RegisterPositionPropertyNotification();
436
437   return true;
438 }
439
440 bool AccessibleImpl::ClearHighlight()
441 {
442   Dali::Actor self = Self();
443
444   if(!Dali::Accessibility::IsUp())
445   {
446     return false;
447   }
448
449   if(GetCurrentlyHighlightedActor() == self)
450   {
451     UnregisterPositionPropertyNotification();
452     self.Remove(mCurrentHighlightActor.GetHandle());
453     mCurrentHighlightActor = {};
454     SetCurrentlyHighlightedActor({});
455     EmitHighlighted(false);
456     return true;
457   }
458   return false;
459 }
460
461 std::string AccessibleImpl::GetActionName(size_t index)
462 {
463   if(index >= GetActionCount())
464   {
465     return {};
466   }
467
468   Dali::TypeInfo type;
469   Self().GetTypeInfo(type);
470   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
471   return type.GetActionName(index);
472 }
473
474 std::string AccessibleImpl::GetLocalizedActionName(size_t index)
475 {
476   return GetLocaleText(GetActionName(index));
477 }
478
479 std::string AccessibleImpl::GetActionDescription(size_t index)
480 {
481   return {};
482 }
483
484 size_t AccessibleImpl::GetActionCount()
485 {
486   Dali::TypeInfo type;
487   Self().GetTypeInfo(type);
488   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
489   return type.GetActionCount();
490 }
491
492 std::string AccessibleImpl::GetActionKeyBinding(size_t index)
493 {
494   return {};
495 }
496
497 bool AccessibleImpl::DoAction(size_t index)
498 {
499   std::string actionName = GetActionName(index);
500   return Self().DoAction(actionName, {});
501 }
502
503 bool AccessibleImpl::DoAction(const std::string& name)
504 {
505   return Self().DoAction(name, {});
506 }
507
508 bool AccessibleImpl::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
509 {
510   auto control = Dali::Toolkit::Control::DownCast(Self());
511
512   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
513   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
514
515   if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
516   {
517     auto ret = std::make_pair(gestureInfo, false);
518     controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
519     return ret.second;
520   }
521
522   return false;
523 }
524
525 std::vector<Dali::Accessibility::Relation> AccessibleImpl::GetRelationSet()
526 {
527   auto control = Dali::Toolkit::Control::DownCast(Self());
528
529   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
530   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
531
532   std::vector<Dali::Accessibility::Relation> ret;
533
534   auto& relation = controlImpl.mAccessibilityRelations;
535   for(auto i = 0u; i < relation.size(); ++i)
536   {
537     if(relation[i].empty()) continue;
538
539     ret.emplace_back(Accessibility::Relation{static_cast<Accessibility::RelationType>(i), relation[i]});
540   }
541
542   return ret;
543 }
544
545 Dali::Actor AccessibleImpl::GetInternalActor()
546 {
547   return Dali::Actor{};
548 }
549
550 bool AccessibleImpl::ScrollToChild(Actor child)
551 {
552   return false;
553 }
554
555 Dali::Property::Index AccessibleImpl::GetNamePropertyIndex()
556 {
557   return Actor::Property::NAME;
558 }
559
560 Dali::Property::Index AccessibleImpl::GetDescriptionPropertyIndex()
561 {
562   return Dali::Property::INVALID_INDEX;
563 }
564
565 void AccessibleImpl::SetLastPosition(Vector2 position)
566 {
567   mLastPosition = position;
568 }
569
570 Vector2 AccessibleImpl::GetLastPosition() const
571 {
572   return mLastPosition;
573 }
574
575 } // namespace Dali::Toolkit::DevelControl