From 4f92560914453c5b7add614f4d97f8b2631435d6 Mon Sep 17 00:00:00 2001 From: eugenytarasov Date: Mon, 29 Aug 2022 10:37:44 +0000 Subject: [PATCH 1/4] !4 Add ScreenSystemManager * GridSystemManager remove redundant api * Add ScreenSystemManager * fix build for screen_system_ manager feature Signed-off-by: Eugeny Tarasov --- .../jsview/js_view_abstract.cpp | 10 +- .../common/layout/grid_column_info.cpp | 86 ++-------- .../common/layout/grid_column_info.h | 84 ++++------ .../common/layout/grid_layout_info.h | 10 +- .../common/layout/grid_system_manager.cpp | 38 ++--- .../common/layout/grid_system_manager.h | 34 ++-- .../common/layout/screen_system_manager.h | 157 ++++++++++++++++++ frameworks/core/pipeline/pipeline_context.cpp | 6 +- 8 files changed, 240 insertions(+), 185 deletions(-) create mode 100644 frameworks/core/components/common/layout/screen_system_manager.h diff --git a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp index f2ff7c5e81..bf44e4996f 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_view_abstract.cpp @@ -54,6 +54,7 @@ #include "core/components/box/box_component_helper.h" #include "core/components/common/layout/align_declaration.h" #include "core/components/common/layout/position_param.h" +#include "core/components/common/layout/screen_system_manager.h" #include "core/components/common/properties/motion_path_option.h" #include "core/components/menu/menu_component.h" #include "core/components/option/option_component.h" @@ -3777,16 +3778,17 @@ void JSViewAbstract::JsUseSizeType(const JSCallbackInfo& info) } auto builder = ViewStackProcessor::GetInstance()->GetBoxComponent()->GetGridColumnInfoBuilder(); builder->SetParent(gridContainerInfo); - for (uint32_t i = 1; i < sizeof(keys) / sizeof(const char*); i++) { - JSRef val = sizeObj->GetProperty(keys[i]); + + for (auto values: SCREEN_SIZE_VALUES) { + JSRef val = sizeObj->GetProperty(values.second.c_str()); if (val->IsNull() || val->IsEmpty()) { continue; } uint32_t span = 0; int32_t offset = 0; if (ParseSpanAndOffset(val, span, offset)) { - builder->SetSizeColumn(static_cast(i), span); - builder->SetOffset(offset, static_cast(i)); + builder->SetSizeColumn(values.first, span); + builder->SetOffset(offset, values.first); } } } diff --git a/frameworks/core/components/common/layout/grid_column_info.cpp b/frameworks/core/components/common/layout/grid_column_info.cpp index 353379c901..7328b0d663 100644 --- a/frameworks/core/components/common/layout/grid_column_info.cpp +++ b/frameworks/core/components/common/layout/grid_column_info.cpp @@ -18,26 +18,15 @@ #include "base/log/log.h" #include "core/components/common/layout/grid_system_manager.h" -namespace { -constexpr int32_t INVALID_OFFSET = -1; -} namespace OHOS::Ace { /* set offset by grid column number */ void GridColumnInfo::Builder::SetOffset(int32_t offset, GridSizeType type) { - if (!columnInfo_->hasColumnOffset_) { - for (uint8_t i = 0; i < sizeof(columnInfo_->offsets_) / sizeof(int32_t); i++) { - columnInfo_->offsets_[i] = INVALID_OFFSET; - } - columnInfo_->hasColumnOffset_ = true; - } - - const int32_t arraySize = static_cast(GridSizeType::XL); - int32_t typeVal = static_cast(type); - if (typeVal < arraySize) { - columnInfo_->offsets_[typeVal] = offset; + columnInfo_->hasColumnOffset_ = true; + if (isValid(type)) { + columnInfo_->offsets_[type] = offset; } } @@ -47,29 +36,9 @@ double GridColumnInfo::GetWidth() const LOGE("no parent info"); return 0.0; } - - uint32_t columns = 0; auto sizeType = parent_->GetSizeType(); - switch (sizeType) { - case GridSizeType::XS: - columns = xsSizeColumn_ > 0 ? xsSizeColumn_ : columns_; - break; - case GridSizeType::SM: - columns = smSizeColumn_ > 0 ? smSizeColumn_ : columns_; - break; - case GridSizeType::MD: - columns = mdSizeColumn_ > 0 ? mdSizeColumn_ : columns_; - break; - case GridSizeType::LG: - columns = lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; - break; - case GridSizeType::XL: - columns = lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; - break; - default: - break; - } - return (columns == 0) ? 0.0 : GetWidth(columns); + uint32_t columns = isValid(sizeType) ? columns_[sizeType] : 0; + return (columns <= 0) ? 0.0 : GetWidth(columns); } double GridColumnInfo::GetWidth(uint32_t columns) const @@ -93,21 +62,11 @@ double GridColumnInfo::GetMaxWidth() const uint32_t columns = 0; auto sizeType = parent_->GetSizeType(); - switch (sizeType) { - case GridSizeType::XS: - columns = xsSizeColumn_ > 0 ? xsSizeColumn_ : columns_; - break; - case GridSizeType::SM: - columns = smSizeMaxColumn_ > 0 ? smSizeMaxColumn_ : smSizeColumn_ > 0 ? smSizeColumn_ : columns_; - break; - case GridSizeType::MD: - columns = mdSizeMaxColumn_ > 0 ? mdSizeMaxColumn_ : mdSizeColumn_ > 0 ? mdSizeColumn_ : columns_; - break; - case GridSizeType::LG: - columns = lgSizeMaxColumn_ > 0 ? lgSizeMaxColumn_ : lgSizeColumn_ > 0 ? lgSizeColumn_ : columns_; - break; - default: - break; + if (isValid(sizeType)) { + columns = maxColumns_[sizeType]; + if (columns == 0) { + columns = columns_[sizeType]; + } } return GetWidth(columns); @@ -121,36 +80,19 @@ Dimension GridColumnInfo::GetOffset() const } /* ace1.0 obsolete logic since 6 */ + auto sizeType = parent_->GetSizeType(); if (!hasColumnOffset_) { - Dimension dim = UNDEFINED_DIMENSION; - switch (parent_->GetSizeType()) { - case GridSizeType::XS: - dim = xsSizeOffset_; - break; - case GridSizeType::SM: - dim = smSizeOffset_; - break; - case GridSizeType::MD: - dim = mdSizeOffset_; - break; - case GridSizeType::LG: - dim = lgSizeOffset_; - break; - default: - break; - } - return dim; + return isValid(sizeType) ? dimOffsets_[sizeType] : UNDEFINED_DIMENSION; } /* ace2.0 */ - int32_t sizeType = static_cast(parent_->GetSizeType()); int32_t offset = INVALID_OFFSET; - if (sizeType < static_cast(GridSizeType::XL)) { + if (isValid(sizeType)) { offset = offsets_[sizeType]; } if (offset == INVALID_OFFSET) { - offset = offsets_[static_cast(GridSizeType::UNDEFINED)]; // use common offset + offset = offsets_[GridSizeType::UNDEFINED]; // use common offset } if (offset == INVALID_OFFSET) { return UNDEFINED_DIMENSION; diff --git a/frameworks/core/components/common/layout/grid_column_info.h b/frameworks/core/components/common/layout/grid_column_info.h index c02a547978..70c8a1dab9 100644 --- a/frameworks/core/components/common/layout/grid_column_info.h +++ b/frameworks/core/components/common/layout/grid_column_info.h @@ -17,6 +17,7 @@ #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_GRID_COLUMN_INFO_H #include "core/components/common/layout/grid_container_info.h" +#include "core/components/common/layout/screen_system_manager.h" namespace OHOS::Ace { @@ -34,59 +35,50 @@ public: } void SetXsSizeColumn(uint32_t xsSizeColumn, const Dimension& offset = UNDEFINED_DIMENSION) { - columnInfo_->xsSizeColumn_ = xsSizeColumn; - columnInfo_->xsSizeOffset_ = offset; + columnInfo_->columns_[ScreenSizeType::XS] = xsSizeColumn; + columnInfo_->dimOffsets_[ScreenSizeType::XS] = offset; } void SetSmSizeColumn(uint32_t smSizeColumn, const Dimension& offset = UNDEFINED_DIMENSION) { - columnInfo_->smSizeColumn_ = smSizeColumn; - columnInfo_->smSizeOffset_ = offset; + columnInfo_->columns_[ScreenSizeType::SM] = smSizeColumn; + columnInfo_->dimOffsets_[ScreenSizeType::SM] = offset; } void SetMdSizeColumn(uint32_t mdSizeColumn, const Dimension& offset = UNDEFINED_DIMENSION) { - columnInfo_->mdSizeColumn_ = mdSizeColumn; - columnInfo_->mdSizeOffset_ = offset; + columnInfo_->columns_[ScreenSizeType::MD] = mdSizeColumn; + columnInfo_->dimOffsets_[ScreenSizeType::MD] = offset; } void SetLgSizeColumn(uint32_t lgSizeColumn, const Dimension& offset = UNDEFINED_DIMENSION) { - columnInfo_->lgSizeColumn_ = lgSizeColumn; - columnInfo_->lgSizeOffset_ = offset; + columnInfo_->columns_[ScreenSizeType::LG] = lgSizeColumn; + columnInfo_->dimOffsets_[ScreenSizeType::LG] = offset; } void SetSizeColumn(GridSizeType type, uint32_t column, const Dimension& offset = UNDEFINED_DIMENSION) { - if (type == GridSizeType::XS) { - SetXsSizeColumn(column, offset); - } else if (type == GridSizeType::SM) { - SetSmSizeColumn(column, offset); - } else if (type == GridSizeType::MD) { - SetMdSizeColumn(column, offset); - } else if (type == GridSizeType::LG) { - SetLgSizeColumn(column, offset); - } else if (type == GridSizeType::UNDEFINED) { - SetColumns(column); - } + columnInfo_->columns_[type] = column; + columnInfo_->dimOffsets_[type] = offset; } void SetSmSizeMaxColumn(uint32_t smSizeMaxColumn) { - columnInfo_->smSizeMaxColumn_ = smSizeMaxColumn; + columnInfo_->maxColumns_[ScreenSizeType::SM] = smSizeMaxColumn; } void SetMdSizeMaxColumn(uint32_t mdSizeMaxColumn) { - columnInfo_->mdSizeMaxColumn_ = mdSizeMaxColumn; + columnInfo_->maxColumns_[ScreenSizeType::MD] = mdSizeMaxColumn; } void SetLgSizeMaxColumn(uint32_t lgSizeMaxColumn) { - columnInfo_->lgSizeMaxColumn_ = lgSizeMaxColumn; + columnInfo_->maxColumns_[ScreenSizeType::LG] = lgSizeMaxColumn; } void SetColumns(uint32_t columns) { - columnInfo_->columns_ = columns; + columnInfo_->columns_[ScreenSizeType::UNDEFINED] = columns; } void ACE_EXPORT SetOffset(int32_t offset, GridSizeType type = GridSizeType::UNDEFINED); @@ -117,51 +109,31 @@ public: } uint32_t GetColumns() const { - return columns_; + return columns_[ScreenSizeType::UNDEFINED]; } int32_t GetOffset(GridSizeType type) const { - return offsets_[static_cast(type)]; + return offsets_[type]; } uint32_t GetColumns(GridSizeType type) const { - switch (type) { - case GridSizeType::UNDEFINED: - return columns_; - case GridSizeType::XS: - return xsSizeColumn_; - case GridSizeType::SM: - return smSizeColumn_; - case GridSizeType::MD: - return mdSizeColumn_; - case GridSizeType::LG: - return lgSizeColumn_; - default: - return DEFAULT_GRID_COLUMN_SPAN; - } + return isValid(type) ? columns_[type] : DEFAULT_GRID_COLUMN_SPAN; } private: - GridColumnInfo() = default; - - uint32_t xsSizeColumn_ = 0; - uint32_t smSizeColumn_ = 0; - uint32_t mdSizeColumn_ = 0; - uint32_t lgSizeColumn_ = 0; - - Dimension xsSizeOffset_ = UNDEFINED_DIMENSION; - Dimension smSizeOffset_ = UNDEFINED_DIMENSION; - Dimension mdSizeOffset_ = UNDEFINED_DIMENSION; - Dimension lgSizeOffset_ = UNDEFINED_DIMENSION; + static constexpr int32_t INVALID_OFFSET = -1; - uint32_t smSizeMaxColumn_ = 0; - uint32_t mdSizeMaxColumn_ = 0; - uint32_t lgSizeMaxColumn_ = 0; + GridColumnInfo() + { + // default column and offset which no define column of the size + columns_[ScreenSizeType::UNDEFINED] = DEFAULT_GRID_COLUMN_SPAN; + } - // default column and offset which no define column of the size - uint32_t columns_ = DEFAULT_GRID_COLUMN_SPAN; bool hasColumnOffset_ = false; - int32_t offsets_[static_cast(GridSizeType::XL)] = { 0 }; + ArrayByScreenType columns_ {0}; + ArrayByScreenType maxColumns_ {0}; + ArrayByScreenType dimOffsets_ {UNDEFINED_DIMENSION}; + ArrayByScreenType offsets_ {INVALID_OFFSET}; // parent container grid infos RefPtr parent_; }; diff --git a/frameworks/core/components/common/layout/grid_layout_info.h b/frameworks/core/components/common/layout/grid_layout_info.h index fe446f13a5..0404289e05 100644 --- a/frameworks/core/components/common/layout/grid_layout_info.h +++ b/frameworks/core/components/common/layout/grid_layout_info.h @@ -17,17 +17,11 @@ #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_GRID_LAYOUT_INFO_H #include "base/memory/ace_type.h" +#include "core/components/common/layout/screen_system_manager.h" namespace OHOS::Ace { -enum class GridSizeType { - UNDEFINED = 0, - XS, - SM, - MD, - LG, - XL, -}; +using GridSizeType = ScreenSizeType; enum class GridTemplateType { NORMAL = 0, diff --git a/frameworks/core/components/common/layout/grid_system_manager.cpp b/frameworks/core/components/common/layout/grid_system_manager.cpp index 0208ea6f22..6d5d86487b 100644 --- a/frameworks/core/components/common/layout/grid_system_manager.cpp +++ b/frameworks/core/components/common/layout/grid_system_manager.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "core/components/common/layout/grid_column_info.h" #include "core/components/common/layout/grid_system_manager.h" #include @@ -24,9 +25,9 @@ namespace OHOS::Ace { namespace { -constexpr Dimension COLUMN_2_AND_4_BREAKPOINT = 320.0_vp; -constexpr Dimension COLUMN_4_AND_8_BREAKPOINT = 600.0_vp; -constexpr Dimension COLUMN_8_AND_12_BREAKPOINT = 840.0_vp; +constexpr Dimension COLUMN_2_AND_4_BREAKPOINT = MAX_SCREEN_WIDTH_SM; +constexpr Dimension COLUMN_4_AND_8_BREAKPOINT = MAX_SCREEN_WIDTH_MD; +constexpr Dimension COLUMN_8_AND_12_BREAKPOINT = MAX_SCREEN_WIDTH_LG; constexpr Dimension SMALL_GUTTER = 12.0_vp; constexpr Dimension SMALL_MARGIN = 12.0_vp; constexpr Dimension LARGE_GUTTER = 24.0_vp; @@ -145,44 +146,41 @@ SystemGridInfo GridSystemManager::GetSystemGridInfo(const GridSizeType& sizeType return SystemGridInfo(); } -void GridSystemManager::OnSurfaceChanged(double width) +const SystemGridInfo& GridSystemManager::GetCurrentGridInfo() { - screenWidth_ = width; - if (width < COLUMN_2_AND_4_BREAKPOINT.Value() * density_) { - currentSize_ = GridSizeType::XS; + const auto width = GetScreenWidth(); + const auto density = GetDensity(); + if (width < COLUMN_2_AND_4_BREAKPOINT.Value() * density) { systemGridInfo_ = GRID_COLUMNS_2; - } else if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * density_) { - currentSize_ = GridSizeType::SM; + } else if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * density) { systemGridInfo_ = GRID_COLUMNS_4; - } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * density_) { - currentSize_ = GridSizeType::MD; + } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * density) { systemGridInfo_ = GRID_COLUMNS_8; } else { - currentSize_ = GridSizeType::LG; systemGridInfo_ = GRID_COLUMNS_12; } - currentSize_ = systemGridInfo_.sizeType; - - LOGD("OnSurfaceChanged: %{public}f: sizeType = %{public}d", width, systemGridInfo_.sizeType); + LOGD("GetCurrentGridInfo: %{public}f: sizeType = %{public}d", width, systemGridInfo_.sizeType); + return systemGridInfo_; } SystemGridInfo GridSystemManager::GetSystemGridInfo(const GridTemplateType& templateType, double width) { // Input width is normalized in px. + const auto dipScale = GetDipScale(); if (templateType == GridTemplateType::NORMAL) { - if (width < COLUMN_2_AND_4_BREAKPOINT.Value() * dipScale_) { + if (width < COLUMN_2_AND_4_BREAKPOINT.Value() * dipScale) { return GRID_COLUMNS_2; - } else if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * dipScale_) { + } else if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * dipScale) { return GRID_COLUMNS_4; - } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * dipScale_) { + } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * dipScale) { return GRID_COLUMNS_8; } else { return GRID_COLUMNS_12; } } - if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * dipScale_) { + if (width < COLUMN_4_AND_8_BREAKPOINT.Value() * dipScale) { return GRID_TEMPLATE_COLUMNS_4; - } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * dipScale_) { + } else if (width < COLUMN_8_AND_12_BREAKPOINT.Value() * dipScale) { return GRID_TEMPLATE_COLUMNS_8; } else { return GRID_TEMPLATE_COLUMNS_12; diff --git a/frameworks/core/components/common/layout/grid_system_manager.h b/frameworks/core/components/common/layout/grid_system_manager.h index 2a233c66a2..c74472fcc9 100644 --- a/frameworks/core/components/common/layout/grid_system_manager.h +++ b/frameworks/core/components/common/layout/grid_system_manager.h @@ -20,10 +20,12 @@ #include "base/geometry/dimension.h" #include "base/memory/ace_type.h" -#include "core/components/common/layout/grid_column_info.h" +#include "core/components/common/layout/grid_layout_info.h" +#include "core/components/common/layout/screen_system_manager.h" namespace OHOS::Ace { +class GridColumnInfo; struct SystemGridInfo { SystemGridInfo() = default; SystemGridInfo( @@ -47,37 +49,30 @@ public: ~GridSystemManager() = default; ACE_EXPORT static GridSystemManager& GetInstance(); - const SystemGridInfo& GetCurrentGridInfo() - { - return systemGridInfo_; - } + const SystemGridInfo& GetCurrentGridInfo(); static SystemGridInfo GetSystemGridInfo(const GridSizeType& sizeType); static RefPtr GetInfoByType(const GridColumnType& type); // width is use px unit. SystemGridInfo GetSystemGridInfo(const GridTemplateType& templateType, double width); - void OnSurfaceChanged(double width); - void SetWindowInfo(double screenWidth, double density, double dipScale) - { - screenWidth_ = screenWidth; - density_ = density; - dipScale_ = dipScale; - viewScale_ = density / dipScale; - } - double GetScreenWidth() const { - return screenWidth_; + return ScreenSystemManager::GetInstance().GetScreenWidth(); } double GetDipScale() const { - return dipScale_; + return ScreenSystemManager::GetInstance().GetDipScale(); } GridSizeType GetCurrentSize() const { - return currentSize_; + return ScreenSystemManager::GetInstance().GetCurrentSize(); + } + + double GetDensity() const + { + return ScreenSystemManager::GetInstance().GetDensity(); } private: @@ -86,11 +81,6 @@ private: static GridSystemManager* instance_; static std::mutex mutex_; - double screenWidth_ = 0.0; - double density_ = 1.0; - double dipScale_ = 1.0; - double viewScale_ = 1.0; - GridSizeType currentSize_ = GridSizeType::UNDEFINED; SystemGridInfo systemGridInfo_; ACE_DISALLOW_COPY_AND_MOVE(GridSystemManager); diff --git a/frameworks/core/components/common/layout/screen_system_manager.h b/frameworks/core/components/common/layout/screen_system_manager.h new file mode 100644 index 0000000000..5137ef7ab0 --- /dev/null +++ b/frameworks/core/components/common/layout/screen_system_manager.h @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H +#define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H + +#include +#include +#include +#include "base/geometry/dimension.h" +#include "base/log/log.h" +#include "base/utils/noncopyable.h" + +namespace OHOS::Ace { + +constexpr Dimension MAX_SCREEN_WIDTH_SM = 320.0_vp; +constexpr Dimension MAX_SCREEN_WIDTH_MD = 600.0_vp; +constexpr Dimension MAX_SCREEN_WIDTH_LG = 840.0_vp; + +enum class ScreenSizeType { + UNDEFINED = 0, + XS, + SM, + MD, + LG, + XL, +}; + +constexpr size_t SCREEN_SIZE_COUNT = static_cast(ScreenSizeType::XL); +const inline std::map SCREEN_SIZE_VALUES = { + {ScreenSizeType::XS, "xs"}, + {ScreenSizeType::SM, "sm"}, + {ScreenSizeType::MD, "md"}, + {ScreenSizeType::LG, "lg"}, +}; + +inline bool isValid(ScreenSizeType val) { + return static_cast(val) < SCREEN_SIZE_COUNT; +} + +class ScreenSystemManager final { +public: + static ScreenSystemManager& GetInstance(); + + void SetWindowInfo(double screenWidth, double density, double dipScale) + { + screenWidth_ = screenWidth; + density_ = density; + dipScale_ = dipScale; + viewScale_ = density / dipScale; + } + + void OnSurfaceChanged(double width) + { + screenWidth_ = width; + if (width < MAX_SCREEN_WIDTH_SM.Value() * density_) { + currentSize_ = ScreenSizeType::XS; + } else if (width < MAX_SCREEN_WIDTH_MD.Value() * density_) { + currentSize_ = ScreenSizeType::SM; + } else if (width < MAX_SCREEN_WIDTH_LG.Value() * density_) { + currentSize_ = ScreenSizeType::MD; + } else { + currentSize_ = ScreenSizeType::LG; + } + LOGD("OnSurfaceChanged: %{public}f", width); + } + + double GetScreenWidth() const + { + return screenWidth_; + } + + double GetDipScale() const + { + return dipScale_; + } + + ScreenSizeType GetCurrentSize() const + { + return currentSize_; + } + + double GetDensity() const + { + return density_; + } + +private: + double screenWidth_ = 0.0; + double density_ = 1.0; + double dipScale_ = 1.0; + double viewScale_ = 1.0; + ScreenSizeType currentSize_ = ScreenSizeType::UNDEFINED; + +private: + ScreenSystemManager() = default; + ~ScreenSystemManager() = default; + + ACE_DISALLOW_COPY_AND_MOVE(ScreenSystemManager); +}; + +inline ScreenSystemManager& ScreenSystemManager::GetInstance() +{ + static ScreenSystemManager instance; + return instance; +} + +template +class ArrayByScreenType final { +public: + ArrayByScreenType() = default; + ArrayByScreenType(const T& defValue) + { + std::fill(values_.begin(), values_.end(), defValue); + } + + ~ArrayByScreenType() = default; + + const T& operator [](ScreenSizeType idx) const + { + return values_[static_cast(idx)]; + } + + T& operator [](ScreenSizeType idx) + { + return values_[static_cast(idx)]; + } + + const T& GetCurrentValue() const + { + auto idx = ScreenSystemManager::GetInstance().GetCurrentSize(); + return values_[static_cast(idx)]; + } + + T& GetCurrentValue() + { + auto idx = ScreenSystemManager::GetInstance().GetCurrentSize(); + return values_[static_cast(idx)]; + } +private: + std::array values_; +}; +} // namespace OHOS::Ace + +#endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_LAYOUT_SCREEN_SYSTEM_MANAGER_H \ No newline at end of file diff --git a/frameworks/core/pipeline/pipeline_context.cpp b/frameworks/core/pipeline/pipeline_context.cpp index 1709b4913b..946211129c 100644 --- a/frameworks/core/pipeline/pipeline_context.cpp +++ b/frameworks/core/pipeline/pipeline_context.cpp @@ -58,7 +58,7 @@ #include "core/common/text_field_manager.h" #include "core/common/thread_checker.h" #include "core/components/checkable/render_checkable.h" -#include "core/components/common/layout/grid_system_manager.h" +#include "core/components/common/layout/screen_system_manager.h" #include "core/components/container_modal/container_modal_component.h" #include "core/components/container_modal/container_modal_element.h" #include "core/components/custom_paint/offscreen_canvas.h" @@ -2297,8 +2297,8 @@ void PipelineContext::SetRootSizeWithWidthHeight(int32_t width, int32_t height, rootNode->MarkNeedRender(); focusAnimationManager_->SetAvailableRect(paintRect); } - GridSystemManager::GetInstance().SetWindowInfo(rootWidth_, density_, dipScale_); - GridSystemManager::GetInstance().OnSurfaceChanged(width); + ScreenSystemManager::GetInstance().SetWindowInfo(rootWidth_, density_, dipScale_); + ScreenSystemManager::GetInstance().OnSurfaceChanged(width); } void PipelineContext::SetAppBgColor(const Color& color) -- Gitee From 633f73f851377e2cda0ef46ad990592d59d7a53e Mon Sep 17 00:00:00 2001 From: "tuzhilkin.ivan" Date: Mon, 12 Sep 2022 10:53:34 +0000 Subject: [PATCH 2/4] Adaptive Grid - initial version Signed-off-by: Tuzhilkin Ivan Change-Id: I11c452775ab995cdeb7246aa6aeb90fda735b2a8 --- .../declarative_frontend/jsview/js_grid.cpp | 32 ++++++++- .../declarative_frontend/jsview/js_grid.h | 1 + .../grid_layout/grid_layout_component.cpp | 12 +++- .../grid_layout/grid_layout_component.h | 14 +++- .../grid_layout/render_grid_layout.cpp | 71 +++++++++++++++---- .../grid_layout/render_grid_layout.h | 10 ++- 6 files changed, 121 insertions(+), 19 deletions(-) diff --git a/frameworks/bridge/declarative_frontend/jsview/js_grid.cpp b/frameworks/bridge/declarative_frontend/jsview/js_grid.cpp index 0f9dd41a33..02d37f2f50 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_grid.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_grid.cpp @@ -30,6 +30,8 @@ namespace OHOS::Ace::Framework { namespace { +const std::string_view AUTO_GAP = "auto-gap"; +constexpr int32_t AUTO_GAP_VALUE = -1; const std::vector DISPLAY_MODE = { DisplayMode::OFF, DisplayMode::AUTO, DisplayMode::ON }; const std::vector EDGE_EFFECT = { EdgeEffect::SPRING, EdgeEffect::FADE, EdgeEffect::NONE }; @@ -134,7 +136,10 @@ void JSGrid::SetColumnsGap(const JSCallbackInfo& info) return; } Dimension colGap; - if (!ParseJsDimensionVp(info[0], colGap)) { + if (info[0]->IsString() && (info[0]->ToString() == AUTO_GAP)) { + colGap.SetValue(AUTO_GAP_VALUE); + colGap.SetUnit(DimensionUnit::PX); + } else if (!ParseJsDimensionVp(info[0], colGap)) { return; } @@ -277,6 +282,7 @@ void JSGrid::JSBind(BindingTarget globalObj) JSClass::StaticMethod("height", &JSGrid::JsGridHeight); JSClass::StaticMethod("onItemDrop", &JSGrid::JsOnGridDrop); JSClass::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage); + JSClass::StaticMethod("setColumnsSizeTypes", &JSGrid::SetColumnsSizeTypes); JSClass::Inherit(); JSClass::Inherit(); JSClass::Bind<>(globalObj); @@ -660,4 +666,28 @@ void JSGrid::SetMultiSelectable(bool multiSelectable) } } +void JSGrid::SetColumnsSizeTypes(const JSCallbackInfo& info) +{ + if (info.Length() < 1) { + LOGE("JSGred::SetColumnsSizeType - The arg is wrong, it is supposed to have at least 1 argument"); + return; + } + auto component = ViewStackProcessor::GetInstance()->GetMainComponent(); + auto grid = AceType::DynamicCast(component); + if (!grid) { + return; + } + + JSRef sizeObj = JSRef::Cast(info[0]); + ArrayByScreenType sizeTypes {""}; + for(auto value: SCREEN_SIZE_VALUES) { + JSRef val = sizeObj->GetProperty(value.second.c_str()); + if (val->IsNull() || val->IsEmpty() || !(val->IsString())) { + return; + } + sizeTypes[value.first] = val->ToString(); + } + grid->SetColumnsSizeTypes(sizeTypes); +} + } // namespace OHOS::Ace::Framework diff --git a/frameworks/bridge/declarative_frontend/jsview/js_grid.h b/frameworks/bridge/declarative_frontend/jsview/js_grid.h index 429a1e84a2..3fe9f6211c 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_grid.h +++ b/frameworks/bridge/declarative_frontend/jsview/js_grid.h @@ -53,6 +53,7 @@ public: static void JsOnGridDragStart(const JSCallbackInfo& info); static void JsOnGridDrop(const JSCallbackInfo& info); static void JsGridHeight(const JSCallbackInfo& info); + static void SetColumnsSizeTypes(const JSCallbackInfo& info); }; } // namespace OHOS::Ace::Framework diff --git a/frameworks/core/components/grid_layout/grid_layout_component.cpp b/frameworks/core/components/grid_layout/grid_layout_component.cpp index 080cba0276..bec38e9ad0 100644 --- a/frameworks/core/components/grid_layout/grid_layout_component.cpp +++ b/frameworks/core/components/grid_layout/grid_layout_component.cpp @@ -117,7 +117,7 @@ void GridLayoutComponent::SetRowsArgs(const std::string& rowsArgs) void GridLayoutComponent::SetColumnGap(const Dimension& columnGap) { - if (columnGap.Value() < 0.0) { + if (columnGap.Value() < 0.0 && columnGap.Value() != -1) { LOGW("Invalid RowGap, use 0px"); columnGap_ = 0.0_px; return; @@ -205,4 +205,14 @@ const OnGridDropFunc& GridLayoutComponent::GetOnGridDropId() const return onGridDropId_; } +void GridLayoutComponent::SetColumnsSizeTypes(const ArrayByScreenType& typesArray) +{ + columnsSizeTypes_ = typesArray; +} + +std::string GridLayoutComponent::GetAdaptiveColumnsTemplate() const +{ + return columnsSizeTypes_.GetCurrentValue(); +} + } // namespace OHOS::Ace diff --git a/frameworks/core/components/grid_layout/grid_layout_component.h b/frameworks/core/components/grid_layout/grid_layout_component.h index 664125c622..b53b06226c 100644 --- a/frameworks/core/components/grid_layout/grid_layout_component.h +++ b/frameworks/core/components/grid_layout/grid_layout_component.h @@ -16,8 +16,10 @@ #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_COMPONENT_H #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_GRID_LAYOUT_GRID_LAYOUT_COMPONENT_H +#include #include "base/utils/macros.h" #include "core/components/common/layout/constants.h" +#include "core/components/common/layout/screen_system_manager.h" #include "core/components/common/properties/scroll_bar.h" #include "core/components/scroll_bar/scroll_bar_proxy.h" #include "core/components_v2/foreach/lazy_foreach_component.h" @@ -36,7 +38,12 @@ class ACE_EXPORT GridLayoutComponent : public ComponentGroup { DECLARE_ACE_TYPE(GridLayoutComponent, ComponentGroup); public: - explicit GridLayoutComponent(const std::list>& children) : ComponentGroup(children) {} + explicit GridLayoutComponent(const std::list>& children) : ComponentGroup(children) { + columnsSizeTypes_[ScreenSizeType::XS] = "1fr"; + columnsSizeTypes_[ScreenSizeType::SM] = "1fr 1fr"; + columnsSizeTypes_[ScreenSizeType::MD] = "1fr 1fr 1fr"; + columnsSizeTypes_[ScreenSizeType::LG] = "1fr 1fr 1fr 1fr"; + } ~GridLayoutComponent() override = default; @@ -300,6 +307,9 @@ public: return multiSelectable_; } + void SetColumnsSizeTypes(const ArrayByScreenType& typesArray); + std::string GetAdaptiveColumnsTemplate() const; + private: FlexDirection direction_ = FlexDirection::COLUMN; FlexAlign flexAlign_ = FlexAlign::CENTER; @@ -340,6 +350,8 @@ private: OnGridDragLeaveFunc onGridDragLeaveId_; OnGridDragStartFunc onGridDragStartId_; OnGridDropFunc onGridDropId_; + + ArrayByScreenType columnsSizeTypes_ {""}; }; } // namespace OHOS::Ace diff --git a/frameworks/core/components/grid_layout/render_grid_layout.cpp b/frameworks/core/components/grid_layout/render_grid_layout.cpp index 465011c08a..102fd01bb8 100644 --- a/frameworks/core/components/grid_layout/render_grid_layout.cpp +++ b/frameworks/core/components/grid_layout/render_grid_layout.cpp @@ -47,6 +47,7 @@ constexpr uint32_t REPEAT_MIN_SIZE = 6; constexpr int32_t GAP_DIVIDE_CONSTEXPR = 2; constexpr int32_t CELL_EMPTY = -1; constexpr int32_t CELL_FOR_INSERT = -2; +constexpr int32_t AUTO_GAP_VALUE = -1; const char UNIT_PIXEL[] = "px"; const char UNIT_VP[] = "vp"; const char UNIT_PERCENT[] = "%"; @@ -54,6 +55,8 @@ const char UNIT_RATIO[] = "fr"; const char UNIT_AUTO[] = "auto"; const char UNIT_AUTO_FILL[] = "auto-fill"; const char REPEAT_PREFIX[] = "repeat"; +const char AUTO_COLUMNS[] = "auto-columns"; +const char AUTO_ADJUST_ROW[] = "auto-adjust-row"; const std::regex REPEAT_NUM_REGEX(R"(^repeat\((\d+),(.+)\))", std::regex::icase); // regex for "repeat(2, 100px)" const std::regex AUTO_REGEX(R"(^repeat\((.+),(.+)\))", std::regex::icase); // regex for "repeat(auto-fill, 10px)" const std::regex TRIM_REGEX(R"(^ +| +$|(\"[^\"\\\\]*(?:\\\\[\\s\\S][^\"\\\\]*)*\")|( ) +)", std::regex::icase); @@ -387,7 +390,7 @@ void RenderGridLayout::DisableChild(const RefPtr& child, int32_t ind child->SetPosition(Offset(0.0, 0.0)); } -Size RenderGridLayout::GetTargetLayoutSize(int32_t row, int32_t col) +Size RenderGridLayout::GetTargetLayoutSize(int32_t row, int32_t col, int32_t rowCount, int32_t colCount) { Size size; if (GetChildren().empty()) { @@ -395,11 +398,8 @@ Size RenderGridLayout::GetTargetLayoutSize(int32_t row, int32_t col) } LayoutParam innerLayout; // Init layout param for auto item. innerLayout.SetMaxSize(Size(colSize_, rowSize_)); - std::vector rows, cols; - StringUtils::StringSplitter(rowsArgs_, ' ', rows); - rowCount_ = rows.size() == 0 ? 1 : rows.size(); - StringUtils::StringSplitter(colsArgs_, ' ', cols); - colCount_ = cols.size() == 0 ? 1 : cols.size(); + rowCount_ = rowCount; + colCount_ = colCount; int32_t rowIndex = 0; int32_t colIndex = 0; int32_t itemIndex = 0; @@ -431,6 +431,15 @@ Size RenderGridLayout::GetTargetLayoutSize(int32_t row, int32_t col) return size; } +Size RenderGridLayout::GetTargetLayoutSize(int32_t row, int32_t col) +{ + std::vector rows; + std::vector cols; + StringUtils::StringSplitter(rowsArgs_, ' ', rows); + StringUtils::StringSplitter(colsArgs_, ' ', cols); + return GetTargetLayoutSize(row, col, rows.size() == 0 ? 1 : rows.size(), cols.size() == 0 ? 1 : cols.size()); +} + std::string RenderGridLayout::PreParseRows() { if (rowsArgs_.empty() || rowsArgs_.find(UNIT_AUTO) == std::string::npos) { @@ -510,6 +519,7 @@ void RenderGridLayout::InitialGridProp() : GetLayoutParam().GetMaxSize().Height(); colSize_ = ((gridWidth_ > 0.0) && (gridWidth_ < GetLayoutParam().GetMaxSize().Width())) ? gridWidth_ : GetLayoutParam().GetMaxSize().Width(); + ConvertAutoColumnsArgs(colsArgs_); if (NearEqual(rowSize_, Size::INFINITE_SIZE) && (rowsArgs_.find(UNIT_PERCENT) != std::string::npos || rowsArgs_.find(UNIT_RATIO) != std::string::npos)) { rowSize_ = viewPort_.Height(); @@ -518,14 +528,28 @@ void RenderGridLayout::InitialGridProp() (colsArgs_.find(UNIT_PERCENT) != std::string::npos || colsArgs_.find(UNIT_RATIO) != std::string::npos)) { colSize_ = viewPort_.Width(); } + std::vector rows = ParseArgs(GetRowTemplate(), rowSize_, rowGap_); std::vector cols = ParseArgs(GetColumnsTemplate(), colSize_, colGap_); - if (rows.empty()) { - rows.push_back(rowSize_); - } + if (cols.empty()) { cols.push_back(colSize_); } + if (rows.empty() && (rowsArgs_.find(AUTO_ADJUST_ROW) != std::string::npos)) { + auto totalNum = (int32_t)GetChildren().size(); + auto rowCount = totalNum / cols.size(); + if (totalNum % cols.size() > 0) { + rowCount++; + } + + for (auto i = 0; i < rowCount; i++) { + double h = GetTargetLayoutSize(i, 0, rowCount, cols.size()).Height(); + gridMatrix_.clear(); + rows.push_back(h); + } + } else if (rows.empty()) { + rows.push_back(rowSize_); + } if (NearEqual(rowSize_, Size::INFINITE_SIZE)) { rowSize_ = std::accumulate(rows.begin(), rows.end(), (rows.size() - 1) * rowGap_); // This case means grid's height is not set and the layout param is infinite(e.g. in a column) @@ -576,10 +600,10 @@ void RenderGridLayout::UpdateAccessibilityAttr() // (3) 30% 20% 50% // (4) repeat(2,100px 20%) -- will be prebuilt by JS Engine to --- 100px 20% 100px 20% // (5) repeat(auto-fill, 100px 300px) -- will be prebuilt by JS Engine to --- auto-fill 100px 300px -std::vector RenderGridLayout::ParseArgsInner(const std::string& args, double size, double gap) +std::vector RenderGridLayout::ParseArgsInner(const std::string& args, double size, double& gap) { std::vector lens; - if (args.empty()) { + if (args.empty() || args.find(AUTO_ADJUST_ROW) != std::string::npos) { return lens; } double pxSum = 0.0; // First priority: such as 50px @@ -607,6 +631,18 @@ std::vector RenderGridLayout::ParseArgsInner(const std::string& args, do if (GreatOrEqual(peSum, FULL_PERCENT)) { peSum = FULL_PERCENT; } + if (gap == AUTO_GAP_VALUE) { + auto gapCount = strs.size() - 1; + if (peSum == 0 && frSum == 0) { + gap = (gapCount > 0) ? ((size - pxSum) / gapCount) : 0; + if (gap < 0) { + gap = 0; + } + } else { + gap = (gapCount > 0) ? (0.1 * size / gapCount) : 0; + } + } + // Second loop calculate actual width or height. double sizeLeft = size - (strs.size() - 1) * gap; double prSumLeft = FULL_PERCENT; @@ -656,6 +692,15 @@ void RenderGridLayout::ConvertRepeatArgs(std::string& handledArg) } } +void RenderGridLayout::ConvertAutoColumnsArgs(std::string& handledArg) +{ + if (handledArg.find(AUTO_COLUMNS) == std::string::npos) { + return; + } + + handledArg = component_->GetAdaptiveColumnsTemplate(); +} + std::vector RenderGridLayout::ParseAutoFill(const std::vector& strs, double size, double gap) { std::vector lens; @@ -3429,7 +3474,7 @@ bool RenderGridLayout::CheckAutoFillParameter( return bRet; } -std::vector RenderGridLayout::ParseArgsWithAutoFill(const std::string& args, double size, double gap) +std::vector RenderGridLayout::ParseArgsWithAutoFill(const std::string& args, double size, double& gap) { std::vector lens; if (args.find(UNIT_AUTO_FILL) == std::string::npos) { @@ -3480,7 +3525,7 @@ std::vector RenderGridLayout::ParseArgsWithAutoFill(const std::string& a return lens; } -std::vector RenderGridLayout::ParseArgs(const std::string& args, double size, double gap) +std::vector RenderGridLayout::ParseArgs(const std::string& args, double size, double& gap) { if (args.find(REPEAT_PREFIX) != std::string::npos && args.find(UNIT_AUTO_FILL) != std::string::npos) { return ParseArgsWithAutoFill(args, size, gap); diff --git a/frameworks/core/components/grid_layout/render_grid_layout.h b/frameworks/core/components/grid_layout/render_grid_layout.h index cc724944f1..bf08b1bd70 100644 --- a/frameworks/core/components/grid_layout/render_grid_layout.h +++ b/frameworks/core/components/grid_layout/render_grid_layout.h @@ -253,11 +253,15 @@ protected: void ConvertRepeatArgs(std::string& args); + void ConvertAutoColumnsArgs(std::string& args); + // Handle direction key move int32_t focusMove(KeyDirection direction); Size GetTargetLayoutSize(int32_t row, int32_t col); + Size GetTargetLayoutSize(int32_t row, int32_t col, int32_t rowCount, int32_t colCount); + std::string PreParseArgs(const std::string& args); std::string PreParseRows(); @@ -268,7 +272,7 @@ protected: void UpdateAccessibilityAttr(); - std::vector ParseArgs(const std::string& args, double size, double gap); + std::vector ParseArgs(const std::string& args, double size, double& gap); std::vector ParseAutoFill(const std::vector& strs, double size, double gap); @@ -618,8 +622,8 @@ private: bool isRepeat = false; } Value; - std::vector ParseArgsWithAutoFill(const std::string& args, double size, double gap); - std::vector ParseArgsInner(const std::string& args, double size, double gap); + std::vector ParseArgsWithAutoFill(const std::string& args, double size, double& gap); + std::vector ParseArgsInner(const std::string& args, double size, double& gap); void RTrim(std::string& str); std::string TrimTemplate(std::string& str); std::string GetRepeat(const std::string& str); -- Gitee From 91b3c8a2418e8dd8fbe6db791923d4d765e43be8 Mon Sep 17 00:00:00 2001 From: "tuzhilkin.ivan" Date: Wed, 5 Oct 2022 12:32:39 +0300 Subject: [PATCH 3/4] Adaptive Grid - fixed failed test cases Signed-off-by: Tuzhilkin Ivan Change-Id: I187b8fb4285f3d18e3e96df1ea1918d7e432fff3 --- .../grid_layout/render_grid_layout.cpp | 85 +++++++++++++------ .../grid_layout/render_grid_layout.h | 6 +- 2 files changed, 63 insertions(+), 28 deletions(-) diff --git a/frameworks/core/components/grid_layout/render_grid_layout.cpp b/frameworks/core/components/grid_layout/render_grid_layout.cpp index 102fd01bb8..1e116e241a 100644 --- a/frameworks/core/components/grid_layout/render_grid_layout.cpp +++ b/frameworks/core/components/grid_layout/render_grid_layout.cpp @@ -97,7 +97,9 @@ void RenderGridLayout::Update(const RefPtr& component) direction_ = grid->GetDirection(); crossAxisAlign_ = grid->GetFlexAlign(); gridWidth_ = grid->GetWidth(); + isGridWidthDefined_ = !(gridWidth_ < 0.0); gridHeight_ = grid->GetHeight(); + isGridHeightDefined_ = !(gridHeight_ < 0.0); colsArgs_ = grid->GetColumnsArgs(); rowsArgs_ = grid->GetRowsArgs(); userColGap_ = grid->GetColumnGap(); @@ -125,7 +127,7 @@ void RenderGridLayout::Update(const RefPtr& component) onGridDragLeaveFunc_ = grid->GetOnGridDragLeaveId(); onGridDropFunc_ = grid->GetOnGridDropId(); - if (((rowsArgs_.empty() && (!colsArgs_.empty())) || ((!rowsArgs_.empty()) && colsArgs_.empty())) && + if (((rowsArgs_.empty() && (!colsArgs_.empty())) || ((!rowsArgs_.empty()) && (rowsArgs_.find(AUTO_ADJUST_ROW) == std::string::npos) && colsArgs_.empty())) && (mainCountMax_ >= mainCountMin_) && (mainCountMin_ >= 1) && (cellLength_ > 0) && (editMode_ == true)) { isDynamicGrid_ = true; } @@ -506,6 +508,27 @@ std::string RenderGridLayout::PreParseArgs(const std::string& args) return arg; } +std::string RenderGridLayout::ConvertAutoAdjustRowArgs(int32_t colCount) +{ + std::string rowsTemplate = GetRowTemplate(); + if ((rowsTemplate.find(AUTO_ADJUST_ROW) != std::string::npos) && (colCount > 0)) { + auto itemCount = static_cast(GetChildren().size()); + auto rowCount = itemCount / colCount; + if (itemCount % colCount > 0) { + rowCount++; + } + rowsTemplate = ""; + std::string cellHeight = (NearEqual(rowSize_, Size::INFINITE_SIZE)) ? "auto" : "1fr"; + for (auto i = 1; i <= rowCount; i++) { + rowsTemplate = rowsTemplate + cellHeight; + if (i < rowCount) { + rowsTemplate = rowsTemplate + " "; + } + } + } + return rowsTemplate; +} + void RenderGridLayout::InitialGridProp() { // Not first time layout after update, no need to initial. @@ -517,37 +540,39 @@ void RenderGridLayout::InitialGridProp() rowSize_ = ((gridHeight_ > 0.0) && (gridHeight_ < GetLayoutParam().GetMaxSize().Height())) ? gridHeight_ : GetLayoutParam().GetMaxSize().Height(); + if (!NearEqual(GetLayoutParam().GetMaxSize().Height(), Size::INFINITE_SIZE)) { + isGridHeightDefined_ = true; + } + if (!isGridHeightDefined_) { + rowSize_ = Size::INFINITE_SIZE; + } colSize_ = ((gridWidth_ > 0.0) && (gridWidth_ < GetLayoutParam().GetMaxSize().Width())) ? gridWidth_ : GetLayoutParam().GetMaxSize().Width(); - ConvertAutoColumnsArgs(colsArgs_); - if (NearEqual(rowSize_, Size::INFINITE_SIZE) && - (rowsArgs_.find(UNIT_PERCENT) != std::string::npos || rowsArgs_.find(UNIT_RATIO) != std::string::npos)) { - rowSize_ = viewPort_.Height(); + if (!NearEqual(GetLayoutParam().GetMaxSize().Width(), Size::INFINITE_SIZE)) { + isGridWidthDefined_ = true; } + if (!isGridWidthDefined_) { + colSize_ = Size::INFINITE_SIZE; + } + + std::string columnsTemplateBackup = colsArgs_; + colsArgs_ = ConvertAutoColumnsArgs(); if (NearEqual(colSize_, Size::INFINITE_SIZE) && (colsArgs_.find(UNIT_PERCENT) != std::string::npos || colsArgs_.find(UNIT_RATIO) != std::string::npos)) { colSize_ = viewPort_.Width(); } - - std::vector rows = ParseArgs(GetRowTemplate(), rowSize_, rowGap_); std::vector cols = ParseArgs(GetColumnsTemplate(), colSize_, colGap_); - if (cols.empty()) { cols.push_back(colSize_); } - if (rows.empty() && (rowsArgs_.find(AUTO_ADJUST_ROW) != std::string::npos)) { - auto totalNum = (int32_t)GetChildren().size(); - auto rowCount = totalNum / cols.size(); - if (totalNum % cols.size() > 0) { - rowCount++; - } - - for (auto i = 0; i < rowCount; i++) { - double h = GetTargetLayoutSize(i, 0, rowCount, cols.size()).Height(); - gridMatrix_.clear(); - rows.push_back(h); - } - } else if (rows.empty()) { + std::string rowsTemplateBackup = rowsArgs_; + rowsArgs_ = ConvertAutoAdjustRowArgs(static_cast(cols.size())); + if (NearEqual(rowSize_, Size::INFINITE_SIZE) && + (rowsArgs_.find(UNIT_PERCENT) != std::string::npos || rowsArgs_.find(UNIT_RATIO) != std::string::npos)) { + rowSize_ = viewPort_.Height(); + } + std::vector rows = ParseArgs(GetRowTemplate(), rowSize_, rowGap_); + if (rows.empty()) { rows.push_back(rowSize_); } if (NearEqual(rowSize_, Size::INFINITE_SIZE)) { @@ -561,6 +586,7 @@ void RenderGridLayout::InitialGridProp() colSize_ = std::accumulate(cols.begin(), cols.end(), (cols.size() - 1) * colGap_); gridWidth_ = colSize_; } + // Initialize the columnCount and rowCount, default is 1 colCount_ = cols.size(); rowCount_ = rows.size(); @@ -576,6 +602,8 @@ void RenderGridLayout::InitialGridProp() ++row; } UpdateAccessibilityAttr(); + colsArgs_ = columnsTemplateBackup; + rowsArgs_ = rowsTemplateBackup; LOGD("GridLayout: %{public}lf %{public}lf %{public}d %{public}d", colSize_, rowSize_, colCount_, rowCount_); } @@ -603,7 +631,7 @@ void RenderGridLayout::UpdateAccessibilityAttr() std::vector RenderGridLayout::ParseArgsInner(const std::string& args, double size, double& gap) { std::vector lens; - if (args.empty() || args.find(AUTO_ADJUST_ROW) != std::string::npos) { + if (args.empty()) { return lens; } double pxSum = 0.0; // First priority: such as 50px @@ -692,13 +720,13 @@ void RenderGridLayout::ConvertRepeatArgs(std::string& handledArg) } } -void RenderGridLayout::ConvertAutoColumnsArgs(std::string& handledArg) +std::string RenderGridLayout::ConvertAutoColumnsArgs() { - if (handledArg.find(AUTO_COLUMNS) == std::string::npos) { - return; + std::string columnsTemplate = GetColumnsTemplate(); + if (columnsTemplate.find(AUTO_COLUMNS) != std::string::npos) { + columnsTemplate = component_->GetAdaptiveColumnsTemplate(); } - - handledArg = component_->GetAdaptiveColumnsTemplate(); + return columnsTemplate; } std::vector RenderGridLayout::ParseAutoFill(const std::vector& strs, double size, double gap) @@ -1702,6 +1730,8 @@ void RenderGridLayout::CalculateVerticalSize( colSize_ = ((gridWidth_ > 0.0) && (gridWidth_ < GetLayoutParam().GetMaxSize().Width())) ? gridWidth_ : GetLayoutParam().GetMaxSize().Width(); + std::string columnsTemplateBackup = colsArgs_; + colsArgs_ = ConvertAutoColumnsArgs(); if (NearEqual(colSize_, Size::INFINITE_SIZE) && (colsArgs_.find(UNIT_PERCENT) != std::string::npos || colsArgs_.find(UNIT_RATIO) != std::string::npos)) { colSize_ = viewPort_.Width(); @@ -1720,6 +1750,7 @@ void RenderGridLayout::CalculateVerticalSize( std::clamp((totalNum / colCount_ + (((totalNum % colCount_) == 0) ? 0 : 1)), mainCountMin_, mainCountMax_); rows = std::vector(rowCount_, cellLength_); rowSize_ = rowCount_ * cellLength_ + (rowCount_ - 1) * rowGap_; + colsArgs_ = columnsTemplateBackup; } void RenderGridLayout::CalculateHorizontalSize( diff --git a/frameworks/core/components/grid_layout/render_grid_layout.h b/frameworks/core/components/grid_layout/render_grid_layout.h index bf08b1bd70..021c1b085a 100644 --- a/frameworks/core/components/grid_layout/render_grid_layout.h +++ b/frameworks/core/components/grid_layout/render_grid_layout.h @@ -253,7 +253,9 @@ protected: void ConvertRepeatArgs(std::string& args); - void ConvertAutoColumnsArgs(std::string& args); + std::string ConvertAutoColumnsArgs(); + + std::string ConvertAutoAdjustRowArgs(int32_t colCount); // Handle direction key move int32_t focusMove(KeyDirection direction); @@ -459,6 +461,8 @@ protected: double rowSize_ = 0.0; double gridWidth_ = -1.0; double gridHeight_ = -1.0; + bool isGridHeightDefined_ = false; + bool isGridWidthDefined_ = false; int32_t colCount_ = 0; int32_t rowCount_ = 0; Dimension userColGap_ = 0.0_px; -- Gitee From 767f64dcb8bc0973e6a244768d3ed938e43f5759 Mon Sep 17 00:00:00 2001 From: "tuzhilkin.ivan" Date: Wed, 5 Oct 2022 12:35:21 +0300 Subject: [PATCH 4/4] Adaptive Grid - unit tests Signed-off-by: Tuzhilkin Ivan Change-Id: I1ae80172d656cc0083eef639a1fbc7d173d42666 --- .../grid_layout/grid_layout_test_utils.cpp | 12 + .../grid_layout/render_grid_layout_test.cpp | 502 ++++++++++++++++++ 2 files changed, 514 insertions(+) diff --git a/frameworks/core/components/test/unittest/grid_layout/grid_layout_test_utils.cpp b/frameworks/core/components/test/unittest/grid_layout/grid_layout_test_utils.cpp index b123fe70ff..94846c6727 100644 --- a/frameworks/core/components/test/unittest/grid_layout/grid_layout_test_utils.cpp +++ b/frameworks/core/components/test/unittest/grid_layout/grid_layout_test_utils.cpp @@ -43,6 +43,18 @@ RefPtr GridLayoutTestUtils::CreateComponent(FlexDirection direction, return component; } +RefPtr GridLayoutTestUtils::CreateComponentWithUndefinedHeight(FlexDirection direction, std::string rows, std::string cols) +{ + constexpr double DIM_SIZE_VALUE_TEST = 1080.0; + std::list> children; + RefPtr component = AceType::MakeRefPtr(children); + component->SetWidth(DIM_SIZE_VALUE_TEST); + component->SetDirection(direction); + component->SetRowsArgs(rows); + component->SetColumnsArgs(cols); + return component; +} + RefPtr GridLayoutTestUtils::CreateRenderItem(int32_t row, int32_t col, int32_t rowSpan, int32_t colSpan) { constexpr double DIM_SIZE_VALUE_TEST = 540.0; diff --git a/frameworks/core/components/test/unittest/grid_layout/render_grid_layout_test.cpp b/frameworks/core/components/test/unittest/grid_layout/render_grid_layout_test.cpp index 5d4acafcbd..d2fac4e3fc 100644 --- a/frameworks/core/components/test/unittest/grid_layout/render_grid_layout_test.cpp +++ b/frameworks/core/components/test/unittest/grid_layout/render_grid_layout_test.cpp @@ -1567,6 +1567,7 @@ HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest029, TestSize.Level1) } } +/** * @tc.name: RenderGridLayoutTestRTL001 * @tc.desc: Test RTL of layout component * @tc.type: FUNC @@ -1590,4 +1591,505 @@ HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTestRTL001, TestSize.Level1) GTEST_LOG_(INFO) << "RenderGridLayoutTest rtl001 stop"; } + +/** + * @tc.name: RenderGridLayoutTest030 + * @tc.desc: Verify Grid Layout PerformLayout can calculate children with columns gap equals "auto-gap" + * and absolute columns template + * @tc.type: FUNC + */ +HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest030, TestSize.Level1) +{ + /** + * @tc.steps: step1. construct component and render with 9 child by column direction. + * @tc.expected: step1. properties and children are set correctly. + */ + std::string rowArgs = "1fr 1fr 1fr"; + std::string colArgs = "150px 150px 150px"; + auto component = GridLayoutTestUtils::CreateComponent(FlexDirection::COLUMN, rowArgs, colArgs); + auto gridcomponent = AceType::DynamicCast(component); + gridcomponent->SetRowGap(30.0_px); + gridcomponent->SetColumnGap(-1_px); + renderNode_->Update(component); + int32_t count = 9; + for (int32_t i = 0; i < count; ++i) { + RefPtr item = GridLayoutTestUtils::CreateRenderItem(-1, -1, 1, 1); + item->GetChildren().front()->Attach(mockContext_); + item->Attach(mockContext_); + renderNode_->AddChild(item); + } + ASSERT_TRUE(renderNode_->GetChildren().size() == 9); + + /** + * @tc.steps: step2. Verify that the properties are calculated correctly. + * @tc.expected: step2. Properties and children are calculated correctly. + */ + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items = renderNode_->GetChildren(); + ASSERT_TRUE(items.size() == 9); + int32_t index = 0; + for (const auto& item : items) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(465.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(930.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(465.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 5) { + ASSERT_TRUE(item->GetPosition() == Offset(930.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 6) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 7) { + ASSERT_TRUE(item->GetPosition() == Offset(465.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + if (index == 8) { + ASSERT_TRUE(item->GetPosition() == Offset(930.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(150, 340)); + } + index++; + } +} + +/** + * @tc.name: RenderGridLayoutTest031 + * @tc.desc: Verify Grid Layout PerformLayout can calculate children with columns gap equals "auto-gap" + * and fraction columns template + * @tc.type: FUNC + */ +HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest031, TestSize.Level1) +{ + /** + * @tc.steps: step1. construct component and render with 9 child by column direction. + * @tc.expected: step1. properties and children are set correctly. + */ + std::string rowArgs = "1fr 1fr 1fr"; + std::string colArgs = "1fr 1fr 1fr"; + auto component = GridLayoutTestUtils::CreateComponent(FlexDirection::COLUMN, rowArgs, colArgs); + auto gridcomponent = AceType::DynamicCast(component); + gridcomponent->SetRowGap(30.0_px); + gridcomponent->SetColumnGap(-1_px); + renderNode_->Update(component); + int32_t count = 9; + for (int32_t i = 0; i < count; ++i) { + RefPtr item = GridLayoutTestUtils::CreateRenderItem(-1, -1, 1, 1); + item->GetChildren().front()->Attach(mockContext_); + item->Attach(mockContext_); + renderNode_->AddChild(item); + } + ASSERT_TRUE(renderNode_->GetChildren().size() == 9); + + /** + * @tc.steps: step2. Verify that the properties are calculated correctly. + * @tc.expected: step2. Properties and children are calculated correctly. + */ + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items = renderNode_->GetChildren(); + ASSERT_TRUE(items.size() == 9); + int32_t index = 0; + for (const auto& item : items) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(378.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(756.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(378.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 5) { + ASSERT_TRUE(item->GetPosition() == Offset(756.0, 370.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 6) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 7) { + ASSERT_TRUE(item->GetPosition() == Offset(378.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + if (index == 8) { + ASSERT_TRUE(item->GetPosition() == Offset(756.0, 740.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(324, 340)); + } + index++; + } +} + +/** + * @tc.name: RenderGridLayoutTest032 + * @tc.desc: Verify Grid Layout PerformLayout can calculate children with rows template equals "auto-adjust-row" + * @tc.type: FUNC + */ +HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest032, TestSize.Level1) +{ + /** + * @tc.steps: step1. construct component and render with 9 child by column direction. + * @tc.expected: step1. properties and children are set correctly. + */ + std::string rowArgs = "1fr 1fr"; + std::string colArgs = "1fr 1fr"; + auto component = GridLayoutTestUtils::CreateComponent(FlexDirection::COLUMN, rowArgs, colArgs); + auto gridcomponent = AceType::DynamicCast(component); + renderNode_->Update(component); + int32_t count = 5; + for (int32_t i = 0; i < count; ++i) { + RefPtr item = GridLayoutTestUtils::CreateRenderItem(-1, -1, 1, 1); + item->GetChildren().front()->Attach(mockContext_); + item->Attach(mockContext_); + renderNode_->AddChild(item); + } + ASSERT_TRUE(renderNode_->GetChildren().size() == 5); + + /** + * @tc.steps: step2. Verify that the properties are calculated correctly. + * @tc.expected: step2. Properties and children are calculated correctly. + */ + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items = renderNode_->GetChildren(); + ASSERT_TRUE(items.size() == 5); + int32_t index = 0; + for (const auto& item : items) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(0.0, 0.0)); + } + index++; + } + gridcomponent->SetRowsArgs("auto-adjust-row") + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items2 = renderNode_->GetChildren(); + ASSERT_TRUE(items2.size() == 5); + int32_t index = 0; + for (const auto& item : items2) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 360.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 360.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 360.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 360.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 360.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 360.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 720.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 360.0)); + } + index++; + } +} + +/** + * @tc.name: RenderGridLayoutTest033 + * @tc.desc: Verify Grid Layout PerformLayout can calculate children with rows template equals "auto-adjust-row" and undefined height + * @tc.type: FUNC + */ +HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest033, TestSize.Level1) +{ + /** + * @tc.steps: step1. construct component and render with 9 child by column direction. + * @tc.expected: step1. properties and children are set correctly. + */ + std::string rowArgs = "auto auto"; + std::string colArgs = "1fr 1fr"; + auto component = GridLayoutTestUtils::CreateComponentWithUndefinedHeight(FlexDirection::COLUMN, rowArgs, colArgs); + auto gridcomponent = AceType::DynamicCast(component); + renderNode_->Update(component); + int32_t count = 5; + for (int32_t i = 0; i < count; ++i) { + RefPtr item = GridLayoutTestUtils::CreateRenderItem(-1, -1, 1, 1); + item->GetChildren().front()->Attach(mockContext_); + item->Attach(mockContext_); + renderNode_->AddChild(item); + } + ASSERT_TRUE(renderNode_->GetChildren().size() == 5); + + /** + * @tc.steps: step2. Verify that the properties are calculated correctly. + * @tc.expected: step2. Properties and children are calculated correctly. + */ + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items = renderNode_->GetChildren(); + ASSERT_TRUE(items.size() == 5); + int32_t index = 0; + for (const auto& item : items) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(0.0, 0.0)); + } + index++; + } + gridcomponent->SetRowsArgs("auto-adjust-row") + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1620.0)); + const std::list>& items2 = renderNode_->GetChildren(); + ASSERT_TRUE(items2.size() == 5); + int32_t index = 0; + for (const auto& item : items2) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(1080.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(540.0, 540.0)); + } + index++; + } +} + +/** + * @tc.name: RenderGridLayoutTest034 + * @tc.desc: Verify Grid Layout PerformLayout can calculate children with columns template equals "auto-columns" + * @tc.type: FUNC + */ +HWTEST_F(RenderGridLayoutTest, RenderGridLayoutTest034, TestSize.Level1) +{ + /** + * @tc.steps: step1. construct component and render with 9 child by column direction. + * @tc.expected: step1. properties and children are set correctly. + */ + std::string rowArgs = "1fr 1fr"; + std::string colArgs = "auto-columns"; + auto component = GridLayoutTestUtils::CreateComponent(FlexDirection::COLUMN, rowArgs, colArgs); + auto gridcomponent = AceType::DynamicCast(component); + renderNode_->Update(component); + int32_t count = 8; + for (int32_t i = 0; i < count; ++i) { + RefPtr item = GridLayoutTestUtils::CreateRenderItem(-1, -1, 1, 1); + item->GetChildren().front()->Attach(mockContext_); + item->Attach(mockContext_); + renderNode_->AddChild(item); + } + ASSERT_TRUE(renderNode_->GetChildren().size() == 8); + ScreenSystemManager scrManager = ScreenSystemManager.GetInstance(); + scrManager.OnSurfaceChanged(MAX_SCREEN_WIDTH_SM.Value() * scrManager.GetDensity()); // medium "1fr 1fr 1fr"; + + + /** + * @tc.steps: step2. Verify that the properties are calculated correctly. + * @tc.expected: step2. Properties and children are calculated correctly. + */ + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items = renderNode_->GetChildren(); + ASSERT_TRUE(items.size() == 8); + int32_t index = 0; + for (const auto& item : items) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(360.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(720.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(360.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0, 540.0)); + } + if (index == 5) { + ASSERT_TRUE(item->GetPosition() == Offset(720.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(360.0, 540.0)); + } + if (index == 6) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(0.0, 0.0)); + } + if (index == 7) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(0.0, 0.0)); + } + index++; + } + + scrManager.OnSurfaceChanged(MAX_SCREEN_WIDTH_MD.Value() * scrManager.GetDensity()); // large "1fr 1fr 1fr 1fr"; + renderNode_->PerformLayout(); + ASSERT_TRUE(renderNode_->GetLayoutSize() == Size(1080.0, 1080.0)); + const std::list>& items2 = renderNode_->GetChildren(); + ASSERT_TRUE(items2.size() == 8); + int32_t index = 0; + for (const auto& item : items2) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(270.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(810.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 5) { + ASSERT_TRUE(item->GetPosition() == Offset(270.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 6) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + if (index == 7) { + ASSERT_TRUE(item->GetPosition() == Offset(810.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(270.0, 540.0)); + } + index++; + } + ArrayByScreenType columnsSizeTypes {""}; + columnsSizeTypes[ScreenSizeType::XS] = "1fr"; + columnsSizeTypes[ScreenSizeType::SM] = "1fr 1fr 1fr"; + columnsSizeTypes[ScreenSizeType::MD] = "1fr 1fr 1fr 1fr 1fr"; + columnsSizeTypes[ScreenSizeType::LG] = "1fr 1fr 1fr 1fr 1fr 1fr"; + gridcomponent->SetColumnsSizeTypes(columnsSizeTypes); + renderNode_->PerformLayout(); + ASSERT_TRUE(items2.size() == 8); + int32_t index = 0; + for (const auto& item : items2) { + GridLayoutTestUtils::PrintNodeInfo(item); + if (index == 0) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 1) { + ASSERT_TRUE(item->GetPosition() == Offset(180.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 2) { + ASSERT_TRUE(item->GetPosition() == Offset(360.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0 540.0)); + } + if (index == 3) { + ASSERT_TRUE(item->GetPosition() == Offset(540.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 4) { + ASSERT_TRUE(item->GetPosition() == Offset(720.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 5) { + ASSERT_TRUE(item->GetPosition() == Offset(900.0, 540.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 6) { + ASSERT_TRUE(item->GetPosition() == Offset(0.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + if (index == 7) { + ASSERT_TRUE(item->GetPosition() == Offset(180.0, 0.0)); + ASSERT_TRUE(item->GetLayoutSize() == Size(180.0, 540.0)); + } + index++; + } +} } // namespace OHOS::Ace -- Gitee