Improve the underline markup
[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::IsHidden() const
286 {
287   auto control = Dali::Toolkit::Control::DownCast(Self());
288
289   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
290   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
291
292   return controlImpl.mAccessibilityHidden;
293 }
294
295 bool ControlAccessible::GrabFocus()
296 {
297   return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
298 }
299
300 void ControlAccessible::ScrollToSelf()
301 {
302   auto* child = this;
303   auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
304
305   while (parent)
306   {
307     if (parent->IsScrollable())
308     {
309       parent->ScrollToChild(child->Self());
310     }
311
312     parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
313   }
314 }
315
316 void ControlAccessible::RegisterPositionPropertyNotification()
317 {
318   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
319   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
320   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
321   controlImpl.RegisterAccessibilityPositionPropertyNotification();
322 }
323
324 void ControlAccessible::UnregisterPositionPropertyNotification()
325 {
326   auto                     control         = Dali::Toolkit::Control::DownCast(Self());
327   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
328   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
329   controlImpl.UnregisterAccessibilityPositionPropertyNotification();
330 }
331
332 bool ControlAccessible::GrabHighlight()
333 {
334   Dali::Actor self = Self();
335   auto oldHighlightedActor = GetCurrentlyHighlightedActor();
336
337   if(!Dali::Accessibility::IsUp())
338   {
339     return false;
340   }
341
342   if(self == oldHighlightedActor)
343   {
344     return true;
345   }
346
347   // Clear the old highlight.
348   if(oldHighlightedActor)
349   {
350     auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
351     if(oldHighlightObject)
352     {
353       oldHighlightObject->ClearHighlight();
354     }
355   }
356
357   auto highlight = GetHighlightActor();
358   if(!highlight)
359   {
360     highlight = CreateHighlightIndicatorActor();
361     SetHighlightActor(highlight);
362   }
363
364   highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
365   highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
366   highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
367
368   // Need to set resize policy again, to update SIZE property which is set by
369   // NUIViewAccessible. The highlight could move from NUIViewAccessible to
370   // ControlAccessible. In this case, highlight has incorrect size.
371   highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
372
373   // Remember the highlight actor, so that when the default is changed with
374   // SetHighlightActor(), the currently displayed highlight can still be cleared.
375   mCurrentHighlightActor = highlight;
376   ScrollToSelf();
377   self.Add(highlight);
378   SetCurrentlyHighlightedActor(self);
379   EmitHighlighted(true);
380   RegisterPositionPropertyNotification();
381
382   return true;
383 }
384
385 bool ControlAccessible::ClearHighlight()
386 {
387   Dali::Actor self = Self();
388
389   if(!Dali::Accessibility::IsUp())
390   {
391     return false;
392   }
393
394   if(GetCurrentlyHighlightedActor() == self)
395   {
396     UnregisterPositionPropertyNotification();
397     self.Remove(mCurrentHighlightActor.GetHandle());
398     mCurrentHighlightActor = {};
399     SetCurrentlyHighlightedActor({});
400     EmitHighlighted(false);
401     return true;
402   }
403   return false;
404 }
405
406 std::string ControlAccessible::GetActionName(size_t index) const
407 {
408   if(index >= GetActionCount())
409   {
410     return {};
411   }
412
413   Dali::TypeInfo type;
414   Self().GetTypeInfo(type);
415   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
416   return type.GetActionName(index);
417 }
418
419 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
420 {
421   return GetLocaleText(GetActionName(index));
422 }
423
424 std::string ControlAccessible::GetActionDescription(size_t index) const
425 {
426   return {};
427 }
428
429 size_t ControlAccessible::GetActionCount() const
430 {
431   Dali::TypeInfo type;
432   Self().GetTypeInfo(type);
433   DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
434   return type.GetActionCount();
435 }
436
437 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
438 {
439   return {};
440 }
441
442 bool ControlAccessible::DoAction(size_t index)
443 {
444   std::string actionName = GetActionName(index);
445   return Self().DoAction(actionName, {});
446 }
447
448 bool ControlAccessible::DoAction(const std::string& name)
449 {
450   return Self().DoAction(name, {});
451 }
452
453 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
454 {
455   auto control = Dali::Toolkit::Control::DownCast(Self());
456
457   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
458   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
459
460   if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
461   {
462     auto ret = std::make_pair(gestureInfo, false);
463     controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
464     return ret.second;
465   }
466
467   return false;
468 }
469
470 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
471 {
472   auto control = Dali::Toolkit::Control::DownCast(Self());
473
474   Internal::Control&       internalControl = Toolkit::Internal::GetImplementation(control);
475   Internal::Control::Impl& controlImpl     = Internal::Control::Impl::Get(internalControl);
476
477   std::vector<Dali::Accessibility::Relation> ret;
478
479   for(auto& relation : controlImpl.mAccessibilityRelations)
480   {
481     auto& targets = relation.second;
482
483     ret.emplace_back(Accessibility::Relation{relation.first, {}});
484
485     // Map every Accessible* to its Address
486     std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
487       return x->GetAddress();
488     });
489   }
490
491   return ret;
492 }
493
494 bool ControlAccessible::ScrollToChild(Actor child)
495 {
496   return false;
497 }
498
499 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
500 {
501   return Actor::Property::NAME;
502 }
503
504 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
505 {
506   return Dali::Property::INVALID_INDEX;
507 }
508
509 void ControlAccessible::SetLastPosition(Vector2 position)
510 {
511   mLastPosition = position;
512 }
513
514 Vector2 ControlAccessible::GetLastPosition() const
515 {
516   return mLastPosition;
517 }
518
519 } // namespace Dali::Toolkit::DevelControl