X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fbuttons%2Fpush-button-impl.cpp;h=dea0ee03935b842f03b1e799965ef3da1d59ad52;hb=refs%2Fchanges%2F06%2F301106%2F9;hp=b6efdf808759d6d9878280d649e998d45b0da7c3;hpb=a8bc013df97fad9f1cc2d95c37248464ac553e6e;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp index b6efdf8..dea0ee0 100644 --- a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 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. @@ -19,69 +19,56 @@ #include "push-button-impl.h" // EXTERNAL INCLUDES -#include +#include +#include +#include #include -#include // INTERNAL INCLUDES +#include +#include +#include #include +#if defined(DEBUG_ENABLED) +extern Debug::Filter* gLogButtonFilter; +#endif + namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { - -const float TEXT_PADDING = 12.0f; -const float ANIMATION_TIME( 0.2f ); - BaseHandle Create() { return Toolkit::PushButton::New(); } -TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create ); +// Properties + +DALI_TYPE_REGISTRATION_BEGIN(Toolkit::PushButton, Toolkit::Button, Create) -const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-up.9.png"; -const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down.9.png"; -const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-disabled.9.png"; -const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down-disabled.9.png"; +DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING) +DALI_PROPERTY_REGISTRATION(Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING) + +DALI_TYPE_REGISTRATION_END() } // unnamed namespace namespace { - -/** - * Get size of Actor if larger than given size - * @param[in] root the actor to get the size of - * @param[out] size the greater of the given size or the size of the Actor - */ -void SizeOfActorIfLarger( Actor root, Vector3& size ) -{ - if ( root ) - { - // RelayoutSize retreived for Actor to use any padding set to it. - size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width ); - size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height ); - } -} - } // unnamed namespace Dali::Toolkit::PushButton PushButton::New() { // Create the implementation, temporarily owned on stack - IntrusivePtr< PushButton > internalPushButton = new PushButton(); + IntrusivePtr internalPushButton = new PushButton(); // Pass ownership to CustomActor - Dali::Toolkit::PushButton pushButton( *internalPushButton ); + Dali::Toolkit::PushButton pushButton(*internalPushButton); // Second-phase init of the implementation // This can only be done after the CustomActor connection has been made... @@ -92,257 +79,145 @@ Dali::Toolkit::PushButton PushButton::New() PushButton::PushButton() : Button(), - mSize() + mIconAlignment(RIGHT) { - SetAnimationTime( ANIMATION_TIME ); } PushButton::~PushButton() { } -void PushButton::OnButtonInitialize() +void PushButton::OnInitialize() { + Button::OnInitialize(); + // Push button requires the Leave event. Actor self = Self(); - self.SetLeaveRequired( true ); - - // Set resize policy to natural size so that buttons will resize to background images - self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS ); - - Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER ); - Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER ); - Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER ); - Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER ); + self.SetProperty(Actor::Property::LEAVE_REQUIRED, true); - SetButtonImage( ImageActor::New( buttonImage ) ); - SetSelectedImage( ImageActor::New( selectedImage ) ); - SetDisabledImage( ImageActor::New( disabledImage ) ); - SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) ); + self.SetProperty(DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::PUSH_BUTTON); } -void PushButton::OnLabelSet() +DevelControl::ControlAccessible* PushButton::CreateAccessibleObject() { - Actor& label = GetLabel(); + return new PushButtonAccessible(Self()); +} - if( label ) +void PushButton::SetIconAlignment(const PushButton::IconAlignment iconAlignment) +{ + mIconAlignment = iconAlignment; + Button::Align labelAlignment; + switch(iconAlignment) { - label.SetAnchorPoint( AnchorPoint::CENTER ); - label.SetParentOrigin( ParentOrigin::CENTER ); - - Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label ); - if( textLabel ) + case RIGHT: { - textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" ); - textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" ); - textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true ); + labelAlignment = Button::BEGIN; + break; } - - ConfigureSizeNegotiation(); + case TOP: + { + labelAlignment = Button::BOTTOM; + break; + } + case BOTTOM: + { + labelAlignment = Button::TOP; + break; + } + case LEFT: + default: + labelAlignment = Button::END; + break; } -} - -void PushButton::OnButtonImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} -void PushButton::OnSelectedImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); + Button::SetLabelAlignment(labelAlignment); } -void PushButton::OnBackgroundImageSet() +const PushButton::IconAlignment PushButton::GetIconAlignment() const { - ConfigureSizeNegotiation(); - RelayoutRequest(); + return mIconAlignment; } -void PushButton::OnSelectedBackgroundImageSet() +void PushButton::SetProperty(BaseObject* object, Property::Index propertyIndex, const Property::Value& value) { - ConfigureSizeNegotiation(); - RelayoutRequest(); -} + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object)); -void PushButton::OnDisabledImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnDisabledSelectedImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnDisabledBackgroundImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnSizeSet( const Vector3& targetSize ) -{ - if( targetSize != mSize ) + if(pushButton) { - mSize = targetSize; + PushButton& pushButtonImpl(GetImplementation(pushButton)); - Actor& label = GetLabel(); + // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class - if( label ) + switch(propertyIndex) { - label.SetSize( mSize ); + case Toolkit::PushButton::Property::LABEL_PADDING: + { + Vector4 padding(value.Get()); + pushButtonImpl.Button::SetLabelPadding(Padding(padding.x, padding.y, padding.z, padding.w)); + break; + } + case Toolkit::PushButton::Property::ICON_PADDING: + { + Vector4 padding(value.Get()); + pushButtonImpl.Button::SetForegroundPadding(Padding(padding.x, padding.y, padding.z, padding.w)); + break; + } } } } -void PushButton::PrepareForTranstionIn( Actor actor ) +Property::Value PushButton::GetProperty(BaseObject* object, Property::Index propertyIndex) { - actor.SetOpacity( 0.0f ); -} + Property::Value value; -void PushButton::PrepareForTranstionOut( Actor actor ) -{ - actor.SetOpacity( 1.0f ); -} + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast(Dali::BaseHandle(object)); -void PushButton::OnTransitionIn( Actor actor ) -{ - FadeImageTo( actor, 1.f ); -} - -void PushButton::OnTransitionOut( Actor actor ) -{ - FadeImageTo( actor, 0.0f ); -} - -void PushButton::FadeImageTo( Actor actor, float opacity ) -{ - if( actor ) + if(pushButton) { - Dali::Animation transitionAnimation = GetTransitionAnimation(); - DALI_ASSERT_DEBUG( transitionAnimation ); + PushButton& pushButtonImpl(GetImplementation(pushButton)); - if( transitionAnimation ) + switch(propertyIndex) { - transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity ); + case Toolkit::PushButton::Property::LABEL_PADDING: + { + Padding padding = pushButtonImpl.Button::GetLabelPadding(); + value = Vector4(padding.x, padding.y, padding.top, padding.bottom); + break; + } + case Toolkit::PushButton::Property::ICON_PADDING: + { + Padding padding = pushButtonImpl.Button::GetForegroundPadding(); + value = Vector4(padding.x, padding.y, padding.top, padding.bottom); + break; + } } } -} -Vector3 PushButton::GetNaturalSize() -{ - Vector3 size; - - // If label, test against it's size - Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() ); - if( label ) - { - Padding padding( 0.0f, 0.0f, 0.0f, 0.0f ); - label.GetPadding( padding ); - size = label.GetNaturalSize(); - size.width += padding.x + padding.width; - size.height += padding.y + padding.height; - } - else - { - // Check Image and Background image and use the largest size as the control's Natural size. - SizeOfActorIfLarger( GetButtonImage(), size ); - SizeOfActorIfLarger( GetBackgroundImage(), size ); - } - - return size; + return value; } -void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) +Dali::Accessibility::States PushButton::PushButtonAccessible::CalculateStates() { - ConfigureSizeNegotiation(); + auto state = Button::ButtonAccessible::CalculateStates(); + auto self = Toolkit::Button::DownCast(Self()); + state[Dali::Accessibility::State::PRESSED] = self.GetProperty(Toolkit::Button::Property::SELECTED); + return state; } -void PushButton::ConfigureSizeNegotiation() +void PushButton::OnStateChange(State newState) { - std::vector< Actor > images; - images.reserve( 7 ); - - images.push_back( GetButtonImage() ); - images.push_back( GetSelectedImage() ); - images.push_back( GetSelectedBackgroundImage() ); - images.push_back( GetBackgroundImage() ); - images.push_back( GetDisabledImage() ); - images.push_back( GetDisabledSelectedImage() ); - images.push_back( GetDisabledBackgroundImage() ); - - Actor label = GetLabel(); - - for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i ) + // TODO: replace it with OnPropertySet hook once Button::Property::SELECTED will be consistently used + if((Dali::Accessibility::Accessible::GetCurrentlyHighlightedActor() == Self()) && (newState == SELECTED_STATE || newState == UNSELECTED_STATE)) { - ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label ); - } - - if( label ) - { - Padding padding; - - if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE ) - { - padding.left = TEXT_PADDING; - padding.right = TEXT_PADDING; - } - - if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE ) + auto* accessible = GetAccessibleObject(); + if(DALI_LIKELY(accessible)) { - padding.top = TEXT_PADDING; - padding.bottom = TEXT_PADDING; - } - - label.SetPadding( padding ); - } -} - -void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label ) -{ - ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT; - ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT; + accessible->EmitStateChanged(Dali::Accessibility::State::PRESSED, newState == SELECTED_STATE ? 1 : 0, 0); - switch( Self().GetResizePolicy( dimension ) ) - { - case ResizePolicy::FIT_TO_CHILDREN: - { - imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; - break; - } - case ResizePolicy::USE_NATURAL_SIZE: - { - if( label ) - { - labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; - } - else + if(Self().GetProperty(Toolkit::Button::Property::TOGGLABLE)) { - imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + accessible->EmitStateChanged(Dali::Accessibility::State::CHECKED, newState == SELECTED_STATE ? 1 : 0, 0); } - break; - } - default: - { - break; - } - } - - if( label ) - { - label.SetResizePolicy( labelResizePolicy, dimension ); - } - - for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it ) - { - Actor actor = *it; - if( actor ) - { - actor.SetResizePolicy( imageResizePolicy, dimension ); } } }