2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "control-accessible.h"
22 #ifdef DGETTEXT_ENABLED
26 #include <dali/devel-api/actors/actor-devel.h>
27 #include <dali/devel-api/adaptor-framework/window-devel.h>
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>
37 namespace Dali::Toolkit::DevelControl
41 static std::string GetLocaleText(std::string string, const char *domain = "dali-toolkit")
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());
52 static Dali::Actor CreateHighlightIndicatorActor()
54 std::string focusBorderImagePath(AssetManager::GetDaliImagePath());
55 focusBorderImagePath += "/keyboard_focus.9.png";
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);
62 DevelControl::AppendAccessibilityAttribute(actor, "highlight", std::string());
63 actor.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, false);
67 } // unnamed namespace
69 ControlAccessible::ControlAccessible(Dali::Actor self, Dali::Accessibility::Role role, bool modal)
70 : ActorAccessible(self),
73 auto control = Dali::Toolkit::Control::DownCast(Self());
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)
79 controlImpl.mAccessibilityRole = role;
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())
88 if(index == DevelControl::Property::ACCESSIBILITY_NAME || (index == GetNamePropertyIndex() && !controlImpl.mAccessibilityNameSet))
90 if(controlImpl.mAccessibilityGetNameSignal.Empty())
92 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::NAME);
96 if(index == DevelControl::Property::ACCESSIBILITY_DESCRIPTION || (index == GetDescriptionPropertyIndex() && !controlImpl.mAccessibilityDescriptionSet))
98 if(controlImpl.mAccessibilityGetDescriptionSignal.Empty())
100 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
106 std::string ControlAccessible::GetName() const
108 auto control = Dali::Toolkit::Control::DownCast(Self());
110 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
111 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
114 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
116 controlImpl.mAccessibilityGetNameSignal.Emit(name);
118 else if(controlImpl.mAccessibilityNameSet)
120 name = controlImpl.mAccessibilityName;
122 else if(auto raw = GetNameRaw(); !raw.empty())
128 name = Self().GetProperty<std::string>(Actor::Property::NAME);
131 if(controlImpl.mAccessibilityTranslationDomainSet)
133 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
136 return GetLocaleText(name);
139 std::string ControlAccessible::GetNameRaw() const
144 std::string ControlAccessible::GetDescription() const
146 auto control = Dali::Toolkit::Control::DownCast(Self());
148 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
149 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
150 std::string description;
152 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
154 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
156 else if(controlImpl.mAccessibilityDescriptionSet)
158 description = controlImpl.mAccessibilityDescription;
162 description = GetDescriptionRaw();
164 if(controlImpl.mAccessibilityTranslationDomainSet)
166 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
169 return GetLocaleText(description);
172 std::string ControlAccessible::GetDescriptionRaw() const
177 Dali::Accessibility::Role ControlAccessible::GetRole() const
179 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
182 std::string ControlAccessible::GetLocalizedRoleName() const
184 return GetLocaleText(GetRoleName());
187 bool ControlAccessible::IsShowing()
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))
196 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
202 auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
205 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
206 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
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))
216 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
222 Dali::Accessibility::States ControlAccessible::CalculateStates()
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;
229 if(self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).GetType() == Dali::Property::NONE)
231 state[Dali::Accessibility::State::HIGHLIGHTABLE] = false;
235 state[Dali::Accessibility::State::HIGHLIGHTABLE] = self.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE).Get<bool>();
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);
245 state[Dali::Accessibility::State::MODAL] = true;
247 state[Dali::Accessibility::State::SHOWING] = IsShowing();
248 state[Dali::Accessibility::State::DEFUNCT] = !self.GetProperty(Dali::DevelActor::Property::CONNECTED_TO_SCENE).Get<bool>();
252 Dali::Accessibility::States ControlAccessible::GetStates()
254 return CalculateStates();
257 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
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();
266 auto mapSize = map->Count();
268 for(unsigned int i = 0; i < mapSize; i++)
270 auto mapKey = map->GetKeyAt(i);
271 if(mapKey.type == Dali::Property::Key::STRING)
273 std::string mapValue;
274 if(map->GetValue(i).Get(mapValue))
276 attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
285 bool ControlAccessible::IsHidden() const
287 auto control = Dali::Toolkit::Control::DownCast(Self());
289 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
290 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
292 return controlImpl.mAccessibilityHidden;
295 bool ControlAccessible::GrabFocus()
297 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
300 void ControlAccessible::ScrollToSelf()
303 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
307 if (parent->IsScrollable())
309 parent->ScrollToChild(child->Self());
312 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
316 void ControlAccessible::RegisterPositionPropertyNotification()
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();
324 void ControlAccessible::UnregisterPositionPropertyNotification()
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();
332 bool ControlAccessible::GrabHighlight()
334 Dali::Actor self = Self();
335 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
337 if(!Dali::Accessibility::IsUp())
342 if(self == oldHighlightedActor)
347 // Clear the old highlight.
348 if(oldHighlightedActor)
350 auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
351 if(oldHighlightObject)
353 oldHighlightObject->ClearHighlight();
357 auto highlight = GetHighlightActor();
360 highlight = CreateHighlightIndicatorActor();
361 SetHighlightActor(highlight);
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));
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);
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;
378 SetCurrentlyHighlightedActor(self);
379 EmitHighlighted(true);
380 RegisterPositionPropertyNotification();
385 bool ControlAccessible::ClearHighlight()
387 Dali::Actor self = Self();
389 if(!Dali::Accessibility::IsUp())
394 if(GetCurrentlyHighlightedActor() == self)
396 UnregisterPositionPropertyNotification();
397 self.Remove(mCurrentHighlightActor.GetHandle());
398 mCurrentHighlightActor = {};
399 SetCurrentlyHighlightedActor({});
400 EmitHighlighted(false);
406 std::string ControlAccessible::GetActionName(size_t index) const
408 if(index >= GetActionCount())
414 Self().GetTypeInfo(type);
415 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
416 return type.GetActionName(index);
419 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
421 return GetLocaleText(GetActionName(index));
424 std::string ControlAccessible::GetActionDescription(size_t index) const
429 size_t ControlAccessible::GetActionCount() const
432 Self().GetTypeInfo(type);
433 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
434 return type.GetActionCount();
437 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
442 bool ControlAccessible::DoAction(size_t index)
444 std::string actionName = GetActionName(index);
445 return Self().DoAction(actionName, {});
448 bool ControlAccessible::DoAction(const std::string& name)
450 return Self().DoAction(name, {});
453 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
455 auto control = Dali::Toolkit::Control::DownCast(Self());
457 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
458 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
460 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
462 auto ret = std::make_pair(gestureInfo, false);
463 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
470 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
472 auto control = Dali::Toolkit::Control::DownCast(Self());
474 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
475 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
477 std::vector<Dali::Accessibility::Relation> ret;
479 for(auto& relation : controlImpl.mAccessibilityRelations)
481 auto& targets = relation.second;
483 ret.emplace_back(Accessibility::Relation{relation.first, {}});
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();
494 bool ControlAccessible::ScrollToChild(Actor child)
499 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
501 return Actor::Property::NAME;
504 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
506 return Dali::Property::INVALID_INDEX;
509 void ControlAccessible::SetLastPosition(Vector2 position)
511 mLastPosition = position;
514 Vector2 ControlAccessible::GetLastPosition() const
516 return mLastPosition;
519 } // namespace Dali::Toolkit::DevelControl