From f2c30909a8fc08fcfc372bdae50a6d3c1791cd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sat, 27 May 2023 00:25:36 -0300 Subject: [PATCH] eepp: Improvements and fixes to the StackLayout. --- include/eepp/ui/css/propertydefinition.hpp | 1 + include/eepp/ui/uistacklayout.hpp | 17 ++++- src/eepp/ui/css/stylesheetspecification.cpp | 3 + src/eepp/ui/uistacklayout.cpp | 69 ++++++++++++++++----- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/include/eepp/ui/css/propertydefinition.hpp b/include/eepp/ui/css/propertydefinition.hpp index ef54837ef..c79ca36c3 100644 --- a/include/eepp/ui/css/propertydefinition.hpp +++ b/include/eepp/ui/css/propertydefinition.hpp @@ -214,6 +214,7 @@ enum class PropertyId : Uint32 { InnerWidgetOrientation = String::hash( "inner-widget-orientation" ), Glyph = String::hash( "glyph" ), Name = String::hash( "name" ), + RowValign = String::hash( "row-valign" ), }; enum class PropertyType : Uint32 { diff --git a/include/eepp/ui/uistacklayout.hpp b/include/eepp/ui/uistacklayout.hpp index 2e75252d9..97e03357c 100644 --- a/include/eepp/ui/uistacklayout.hpp +++ b/include/eepp/ui/uistacklayout.hpp @@ -7,6 +7,8 @@ namespace EE { namespace UI { class EE_API UIStackLayout : public UILayout { public: + enum class RowValign { Top, Center, Bottom }; + static UIStackLayout* New(); static UIStackLayout* NewWithTag( const std::string& tag = "stacklayout" ); @@ -24,15 +26,24 @@ class EE_API UIStackLayout : public UILayout { void updateLayout(); - protected: - UIStackLayout(); + const RowValign& getRowValign() const; - virtual Uint32 onMessage( const NodeMessage* Msg ); + void setRowValign( const RowValign& rowValign ); + + protected: + RowValign mRowValign{ RowValign::Bottom }; + + UIStackLayout(); explicit UIStackLayout( const std::string& tag ); + virtual Uint32 onMessage( const NodeMessage* Msg ); + void applySizePolicyOnChilds(); + void setRowValign( const std::string& rowValign ); + + static std::string rowValignToStr( const RowValign& rowValign ); }; }} // namespace EE::UI diff --git a/src/eepp/ui/css/stylesheetspecification.cpp b/src/eepp/ui/css/stylesheetspecification.cpp index be1e3b8f7..a46966cb2 100644 --- a/src/eepp/ui/css/stylesheetspecification.cpp +++ b/src/eepp/ui/css/stylesheetspecification.cpp @@ -402,6 +402,9 @@ void StyleSheetSpecification::registerDefaultProperties() { registerProperty( "glyph", "" ).setType( PropertyType::String ); registerProperty( "name", "" ).setType( PropertyType::String ); + registerProperty( "row-valign", "" ) + .addAlias( "row-vertical-align" ) + .setType( PropertyType::String ); // Shorthands registerShorthand( "margin", { "margin-top", "margin-right", "margin-bottom", "margin-left" }, diff --git a/src/eepp/ui/uistacklayout.cpp b/src/eepp/ui/uistacklayout.cpp index dbd80ac06..48cd0453f 100644 --- a/src/eepp/ui/uistacklayout.cpp +++ b/src/eepp/ui/uistacklayout.cpp @@ -10,14 +10,12 @@ UIStackLayout* UIStackLayout::New() { return eeNew( UIStackLayout, () ); } -UIStackLayout::UIStackLayout() : UILayout( "stacklayout" ) { - mFlags |= UI_OWNS_CHILDS_POSITION; - setClipType( ClipType::ContentBox ); -} +UIStackLayout::UIStackLayout() : UIStackLayout( "stacklayout" ) {} UIStackLayout::UIStackLayout( const std::string& tag ) : UILayout( tag ) { mFlags |= UI_OWNS_CHILDS_POSITION; setClipType( ClipType::ContentBox ); + setGravity( UI_HALIGN_LEFT | UI_VALIGN_TOP ); } Uint32 UIStackLayout::getType() const { @@ -80,6 +78,28 @@ void UIStackLayout::applySizePolicyOnChilds() { } } +void UIStackLayout::setRowValign( const std::string& rowValign ) { + if ( rowValign == "top" ) { + setRowValign( UIStackLayout::RowValign::Top ); + } else if ( rowValign == "center" ) { + setRowValign( UIStackLayout::RowValign::Center ); + } else if ( rowValign == "bottom" ) { + setRowValign( UIStackLayout::RowValign::Bottom ); + } +} + +std::string UIStackLayout::rowValignToStr( const RowValign& rowValign ) { + switch ( rowValign ) { + case UIStackLayout::RowValign::Top: + return "top"; + case UIStackLayout::RowValign::Center: + return "center"; + case UIStackLayout::RowValign::Bottom: + default: + return "bottom"; + } +} + std::string UIStackLayout::getPropertyString( const PropertyDefinition* propertyDef, const Uint32& propertyIndex ) const { if ( NULL == propertyDef ) @@ -88,6 +108,8 @@ std::string UIStackLayout::getPropertyString( const PropertyDefinition* property switch ( propertyDef->getPropertyId() ) { case PropertyId::GravityOwner: return isGravityOwner() ? "true" : "false"; + case PropertyId::RowValign: + return rowValignToStr( mRowValign ); default: return UILayout::getPropertyString( propertyDef, propertyIndex ); } @@ -95,7 +117,7 @@ std::string UIStackLayout::getPropertyString( const PropertyDefinition* property std::vector UIStackLayout::getPropertiesImplemented() const { auto props = UILayout::getPropertiesImplemented(); - auto local = { PropertyId::GravityOwner }; + auto local = { PropertyId::GravityOwner, PropertyId::RowValign }; props.insert( props.end(), local.begin(), local.end() ); return props; } @@ -109,6 +131,10 @@ bool UIStackLayout::applyProperty( const StyleSheetProperty& attribute ) { setGravityOwner( attribute.asBool() ); break; } + case PropertyId::RowValign: { + setRowValign( attribute.value() ); + break; + } default: return UILayout::applyProperty( attribute ); } @@ -200,13 +226,6 @@ void UIStackLayout::updateLayout() { } totHeight += mPaddingPx.Bottom; - if ( getLayoutHeightPolicy() == SizePolicy::WrapContent ) { - if ( totHeight != (int)getPixelsSize().getHeight() ) { - setInternalPixelsHeight( totHeight ); - notifyLayoutAttrChangeParent(); - } - } - for ( const auto& line : lines ) { Float xDisplacement = 0.f; Float yDisplacement = 0.f; @@ -251,15 +270,16 @@ void UIStackLayout::updateLayout() { break; } - switch ( Font::getVerticalAlign( widget->getLayoutGravity() ) ) { - case UI_VALIGN_CENTER: + switch ( mRowValign ) { + case UIStackLayout::RowValign::Center: pos.y = yDisplacement + maxY + eeceil( ( line.maxY - widget->getPixelsSize().getHeight() ) * 0.5f ); break; - case UI_VALIGN_BOTTOM: - pos.y = yDisplacement + maxY + line.maxY - widget->getPixelsSize().getHeight(); + case UIStackLayout::RowValign::Bottom: + pos.y = yDisplacement + maxY + line.maxY - widget->getPixelsSize().getHeight() - + widget->getLayoutPixelsMargin().Bottom; break; - case UI_VALIGN_TOP: + case UIStackLayout::RowValign::Top: default: pos.y = yDisplacement + maxY + widget->getLayoutPixelsMargin().Top; break; @@ -271,6 +291,13 @@ void UIStackLayout::updateLayout() { maxY += line.maxY; } + if ( getLayoutHeightPolicy() == SizePolicy::WrapContent ) { + if ( totHeight != (int)getPixelsSize().getHeight() ) { + setInternalPixelsHeight( totHeight ); + notifyLayoutAttrChangeParent(); + } + } + if ( getParent()->isUINode() && ( !getParent()->asType()->ownsChildPosition() || isGravityOwner() ) ) { alignAgainstLayout(); @@ -280,4 +307,12 @@ void UIStackLayout::updateLayout() { mDirtyLayout = false; } +const UIStackLayout::RowValign& UIStackLayout::getRowValign() const { + return mRowValign; +} + +void UIStackLayout::setRowValign( const RowValign& rowValign ) { + mRowValign = rowValign; +} + }} // namespace EE::UI