Merge "Set focusable to true when touched in default." into devel/master
[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 : mSelf(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::Accessible* ControlAccessible::GetParent()
178 {
179   return Dali::Accessibility::Accessible::Get(Self().GetParent());
180 }
181
182 size_t ControlAccessible::GetChildCount() const
183 {
184   return Self().GetChildCount();
185 }
186
187 Dali::Accessibility::Accessible* ControlAccessible::GetChildAtIndex(size_t index)
188 {
189   return Dali::Accessibility::Accessible::Get(Self().GetChildAt(static_cast<unsigned int>(index)));
190 }
191
192 size_t ControlAccessible::GetIndexInParent()
193 {
194   auto self = Self();
195   auto parent = self.GetParent();
196   DALI_ASSERT_ALWAYS(parent && "can't call GetIndexInParent on object without parent");
197
198   auto count = parent.GetChildCount();
199   for(auto i = 0u; i < count; ++i)
200   {
201     auto child = parent.GetChildAt(i);
202     if(child == self)
203     {
204       return i;
205     }
206   }
207   DALI_ASSERT_ALWAYS(false && "object isn't child of it's parent");
208   return static_cast<size_t>(-1);
209 }
210
211 Dali::Accessibility::Role ControlAccessible::GetRole() const
212 {
213   return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
214 }
215
216 std::string ControlAccessible::GetLocalizedRoleName() const
217 {
218   return GetLocaleText(GetRoleName());
219 }
220
221 bool ControlAccessible::IsShowing()
222 {
223   Dali::Actor self = Self();
224   if(!self.GetProperty<bool>(Actor::Property::VISIBLE) || self.GetProperty<Vector4>(Actor::Property::WORLD_COLOR).a == 0 || self.GetProperty<bool>(Dali::DevelActor::Property::CULLED))
225   {
226     return false;
227   }
228
229   auto* child  = this;
230   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
231   if(!parent)
232   {
233     return true;
234   }
235
236   auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
237   while(parent)
238   {
239     auto control      = Dali::Toolkit::Control::DownCast(parent->Self());
240     if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
241     {
242       return false;
243     }
244     auto clipMode     = control.GetProperty(Actor::Property::CLIPPING_MODE).Get<bool>();
245     auto parentExtent = parent->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
246     if ((clipMode != ClippingMode::DISABLED) && !parentExtent.Intersects(childExtent))
247     {
248       return false;
249     }
250     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
251   }
252
253   return true;
254 }
255
256 Dali::Accessibility::States ControlAccessible::CalculateStates()
257 {
258   Dali::Actor self = Self();
259   Dali::Accessibility::States state;
260   state[Dali::Accessibility::State::FOCUSABLE] = self.GetProperty<bool>(Actor::Property::KEYBOARD_FOCUSABLE);
261   state[Dali::Accessibility::State::FOCUSED]   = Toolkit::KeyboardFocusManager::Get().GetCurrentFocusActor() == self;
262
263   if(self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).GetType() == Dali::Property::NONE)
264   {
265     state[Dali::Accessibility::State::HIGHLIGHTABLE] = false;
266   }
267   else
268   {
269     state[Dali::Accessibility::State::HIGHLIGHTABLE] = self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).Get<bool>();
270   }
271
272   state[Dali::Accessibility::State::HIGHLIGHTED] = GetCurrentlyHighlightedActor() == self;
273   state[Dali::Accessibility::State::ENABLED]     = true;
274   state[Dali::Accessibility::State::SENSITIVE]   = true;
275   state[Dali::Accessibility::State::VISIBLE]     = self.GetProperty<bool>(Actor::Property::VISIBLE);
276
277   if(mIsModal)
278   {
279     state[Dali::Accessibility::State::MODAL] = true;
280   }
281   state[Dali::Accessibility::State::SHOWING] = IsShowing();
282   state[Dali::Accessibility::State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
283   return state;
284 }
285
286 Dali::Accessibility::States ControlAccessible::GetStates()
287 {
288   return CalculateStates();
289 }
290
291 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
292 {
293   std::unordered_map<std::string, std::string> attributeMap;
294   auto control = Dali::Toolkit::Control::DownCast(Self());
295   auto attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
296   auto map = attribute.GetMap();
297
298   if(map)
299   {
300     auto mapSize = map->Count();
301
302     for(unsigned int i = 0; i < mapSize; i++)
303     {
304       auto mapKey = map->GetKeyAt(i);
305       if(mapKey.type == Dali::Property::Key::STRING)
306       {
307         std::string mapValue;
308         if(map->GetValue(i).Get(mapValue))
309         {
310           attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
311         }
312       }
313     }
314   }
315
316   return attributeMap;
317 }
318
319 Dali::Accessibility::ComponentLayer ControlAccessible::GetLayer() const
320 {
321   return Dali::Accessibility::ComponentLayer::WINDOW;
322 }
323
324 Dali::Rect<> ControlAccessible::GetExtents(Dali::Accessibility::CoordinateType type) const
325 {
326   Dali::Actor self = Self();
327
328   Vector2 screenPosition = self.GetProperty(Dali::DevelActor::Property::SCREEN_POSITION).Get<Vector2>();
329   auto size = self.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * self.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
330   bool positionUsesAnchorPoint = self.GetProperty(Dali::DevelActor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
331   Vector3 anchorPointOffSet = size * (positionUsesAnchorPoint ? self.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
332   Vector2 position = Vector2((screenPosition.x - anchorPointOffSet.x), (screenPosition.y - anchorPointOffSet.y));
333
334   if(type == Dali::Accessibility::CoordinateType::WINDOW)
335   {
336     return {position.x, position.y, size.x, size.y};
337   }
338   else // Dali::Accessibility::CoordinateType::SCREEN
339   {
340     auto window = Dali::DevelWindow::Get(self);
341     auto windowPosition = window.GetPosition();
342     return {position.x + windowPosition.GetX(), position.y + windowPosition.GetY(), size.x, size.y};
343   }
344 }
345
346 int16_t ControlAccessible::GetMdiZOrder() const
347 {
348   return 0;
349 }
350
351 double ControlAccessible::GetAlpha() const
352 {
353   return 0;
354 }
355
356 bool ControlAccessible::GrabFocus()
357 {
358   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
359 }
360
361 void ControlAccessible::ScrollToSelf()
362 {
363   auto* child = this;
364   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
365
366   while (parent)
367   {
368     if (parent->IsScrollable())
369     {
370       parent->ScrollToChild(child->Self());
371     }
372
373     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
374   }
375 }
376
377 void ControlAccessible::RegisterPositionPropertyNotification()
378 {
379   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
380   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
381   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
382   controlImpl.RegisterAccessibilityPositionPropertyNotification();
383 }
384
385 void ControlAccessible::UnregisterPositionPropertyNotification()
386 {
387   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
388   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
389   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
390   controlImpl.UnregisterAccessibilityPositionPropertyNotification();
391 }
392
393 bool ControlAccessible::GrabHighlight()
394 {
395   Dali::Actor self = Self();
396   auto oldHighlightedActor = GetCurrentlyHighlightedActor();
397
398   if(!Dali::Accessibility::IsUp())
399   {
400     return false;
401   }
402
403   if(self == oldHighlightedActor)
404   {
405     return true;
406   }
407
408   // Clear the old highlight.
409   if(oldHighlightedActor)
410   {
411     auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
412     if(oldHighlightObject)
413     {
414       oldHighlightObject->ClearHighlight();
415     }
416   }
417
418   auto highlight = GetHighlightActor();
419   if(!highlight)
420   {
421     highlight = CreateHighlightIndicatorActor();
422     SetHighlightActor(highlight);
423   }
424
425   highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
426   highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
427   highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
428
429   // Need to set resize policy again, to update SIZE property which is set by
430   // NUIViewAccessible. The highlight could move from NUIViewAccessible to
431   // ControlAccessible. In this case, highlight has incorrect size.
432   highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
433
434   // Remember the highlight actor, so that when the default is changed with
435   // SetHighlightActor(), the currently displayed highlight can still be cleared.
436   mCurrentHighlightActor = highlight;
437   ScrollToSelf();
438   self.Add(highlight);
439   SetCurrentlyHighlightedActor(self);
440   EmitHighlighted(true);
441   RegisterPositionPropertyNotification();
442
443   return true;
444 }
445
446 bool ControlAccessible::ClearHighlight()
447 {
448   Dali::Actor self = Self();
449
450   if(!Dali::Accessibility::IsUp())
451   {
452     return false;
453   }
454
455   if(GetCurrentlyHighlightedActor() == self)
456   {
457     UnregisterPositionPropertyNotification();
458     self.Remove(mCurrentHighlightActor.GetHandle());
459     mCurrentHighlightActor = {};
460     SetCurrentlyHighlightedActor({});
461     EmitHighlighted(false);
462     return true;
463   }
464   return false;
465 }
466
467 std::string ControlAccessible::GetActionName(size_t index) const
468 {
469   if(index >= GetActionCount())
470   {
471     return {};
472   }
473
474   Dali::TypeInfo type;
475   Self().GetTypeInfo(type);
476   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
477   return type.GetActionName(index);
478 }
479
480 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
481 {
482   return GetLocaleText(GetActionName(index));
483 }
484
485 std::string ControlAccessible::GetActionDescription(size_t index) const
486 {
487   return {};
488 }
489
490 size_t ControlAccessible::GetActionCount() const
491 {
492   Dali::TypeInfo type;
493   Self().GetTypeInfo(type);
494   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
495   return type.GetActionCount();
496 }
497
498 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
499 {
500   return {};
501 }
502
503 bool ControlAccessible::DoAction(size_t index)
504 {
505   std::string actionName = GetActionName(index);
506   return Self().DoAction(actionName, {});
507 }
508
509 bool ControlAccessible::DoAction(const std::string& name)
510 {
511   return Self().DoAction(name, {});
512 }
513
514 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
515 {
516   auto control = Dali::Toolkit::Control::DownCast(Self());
517
518   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
519   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
520
521   if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
522   {
523     auto ret = std::make_pair(gestureInfo, false);
524     controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
525     return ret.second;
526   }
527
528   return false;
529 }
530
531 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
532 {
533   auto control = Dali::Toolkit::Control::DownCast(Self());
534
535   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
536   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
537
538   std::vector<Dali::Accessibility::Relation> ret;
539
540   for(auto& relation : controlImpl.mAccessibilityRelations)
541   {
542     auto& targets = relation.second;
543
544     ret.emplace_back(Accessibility::Relation{relation.first, {}});
545
546     // Map every Accessible* to its Address
547     std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
548       return x->GetAddress();
549     });
550   }
551
552   return ret;
553 }
554
555 Dali::Actor ControlAccessible::GetInternalActor()
556 {
557   return Dali::Actor{};
558 }
559
560 bool ControlAccessible::ScrollToChild(Actor child)
561 {
562   return false;
563 }
564
565 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
566 {
567   return Actor::Property::NAME;
568 }
569
570 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
571 {
572   return Dali::Property::INVALID_INDEX;
573 }
574
575 void ControlAccessible::SetLastPosition(Vector2 position)
576 {
577   mLastPosition = position;
578 }
579
580 Vector2 ControlAccessible::GetLastPosition() const
581 {
582   return mLastPosition;
583 }
584
585 } // namespace Dali::Toolkit::DevelControl