1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_FOCUS_MODIFIER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_RENDER_ADAPTER_FOCUS_MODIFIER_H 18 19 #include "render_service_client/core/modifier/rs_property.h" 20 #include "render_service_client/core/ui/rs_node.h" 21 22 #include "base/memory/referenced.h" 23 #include "core/components_ng/base/frame_node.h" 24 #include "core/components_ng/property/gradient_property.h" 25 #include "core/components_ng/render/adapter/rosen_modifier_adapter.h" 26 27 namespace OHOS::Ace::NG { 28 using RSPropertyBase = Rosen::RSPropertyBase; 29 30 class FocusModifier { 31 public: 32 FocusModifier() = default; 33 ~FocusModifier() = default; SetRoundRect(const RoundRect & rect,float borderWidth)34 virtual void SetRoundRect(const RoundRect& rect, float borderWidth) 35 { 36 CHECK_NULL_VOID(&rect); 37 auto cornTopLeft = rect.GetCornerRadius(RoundRect::CornerPos::TOP_LEFT_POS); 38 auto cornTopRight = rect.GetCornerRadius(RoundRect::CornerPos::TOP_RIGHT_POS); 39 auto cornBottomLeft = rect.GetCornerRadius(RoundRect::CornerPos::BOTTOM_LEFT_POS); 40 auto cornBottomRight = rect.GetCornerRadius(RoundRect::CornerPos::BOTTOM_RIGHT_POS); 41 #ifndef USE_ROSEN_DRAWING 42 roundRect_.SetRect( 43 RSRect(rect.GetRect().Left(), rect.GetRect().Top(), rect.GetRect().Right(), rect.GetRect().Bottom())); 44 InitRoundRect(cornTopLeft, cornTopRight, cornBottomLeft, cornBottomRight); 45 #else 46 roundRect_.SetRect( 47 RSRect(rect.GetRect().Left(), rect.GetRect().Top(), rect.GetRect().Right(), rect.GetRect().Bottom())); 48 InitRoundRect(cornTopLeft, cornTopRight, cornBottomLeft, cornBottomRight); 49 #endif 50 InitOverlayRect(rect, borderWidth); 51 } 52 GetOverlayRect()53 std::shared_ptr<Rosen::RectF> GetOverlayRect() 54 { 55 return overlayRect_; 56 } 57 SetPaintColor(const Color & paintColor)58 void SetPaintColor(const Color& paintColor) 59 { 60 paintColor_ = paintColor; 61 } 62 SetFrameNode(const RefPtr<FrameNode> & frameNode)63 void SetFrameNode(const RefPtr<FrameNode>& frameNode) 64 { 65 weakFrameNode_ = AceType::WeakClaim(AceType::RawPtr(frameNode)); 66 } 67 68 protected: 69 RSRoundRect roundRect_; 70 Color paintColor_; 71 float paintWidthPx_ = 0.0f; 72 WeakPtr<FrameNode> weakFrameNode_; 73 74 private: InitOverlayRect(const RoundRect & rect,float borderWidth)75 void InitOverlayRect(const RoundRect& rect, float borderWidth) 76 { 77 RSScalar halfDenominator = 2.0f; 78 overlayRect_ = std::make_shared<Rosen::RectF>(rect.GetRect().Left() - borderWidth / halfDenominator, 79 rect.GetRect().Top() - borderWidth / halfDenominator, rect.GetRect().Width() + borderWidth, 80 rect.GetRect().Height() + borderWidth); 81 paintWidthPx_ = borderWidth; 82 } 83 InitRoundRect(EdgeF cornTopLeft,EdgeF cornTopRight,EdgeF cornBottomLeft,EdgeF cornBottomRight)84 void InitRoundRect(EdgeF cornTopLeft, EdgeF cornTopRight, EdgeF cornBottomLeft, EdgeF cornBottomRight) 85 { 86 roundRect_.SetCornerRadius(RSRoundRect::CornerPos::TOP_LEFT_POS, cornTopLeft.x, cornTopLeft.y); 87 roundRect_.SetCornerRadius(RSRoundRect::CornerPos::TOP_RIGHT_POS, cornTopRight.x, cornTopRight.y); 88 roundRect_.SetCornerRadius(RSRoundRect::CornerPos::BOTTOM_LEFT_POS, cornBottomLeft.x, cornBottomLeft.y); 89 roundRect_.SetCornerRadius(RSRoundRect::CornerPos::BOTTOM_RIGHT_POS, cornBottomRight.x, cornBottomRight.y); 90 } 91 92 std::shared_ptr<Rosen::RectF> overlayRect_; 93 }; 94 95 } // namespace OHOS::Ace::NG 96 97 #endif