Merge "Fix ImageView Padding and Margin issues" into devel/master
authorAdeel Kazmi <adeel.kazmi@samsung.com>
Thu, 5 Jul 2018 15:03:18 +0000 (15:03 +0000)
committerGerrit Code Review <gerrit@review.ap-northeast-2.compute.internal>
Thu, 5 Jul 2018 15:03:18 +0000 (15:03 +0000)
automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp
dali-toolkit/internal/controls/control/control-data-impl.cpp
dali-toolkit/internal/controls/image-view/image-view-impl.cpp
dali-toolkit/public-api/controls/control-impl.cpp

index c9cbdc5..02446a0 100644 (file)
@@ -26,6 +26,7 @@
 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
+#include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
 
 #include <test-native-image.h>
 #include <sstream>
@@ -1619,3 +1620,91 @@ int UtcDaliImageViewResourceReadySignalWithReusedImage02(void)
 
   END_TEST;
 }
+
+int UtcDaliImageViewPaddingProperty(void)
+{
+  ToolkitTestApplication application;
+
+  ImageView imageView = ImageView::New();
+  Property::Map imagePropertyMap;
+  imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
+  imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = TEST_RESOURCE_DIR "/gallery-small-1.jpg" ;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 128;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 128;
+  imageView.SetProperty( Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
+  imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  imageView.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  imageView.SetProperty( Control::Property::PADDING, Extents( 15, 10, 5, 10 ) );
+  Stage::GetCurrent().Add( imageView );
+
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Extents>( Control::Property::PADDING ), Extents( 15, 10, 5, 10 ), TEST_LOCATION );
+
+  ImageView childImage = ImageView::New();
+  childImage.SetBackgroundColor( Color::BLACK );
+  childImage.SetSize( 10.f, 10.f );
+  imageView.Add( childImage );
+
+  application.SendNotification();
+  application.Render();
+
+  // Child ImageView should be positioned dependinig on Parent ImageView's Padding value
+  DALI_TEST_EQUALS( childImage.GetProperty<Vector3>( Dali::Actor::Property::POSITION ), Vector3( 15, 5, 0 ), TEST_LOCATION );
+
+  // Check whether Image Visual transforms on ImageVieiw::OnRelayout()
+  Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView );
+  Toolkit::Visual::Base imageVisual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
+  Property::Map resultMap;
+  imageVisual.CreatePropertyMap( resultMap );
+
+  Property::Value* transformValue = resultMap.Find( Visual::Property::TRANSFORM );
+  DALI_TEST_CHECK( transformValue );
+  Property::Map* retMap = transformValue->GetMap();
+  DALI_TEST_CHECK( retMap );
+
+  // Image Visual should be positioned depending on ImageView's padding
+  DALI_TEST_EQUALS( retMap->Find( Visual::Transform::Property::OFFSET )->Get< Vector2 >(), Vector2( 15, 5 ), TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliImageViewPaddingProperty02(void)
+{
+  ToolkitTestApplication application;
+
+  ImageView imageView = ImageView::New();
+  Property::Map imagePropertyMap;
+  imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
+  imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = TEST_RESOURCE_DIR "/Kid1.svg" ;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 128;
+  imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 128;
+  imagePropertyMap[ DevelVisual::Property::VISUAL_FITTING_MODE ] = Toolkit::DevelVisual::FIT_KEEP_ASPECT_RATIO;
+  imageView.SetProperty( Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
+  imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  imageView.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  imageView.SetProperty( Control::Property::PADDING, Extents( 15, 10, 5, 10 ) );
+  Stage::GetCurrent().Add( imageView );
+
+  application.SendNotification();
+  application.Render();
+
+  DALI_TEST_EQUALS( imageView.GetProperty<Extents>( Control::Property::PADDING ), Extents( 15, 10, 5, 10 ), TEST_LOCATION );
+
+  // Check whether Image Visual transforms on ImageVieiw::OnRelayout()
+  Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView );
+  Toolkit::Visual::Base imageVisual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
+  Property::Map resultMap;
+  imageVisual.CreatePropertyMap( resultMap );
+
+  Property::Value* transformValue = resultMap.Find( Visual::Property::TRANSFORM );
+  DALI_TEST_CHECK( transformValue );
+  Property::Map* retMap = transformValue->GetMap();
+  DALI_TEST_CHECK( retMap );
+
+  // Image Visual should be positioned depending on ImageView's padding
+  DALI_TEST_EQUALS( retMap->Find( Visual::Transform::Property::OFFSET )->Get< Vector2 >(), Vector2( 15, 5 ), TEST_LOCATION );
+
+  END_TEST;
+}
index a6d30d9..cbeb4c5 100755 (executable)
@@ -1391,6 +1391,9 @@ void Control::Impl::OnStageDisconnection()
 void Control::Impl::SetMargin( Extents margin )
 {
   mControlImpl.mImpl->mMargin = margin;
+
+  // Trigger a size negotiation request that may be needed when setting a margin.
+  mControlImpl.RelayoutRequest();
 }
 
 Extents Control::Impl::GetMargin() const
@@ -1401,6 +1404,9 @@ Extents Control::Impl::GetMargin() const
 void Control::Impl::SetPadding( Extents padding )
 {
   mControlImpl.mImpl->mPadding = padding;
+
+  // Trigger a size negotiation request that may be needed when setting a padding.
+  mControlImpl.RelayoutRequest();
 }
 
 Extents Control::Impl::GetPadding() const
index 20869d3..720ef21 100755 (executable)
@@ -264,46 +264,47 @@ void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container )
   {
     Property::Map transformMap = Property::Map();
 
-    // Don't transform if fitting mode is FILL
-    if(Toolkit::GetImplementation(mVisual).GetFittingMode() == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO)
+    Extents padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
+
+    Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
+          Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get<int>() );
+
+    if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection )
     {
-      Extents padding;
-      padding = Self().GetProperty<Extents>( Toolkit::Control::Property::PADDING );
+      std::swap( padding.start, padding.end );
+    }
 
