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