diff --git a/frameworks/core/components_ng/pattern/grid/grid_item_pattern.cpp b/frameworks/core/components_ng/pattern/grid/grid_item_pattern.cpp index 0e411fa4981..242c015889a 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_item_pattern.cpp +++ b/frameworks/core/components_ng/pattern/grid/grid_item_pattern.cpp @@ -232,18 +232,11 @@ void GridItemPattern::InitDisableStyle() CHECK_NULL_VOID(pipeline); auto theme = pipeline->GetTheme(); CHECK_NULL_VOID(theme); - auto userDefineOpacity = renderContext->GetOpacityValue(1.0); - + float opacity = 1.0f; if (!eventHub->IsDeveloperEnabled()) { - enableOpacity_ = renderContext->GetOpacityValue(1.0); - lastOpacity_ = enableOpacity_.value() * theme->GetGridItemDisabledAlpha(); - } else if (enableOpacity_.has_value() && userDefineOpacity == lastOpacity_) { - lastOpacity_ = enableOpacity_.value(); - enableOpacity_.reset(); - } else { - lastOpacity_ = userDefineOpacity; + opacity = theme->GetGridItemDisabledAlpha(); } - renderContext->UpdateOpacity(lastOpacity_); + renderContext->SetOpacityMultiplier(opacity); } void GridItemPattern::InitFocusPaintRect(const RefPtr& focusHub) @@ -310,9 +303,6 @@ void GridItemPattern::DumpAdvanceInfo() property->GetColumnEnd().has_value() ? DumpLog::GetInstance().AddDesc("ColumnEnd:" + std::to_string(property->GetColumnEnd().value())) : DumpLog::GetInstance().AddDesc("ColumnEnd:null"); - enableOpacity_.has_value() - ? DumpLog::GetInstance().AddDesc("enableOpacity:" + std::to_string(enableOpacity_.value())) - : DumpLog::GetInstance().AddDesc("enableOpacity:null"); selectable_ ? DumpLog::GetInstance().AddDesc("selectable:true") : DumpLog::GetInstance().AddDesc("selectable:false"); isSelected_ ? DumpLog::GetInstance().AddDesc("isSelected:true") diff --git a/frameworks/core/components_ng/pattern/grid/grid_item_pattern.h b/frameworks/core/components_ng/pattern/grid/grid_item_pattern.h index e67227d92ec..dee8cef19c7 100644 --- a/frameworks/core/components_ng/pattern/grid/grid_item_pattern.h +++ b/frameworks/core/components_ng/pattern/grid/grid_item_pattern.h @@ -163,8 +163,6 @@ private: bool isHover_ = false; bool isPressed_ = false; GridItemStyle gridItemStyle_ = GridItemStyle::NONE; - std::optional enableOpacity_; - double lastOpacity_ = 0.0; std::optional irregularItemInfo_; ACE_DISALLOW_COPY_AND_MOVE(GridItemPattern); diff --git a/frameworks/core/components_ng/render/adapter/rosen_render_context.cpp b/frameworks/core/components_ng/render/adapter/rosen_render_context.cpp index cec6fae71a7..d6fb3e393c2 100644 --- a/frameworks/core/components_ng/render/adapter/rosen_render_context.cpp +++ b/frameworks/core/components_ng/render/adapter/rosen_render_context.cpp @@ -6012,6 +6012,12 @@ void RosenRenderContext::SetOpacity(float opacity) rsNode_->SetAlpha(opacity); } +void RosenRenderContext::SetOpacityMultiplier(float opacity) +{ + CHECK_NULL_VOID(rsNode_); + SetAnimatableProperty(alphaModifier_, opacity); +} + void RosenRenderContext::SetTranslate(float translateX, float translateY, float translateZ) { CHECK_NULL_VOID(rsNode_); diff --git a/frameworks/core/components_ng/render/adapter/rosen_render_context.h b/frameworks/core/components_ng/render/adapter/rosen_render_context.h index 234e1db9077..937dba86fe4 100755 --- a/frameworks/core/components_ng/render/adapter/rosen_render_context.h +++ b/frameworks/core/components_ng/render/adapter/rosen_render_context.h @@ -392,6 +392,7 @@ public: void UpdatePaintRect(const RectF& paintRect) override; Matrix4 GetRevertMatrix() override; void SuggestOpIncNode(bool isOpincNode, bool isNeedCalculate) override; + void SetOpacityMultiplier(float opacity) override; private: void OnBackgroundImageUpdate(const ImageSourceInfo& src) override; @@ -665,6 +666,7 @@ private: std::shared_ptr scaleXYUserModifier_; std::shared_ptr drawRegionRects_[DRAW_REGION_RECT_COUNT] = { nullptr }; RefPtr focusAnimationModifier_; + std::shared_ptr alphaModifier_; // translate modifiers for interruption std::shared_ptr translateXY_; diff --git a/frameworks/core/components_ng/render/render_context.h b/frameworks/core/components_ng/render/render_context.h index 1cc6cb6b866..604a56dc61a 100644 --- a/frameworks/core/components_ng/render/render_context.h +++ b/frameworks/core/components_ng/render/render_context.h @@ -699,6 +699,9 @@ public: virtual void SuggestOpIncNode(bool isOpincNode, bool isNeedCalculate) {} + // The additional opacity will be multiplied with the base opacity. + virtual void SetOpacityMultiplier(float opacity) {} + protected: RenderContext() = default; std::shared_ptr sharedTransitionOption_; diff --git a/test/mock/core/render/mock_render_context.h b/test/mock/core/render/mock_render_context.h index ac1ca9ac60c..3bf996523db 100644 --- a/test/mock/core/render/mock_render_context.h +++ b/test/mock/core/render/mock_render_context.h @@ -100,11 +100,17 @@ public: return 0; } + void SetOpacityMultiplier(float opacity) + { + opacityMultiplier_ = opacity; + } + bool isVisible_ = true; RectF rect_; RectF paintRect_; Color blendColor_ = Color::TRANSPARENT; std::vector transInfo_ = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + float opacityMultiplier_ = 1.0f; }; } // namespace OHOS::Ace::NG #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MOCK_RENDER_CONTEXT_H diff --git a/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp b/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp index 9148e1093f7..aea3e8671ae 100644 --- a/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp +++ b/test/unittest/core/pattern/grid/grid_layout_test_ng.cpp @@ -413,12 +413,16 @@ HWTEST_F(GridLayoutTestNg, GridItemDisableEventTest001, TestSize.Level1) */ auto gridItemPattern = GetChildPattern(frameNode_, 0); auto gridItemEventHub = GetChildEventHub(frameNode_, 0); - EXPECT_FALSE(gridItemPattern->enableOpacity_.has_value()); + auto gridItemFrameNode = GetChildFrameNode(frameNode_, 0); + auto renderContext = gridItemFrameNode->renderContext_; + auto mockRenderContext = AceType::DynamicCast(renderContext); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 1.0f); gridItemEventHub->SetEnabled(false); gridItemPattern->InitDisableStyle(); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 0.4f); gridItemEventHub->SetEnabled(true); gridItemPattern->InitDisableStyle(); - EXPECT_FALSE(gridItemPattern->enableOpacity_.has_value()); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 1.0f); } /** @@ -1670,4 +1674,32 @@ HWTEST_F(GridLayoutTestNg, LayoutWithAutoStretch003, TestSize.Level1) EXPECT_TRUE(IsEqual(childRect, expectRect)) << "index: " << index; } } + +/** + * @tc.name: GridItemDisableEventTest002 + * @tc.desc: GirdItem disable event test. + * @tc.type: FUNC + */ +HWTEST_F(GridLayoutTestNg, GridItemDisableEventTest002, TestSize.Level1) +{ + GridModelNG model = CreateGrid(); + CreateFixedItems(10, GridItemStyle::PLAIN); + CreateDone(frameNode_); + + /** + * @tc.steps: step2. Get girdItem frameNode and pattern, set callback function. + * @tc.expected: Related function is called. + */ + auto gridItemPattern = GetChildPattern(frameNode_, 0); + auto gridItemEventHub = GetChildEventHub(frameNode_, 0); + auto gridItemFrameNode = GetChildFrameNode(frameNode_, 0); + auto renderContext = gridItemFrameNode->renderContext_; + auto mockRenderContext = AceType::DynamicCast(renderContext); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 1.0f); + gridItemEventHub->SetEnabled(false); + gridItemPattern->InitDisableStyle(); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 0.4f); + gridItemPattern->InitDisableStyle(); + EXPECT_EQ(mockRenderContext->opacityMultiplier_, 0.4f); +} } // namespace OHOS::Ace::NG