1 /*
2  * Copyright (c) 2022-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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_PATTERN_H
18 
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/base/inspector_filter.h"
21 #include "core/components_ng/event/event_hub.h"
22 #include "core/common/container.h"
23 #include "core/components_ng/pattern/checkbox/checkbox_model.h"
24 #include "core/components_ng/pattern/checkbox/checkbox_paint_property.h"
25 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_accessibility_property.h"
26 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_event_hub.h"
27 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_layout_algorithm.h"
28 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_modifier.h"
29 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_method.h"
30 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_property.h"
31 #include "core/components_ng/pattern/pattern.h"
32 
33 namespace OHOS::Ace::NG {
34 
35 class CheckBoxGroupPattern : public Pattern {
36     DECLARE_ACE_TYPE(CheckBoxGroupPattern, Pattern);
37 
38 public:
39     CheckBoxGroupPattern() = default;
40     ~CheckBoxGroupPattern() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return true;
45     }
46 
CreatePaintProperty()47     RefPtr<PaintProperty> CreatePaintProperty() override
48     {
49         return MakeRefPtr<CheckBoxGroupPaintProperty>();
50     }
51 
CreateLayoutAlgorithm()52     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
53     {
54         return MakeRefPtr<CheckBoxGroupLayoutAlgorithm>();
55     }
56 
CreateNodePaintMethod()57     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
58     {
59         auto host = GetHost();
60         CHECK_NULL_RETURN(host, nullptr);
61         auto paintProperty = host->GetPaintProperty<CheckBoxGroupPaintProperty>();
62         paintProperty->SetHost(host);
63         auto eventHub = host->GetEventHub<EventHub>();
64         CHECK_NULL_RETURN(eventHub, nullptr);
65         auto enabled = eventHub->IsEnabled();
66         if (!checkBoxGroupModifier_) {
67             CheckBoxGroupModifier::Parameters paintParameters;
68             InitializeModifierParam(paintParameters);
69             UpdateModifierParam(paintParameters);
70             checkBoxGroupModifier_ = AceType::MakeRefPtr<CheckBoxGroupModifier>(paintParameters);
71         }
72         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
73             auto checkboxStyle = CheckBoxStyle::CIRCULAR_STYLE;
74             if (paintProperty->HasCheckBoxGroupSelectedStyle()) {
75                 checkboxStyle = paintProperty->GetCheckBoxGroupSelectedStyleValue(CheckBoxStyle::CIRCULAR_STYLE);
76             }
77             checkBoxGroupModifier_->SetCheckboxGroupStyle(checkboxStyle);
78         }
79         auto paintMethod = MakeRefPtr<CheckBoxGroupPaintMethod>(checkBoxGroupModifier_);
80         paintMethod->SetEnabled(enabled);
81         paintMethod->SetUiStatus(uiStatus_);
82         paintMethod->SetTouchHoverAnimationType(touchHoverType_);
83         paintMethod->SetHotZoneOffset(hotZoneOffset_);
84         paintMethod->SetHotZoneSize(hotZoneSize_);
85         return paintMethod;
86     }
87 
CreateAccessibilityProperty()88     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
89     {
90         return MakeRefPtr<CheckBoxGroupAccessibilityProperty>();
91     }
92 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)93     bool OnDirtyLayoutWrapperSwap(
94         const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override
95     {
96         auto geometryNode = dirty->GetGeometryNode();
97         offset_ = geometryNode->GetContentOffset();
98         size_ = geometryNode->GetContentSize();
99         if (!isUserSetResponseRegion_) {
100             AddHotZoneRect();
101         }
102         if (Container::GreatOrEqualAPITargetVersion(PlatformVersion::VERSION_TWELVE)) {
103             UpdateCheckBoxStyle();
104         }
105         return true;
106     }
107 
CreateEventHub()108     RefPtr<EventHub> CreateEventHub() override
109     {
110         return MakeRefPtr<CheckBoxGroupEventHub>();
111     }
112 
GetPreGroup()113     const std::optional<std::string>& GetPreGroup()
114     {
115         return preGroup_;
116     }
117 
SetPreGroup(const std::string & group)118     void SetPreGroup(const std::string& group)
119     {
120         preGroup_ = group;
121     }
122 
GetIsAddToMap()123     bool GetIsAddToMap() const
124     {
125         return isAddToMap_;
126     }
127 
SetIsAddToMap(bool isAddToMap)128     void SetIsAddToMap(bool isAddToMap)
129     {
130         isAddToMap_ = isAddToMap;
131     }
132 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)133     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
134     {
135         isUserSetResponseRegion_ = isUserSetResponseRegion;
136     }
137 
SetUpdateFlag(bool updateFlag)138     void SetUpdateFlag(bool updateFlag)
139     {
140         updateFlag_ = updateFlag;
141     }
142 
SetSkipFlag(bool skipFlag)143     void SetSkipFlag(bool skipFlag)
144     {
145         skipFlag_ = skipFlag;
146     }
147 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)148     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override
149     {
150         Pattern::ToJsonValue(json, filter);
151         /* no fixed attr below, just return */
152         if (filter.IsFastFilter()) {
153             return;
154         }
155         auto host = GetHost();
156         CHECK_NULL_VOID(host);
157         auto checkBoxEventHub = host->GetEventHub<NG::CheckBoxGroupEventHub>();
158         auto group = checkBoxEventHub ? checkBoxEventHub->GetGroupName() : "";
159         json->PutExtAttr("group", group.c_str(), filter);
160     }
161 
ResetUIStatus()162     void ResetUIStatus()
163     {
164         uiStatus_ = UIStatus::UNSELECTED;
165     }
166 
167     RefPtr<GroupManager> GetGroupManager();
168 
169     FocusPattern GetFocusPattern() const override;
170     void UpdateUIStatus(bool check);
171     void UpdateModifierParam(CheckBoxGroupModifier::Parameters& paintParameters);
172     void OnColorConfigurationUpdate() override;
173     void MarkIsSelected(bool isSelected);
174     void OnAttachToMainTree() override;
175     void UpdateCheckBoxStyle();
176 
177 private:
178     void OnAttachToFrameNode() override;
179     void OnDetachFromFrameNode(FrameNode* frameNode) override;
180     void OnModifyDone() override;
181     void OnAfterModifyDone() override;
182     void InitClickEvent();
183     void InitTouchEvent();
184     void InitMouseEvent();
185     void OnClick();
186     void OnTouchDown();
187     void OnTouchUp();
188     void HandleMouseEvent(bool isHover);
189     void UpdateUnSelect();
190     void UpdateState();
191     void UpdateGroupCheckStatus(const RefPtr<FrameNode>& frameNode, bool select);
192     void UpdateRepeatedGroupStatus(const RefPtr<FrameNode>& frameNode, bool select);
193     void UpdateCheckBoxStatus(const RefPtr<FrameNode>& frameNode, bool select);
194     // Init key event
195     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
196     void GetInnerFocusPaintRect(RoundRect& paintRect);
197     void AddHotZoneRect();
198     void RemoveLastHotZoneRect() const;
199     void InitializeModifierParam(CheckBoxGroupModifier::Parameters& paintParameters);
200     void SetAccessibilityAction();
201     void UpdateSelectStatus(bool isSelected);
202     void InitPreGroup();
203     std::string GetGroupNameWithNavId();
204 
205     void SetCheckBoxStyle(const RefPtr<CheckBoxPaintProperty>& paintProperty, const RefPtr<FrameNode>& frameNode,
206         CheckBoxStyle checkBoxGroupStyle);
207     void GetCheckBoxGroupStyle(const RefPtr<FrameNode>& frameNode, CheckBoxStyle& checkboxGroupStyle);
208     void InnerFocusPaintCircle(RoundRect& paintRect);
209     std::optional<std::string> preGroup_;
210     bool isAddToMap_ = true;
211     RefPtr<ClickEvent> clickListener_;
212     RefPtr<TouchEventImpl> touchListener_;
213     RefPtr<InputEvent> mouseEvent_;
214     RefPtr<CheckBoxGroupModifier> checkBoxGroupModifier_;
215     WeakPtr<GroupManager> groupManager_;
216     bool isHover_ = false;
217     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
218     bool updateFlag_ = false;
219     bool skipFlag_ = false;
220     bool isFirstCreated_ = true;
221     bool isUserSetResponseRegion_ = false;
222     UIStatus uiStatus_ = UIStatus::UNSELECTED;
223     Dimension hotZoneHorizontalPadding_;
224     Dimension hotZoneVerticalPadding_;
225     OffsetF offset_;
226     SizeF size_;
227     OffsetF hotZoneOffset_;
228     SizeF hotZoneSize_;
229     bool initSelected_ = false;
230     std::optional<std::string> currentNavId_ = std::nullopt;
231 
232     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxGroupPattern);
233 };
234 } // namespace OHOS::Ace::NG
235 
236 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_PATTERN_H
237