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)
70 : ActorAccessible(self)
72 auto control = Toolkit::Control::DownCast(self);
74 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
75 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
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())
83 if(index == DevelControl::Property::ACCESSIBILITY_NAME || (index == GetNamePropertyIndex() && controlImpl.mAccessibilityName.empty()))
85 if(controlImpl.mAccessibilityGetNameSignal.Empty())
87 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::NAME);
91 if(index == DevelControl::Property::ACCESSIBILITY_DESCRIPTION || (index == GetDescriptionPropertyIndex() && controlImpl.mAccessibilityDescription.empty()))
93 if(controlImpl.mAccessibilityGetDescriptionSignal.Empty())
95 Emit(Dali::Accessibility::ObjectPropertyChangeEvent::DESCRIPTION);
101 std::string ControlAccessible::GetName() const
103 auto control = Dali::Toolkit::Control::DownCast(Self());
105 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
106 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
109 if(!controlImpl.mAccessibilityGetNameSignal.Empty())
111 controlImpl.mAccessibilityGetNameSignal.Emit(name);
113 else if(!controlImpl.mAccessibilityName.empty())
115 name = controlImpl.mAccessibilityName;
117 else if(auto raw = GetNameRaw(); !raw.empty())
123 name = Self().GetProperty<std::string>(Actor::Property::NAME);
126 if(!controlImpl.mAccessibilityTranslationDomain.empty())
128 return GetLocaleText(name, controlImpl.mAccessibilityTranslationDomain.c_str());
131 return GetLocaleText(name);
134 std::string ControlAccessible::GetNameRaw() const
139 std::string ControlAccessible::GetDescription() const
141 auto control = Dali::Toolkit::Control::DownCast(Self());
143 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
144 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
145 std::string description;
147 if(!controlImpl.mAccessibilityGetDescriptionSignal.Empty())
149 controlImpl.mAccessibilityGetDescriptionSignal.Emit(description);
151 else if(!controlImpl.mAccessibilityDescription.empty())
153 description = controlImpl.mAccessibilityDescription;
157 description = GetDescriptionRaw();
160 if(!controlImpl.mAccessibilityTranslationDomain.empty())
162 return GetLocaleText(description, controlImpl.mAccessibilityTranslationDomain.c_str());
165 return GetLocaleText(description);
168 std::string ControlAccessible::GetDescriptionRaw() const
173 Dali::Accessibility::Role ControlAccessible::GetRole() const
175 return Self().GetProperty<Dali::Accessibility::Role>(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE);
178 std::string ControlAccessible::GetLocalizedRoleName() const
180 return GetLocaleText(GetRoleName());
183 bool ControlAccessible::IsShowing()
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))
192 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
198 auto childExtent = child->GetExtents(Dali::Accessibility::CoordinateType::WINDOW);
201 auto control = Dali::Toolkit::Control::DownCast(parent->Self());
202 if(!control.GetProperty<bool>(Actor::Property::VISIBLE))
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))
212 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
218 Dali::Accessibility::States ControlAccessible::CalculateStates()
220 using Dali::Accessibility::State;
222 Dali::Actor self = Self();
223 Dali::Accessibility::States states;
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>();
238 Dali::Accessibility::States ControlAccessible::GetStates()
240 return CalculateStates();
243 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
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();
252 auto mapSize = map->Count();
254 for(unsigned int i = 0; i < mapSize; i++)
256 auto mapKey = map->GetKeyAt(i);
257 if(mapKey.type == Dali::Property::Key::STRING)
259 std::string mapValue;
260 if(map->GetValue(i).Get(mapValue))
262 attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
271 bool ControlAccessible::IsHidden() const
273 auto control = Dali::Toolkit::Control::DownCast(Self());
275 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
276 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
278 return controlImpl.mAccessibilityHidden;
281 bool ControlAccessible::GrabFocus()
283 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
286 void ControlAccessible::ScrollToSelf()
289 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
293 if (parent->IsScrollable())
295 parent->ScrollToChild(child->Self());
298 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
302 void ControlAccessible::RegisterPositionPropertyNotification()
304 auto control = Dali::Toolkit::Control::DownCast(Self());
305 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
306 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
307 controlImpl.RegisterAccessibilityPositionPropertyNotification();
310 void ControlAccessible::UnregisterPositionPropertyNotification()
312 auto control = Dali::Toolkit::Control::DownCast(Self());
313 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
314 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
315 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
318 bool ControlAccessible::GrabHighlight()
320 Dali::Actor self = Self();
321 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
323 if(!Dali::Accessibility::IsUp())
328 if(self == oldHighlightedActor)
333 // Clear the old highlight.
334 if(oldHighlightedActor)
336 auto oldHighlightedObject = Dali::Accessibility::Component::DownCast(Accessible::Get(oldHighlightedActor));
337 if(oldHighlightedObject)
339 oldHighlightedObject->ClearHighlight();
343 auto highlight = GetHighlightActor();
346 highlight = CreateHighlightIndicatorActor();
347 SetHighlightActor(highlight);
350 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
351 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
352 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
354 // Need to set resize policy again, to update SIZE property which is set by
355 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
356 // ControlAccessible. In this case, highlight has incorrect size.
357 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
359 // Remember the highlight actor, so that when the default is changed with
360 // SetHighlightActor(), the currently displayed highlight can still be cleared.
361 mCurrentHighlightActor = highlight;
364 SetCurrentlyHighlightedActor(self);
365 EmitHighlighted(true);
366 RegisterPositionPropertyNotification();
371 bool ControlAccessible::ClearHighlight()
373 Dali::Actor self = Self();
375 if(!Dali::Accessibility::IsUp())
380 if(GetCurrentlyHighlightedActor() == self)
382 UnregisterPositionPropertyNotification();
383 self.Remove(mCurrentHighlightActor.GetHandle());
384 mCurrentHighlightActor = {};
385 SetCurrentlyHighlightedActor({});
386 EmitHighlighted(false);
392 std::string ControlAccessible::GetActionName(size_t index) const
394 if(index >= GetActionCount())
400 Self().GetTypeInfo(type);
401 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
402 return type.GetActionName(index);
405 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
407 return GetLocaleText(GetActionName(index));
410 std::string ControlAccessible::GetActionDescription(size_t index) const
415 size_t ControlAccessible::GetActionCount() const
418 Self().GetTypeInfo(type);
419 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
420 return type.GetActionCount();
423 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
428 bool ControlAccessible::DoAction(size_t index)
430 std::string actionName = GetActionName(index);
431 return Self().DoAction(actionName, {});
434 bool ControlAccessible::DoAction(const std::string& name)
436 return Self().DoAction(name, {});
439 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
441 auto control = Dali::Toolkit::Control::DownCast(Self());
443 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
444 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
446 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
448 auto ret = std::make_pair(gestureInfo, false);
449 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
456 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
458 auto control = Dali::Toolkit::Control::DownCast(Self());
460 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
461 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
463 std::vector<Dali::Accessibility::Relation> ret;
465 for(auto& relation : controlImpl.mAccessibilityRelations)
467 auto& targets = relation.second;
469 ret.emplace_back(Accessibility::Relation{relation.first, {}});
471 // Map every Accessible* to its Address
472 std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
473 return x->GetAddress();
480 bool ControlAccessible::ScrollToChild(Actor child)
485 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
487 return Actor::Property::NAME;
490 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
492 return Dali::Property::INVALID_INDEX;
495 void ControlAccessible::SetLastPosition(Vector2 position)
497 mLastPosition = position;
500 Vector2 ControlAccessible::GetLastPosition() const
502 return mLastPosition;
505 } // namespace Dali::Toolkit::DevelControl