[ATSPI] missing variables provided in ReadingMaterialType
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-accessible.cpp
index 4f5ebb2..2ba6212 100644 (file)
@@ -19,6 +19,7 @@
 #include <dali/internal/accessibility/bridge/bridge-accessible.h>
 
 // EXTERNAL INCLUDES
+#include <algorithm>
 #include <iostream>
 
 //comment out 2 lines below to get more logs
@@ -29,6 +30,72 @@ using namespace Dali::Accessibility;
 
 #define GET_NAVIGABLE_AT_POINT_MAX_RECURSION_DEPTH 10000
 
+namespace {
+
+bool SortVertically(Component* lhs, Component* rhs)
+{
+  auto leftRect  = lhs->GetExtents(CoordType::WINDOW);
+  auto rightRect = rhs->GetExtents(CoordType::WINDOW);
+
+  return leftRect.y < rightRect.y;
+}
+
+bool SortHorizontally(Component* lhs, Component* rhs)
+{
+  auto leftRect  = lhs->GetExtents(CoordType::WINDOW);
+  auto rightRect = rhs->GetExtents(CoordType::WINDOW);
+
+  return leftRect.x < rightRect.x;
+}
+
+std::vector<std::vector<Component*>> SplitLines(const std::vector<Component*>& children)
+{
+  // Find first with non-zero area
+  auto first = std::find_if(children.begin(), children.end(), [](Component* component) -> bool {
+    auto extents = component->GetExtents(CoordType::WINDOW);
+    return extents.height != 0.0f && extents.width != 0.0f;
+  });
+
+  if(first == children.end())
+  {
+    return {};
+  }
+
+  std::vector<std::vector<Component*>> lines(1);
+  Dali::Rect<> lineRect = (*first)->GetExtents(CoordType::WINDOW);
+  Dali::Rect<> rect;
+
+  // Split into lines
+  for(auto it = first; it != children.end(); ++it)
+  {
+    auto child = *it;
+
+    rect = child->GetExtents(CoordType::WINDOW);
+    if(rect.height == 0.0f || rect.width == 0.0f)
+    {
+      // Zero area, ignore
+      continue;
+    }
+
+    if(lineRect.y + (0.25 * lineRect.height) >= rect.y)
+    {
+      // Same line
+      lines.back().push_back(child);
+    }
+    else
+    {
+      // Start a new line
+      lineRect = rect;
+      lines.emplace_back();
+      lines.back().push_back(child);
+    }
+  }
+
+  return lines;
+}
+
+} // anonymous namespace
+
 BridgeAccessible::BridgeAccessible()
 {
 }
@@ -58,46 +125,6 @@ void BridgeAccessible::RegisterInterfaces()
   dbusServer.addInterface("/", desc, true);
 }
 
-static bool AcceptObjectCheckRole(Component* obj)
-{
-  if(!obj)
-    return false;
-  switch(obj->GetRole())
-  {
-    case Role::APPLICATION:
-    case Role::FILLER:
-    case Role::SCROLL_PANE:
-    case Role::SPLIT_PANE:
-    case Role::WINDOW:
-    case Role::IMAGE:
-    case Role::IMAGE_MAP:
-    case Role::LIST:
-    case Role::ICON:
-    case Role::TOOL_BAR:
-    case Role::REDUNDANT_OBJECT:
-    case Role::COLOR_CHOOSER:
-    case Role::TREE_TABLE:
-    case Role::PAGE_TAB_LIST:
-    case Role::PAGE_TAB:
-    case Role::SPIN_BUTTON:
-    case Role::INPUT_METHOD_WINDOW:
-    case Role::EMBEDDED:
-    case Role::INVALID:
-    case Role::NOTIFICATION:
-    case Role::DATE_EDITOR:
-    case Role::TABLE:
-    {
-      return false;
-    }
-    default:
-    {
-      break;
-    }
-  }
-
-  return true;
-}
-
 static bool AcceptObjectCheckRelations(Component* obj)
 {
   auto r = obj->GetRelationSet();
@@ -152,8 +179,6 @@ static bool AcceptObject(Component* obj)
   const auto states = obj->GetStates();
   if(!states[State::VISIBLE])
     return false;
-  if(!AcceptObjectCheckRole(obj))
-    return false;
   if(!AcceptObjectCheckRelations(obj))
     return false;
   if(!states[State::HIGHLIGHTABLE])
@@ -188,6 +213,44 @@ static bool AcceptObject(Accessible* obj)
   return AcceptObject(c);
 }
 
