Merge branch 'devel/master' into tizen_6.5
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / accessibility-impl.cpp
index dcf1149..774915f 100644 (file)
 #include <dali/public-api/object/object-registry.h>
 #include <dali/public-api/object/type-info.h>
 #include <dali/public-api/object/type-registry-helper.h>
+#include <dali/public-api/object/weak-handle.h>
 
 // INTERNAL INCLUDES
-#include <dali/public-api/dali-adaptor-common.h>
 #include <dali/devel-api/adaptor-framework/accessibility-impl.h>
+#include <dali/devel-api/adaptor-framework/window-devel.h>
 #include <dali/internal/adaptor/common/adaptor-impl.h>
+#include <dali/public-api/dali-adaptor-common.h>
 
 using namespace Dali::Accessibility;
 using namespace Dali;
@@ -50,7 +52,7 @@ std::string Accessible::GetLocalizedRoleName()
 
 std::string Accessible::GetRoleName()
 {
-  switch( GetRole() )
+  switch(GetRole())
   {
     case Role::INVALID:
     {
@@ -546,177 +548,222 @@ std::string Accessible::GetRoleName()
 
 Dali::Actor Accessible::GetCurrentlyHighlightedActor()
 {
-  return IsUp() ? Bridge::GetCurrentBridge()->data->currentlyHighlightedActor : Dali::Actor{};
+  return IsUp() ? Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor : Dali::Actor{};
 }
 
 void Accessible::SetCurrentlyHighlightedActor(Dali::Actor actor)
 {
-  if (IsUp())
+  if(IsUp())
   {
-    Bridge::GetCurrentBridge()->data->currentlyHighlightedActor = actor;
+    Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor = actor;
   }
 }
 
 Dali::Actor Accessible::GetHighlightActor()
 {
-  return IsUp() ? Bridge::GetCurrentBridge()->data->highlightActor : Dali::Actor{};
+  return IsUp() ? Bridge::GetCurrentBridge()->mData->mHighlightActor : Dali::Actor{};
 }
 
 void Accessible::SetHighlightActor(Dali::Actor actor)
 {
-  if (IsUp())
+  if(IsUp())
   {
-    Bridge::GetCurrentBridge()->data->highlightActor = actor;
+    Bridge::GetCurrentBridge()->mData->mHighlightActor = actor;
   }
 }
 
 void Bridge::ForceDown()
 {
   auto highlighted = Accessible::GetCurrentlyHighlightedActor();
-  if( highlighted )
+  if(highlighted)
   {
-    auto p = dynamic_cast< Component* >( Accessible::Get( highlighted ) );
-    if( p )
+    auto component = dynamic_cast<Component*>(Accessible::Get(highlighted));
+    if(component)
     {
-      p->ClearHighlight();
+      component->ClearHighlight();
     }
   }
-  data = {};
+  mData = {};
 }
 
-void Bridge::SetIsOnRootLevel( Accessible* o )
+void Bridge::SetIsOnRootLevel(Accessible* owner)
 {
-  o->isOnRootLevel = true;
+  owner->mIsOnRootLevel = true;
 }
 
 namespace
 {
-class NonControlAccessible : public virtual Accessible, public virtual Collection, public virtual Component
+class AdaptorAccessible : public virtual Accessible, public virtual Collection, public virtual Component
 {
-public:
-  Dali::Actor actor;
-  bool root = false;
+protected:
+  Dali::WeakHandle<Dali::Actor> mSelf;
+  bool                          mRoot = false;
+
+  Dali::Actor Self()
+  {
+    auto handle = mSelf.GetHandle();
 
-  NonControlAccessible( Dali::Actor actor, bool root ) : actor( actor ), root( root )
+    // AdaptorAccessible is deleted on ObjectDestroyedSignal
+    // for the respective actor (see `nonControlAccessibles`).
+    DALI_ASSERT_ALWAYS(handle);
+
+    return handle;
+  }
+
+public:
+  AdaptorAccessible(Dali::Actor actor, bool isRoot)
+  : mSelf(actor),
+    mRoot(isRoot)
   {
   }
 
-  Dali::Rect<> GetExtents( Dali::Accessibility::CoordType ctype ) override
+  Dali::Rect<> GetExtents(Dali::Accessibility::CoordinateType type) override
   {
-    Vector2 screenPosition = actor.GetProperty( Actor::Property::SCREEN_POSITION ).Get< Vector2 >();
-    Vector3 size = actor.GetCurrentProperty< Vector3 >( Actor::Property::SIZE ) * actor.GetCurrentProperty< Vector3 >( Actor::Property::WORLD_SCALE );
-    bool positionUsesAnchorPoint = actor.GetProperty( Actor::Property::POSITION_USES_ANCHOR_POINT ).Get< bool >();
-    Vector3 anchorPointOffSet = size * ( positionUsesAnchorPoint ? actor.GetCurrentProperty< Vector3 >( Actor::Property::ANCHOR_POINT ) : AnchorPoint::TOP_LEFT );
-    Vector2 position = Vector2( screenPosition.x - anchorPointOffSet.x, screenPosition.y - anchorPointOffSet.y );
+    Dali::Actor actor                   = Self();
+    Vector2     screenPosition          = actor.GetProperty(Actor::Property::SCREEN_POSITION).Get<Vector2>();
+    Vector3     size                    = actor.GetCurrentProperty<Vector3>(Actor::Property::SIZE) * actor.GetCurrentProperty<Vector3>(Actor::Property::WORLD_SCALE);
+    bool        positionUsesAnchorPoint = actor.GetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT).Get<bool>();
+    Vector3     anchorPointOffSet       = size * (positionUsesAnchorPoint ? actor.GetCurrentProperty<Vector3>(Actor::Property::ANCHOR_POINT) : AnchorPoint::TOP_LEFT);
+    Vector2     position                = Vector2(screenPosition.x - anchorPointOffSet.x, screenPosition.y - anchorPointOffSet.y);
 
-    return { position.x, position.y, size.x, size.y };
+    if(type == Dali::Accessibility::CoordinateType::WINDOW)
+    {
+      return {position.x, position.y, size.x, size.y};
+    }
+    else // Dali::Accessibility::CoordinateType::SCREEN
+    {
+      auto window = Dali::DevelWindow::Get(actor);
+      auto windowPosition = window.GetPosition();
+      return {position.x + windowPosition.GetX(), position.y + windowPosition.GetY(), size.x, size.y};
+    }
   }
+
   Dali::Accessibility::ComponentLayer GetLayer() override
   {
     return Dali::Accessibility::ComponentLayer::WINDOW;
   }
-  int16_t GetMdiZOrder()
+
+  int16_t GetMdiZOrder() override
   {
     return 0;
   }
-  double GetAlpha()
+
+  double GetAlpha() override
   {
     return 0;
   }
+
   bool GrabFocus() override
   {
     return false;
   }
+
   bool GrabHighlight() override
   {
     return false;
   }
+
   bool ClearHighlight() override
   {
     return false;
   }
+
   bool IsScrollable() override
   {
     return false;
   }
+
   std::string GetName() override
   {
-    return actor.GetProperty< std::string >( Dali::Actor::Property::NAME );
+    return Self().GetProperty<std::string>(Dali::Actor::Property::NAME);
   }
+
   std::string GetDescription() override
   {
     return "";
   }
+
   Accessible* GetParent() override
   {
-    if( GetIsOnRootLevel() )
+    if(IsOnRootLevel())
     {
-      auto b = GetBridgeData();
-      return b->bridge->GetApplication();
+      auto data = GetBridgeData();
+      return data->mBridge->GetApplication();
     }
-    return Get( actor.GetParent() );
+    return Get(Self().GetParent());
   }
+
   size_t GetChildCount() override
   {
-    return static_cast< size_t >( actor.GetChildCount() );
+    return static_cast<size_t>(Self().GetChildCount());
   }
-  Accessible* GetChildAtIndex( size_t index ) override
+
+  Accessible* GetChildAtIndex(size_t index) override
   {
-    auto s = static_cast< size_t >( actor.GetChildCount() );
-    if( index >= s )
+    auto numberOfChildren = static_cast<size_t>(Self().GetChildCount());
+    if(index >= numberOfChildren)
     {
-      throw std::domain_error{"invalid index " + std::to_string( index ) + " for object with " + std::to_string( s ) + " children"};
+      throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(numberOfChildren) + " children"};
     }
-    return Get( actor.GetChildAt( static_cast< unsigned int >( index ) ) );
+    return Get(Self().GetChildAt(static_cast<unsigned int>(index)));
   }
+
   size_t GetIndexInParent() override
   {
-    auto parent = actor.GetParent();
-    if( !parent )
+    auto parent = Self().GetParent();
+    if(!parent)
     {
       return 0;
     }
-    auto size = static_cast< size_t >( parent.GetChildCount() );
-    for( auto i = 0u; i < size; ++i )
+    auto size = static_cast<size_t>(parent.GetChildCount());
+    for(auto i = 0u; i < size; ++i)
     {
-      if( parent.GetChildAt( i ) == actor )
+      if(parent.GetChildAt(i) == Self())
       {
         return i;
       }
     }
     throw std::domain_error{"actor is not a child of it's parent"};
   }
+
   Role GetRole() override
   {
-    return root ? Role::WINDOW : Role::REDUNDANT_OBJECT;
+    return mRoot ? Role::WINDOW : Role::REDUNDANT_OBJECT;
   }
+
   States GetStates() override
   {
-    States s;
-    if( root )
+    States state;
+    if(mRoot)
     {
-      s[State::ENABLED] = true;
-      s[State::SENSITIVE] = true;
-      s[State::SHOWING] = true;
-      s[State::VISIBLE] = true;
-      s[State::ACTIVE] = true;
+      auto window = Dali::DevelWindow::Get(Self());
+      auto visible = window.IsVisible();
+      state[State::ENABLED]   = true;
+      state[State::SENSITIVE] = true;
+      state[State::SHOWING]   = visible;
+      state[State::VISIBLE]   = true;
+      state[State::ACTIVE]    = visible;
     }
     else
     {
-      auto t = GetParent()->GetStates();
-      s[State::SHOWING] = t[State::SHOWING];
-      s[State::VISIBLE] = t[State::VISIBLE];
+      auto parentState = GetParent()->GetStates();
+      state[State::SHOWING] = parentState[State::SHOWING];
+      state[State::VISIBLE] = parentState[State::VISIBLE];
     }
-    return s;
+    return state;
   }
+
   Attributes GetAttributes() override
   {
     Dali::TypeInfo type;
-    actor.GetTypeInfo( type );
-    return { {"t", type.GetName()}, };
+    Self().GetTypeInfo(type);
+    return
+    {
+      {"class", type.GetName()},
+    };
   }
 
-  bool DoGesture(const GestureInfo &gestureInfo) override
+  bool DoGesture(const GestureInfogestureInfo) override
   {
     return false;
   }
@@ -725,14 +772,19 @@ public:
   {
     return {};
   }
-};
 
-using NonControlAccessiblesType = std::unordered_map< const Dali::RefObject*, std::unique_ptr< NonControlAccessible > >;
+  Dali::Actor GetInternalActor() override
+  {
+    return mSelf.GetHandle();
+  }
+}; // AdaptorAccessible
 
-NonControlAccessiblesType nonControlAccessibles;
+using AdaptorAccessiblesType = std::unordered_map<const Dali::RefObject*, std::unique_ptr<AdaptorAccessible> >;
 
-std::function< Accessible*( Dali::Actor ) > convertingFunctor = []( Dali::Actor ) -> Accessible*
-{
+// Save RefObject from an Actor in Accessible::Get()
+AdaptorAccessiblesType gAdaptorAccessibles;
+
+std::function<Accessible*(Dali::Actor)> convertingFunctor = [](Dali::Actor) -> Accessible* {
   return nullptr;
 };
 
@@ -744,34 +796,34 @@ void Accessible::SetObjectRegistry(ObjectRegistry registry)
   objectRegistry = registry;
 }
 
-void Accessible::RegisterControlAccessibilityGetter( std::function< Accessible*( Dali::Actor ) > functor )
+void Accessible::RegisterExternalAccessibleGetter(std::function<Accessible*(Dali::Actor)> functor)
 {
   convertingFunctor = functor;
 }
 
-Accessible* Accessible::Get( Dali::Actor actor, bool root )
+Accessible* Accessible::Get(Dali::Actor actor, bool isRoot)
 {
-  if( !actor )
+  if(!actor)
   {
     return nullptr;
   }
-  auto accessible = convertingFunctor( actor );
-  if( !accessible )
+
+  auto accessible = convertingFunctor(actor);
+  if(!accessible)
   {
-    if( nonControlAccessibles.empty() && objectRegistry )
+    if(gAdaptorAccessibles.empty() && objectRegistry)
     {
-      objectRegistry.ObjectDestroyedSignal().Connect( []( const Dali::RefObject* obj )
-      {
-        nonControlAccessibles.erase( obj );
-      }
-      );
+      objectRegistry.ObjectDestroyedSignal().Connect([](const Dali::RefObject* obj) {
+        gAdaptorAccessibles.erase(obj);
+      });
     }
-    auto it = nonControlAccessibles.emplace( &actor.GetBaseObject(), nullptr );
-    if( it.second )
+    auto pair = gAdaptorAccessibles.emplace(&actor.GetBaseObject(), nullptr);
+    if(pair.second)
     {
-      it.first->second.reset( new NonControlAccessible( actor, root ) );
+      pair.first->second.reset(new AdaptorAccessible(actor, isRoot));
     }
-    accessible = it.first->second.get();
+    accessible = pair.first->second.get();
   }
+
   return accessible;
 }