Merge "If it is a key event that occurred in another window, it skipped" into devel...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / controls / control-accessible.cpp
1 /*
2  * Copyright (c) 2023 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)
70 : ActorAccessible(self)
71 {
72 }
73
74 std::string ControlAccessible::GetName() const
75 {
76   auto control = Dali::Toolkit::Control::DownCast(Self());
77
78   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
79   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
80   std::string              name;
81
82   if(!controlImpl.mAccessibilityGetNameSignal.Empty())
83   {
84     controlImpl.mAccessibilityGetNameSignal.Emit(name);
85   }
86   else if(!controlImpl.mAccessibilityName.empty())
87   {
88     name = controlImpl.mAccessibilityName;
89   }
90   else if(auto raw = GetNameRaw(); !raw.empty())
91   {
92     name = raw;
93   }
94   else
95   {
96     name = Self().GetProperty<std::string>(Actor::Property::NAME);
97   }
98
99   if(!controlImpl.mAccessibilityTranslationDomain.empty())
100   {
101     return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
102   }
103
104   return GetLocaleText(name);
105 }
106
107 std::string ControlAccessible::GetNameRaw() const
108 {
109   return {};
110 }
111
112 std::string ControlAccessible::GetDescription() const
113 {
114   auto control = Dali::Toolkit::Control::DownCast(Self());
115
116   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
117   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
118   std::string              description;
119
120   if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
121   {
122     controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
123   }
124   else if(!controlImpl.mAccessibilityDescription.empty())
125   {
126     description = controlImpl.mAccessibilityDescription;
127   }
128   else
129   {
130     description = GetDescriptionRaw();
131   }
132
133   if(!controlImpl.mAccessibilityTranslationDomain.empty())
134   {
135     return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
136   }
137
138   return GetLocaleText(description);
139 }
140
141 std::string ControlAccessible::GetDescriptionRaw() const
142 {
143   return {};
144 }
145
146 Dali::Accessibility::Role ControlAccessible::GetRole() const
147 {
148   return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
149 }
150
151 std::string ControlAccessible::GetLocalizedRoleName() const
152 {
153   return GetLocaleText(GetRoleName());
154 }
155
156 bool ControlAccessible::IsShowing()
157 {
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))
160   {
161     return false;
162   }
163
164   auto* child  = this;
165   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
166   if(!parent)
167   {
168     return true;
169   }
170
171   while(parent)
172   {
173     auto control = Dali::Toolkit::Control::DownCast(parent->Self());
174     if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
175     {
176       return false;
177     }
178     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
179   }
180
181   return true;
182 }
183
184 Dali::Accessibility::States ControlAccessible::CalculateStates()
185 {
186   using Dali::Accessibility::State;
187
188   Dali::Actor                 self = Self();
189   Dali::Accessibility::States states;
190
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>();
200
201   return states;
202 }
203
204 Dali::Accessibility::States ControlAccessible::GetStates()
205 {
206   return CalculateStates();
207 }
208
209 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
210 {
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();
215
216   if(map)
217   {
218     auto mapSize = map->Count();
219
220     for(unsigned int i = 0; i < mapSize; i++)
221     {
222       auto mapKey = map->GetKeyAt(i);
223       if(mapKey.type == Dali::Property::Key::STRING)
224       {
225         std::string mapValue;
226         if(map->GetValue(i).Get(mapValue))
227         {
228           attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
229         }
230       }
231     }
232   }
233
234   auto automationId = control.GetProperty<std::string>(Dali::Toolkit::DevelControl::Property::AUTOMATION_ID);
235   if(!automationId.empty())
236   {
237     attributeMap.emplace("automationId", std::move(automationId));
238   }
239
240   return attributeMap;
241 }
242
243 bool ControlAccessible::IsHidden() const
244 {
245   auto control = Dali::Toolkit::Control::DownCast(Self());
246
247   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
248   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
249
250   return controlImpl.mAccessibilityHidden;
251 }
252
253 bool ControlAccessible::GrabFocus()
254 {
255   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
256 }
257
258 void ControlAccessible::ScrollToSelf()
259 {
260   auto* child  = this;
261   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
262
263   while(parent)
264   {
265     if(parent->IsScrollable())
266     {
267       parent->ScrollToChild(child->Self());
268     }
269
270     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
271   }
272 }
273
274 void ControlAccessible::RegisterPositionPropertyNotification()
275 {
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();
280 }
281
282 void ControlAccessible::UnregisterPositionPropertyNotification()
283 {
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();
288 }
289
290 void ControlAccessible::RegisterPropertySetSignal()
291 {
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();
296 }
297
298 void ControlAccessible::UnregisterPropertySetSignal()
299 {
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();
304 }
305
306 bool ControlAccessible::GrabHighlight()
307 {
308   Dali::Actor self                = Self();
309   auto        oldHighlightedActor = GetCurrentlyHighlightedActor();
310
311   if(!Dali::Accessibility::IsUp())
312   {
313     return false;
314   }
315
316   if(self == oldHighlightedActor)
317   {
318     return true;
319   }
320
321   // Clear the old highlight.
322   if(oldHighlightedActor)
323   {
324     auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
325     if(oldHighlightedObject)
326     {
327       oldHighlightedObject->ClearHighlight();
328     }
329   }
330
331   auto highlight = GetHighlightActor();
332   if(!highlight)
333   {
334     highlight = CreateHighlightIndicatorActor();
335     SetHighlightActor(highlight);
336   }
337
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));
341
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);
346
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;
350   ScrollToSelf();
351   self.Add(highlight);
352   SetCurrentlyHighlightedActor(self);
353   EmitHighlighted(true);
354   RegisterPositionPropertyNotification();
355   RegisterPropertySetSignal();
356
357   return true;
358 }
359
360 bool ControlAccessible::ClearHighlight()
361 {
362   Dali::Actor self = Self();
363
364   if(!Dali::Accessibility::IsUp())
365   {
366     return false;
367   }
368
369   if(GetCurrentlyHighlightedActor() == self)
370   {
371     UnregisterPropertySetSignal();
372     UnregisterPositionPropertyNotification();
373     self.Remove(mCurrentHighlightActor.GetHandle());
374     mCurrentHighlightActor = {};
375     SetCurrentlyHighlightedActor({});
376     EmitHighlighted(false);
377     return true;
378   }
379   return false;
380 }
381
382 std::string ControlAccessible::GetActionName(size_t index) const
383 {
384   if(index >= GetActionCount())
385   {
386     return {};
387   }
388
389   Dali::TypeInfo type;
390   Self().GetTypeInfo(type);
391   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
392   return type.GetActionName(index);
393 }
394
395 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
396 {
397   return GetLocaleText(GetActionName(index));
398 }
399
400 std::string ControlAccessible::GetActionDescription(size_t index) const
401 {
402   return {};
403 }
404
405 size_t ControlAccessible::GetActionCount() const
406 {
407   Dali::TypeInfo type;
408   Self().GetTypeInfo(type);
409   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
410   return type.GetActionCount();
411 }
412
413 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
414 {
415   return {};
416 }
417
418 bool ControlAccessible::DoAction(size_t index)
419 {
420   std::string actionName = GetActionName(index);
421   return Self().DoAction(actionName, {});
422 }
423
424 bool ControlAccessible::DoAction(const std::string& name)
425 {
426   return Self().DoAction(name, {});
427 }
428
429 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
430 {
431   auto control = Dali::Toolkit::Control::DownCast(Self());
432
433   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
434   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
435
436   if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
437   {
438     auto ret = std::make_pair(gestureInfo, false);
439     controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
440     return ret.second;
441   }
442
443   return false;
444 }
445
446 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
447 {
448   auto control = Dali::Toolkit::Control::DownCast(Self());
449
450   return DevelControl::GetAccessibilityRelations(control);
451 }
452
453 bool ControlAccessible::ScrollToChild(Actor child)
454 {
455   return false;
456 }
457
458 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
459 {
460   return Actor::Property::NAME;
461 }
462
463 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
464 {
465   return Dali::Property::INVALID_INDEX;
466 }
467
468 void ControlAccessible::SetLastPosition(Vector2 position)
469 {
470   mLastPosition = position;
471 }
472
473 Vector2 ControlAccessible::GetLastPosition() const
474 {
475   return mLastPosition;
476 }
477
478 } // namespace Dali::Toolkit::DevelControl