+static int32_t GetItemCountOfList(Accessible* obj)
+{
+  int32_t itemCount = 0;
+  if(obj && obj->GetRole() == Role::LIST)
+  {
+    for(auto i = 0u; i < static_cast<size_t>(obj->GetChildCount()); ++i)
+    {
+      auto child = obj->GetChildAtIndex(i);
+      if(child && child->GetRole() == Role::LIST_ITEM)
+      {
+        itemCount++;
+      }
+    }
+  }
+  return itemCount;
+}
+
+static int32_t GetItemCountOfFirstDescendantList(Accessible* obj)
+{
+  int32_t itemCount = 0;
+  itemCount         = GetItemCountOfList(obj);
+  if(itemCount > 0 || !obj)
+  {
+    return itemCount;
+  }
+
+  for(auto i = 0u; i < static_cast<size_t>(obj->GetChildCount()); ++i)
+  {
+    auto child = obj->GetChildAtIndex(i);
+    itemCount  = GetItemCountOfFirstDescendantList(child);
+    if(itemCount > 0)
+    {
+      return itemCount;
+    }
+  }
+  return itemCount;
+}
+
 static std::string objDump(Component* obj)
 {
   if(!obj)
@@ -259,69 +322,100 @@ Component* BridgeAccessible::CalculateNavigableAccessibleAtPoint(Accessible* roo
 
 BridgeAccessible::ReadingMaterialType BridgeAccessible::GetReadingMaterial()
 {
-  auto        self          = FindSelf();
-  auto        attributes    = self->GetAttributes();
-  auto        name          = self->GetName();
-  std::string labeledByName = "";
-  std::string textIfceName  = "";
-  auto        role          = static_cast<uint32_t>(self->GetRole());
-  auto        states        = self->GetStates();
-  auto        localizedName = self->GetLocalizedRoleName();
-  auto        childCount    = static_cast<int32_t>(self->GetChildCount());
+  auto self                     = FindSelf();
+  auto findObjectByRelationType = [this, &self](RelationType relationType) {
+    auto relations = self->GetRelationSet();
+    auto relation  = std::find_if(relations.begin(),
+                                 relations.end(),
+                                 [relationType](const Dali::Accessibility::Relation& relation) -> bool {
+                                   return relation.relationType == relationType;
+                                 });
+    return relations.end() != relation && !relation->targets.empty() ? Find(relation->targets.back()) : nullptr;
+  };
+
+  auto        labellingObject = findObjectByRelationType(RelationType::LABELLED_BY);
+  std::string labeledByName   = labellingObject ? labellingObject->GetName() : "";
+
+  auto describedByObject = findObjectByRelationType(RelationType::DESCRIBED_BY);
 
   double currentValue     = 0.0;
   double minimumIncrement = 0.0;
   double maximumValue     = 0.0;
   double minimumValue     = 0.0;
-
-  auto* value = dynamic_cast<Dali::Accessibility::Value*>(self);
-  if(value)
+  auto*  valueInterface   = dynamic_cast<Dali::Accessibility::Value*>(self);
+  if(valueInterface)
   {
-    currentValue     = value->GetCurrent();
-    minimumIncrement = value->GetMinimumIncrement();
-    maximumValue     = value->GetMaximum();
-    minimumValue     = value->GetMinimum();
+    currentValue     = valueInterface->GetCurrent();
+    minimumIncrement = valueInterface->GetMinimumIncrement();
+    maximumValue     = valueInterface->GetMaximum();
+    minimumValue     = valueInterface->GetMinimum();
   }
 
-  auto    description             = self->GetDescription();
-  auto    indexInParent           = static_cast<int32_t>(self->GetIndexInParent());
-  bool    isSelectedInParent      = false;
-  bool    hasCheckBoxChild        = false;
   int32_t firstSelectedChildIndex = -1;
   int32_t selectedChildCount      = 0;
+  auto*   selfSelectionInterface  = dynamic_cast<Dali::Accessibility::Selection*>(self);
+  if(selfSelectionInterface)
+  {
+    selectedChildCount      = selfSelectionInterface->GetSelectedChildrenCount();
+    auto firstSelectedChild = selfSelectionInterface->GetSelectedChild(0);
+    if(firstSelectedChild)
+    {
+      firstSelectedChildIndex = firstSelectedChild->GetIndexInParent();
+    }
+  }
 
+  auto childCount       = static_cast<int32_t>(self->GetChildCount());
+  bool hasCheckBoxChild = false;
   for(auto i = 0u; i < static_cast<size_t>(childCount); ++i)
   {
-    auto q = self->GetChildAtIndex(i);
-    auto s = q->GetStates();
-    if(s[State::SELECTABLE])
+    auto child = self->GetChildAtIndex(i);
+    if(child->GetRole() == Role::CHECK_BOX)
     {
-      if(s[State::SELECTED])
-      {
-        ++selectedChildCount;
-        if(firstSelectedChildIndex < 0)
-          firstSelectedChildIndex = static_cast<int32_t>(i);
-      }
-    }
-    if(q->GetRole() == Role::CHECK_BOX)
       hasCheckBoxChild = true;
+      break;
+    }
+  }
+
+  auto    role              = static_cast<uint32_t>(self->GetRole());
+  int32_t listChildrenCount = 0;
+  if(role == static_cast<uint32_t>(Role::DIALOG))
+  {
+    listChildrenCount = GetItemCountOfFirstDescendantList(self);
+  }
+
+  auto*       textInterface         = dynamic_cast<Dali::Accessibility::Text*>(self);
+  std::string nameFromTextInterface = "";
+  if(textInterface)
+  {
+    nameFromTextInterface = textInterface->GetText(0, textInterface->GetCharacterCount());
   }
 
-  int32_t     listChildrenCount = 0;
-  Accessible* parent            = self->GetParent();
-  auto        parentStateSet    = parent ? parent->GetStates() : States{};
-  auto        parentChildCount  = parent ? static_cast<int32_t>(parent->GetChildCount()) : 0;
-  auto        parentRole        = static_cast<uint32_t>(parent ? parent->GetRole() : Role{});
-  Accessible* describedByObject = nullptr;
+  auto description       = self->GetDescription();
+  auto attributes        = self->GetAttributes();
+  auto states            = self->GetStates();
+  auto name              = self->GetName();
+  auto localizedRoleName = self->GetLocalizedRoleName();
+  auto indexInParent     = static_cast<int32_t>(self->GetIndexInParent());
+
+  auto  parent                   = self->GetParent();
+  auto  parentRole               = static_cast<uint32_t>(parent ? parent->GetRole() : Role{});
+  auto  parentChildCount         = parent ? static_cast<int32_t>(parent->GetChildCount()) : 0;
+  auto  parentStateSet           = parent ? parent->GetStates() : States{};
+  bool  isSelectedInParent       = false;
+  auto* parentSelectionInterface = dynamic_cast<Dali::Accessibility::Selection*>(parent);
+  if(parentSelectionInterface)
+  {
+    isSelectedInParent = parentSelectionInterface->IsChildSelected(indexInParent);
+  }
 
   return {
     attributes,
     name,
     labeledByName,
-    textIfceName,
+    nameFromTextInterface,
     role,
     states,
-    localizedName,
+    localizedRoleName,
     childCount,
     currentValue,
     minimumIncrement,
@@ -343,7 +437,7 @@ BridgeAccessible::ReadingMaterialType BridgeAccessible::GetReadingMaterial()
 
 void BridgeAccessible::SuppressScreenReader(bool suppress)
 {
-   suppressScreenReader = suppress;
+  suppressScreenReader = suppress;
 }
 
 DBus::ValueOrError<bool> BridgeAccessible::DoGesture(Dali::Accessibility::Gesture type, int32_t xBeg, int32_t yBeg, int32_t xEnd, int32_t yEnd, Dali::Accessibility::GestureState state, uint32_t eventTime)
@@ -398,7 +492,26 @@ Accessible* BridgeAccessible::GetCurrentlyHighlighted()
 
 std::vector<Accessible*> BridgeAccessible::ValidChildrenGet(const std::vector<Accessible*>& children, Accessible* start, Accessible* root)
 {
-  return children;
+  std::vector<Component*> vec;
+  std::vector<Accessible*> ret;
+
+  for(auto child : children)
+  {
+    if(auto* component = dynamic_cast<Component*>(child); component)
+    {
+      vec.push_back(component);
+    }
+  }
+
+  std::sort(vec.begin(), vec.end(), &SortVertically);
+
+  for(auto& line : SplitLines(vec))
+  {
+    std::sort(line.begin(), line.end(), &SortHorizontally);
+    ret.insert(ret.end(), line.begin(), line.end());
+  }
+
+  return ret;
 }
 
 static bool DeputyIs(Accessible* obj)
@@ -691,7 +804,7 @@ Accessible* BridgeAccessible::GetParent()
   // if you want more, then you need to change setApplicationRoot to
   // add/remove ApplicationRoot and make roots a vector.
   auto p = FindSelf()->GetParent();
-  assert(p);
+
   return p;
 }
 DBus::ValueOrError<std::vector<Accessible*>> BridgeAccessible::GetChildren()