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::GrabFocus()
287 return Toolkit::KeyboardFocusManager::Get().SetCurrentFocusActor(Self());
290 void ControlAccessible::ScrollToSelf()
293 auto* parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(child->GetParent());
297 if (parent->IsScrollable())
299 parent->ScrollToChild(child->Self());
302 parent = dynamic_cast<Toolkit::DevelControl::ControlAccessible*>(parent->GetParent());
306 void ControlAccessible::RegisterPositionPropertyNotification()
308 auto control = Dali::Toolkit::Control::DownCast(Self());
309 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
310 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
311 controlImpl.RegisterAccessibilityPositionPropertyNotification();
314 void ControlAccessible::UnregisterPositionPropertyNotification()
316 auto control = Dali::Toolkit::Control::DownCast(Self());
317 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
318 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
319 controlImpl.UnregisterAccessibilityPositionPropertyNotification();
322 bool ControlAccessible::GrabHighlight()
324 Dali::Actor self = Self();
325 auto oldHighlightedActor = GetCurrentlyHighlightedActor();
327 if(!Dali::Accessibility::IsUp())
332 if(self == oldHighlightedActor)
337 // Clear the old highlight.
338 if(oldHighlightedActor)
340 auto oldHighlightObject = dynamic_cast<Dali::Accessibility::Component*>(Internal::Control::Impl::GetAccessibilityObject(oldHighlightedActor));
341 if(oldHighlightObject)
343 oldHighlightObject->ClearHighlight();
347 auto highlight = GetHighlightActor();
350 highlight = CreateHighlightIndicatorActor();
351 SetHighlightActor(highlight);
354 highlight.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
355 highlight.SetProperty(Actor::Property::POSITION_Z, 1.0f);
356 highlight.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
358 // Need to set resize policy again, to update SIZE property which is set by
359 // NUIViewAccessible. The highlight could move from NUIViewAccessible to
360 // ControlAccessible. In this case, highlight has incorrect size.
361 highlight.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
363 // Remember the highlight actor, so that when the default is changed with
364 // SetHighlightActor(), the currently displayed highlight can still be cleared.
365 mCurrentHighlightActor = highlight;
368 SetCurrentlyHighlightedActor(self);
369 EmitHighlighted(true);
370 RegisterPositionPropertyNotification();
375 bool ControlAccessible::ClearHighlight()
377 Dali::Actor self = Self();
379 if(!Dali::Accessibility::IsUp())
384 if(GetCurrentlyHighlightedActor() == self)
386 UnregisterPositionPropertyNotification();
387 self.Remove(mCurrentHighlightActor.GetHandle());
388 mCurrentHighlightActor = {};
389 SetCurrentlyHighlightedActor({});
390 EmitHighlighted(false);
396 std::string ControlAccessible::GetActionName(size_t index) const
398 if(index >= GetActionCount())
404 Self().GetTypeInfo(type);
405 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
406 return type.GetActionName(index);
409 std::string ControlAccessible::GetLocalizedActionName(size_t index) const
411 return GetLocaleText(GetActionName(index));
414 std::string ControlAccessible::GetActionDescription(size_t index) const
419 size_t ControlAccessible::GetActionCount() const
422 Self().GetTypeInfo(type);
423 DALI_ASSERT_ALWAYS(type && "no TypeInfo object");
424 return type.GetActionCount();
427 std::string ControlAccessible::GetActionKeyBinding(size_t index) const
432 bool ControlAccessible::DoAction(size_t index)
434 std::string actionName = GetActionName(index);
435 return Self().DoAction(actionName, {});
438 bool ControlAccessible::DoAction(const std::string& name)
440 return Self().DoAction(name, {});
443 bool ControlAccessible::DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo)
445 auto control = Dali::Toolkit::Control::DownCast(Self());
447 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
448 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
450 if(!controlImpl.mAccessibilityDoGestureSignal.Empty())
452 auto ret = std::make_pair(gestureInfo, false);
453 controlImpl.mAccessibilityDoGestureSignal.Emit(ret);
460 std::vector<Dali::Accessibility::Relation> ControlAccessible::GetRelationSet()
462 auto control = Dali::Toolkit::Control::DownCast(Self());
464 Internal::Control& internalControl = Toolkit::Internal::GetImplementation(control);
465 Internal::Control::Impl& controlImpl = Internal::Control::Impl::Get(internalControl);
467 std::vector<Dali::Accessibility::Relation> ret;
469 for(auto& relation : controlImpl.mAccessibilityRelations)
471 auto& targets = relation.second;
473 ret.emplace_back(Accessibility::Relation{relation.first, {}});
475 // Map every Accessible* to its Address
476 std::transform(targets.begin(), targets.end(), std::back_inserter(ret.back().targets), [](auto* x) {
477 return x->GetAddress();
484 bool ControlAccessible::ScrollToChild(Actor child)
489 Dali::Property::Index ControlAccessible::GetNamePropertyIndex()
491 return Actor::Property::NAME;
494 Dali::Property::Index ControlAccessible::GetDescriptionPropertyIndex()
496 return Dali::Property::INVALID_INDEX;
499 void ControlAccessible::SetLastPosition(Vector2 position)
501 mLastPosition = position;
504 Vector2 ControlAccessible::GetLastPosition() const
506 return mLastPosition;
509 } // namespace Dali::Toolkit::DevelControl