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