1 /*
2  * Copyright (c) 2023 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 #include "core/components_ng/pattern/panel/close_icon_pattern.h"
17 
18 #include "base/utils/utils.h"
19 #include "core/components_ng/base/frame_node.h"
20 #include "core/components_ng/pattern/image/image_pattern.h"
21 
22 namespace OHOS::Ace::NG {
OnModifyDone()23 void CloseIconPattern::OnModifyDone()
24 {
25     Pattern::OnModifyDone();
26     if (isFirstLayout_) {
27         InitCloseIcon();
28         isFirstLayout_ = false;
29     }
30     InitButtonEvent();
31     auto host = GetHost();
32     CHECK_NULL_VOID(host);
33     host->MarkDirtyNode(PROPERTY_UPDATE_LAYOUT);
34 }
35 
InitCloseIcon()36 void CloseIconPattern::InitCloseIcon()
37 {
38     auto closeIconLayoutProperty = GetCloseIconLayoutProperty();
39     CHECK_NULL_VOID(closeIconLayoutProperty);
40     auto buttonNode = FrameNode::GetOrCreateFrameNode(V2::BUTTON_ETS_TAG,
41         ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<ButtonPattern>(); });
42     auto buttonLayoutProperty = buttonNode->GetLayoutProperty<ButtonLayoutProperty>();
43     buttonNode->GetRenderContext()->UpdateBackgroundColor(Color::TRANSPARENT);
44     CHECK_NULL_VOID(buttonLayoutProperty);
45     buttonLayoutProperty->UpdateUserDefinedIdealSize(
46         CalcSize(CalcLength(closeIconLayoutProperty->GetCloseIconWidthValue()),
47             CalcLength(closeIconLayoutProperty->GetCloseIconHeightValue())));
48     buttonNode->MarkModifyDone();
49     auto pattern = buttonNode->GetPattern<ButtonPattern>();
50     CHECK_NULL_VOID(pattern);
51     pattern->SetSkipColorConfigurationUpdate();
52     auto imageNode = FrameNode::CreateFrameNode(
53         V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(), AceType::MakeRefPtr<ImagePattern>());
54     auto imageLayoutProperty = imageNode->GetLayoutProperty<ImageLayoutProperty>();
55     CHECK_NULL_VOID(imageLayoutProperty);
56     imageLayoutProperty->UpdateUserDefinedIdealSize(
57         CalcSize(CalcLength(closeIconLayoutProperty->GetCloseIconWidthValue()),
58             CalcLength(closeIconLayoutProperty->GetCloseIconHeightValue())));
59     imageLayoutProperty->UpdateImageFit(ImageFit::FILL);
60     ImageSourceInfo imageSourceInfo;
61     imageSourceInfo.SetResourceId(InternalResource::ResourceId::IC_BOTTOMSHEET_CLOSE_SVG);
62     imageLayoutProperty->UpdateImageSourceInfo(imageSourceInfo);
63     imageNode->MarkModifyDone();
64     auto host = GetHost();
65     CHECK_NULL_VOID(host);
66     auto renderContext = host->GetRenderContext();
67     CHECK_NULL_VOID(renderContext);
68     BorderRadiusProperty radius;
69     radius.SetRadius(closeIconLayoutProperty->GetCloseIconRadiusValue());
70     renderContext->UpdateBorderRadius(radius);
71     host->AddChild(buttonNode);
72     buttonNode->AddChild(imageNode);
73 }
74 
InitButtonEvent()75 void CloseIconPattern::InitButtonEvent()
76 {
77     auto host = GetHost();
78     CHECK_NULL_VOID(host);
79     auto buttonNode = DynamicCast<FrameNode>(host->GetFirstChild());
80     CHECK_NULL_VOID(buttonNode);
81     auto gestureHub = buttonNode->GetOrCreateGestureEventHub();
82     auto clickButtonCallback = [weak = WeakClaim(this)](const GestureEvent& info) {
83         auto pattern = weak.Upgrade();
84         CHECK_NULL_VOID(pattern);
85         pattern->ButtonClickEvent();
86     };
87     if (buttonClickListenr_) {
88         gestureHub->RemoveClickEvent(buttonClickListenr_);
89     }
90     buttonClickListenr_ = MakeRefPtr<ClickEvent>(std::move(clickButtonCallback));
91     gestureHub->AddClickEvent(buttonClickListenr_);
92 }
93 
ButtonClickEvent()94 void CloseIconPattern::ButtonClickEvent()
95 {
96     if (!clickButtonCallback_) {
97         return;
98     }
99     clickButtonCallback_();
100 }
101 } // namespace OHOS::Ace::NG
102