[AT-SPI] Lazily calculate the "class" attribute 06/308006/3
authorArtur Świgoń <a.swigon@samsung.com>
Fri, 15 Mar 2024 07:16:16 +0000 (08:16 +0100)
committerArtur Świgoń <a.swigon@samsung.com>
Fri, 15 Mar 2024 10:21:44 +0000 (11:21 +0100)
This helps streamline Control::Initialize() by moving the attribute calculation
to ControlAccessible::GetAttributes() which is where the value is really
needed. Additionally, GetAttributes() is cleaned up a little for readability.

Change-Id: Iecc2a63a200d23e4bfb30681fa00b63ca4d72c5a

automated-tests/src/dali-toolkit-internal/utc-Dali-Accessibility-Controls-BridgeUp.cpp
dali-toolkit/devel-api/controls/control-accessible.cpp
dali-toolkit/public-api/controls/control-impl.cpp

index 8a1772a..ad2fba7 100644 (file)
@@ -461,6 +461,10 @@ int utcDaliAccessibilityControlAttributes(void)
   auto ptr                  = Dali::Accessibility::Accessible::Get(check_box_button);
   auto attribute_map_bridge = TestGetAttributes(ptr->GetAddress());
   auto counter              = 0u;
+
+  // Refresh the attributes since ControlAccessible::GetAttributes() might have added something
+  attributes     = check_box_button.GetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
+  attributes_map = attributes.GetMap();
   for(auto i = 0u; i < attributes_map->Count(); ++i)
     if((attributes_map->GetValue(i)).GetType() != Property::NONE)
       ++counter;
index c4ad881..8788ade 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 
 #include <dali/devel-api/actors/actor-devel.h>
 #include <dali/devel-api/adaptor-framework/window-devel.h>
+#include <dali/public-api/object/type-info.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
@@ -208,36 +209,49 @@ Dali::Accessibility::States ControlAccessible::GetStates()
 
 Dali::Accessibility::Attributes ControlAccessible::GetAttributes() const
 {
-  std::unordered_map<std::string, std::string> attributeMap;
-  auto                                         control   = Dali::Toolkit::Control::DownCast(Self());
-  auto                                         attribute = control.GetProperty(Dali::Toolkit::DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
-  auto                                         map       = attribute.GetMap();
+  static const std::string automationIdKey = "automationId";
+  static const std::string classKey        = "class";
 
-  if(map)
+  Accessibility::Attributes result;
+  Toolkit::Control          control        = Toolkit::Control::DownCast(Self());
+  Dali::Property::Value     property       = control.GetProperty(DevelControl::Property::ACCESSIBILITY_ATTRIBUTES);
+  Dali::Property::Map*      attributeMap   = property.GetMap();
+  std::size_t               attributeCount = attributeMap ? attributeMap->Count() : 0U;
+
+  for(std::size_t i = 0; i < attributeCount; i++)
   {
-    auto mapSize = map->Count();
+    Dali::Property::Key mapKey = attributeMap->GetKeyAt(i);
+    std::string         mapValue;
 
-    for(unsigned int i = 0; i < mapSize; i++)
+    if(mapKey.type == Dali::Property::Key::STRING && attributeMap->GetValue(i).Get(mapValue))
     {
-      auto mapKey = map->GetKeyAt(i);
-      if(mapKey.type == Dali::Property::Key::STRING)
-      {
-        std::string mapValue;
-        if(map->GetValue(i).Get(mapValue))
-        {
-          attributeMap.emplace(std::move(mapKey.stringKey), std::move(mapValue));
-        }
-      }
+      result.emplace(std::move(mapKey.stringKey), std::move(mapValue));
     }
   }
 
-  auto automationId = control.GetProperty<std::string>(Dali::Toolkit::DevelControl::Property::AUTOMATION_ID);
+  auto automationId = control.GetProperty<std::string>(DevelControl::Property::AUTOMATION_ID);
   if(!automationId.empty())
   {
-    attributeMap.emplace("automationId", std::move(automationId));
+    result.emplace(automationIdKey, std::move(automationId));
+  }
+
+  // Add "class" if not present already
+  if(result.find(classKey) == result.end())
+  {
+    Dali::TypeInfo typeInfo;
+    Self().GetTypeInfo(typeInfo);
+    if(typeInfo)
+    {
+      const std::string& typeName = typeInfo.GetName();
+
+      result.emplace(classKey, typeName);
+
+      // Save the 'typeName' so we don't have to calculate it again
+      DevelControl::AppendAccessibilityAttribute(control, classKey, typeName);
+    }
   }
 
-  return attributeMap;
+  return result;
 }
 
 bool ControlAccessible::IsHidden() const
index fbf364a..9af21f6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <dali/integration-api/debug.h>
 #include <dali/public-api/animation/constraint.h>
 #include <dali/public-api/math/math-utils.h>
-#include <dali/public-api/object/type-info.h>
-#include <dali/public-api/object/type-registry-helper.h>
 #include <dali/public-api/size-negotiation/relayout-container.h>
 #include <cstring> // for strcmp
 #include <limits>
 #include <stack>
-#include <typeinfo>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/dali-toolkit.h>
@@ -443,14 +440,6 @@ void Control::Initialize()
   {
     SetKeyboardNavigationSupport(true);
   }
-
-  Dali::TypeInfo type;
-  Self().GetTypeInfo(type);
-  if(type)
-  {
-    const auto& typeName = type.GetName();
-    DevelControl::AppendAccessibilityAttribute(Toolkit::Control::DownCast(Self()), "class", typeName);
-  }
 }
 
 void Control::OnInitialize()