• 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/event/event_hub.h"
21 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_accessibility_property.h"
22 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_event_hub.h"
23 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_layout_algorithm.h"
24 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_modifier.h"
25 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_method.h"
26 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_property.h"
27 #include "core/components_ng/pattern/pattern.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 class CheckBoxGroupPattern : public Pattern {
32     DECLARE_ACE_TYPE(CheckBoxGroupPattern, Pattern);
33 
34 public:
35     CheckBoxGroupPattern() = default;
36     ~CheckBoxGroupPattern() override = default;
37 
IsAtomicNode()38     bool IsAtomicNode() const override
39     {
40         return true;
41     }
42 
CreatePaintProperty()43     RefPtr<PaintProperty> CreatePaintProperty() override
44     {
45         return MakeRefPtr<CheckBoxGroupPaintProperty>();
46     }
47 
CreateLayoutAlgorithm()48     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
49     {
50         return MakeRefPtr<CheckBoxGroupLayoutAlgorithm>();
51     }
52 
CreateNodePaintMethod()53     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
54     {
55         auto host = GetHost();
56         CHECK_NULL_RETURN(host, nullptr);
57         auto eventHub = host->GetEventHub<EventHub>();
58         CHECK_NULL_RETURN(eventHub, nullptr);
59         auto enabled = eventHub->IsEnabled();
60         if (!checkBoxGroupModifier_) {
61             CheckBoxGroupModifier::Parameters paintParameters;
62             InitializeModifierParam(paintParameters);
63             UpdateModifierParam(paintParameters);
64             checkBoxGroupModifier_ = AceType::MakeRefPtr<CheckBoxGroupModifier>(paintParameters);
65         }
66         auto paintMethod = MakeRefPtr<CheckBoxGroupPaintMethod>(checkBoxGroupModifier_);
67         paintMethod->SetEnabled(enabled);
68         paintMethod->SetUiStatus(uiStatus_);
69         paintMethod->SetTouchHoverAnimationType(touchHoverType_);
70         paintMethod->SetHotZoneOffset(hotZoneOffset_);
71         paintMethod->SetHotZoneSize(hotZoneSize_);
72         return paintMethod;
73     }
74 
CreateAccessibilityProperty()75     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
76     {
77         return MakeRefPtr<CheckBoxGroupAccessibilityProperty>();
78     }
79 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)80     bool OnDirtyLayoutWrapperSwap(
81         const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override
82     {
83         auto geometryNode = dirty->GetGeometryNode();
84         offset_ = geometryNode->GetContentOffset();
85         size_ = geometryNode->GetContentSize();
86         if (!isUserSetResponseRegion_) {
87             AddHotZoneRect();
88         }
89         return true;
90     }
91 
CreateEventHub()92     RefPtr<EventHub> CreateEventHub() override
93     {
94         return MakeRefPtr<CheckBoxGroupEventHub>();
95     }
96 
GetPreGroup()97     const std::optional<std::string>& GetPreGroup()
98     {
99         return preGroup_;
100     }
101 
SetPreGroup(const std::string & group)102     void SetPreGroup(const std::string& group)
103     {
104         preGroup_ = group;
105     }
106 
GetIsAddToMap()107     bool GetIsAddToMap() const
108     {
109         return isAddToMap_;
110     }
111 
SetIsAddToMap(bool isAddToMap)112     void SetIsAddToMap(bool isAddToMap)
113     {
114         isAddToMap_ = isAddToMap;
115     }
116 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)117     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
118     {
119         isUserSetResponseRegion_ = isUserSetResponseRegion;
120     }
121 
ToJsonValue(std::unique_ptr<JsonValue> & json)122     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
123     {
124         Pattern::ToJsonValue(json);
125         auto host = GetHost();
126         CHECK_NULL_VOID(host);
127         auto checkBoxEventHub = host->GetEventHub<NG::CheckBoxGroupEventHub>();
128         auto group = checkBoxEventHub ? checkBoxEventHub->GetGroupName() : "";
129         json->Put("group", group.c_str());
130     }
131 
ResetUIStatus()132     void ResetUIStatus()
133     {
134         uiStatus_ = UIStatus::UNSELECTED;
135     }
136 
137     FocusPattern GetFocusPattern() const override;
138     void UpdateUIStatus(bool check);
139     void UpdateModifierParam(CheckBoxGroupModifier::Parameters& paintParameters);
140     void OnColorConfigurationUpdate() override;
141 
142 private:
143     void OnAttachToFrameNode() override;
144     void OnDetachFromFrameNode(FrameNode* frameNode) override;
145     void OnModifyDone() override;
146     void InitClickEvent();
147     void InitTouchEvent();
148     void InitMouseEvent();
149     void OnClick();
150     void OnTouchDown();
151     void OnTouchUp();
152     void HandleMouseEvent(bool isHover);
153     void UpdateUnSelect();
154     void UpdateState();
155     void UpdateGroupCheckStatus(const RefPtr<FrameNode>& frameNode, bool select);
156     void UpdateRepeatedGroupStatus(const RefPtr<FrameNode>& frameNode, bool select);
157     void UpdateCheckBoxStatus(const RefPtr<FrameNode>& frameNode,
158         std::unordered_map<std::string, std::list<WeakPtr<FrameNode>>> checkBoxGroupMap, const std::string& group,
159         bool select);
160     // Init key event
161     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
162     void GetInnerFocusPaintRect(RoundRect& paintRect);
163     void AddHotZoneRect();
164     void RemoveLastHotZoneRect() const;
165     void InitializeModifierParam(CheckBoxGroupModifier::Parameters& paintParameters);
166 
167     std::optional<std::string> preGroup_;
168     bool isAddToMap_ = true;
169     RefPtr<ClickEvent> clickListener_;
170     RefPtr<TouchEventImpl> touchListener_;
171     RefPtr<InputEvent> mouseEvent_;
172     RefPtr<CheckBoxGroupModifier> checkBoxGroupModifier_;
173     bool isHover_ = false;
174     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
175     bool isClick_ = false;
176     bool isFirstCreated_ = true;
177     bool isUserSetResponseRegion_ = false;
178     UIStatus uiStatus_ = UIStatus::UNSELECTED;
179     Dimension hotZoneHorizontalPadding_;
180     Dimension hotZoneVerticalPadding_;
181     OffsetF offset_;
182     SizeF size_;
183     OffsetF hotZoneOffset_;
184     SizeF hotZoneSize_;
185 
186     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxGroupPattern);
187 };
188 } // namespace OHOS::Ace::NG
189 
190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOXGROUP_CHECKBOXGROUP_PATTERN_H
191