[AT-SPI] Derive ControlAccessible from ActorAccessible
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / control-accessible.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 "control-accessible.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 namespace
40 {
41 static std::string GetLocaleText(std::string string, const char *domain = "dali-toolkit")
42 {
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());
47 #else
48     return string;
49 #endif
50 }
51
52 static Dali::Actor CreateHighlightIndicatorActor()
53 {
54   std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
55   focusBorderImagePath += "/keyboard_focus.9.png";
56
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);
61
62   DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
63   actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
64
65   return actor;
66 }
67 } // unnamed namespace
68
69 ControlAccessible::ControlAccessible(Dali::Actor self, Dali::Accessibility::Role role, bool modal)
70 : ActorAccessible(self),
71   mIsModal(modal)
72 {
73   auto control = Dali::Toolkit::Control::DownCast(Self());
74
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)
78   {
79     controlImpl.mAccessibilityRole = role;
80   }
81
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())
84     {
85       return;
86     }
87
88     if(index == DevelControl::Property::ACCESSIBILITY_NAME || (index == GetNamePropertyIndex() && !controlImpl.mAccessibilityNameSet))
89     {
90       if(controlImpl.mAccessibilityGetNameSignal.Empty())
91       {
92         Emit(Dali::Accessibility::ObjectPropertyChangeEvent::NAME);
93       }
94     }
95
96     if(index == DevelControl::Property::ACCESSIBILITY_DESCRIPTION || (index == GetDescriptionPropertyIndex() && !controlImpl.mAccessibilityDescriptionSet))
97     {
98       if(controlImpl.mAccessibilityGetDescriptionSignal.Empty())
99       {
100         Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
101       }
102     }
103   });
104 }
105
106 std::string ControlAccessible::GetName() const
107 {
108   auto control = Dali::Toolkit::Control::DownCast(Self());
109
110   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
111   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
112   std::string name;
113
114   if(!controlImpl.mAccessibilityGetNameSignal.Empty())
115   {
116     controlImpl.mAccessibilityGetNameSignal.Emit(name);
117   }
118   else if(controlImpl.mAccessibilityNameSet)
119   {
120     name = controlImpl.mAccessibilityName;
121   }
122   else if(auto raw = GetNameRaw(); !raw.empty())
123   {
124     name = raw;
125   }
126   else
127   {
128     name = Self().GetProperty<std::string>(Actor::Property::NAME);
129   }
130
131   if(controlImpl.mAccessibilityTranslationDomainSet)
132   {
133     return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
134   }
135
136   return GetLocaleText(name);
137 }
138
139 std::string ControlAccessible::GetNameRaw() const
140 {
141   return {};
142 }
143
144 std::string ControlAccessible::GetDescription() const
145 {
146   auto control = Dali::Toolkit::Control::DownCast(Self());
147
148   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
149   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
150   std::string description;
151
152   if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
153   {
154     controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
155   }
156   else if(controlImpl.mAccessibilityDescriptionSet)
157   {
158     description = controlImpl.mAccessibilityDescription;
159   }
160   else
161   {
162     description = GetDescriptionRaw();
163   }
164   if(controlImpl.mAccessibilityTranslationDomainSet)
165   {
166     return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
167   }
168
169   return GetLocaleText(description);
170 }
171
172 std::string ControlAccessible::GetDescriptionRaw() const
173 {
174   return {};
175 }
176
177 Dali::Accessibility::Role ControlAccessible::GetRole() const
178 {
179   return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
180 }
181
182 std::string ControlAccessible::GetLocalizedRoleName() const
183 {
184   return GetLocaleText(GetRoleName());
185 }
186
187 bool ControlAccessible::IsShowing()
188 {
189   Dali::Actor self = Self();
190   if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a == 0 || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
191   {
192     return false;
193   }
194
195   auto* child  = this;
196   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
197   if(!parent)
198   {
199     return true;
200   }
201
202   auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
203   while(parent)
204   {
205     auto control      = Dali::Toolkit::Control::DownCast(parent->Self());
206     if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
207     {
208       return false;
209     }
210     auto clipMode     = control.GetProperty(Actor::Property::CLIPPING_MODE).Get<bool>();
211     auto parentExtent = parent->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
212     if ((clipMode != ClippingMode::DISABLED) && !parentExtent.Intersects(childExtent))
213     {
214       return false;
215     }
216     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
217   }
218
219   return true;
220 }
221
222 Dali::Accessibility::States ControlAccessible::CalculateStates()
223 {
224   Dali::Actor self = Self();
225   Dali::Accessibility::States state;
226   state[Dali::Accessibility::State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
227   state[Dali::Accessibility::State::FOCUSED]   = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
228
229   if(self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).GetType() == Dali::Property::NONE)
230   {
231     state[Dali::Accessibility::State::HIGHLIGHTABLE] = false;
232   }
233   else
234   {
235     state[Dali::Accessibility::State::HIGHLIGHTABLE] = self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).Get<bool>();
236   }
237
238   state[Dali::Accessibility::State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
239   state[Dali::Accessibility::State::ENABLED]     = true;
240   state[Dali::Accessibility::State::SENSITIVE]   = true;
241   state[Dali::Accessibility::State::VISIBLE]     = self.GetProperty<bool>(Actor::Property::VISIBLE);
242
243   if(mIsModal)
244   {
245     state[Dali::Accessibility::State::MODAL] = true;
246   }
247   state[Dali::Accessibility::State::SHOWING] = IsShowing();
248   state[Dali::Accessibility::State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
249   return state;
250 }
251
252 Dali::Accessibility::States ControlAccessible::GetStates()
253 {
254   return CalculateStates();
255 }
256
257 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
258 {
259   std::unordered_map<std::string, std::string> attributeMap;
260   auto control = Dali::Toolkit::Control::DownCast(Self());
261   auto attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
262   auto map = attribute.GetMap();
263
264   if(map)
265   {
266     auto mapSize = map->Count();
267
268     for(unsigned int i = 0; i < mapSize; i++)
269     {
270       auto mapKey = map->GetKeyAt(i);
271       if(mapKey.type == Dali::Property::Key::STRING)
272       {
273         std::string mapValue;
274         if(map->GetValue(i).Get(mapValue))
275         {
276           attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
277         }
278       }
279     }
280   }
281
282   return attributeMap;
283 }
284
285 bool ControlAccessible::GrabFocus()
286 {
287   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
288 }
289
290 void ControlAccessible::ScrollToSelf()
291 {
292   auto* child = this;
293   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
294
295   while (parent)
296   {
297     if (parent->IsScrollable())
298     {
299       parent->ScrollToChild(child->Self());
300     }
301
302     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
303   }
304 }
305
306 void ControlAccessible::RegisterPositionPropertyNotification()
307 {
308   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
309   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
310   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
311   controlImpl.RegisterAccessibilityPositionPropertyNotification();
312 }
313
314 void ControlAccessible::UnregisterPositionPropertyNotification()
315 {
316   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
317   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
318   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
319   controlImpl.UnregisterAccessibilityPositionPropertyNotification();
320 }
321
322 bool ControlAccessible::GrabHighlight()
323 {
324   Dali::Actor self = Self();
325   auto oldHighlightedActor = GetCurrentlyHighlightedActor();
326
327   if(!Dali::Accessibility::IsUp())
328   {
329     return false;
330   }
331
332   if(self == oldHighlightedActor)
333   {
334     return true;
335   }
336
337   // Clear the old highlight.
338   if(oldHighlightedActor)
339   {
340     auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
341     if(oldHighlightObject)
342     {
343       oldHighlightObject->ClearHighlight();
344     }
345   }
346
347   auto highlight = GetHighlightActor();
348   if(!highlight)
349   {
350     highlight = CreateHighlightIndicatorActor();
351     SetHighlightActor(highlight);
352   }
353
354   highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
355   highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
356   highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
357
358   // Need to set resize policy again, to update SIZE property which is set by
359   // NUIViewAccessible. The highlight could move from NUIViewAccessible to
360   // ControlAccessible. In this case, highlight has incorrect size.
361   highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
362
363   // Remember the highlight actor, so that when the default is changed with
364   // SetHighlightActor(), the currently displayed highlight can still be cleared.
365   mCurrentHighlightActor = highlight;
366   ScrollToSelf();
367   self.Add(highlight);
368   SetCurrentlyHighlightedActor(self);
369   EmitHighlighted(true);
370   RegisterPositionPropertyNotification();
371
372   return true;
373 }
374
375 bool ControlAccessible::ClearHighlight()
376 {
377   Dali::Actor self = Self();
378
379   if(!Dali::Accessibility::IsUp())
380   {
381     return false;
382   }
383
384   if(GetCurrentlyHighlightedActor() == self)
385   {
386     UnregisterPositionPropertyNotification();
387     self.Remove(mCurrentHighlightActor.GetHandle());
388     mCurrentHighlightActor = {};
389     SetCurrentlyHighlightedActor({});
390     EmitHighlighted(false);
391     return true;
392   }
393   return false;
394 }
395
396 std::string ControlAccessible::GetActionName(size_t index) const
397 {
398   if(index >= GetActionCount())
399   {
400     return {};
401   }
402
403   Dali::TypeInfo type;
404   Self().GetTypeInfo(type);
405   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
406   return type.GetActionName(index);
407 }
408
409 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
410 {
411   return GetLocaleText(GetActionName(index));
412 }
413
414 std::string ControlAccessible::GetActionDescription(size_t index) const
415 {
416   return {};
417 }
418
419 size_t ControlAccessible::GetActionCount() const
420 {
421   Dali::TypeInfo type;
422   Self().GetTypeInfo(type);
423   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
424   return type.GetActionCount();
425 }
426
427 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
428 {
429   return {};
430 }
431
432 bool ControlAccessible::DoAction(size_t index)
433 {
434   std::string actionName = GetActionName(index);
435   return Self().DoAction(actionName, {});
436 }
437
438 bool ControlAccessible::DoAction(const std::string& name)
439 {
440   return Self().DoAction(name, {});
441 }
442
443 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
444 {
445   auto control = Dali::Toolkit::Control::DownCast(Self());
446
447   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
448   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
449
450   if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
451   {
452     auto ret = std::make_pair(gestureInfo, false);
453     controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
454     return ret.second;
455   }
456
457   return false;
458 }
459
460 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
461 {
462   auto control = Dali::Toolkit::Control::DownCast(Self());
463
464   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
465   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
466
467   std::vector<Dali::Accessibility::Relation> ret;
468
469   for(auto& relation : controlImpl.mAccessibilityRelations)
470   {
471     auto& targets = relation.second;
472
473     ret.emplace_back(Accessibility::Relation{relation.first, {}});
474
475     // Map every Accessible* to its Address
476     std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
477       return x->GetAddress();
478     });
479   }
480
481   return ret;
482 }
483
484 bool ControlAccessible::ScrollToChild(Actor child)
485 {
486   return false;
487 }
488
489 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
490 {
491   return Actor::Property::NAME;
492 }
493
494 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
495 {
496   return Dali::Property::INVALID_INDEX;
497 }
498
499 void ControlAccessible::SetLastPosition(Vector2 position)
500 {
501   mLastPosition = position;
502 }
503
504 Vector2 ControlAccessible::GetLastPosition() const
505 {
506   return mLastPosition;
507 }
508
509 } // namespace Dali::Toolkit::DevelControl