From 2449da29badef1ca705e1dc9ec2ec371ca7548ab Mon Sep 17 00:00:00 2001 From: piggyguy Date: Tue, 20 Oct 2020 20:49:30 +0800 Subject: [PATCH] optimize components' dirty invalide for some situation --- src/core/components/chart_component.cpp | 3 ++- src/core/components/component.cpp | 14 ++++++++++---- src/core/components/component.h | 10 +++++++++- src/core/components/text_component.cpp | 3 +-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/core/components/chart_component.cpp b/src/core/components/chart_component.cpp index 9314fb24..fa80556a 100755 --- a/src/core/components/chart_component.cpp +++ b/src/core/components/chart_component.cpp @@ -417,6 +417,7 @@ void ChartComponent::AppendValues(UIChartDataSerial& dataserial, Point* pointArr } // Get the data set beyond the screen, and replace the existing data in the sequence one by one // from the beginning + chartView_->RefreshChart(); for (uint16_t i = 0; i < (expectedDatasLen - (xMaxValue_ + 1)); i++) { if (((xMaxValue_ - latestIndex) + i) >= dataLen) { HILOG_ERROR(HILOG_MODULE_ACE, "append data error2"); @@ -424,7 +425,7 @@ void ChartComponent::AppendValues(UIChartDataSerial& dataserial, Point* pointArr } pointArray[(xMaxValue_ - latestIndex) + i].x = i; dataserial.ModifyPoint(i, pointArray[(xMaxValue_ - latestIndex) + i]); - dataserial.HidePoint(i + latestIndex + 1, seriesOptions_->margin); + dataserial.HidePoint(i, seriesOptions_->margin); } } else { // after adding data, the length will not exceed the maximum value of the x axis if (latestIndex < existingDataLen - 1) { diff --git a/src/core/components/component.cpp b/src/core/components/component.cpp index 0103bef2..c81b189e 100755 --- a/src/core/components/component.cpp +++ b/src/core/components/component.cpp @@ -188,6 +188,8 @@ bool Component::UpdateView(uint16_t attrKeyId, jerry_value_t attrValue) START_TRACING_WITH_EXTRA_INFO(SET_ATTR_SET_TO_NATIVE, componentName_, attrKeyId); PreUpdate(); + // check if need to invalided self before changing in case component's area will be changed + InvalidateIfNeeded(attrKeyId, true); bool updateResult = SetAttribute(attrKeyId, attrValue); if (!updateResult) { AppStyleItem *styleItem = AppStyleItem::CreateStyleItem(attrKeyId, attrValue); @@ -199,7 +201,8 @@ bool Component::UpdateView(uint16_t attrKeyId, jerry_value_t attrValue) } RefreshRect(); - ReLayoutChildrenIfNeeded(attrKeyId); + // force parent to relayout the children in case component's area is changed + InvalidateIfNeeded(attrKeyId, false); PostUpdate(attrKeyId, updateResult); StartAnimation(); @@ -1111,7 +1114,7 @@ void Component::AppendDescriptorOrElements(UIViewGroup *viewGroup, const JSValue } } -void Component::ReLayoutChildrenIfNeeded(uint16_t attrKeyId) const +void Component::InvalidateIfNeeded(uint16_t attrKeyId, bool invalidateSelf) const { UIView *uiView = GetComponentRootView(); if ((uiView == nullptr) || !KeyParser::IsKeyValid(attrKeyId)) { @@ -1124,10 +1127,13 @@ void Component::ReLayoutChildrenIfNeeded(uint16_t attrKeyId) const attrKeyId == K_PADDING_RIGHT || attrKeyId == K_PADDING_TOP || attrKeyId == K_BORDER_BOTTOM_WIDTH || attrKeyId == K_BORDER_LEFT_WIDTH || attrKeyId == K_BORDER_RIGHT_WIDTH || attrKeyId == K_BORDER_TOP_WIDTH || attrKeyId == K_BORDER_WIDTH || attrKeyId == K_BORDER_RADIUS || attrKeyId == K_LEFT || attrKeyId == K_TOP) { + if (invalidateSelf) { + uiView->Invalidate(); + return; + } UIView *parent = uiView->GetParent(); if (parent != nullptr) { - parent->LayoutChildren(); - parent->Invalidate(); + parent->LayoutChildren(true); } } } diff --git a/src/core/components/component.h b/src/core/components/component.h index f1d916eb..705ff8f7 100755 --- a/src/core/components/component.h +++ b/src/core/components/component.h @@ -408,7 +408,15 @@ private: */ void ReleaseCommonEventListeners(); void AppendDescriptorOrElements(UIViewGroup *viewGroup, const JSValue descriptorOrElements); - void ReLayoutChildrenIfNeeded(uint16_t attrKeyId) const; + /** + * @brief For some situations, if the component's rect(position, width, height and so on) area is changed, need + * to force the parent container to relayout all children, and need to invalided self before the changes are + * applied. + * + * @param attrKeyId which attribute or style is being changing + * @param invalidateSelf true for just invaliding self, false for to relayout parent, default is false. + */ + void InvalidateIfNeeded(uint16_t attrKeyId, bool invalidateSelf = false) const; void AddAnimationToList(const TransitionImpl *transitionImpl) const; AppStyleManager *styleManager_; diff --git a/src/core/components/text_component.cpp b/src/core/components/text_component.cpp index f60d7650..a9eba47d 100755 --- a/src/core/components/text_component.cpp +++ b/src/core/components/text_component.cpp @@ -220,8 +220,7 @@ void TextComponent::PostUpdate(uint16_t attrKeyId, bool updateResult) } UIView *parent = uiLabel_->GetParent(); if (parent != nullptr) { - parent->LayoutChildren(); - parent->Invalidate(); + parent->LayoutChildren(true); } } -- Gitee