-      Dali::LayoutDirection::Type layoutDirection = static_cast<Dali::LayoutDirection::Type>(
-              Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get<int>());
+    auto finalOffset = Vector2( padding.start, padding.top );
 
-      if (Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection)
-      {
-        std::swap(padding.start, padding.end);
-      }
+    // remove padding from the size to know how much is left for the visual
+    auto finalSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom );
 
-      // remove padding from the size to know how much is left for the visual
-      auto paddedSize = size - Vector2(padding.start + padding.end, padding.top + padding.bottom);
+    // Should provide a transform that handles aspect ratio according to image size
+    if( Toolkit::GetImplementation(mVisual).GetFittingMode() == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO )
+    {
+      auto availableVisualSize = finalSize;
 
       Vector2 naturalSize;
-      mVisual.GetNaturalSize(naturalSize);
+      mVisual.GetNaturalSize( naturalSize );
 
       // scale to fit the padded area
-      auto finalSize =
-             naturalSize * std::min( ( naturalSize.width  ? ( paddedSize.width  / naturalSize.width  ) : 0 ),
-                                     ( naturalSize.height ? ( paddedSize.height / naturalSize.height ) : 0 ) );
+      finalSize = naturalSize * std::min( ( naturalSize.width  ? ( availableVisualSize.width  / naturalSize.width  ) : 0 ),
+                                          ( naturalSize.height ? ( availableVisualSize.height / naturalSize.height ) : 0 ) );
 
       // calculate final offset within the padded area
-      auto finalOffset = Vector2(padding.start, padding.top) + (paddedSize - finalSize) * .5f;
-
-      // populate the transform map
-      transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset)
-          .Add(Toolkit::Visual::Transform::Property::OFFSET_POLICY,
-              Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE))
-          .Add(Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN)
-          .Add(Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN)
-          .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize)
-          .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY,
-              Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE));
-
+      finalOffset += ( availableVisualSize - finalSize ) * .5f;
     }
-    // Should provide a transform that handles aspect ratio according to image size
+
+    // populate the transform map
+    transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset )
+                .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY,
+                    Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
+                .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
+                .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN )
+                .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize )
+                .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY,
+                    Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) );
+
     mVisual.SetTransformAndSize( transformMap, size );
   }
 }
index 8a79594..664c7e0 100755 (executable)
@@ -129,46 +129,6 @@ void CreateClippingRenderer( Control& controlImpl )
   }
 }
 
-/**
- * @brief Sets Control::Property::BACKGROUND visual
- * @param[in] controlImpl The control implementation
- * @param[in] visual The control background visual
- * @param[in] size The current size
- */
-void SetBackgroundVisual( Control::Impl& controlImpl, Toolkit::Visual::Base& visual, const Vector2& size )
-{
-  Property::Map transformMap = Property::Map();
-
-  Vector2 newSize( 0.f, 0.f );
-  newSize.width = size.width + ( controlImpl.mPadding.start + controlImpl.mPadding.end );
-  newSize.height = size.height + ( controlImpl.mPadding.top + controlImpl.mPadding.bottom );
-
-  if( ( controlImpl.mMargin.start != 0 ) ||
-      ( controlImpl.mMargin.end != 0 ) ||
-      ( controlImpl.mMargin.top != 0 ) ||
-      ( controlImpl.mMargin.bottom != 0 ) )
-  {
-    transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize )
-                .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
-                .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( controlImpl.mMargin.start, controlImpl.mMargin.top ) )
-                .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
-                .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
-                .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
-  }
-  else if( ( controlImpl.mPadding.start != 0 ) ||
-           ( controlImpl.mPadding.end != 0 ) ||
-           ( controlImpl.mPadding.top != 0 ) ||
-           ( controlImpl.mPadding.bottom != 0 ) )
-  {
-    transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize )
-                .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) )
-                .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN )
-                .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, Toolkit::Align::TOP_BEGIN );
-  }
-
-  visual.SetTransformAndSize( transformMap, newSize ); // Send an empty map as we do not want to modify the visual's set transform
-}
-
 } // unnamed namespace
 
 
@@ -659,8 +619,7 @@ void Control::OnSizeSet(const Vector3& targetSize)
   if( visual )
   {
     Vector2 size( targetSize );
-    SetBackgroundVisual( *mImpl, visual, size );
-
+    visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
   }
 }
 
@@ -739,7 +698,7 @@ void Control::OnRelayout( const Vector2& size, RelayoutContainer& container )
   Toolkit::Visual::Base visual = mImpl->GetVisual( Toolkit::Control::Property::BACKGROUND );
   if( visual )
   {
-    SetBackgroundVisual( *mImpl, visual, size );
+    visual.SetTransformAndSize( Property::Map(), size ); // Send an empty map as we do not want to modify the visual's set transform
   }
 }