Merge "[AT-SPI] Lazily calculate the "class" attribute" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / control-accessible.cpp
1 /*
2  * Copyright (c) 2024 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 #include <dali/public-api/object/type-info.h>
29
30 // INTERNAL INCLUDES
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>
37
38 namespace Dali::Toolkit::DevelControl
39 {
40 namespace
41 {
42 static std::string GetLocaleText(std::string string, const char* domain = "dali-toolkit")
43 {
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());
48 #else
49   return string;
50 #endif
51 }
52
53 static Dali::Actor CreateHighlightIndicatorActor()
54 {
55   std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
56   focusBorderImagePath += "/keyboard_focus.9.png";
57
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);
62
63   DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
64   actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
65
66   return actor;
67 }
68 } // unnamed namespace
69
70 ControlAccessible::ControlAccessible(Dali::Actor self)
71 : ActorAccessible(self)
72 {
73 }
74
75 std::string ControlAccessible::GetName() const
76 {
77   auto control = Dali::Toolkit::Control::DownCast(Self());
78
79   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
80   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
81   std::string              name;
82
83   if(!controlImpl.mAccessibilityGetNameSignal.Empty())
84   {
85     controlImpl.mAccessibilityGetNameSignal.Emit(name);
86   }
87   else if(!controlImpl.mAccessibilityName.empty())
88   {
89     name = controlImpl.mAccessibilityName;
90   }
91   else if(auto raw = GetNameRaw(); !raw.empty())
92   {
93     name = raw;
94   }
95   else
96   {
97     name = Self().GetProperty<std::string>(Actor::Property::NAME);
98   }
99
100   if(!controlImpl.mAccessibilityTranslationDomain.empty())
101   {
102     return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
103   }
104
105   return GetLocaleText(name);
106 }
107
108 std::string ControlAccessible::GetNameRaw() const
109 {
110   return {};
111 }
112
113 std::string ControlAccessible::GetDescription() const
114 {
115   auto control = Dali::Toolkit::Control::DownCast(Self());
116
117   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
118   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
119   std::string              description;
120
121   if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
122   {
123     controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
124   }
125   else if(!controlImpl.mAccessibilityDescription.empty())
126   {
127     description = controlImpl.mAccessibilityDescription;
128   }
129   else
130   {
131     description = GetDescriptionRaw();
132   }
133
134   if(!controlImpl.mAccessibilityTranslationDomain.empty())
135   {
136     return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
137   }
138
139   return GetLocaleText(description);
140 }
141
142 std::string ControlAccessible::GetDescriptionRaw() const
143 {
144   return {};
145 }
146
147 Dali::Accessibility::Role ControlAccessible::GetRole() const
148 {
149   return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
150 }
151
152 std::string ControlAccessible::GetLocalizedRoleName() const
153 {
154   return GetLocaleText(GetRoleName());
155 }
156
157 bool ControlAccessible::IsShowing()
158 {
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))
161   {
162     return false;
163   }
164
165   auto* child  = this;
166   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
167   if(!parent)
168   {
169     return true;
170   }
171
172   while(parent)
173   {
174     auto control = Dali::Toolkit::Control::DownCast(parent->Self());
175     if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
176     {
177       return false;
178     }
179     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
180   }
181
182   return true;
183 }
184
185 Dali::Accessibility::States ControlAccessible::CalculateStates()
186 {
187   using Dali::Accessibility::State;
188
189   Dali::Actor                 self = Self();
190   Dali::Accessibility::States states;
191
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>();
201
202   return states;
203 }
204
205 Dali::Accessibility::States ControlAccessible::GetStates()
206 {
207   return CalculateStates();
208 }
209
210 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
211 {
212   static const std::string automationIdKey = "automationId";
213   static const std::string classKey        = "class";
214
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;
220
221   for(std::size_t i = 0; i < attributeCount; i++)
222   {
223     Dali::Property::Key mapKey = attributeMap->GetKeyAt(i);
224     std::string         mapValue;
225
226     if(mapKey.type == Dali::Property::Key::STRING && attributeMap->GetValue(i).Get(mapValue))
227     {
228       result.emplace(std::move(mapKey.stringKey), std::move(mapValue));
229     }
230   }
231
232   auto automationId = control.GetProperty<std::string>(DevelControl::Property::AUTOMATION_ID);
233   if(!automationId.empty())
234   {
235     result.emplace(automationIdKey, std::move(automationId));
236   }
237
238   // Add "class" if not present already
239   if(result.find(classKey) == result.end())
240   {
241     Dali::TypeInfo typeInfo;
242     Self().GetTypeInfo(typeInfo);
243     if(typeInfo)
244     {
245       const std::string& typeName = typeInfo.GetName();
246
247       result.emplace(classKey, typeName);
248
249       // Save the 'typeName' so we don't have to calculate it again
250       DevelControl::AppendAccessibilityAttribute(control, classKey, typeName);
251     }
252   }
253
254   return result;
255 }
256
257 bool ControlAccessible::IsHidden() const
258 {
259   auto control = Dali::Toolkit::Control::DownCast(Self());
260
261   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
262   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
263
264   return controlImpl.mAccessibilityHidden;
265 }
266
267 bool ControlAccessible::GrabFocus()
268 {
269   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
270 }
271
272 void ControlAccessible::ScrollToSelf()
273 {
274   auto* child  = this;
275   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
276
277   while(parent)
278   {
279     if(parent->IsScrollable())
280     {
281       parent->ScrollToChild(child->Self());
282     }
283
284     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
285   }
286 }
287
288 void ControlAccessible::RegisterPositionPropertyNotification()
289 {
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();
294 }
295
296 void ControlAccessible::UnregisterPositionPropertyNotification()
297 {
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();
302 }
303
304 void ControlAccessible::RegisterPropertySetSignal()
305 {
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();
310 }
311
312 void ControlAccessible::UnregisterPropertySetSignal()
313 {
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();
318 }
319
320 bool ControlAccessible::GrabHighlight()
321 {
322   Dali::Actor self                = Self();
323   auto        oldHighlightedActor = GetCurrentlyHighlightedActor();
324
325   if(!Dali::Accessibility::IsUp())
326   {
327     return false;
328   }
329
330   if(self == oldHighlightedActor)
331   {
332     return true;
333   }
334
335   // Clear the old highlight.
336   if(oldHighlightedActor)
337   {
338     auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
339     if(oldHighlightedObject)
340     {
341       oldHighlightedObject->ClearHighlight();
342     }
343   }
344
345   auto highlight = GetHighlightActor();
346   if(!highlight)
347   {
348     highlight = CreateHighlightIndicatorActor();
349     SetHighlightActor(highlight);
350   }
351
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));
355
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);
360
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;
364   ScrollToSelf();
365   self.Add(highlight);
366   SetCurrentlyHighlightedActor(self);
367   EmitHighlighted(true);
368   RegisterPositionPropertyNotification();
369   RegisterPropertySetSignal();
370
371   return true;
372 }
373
374 bool ControlAccessible::ClearHighlight()
375 {
376   Dali::Actor self = Self();
377
378   if(!Dali::Accessibility::IsUp())
379   {
380     return false;
381   }
382
383   if(GetCurrentlyHighlightedActor() == self)
384   {
385     UnregisterPropertySetSignal();
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   return DevelControl::GetAccessibilityRelations(control);
465 }
466
467 bool ControlAccessible::ScrollToChild(Actor child)
468 {
469   return false;
470 }
471
472 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
473 {
474   return Actor::Property::NAME;
475 }
476
477 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
478 {
479   return Dali::Property::INVALID_INDEX;
480 }
481
482 void ControlAccessible::SetLastPosition(Vector2 position)
483 {
484   mLastPosition = position;
485 }
486
487 Vector2 ControlAccessible::GetLastPosition() const
488 {
489   return mLastPosition;
490 }
491
492 } // namespace Dali::Toolkit::DevelControl