• 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_CHECKBOX_CHECKBOX_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_PATTERN_H
18 
19 #include "base/geometry/axis.h"
20 #include "base/memory/referenced.h"
21 #include "base/utils/utils.h"
22 #include "core/components/checkable/checkable_theme.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/event/event_hub.h"
25 #include "core/components_ng/pattern/checkbox/checkbox_accessibility_property.h"
26 #include "core/components_ng/pattern/checkbox/checkbox_event_hub.h"
27 #include "core/components_ng/pattern/checkbox/checkbox_layout_algorithm.h"
28 #include "core/components_ng/pattern/checkbox/checkbox_paint_method.h"
29 #include "core/components_ng/pattern/checkbox/checkbox_paint_property.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/pipeline_ng/pipeline_context.h"
32 
33 namespace OHOS::Ace::NG {
34 
35 class CheckBoxPattern : public Pattern {
36     DECLARE_ACE_TYPE(CheckBoxPattern, Pattern);
37 
38 public:
39     CheckBoxPattern() = default;
40     ~CheckBoxPattern() 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<CheckBoxPaintProperty>();
50     }
51 
CreateLayoutAlgorithm()52     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
53     {
54         return MakeRefPtr<CheckBoxLayoutAlgorithm>();
55     }
56 
CreateNodePaintMethod()57     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
58     {
59         auto host = GetHost();
60         CHECK_NULL_RETURN(host, nullptr);
61         auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>();
62         auto isSelect = paintProperty->GetCheckBoxSelectValue(false);
63         if (!checkboxModifier_) {
64             auto pipeline = PipelineBase::GetCurrentContext();
65             auto checkBoxTheme = pipeline->GetTheme<CheckboxTheme>();
66             auto boardColor = isSelect ? paintProperty->GetCheckBoxSelectedColorValue(checkBoxTheme->GetActiveColor())
67                                        : checkBoxTheme->GetInactivePointColor();
68             auto checkColor = isSelect ? checkBoxTheme->GetPointColor() : Color::TRANSPARENT;
69             auto borderColor = isSelect ? Color::TRANSPARENT : checkBoxTheme->GetInactiveColor();
70             auto shadowColor = isSelect ? checkBoxTheme->GetShadowColor() : Color::TRANSPARENT;
71             checkboxModifier_ =
72                 AceType::MakeRefPtr<CheckBoxModifier>(isSelect, boardColor, checkColor, borderColor, shadowColor);
73         }
74         auto paintMethod = MakeRefPtr<CheckBoxPaintMethod>(checkboxModifier_);
75         auto eventHub = host->GetEventHub<EventHub>();
76         CHECK_NULL_RETURN(eventHub, nullptr);
77         auto enabled = eventHub->IsEnabled();
78         paintMethod->SetEnabled(enabled);
79         paintMethod->SetTouchHoverAnimationType(touchHoverType_);
80         return paintMethod;
81     }
82 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)83     bool OnDirtyLayoutWrapperSwap(
84         const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override
85     {
86         auto geometryNode = dirty->GetGeometryNode();
87         offset_ = geometryNode->GetContentOffset();
88         size_ = geometryNode->GetContentSize();
89         if (!isUserSetResponseRegion_) {
90             AddHotZoneRect();
91         }
92         return true;
93     }
94 
CreateEventHub()95     RefPtr<EventHub> CreateEventHub() override
96     {
97         return MakeRefPtr<CheckBoxEventHub>();
98     }
99 
CreateAccessibilityProperty()100     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
101     {
102         return MakeRefPtr<CheckBoxAccessibilityProperty>();
103     }
104 
GetPreName()105     const std::optional<std::string>& GetPreName()
106     {
107         return preName_;
108     }
109 
GetPreGroup()110     const std::optional<std::string>& GetPreGroup()
111     {
112         return preGroup_;
113     }
114 
GetPrePageId()115     int32_t GetPrePageId() const
116     {
117         return prePageId_;
118     }
119 
SetPreName(const std::string & name)120     void SetPreName(const std::string& name)
121     {
122         preName_ = name;
123     }
124 
SetPreGroup(const std::string & group)125     void SetPreGroup(const std::string& group)
126     {
127         preGroup_ = group;
128     }
129 
SetPrePageId(int32_t pageId)130     void SetPrePageId(int32_t pageId)
131     {
132         prePageId_ = pageId;
133     }
134 
SetLastSelect(bool select)135     void SetLastSelect(bool select)
136     {
137         lastSelect_ = select;
138     }
139 
SetIsUserSetResponseRegion(bool isUserSetResponseRegion)140     void SetIsUserSetResponseRegion(bool isUserSetResponseRegion)
141     {
142         isUserSetResponseRegion_ = isUserSetResponseRegion;
143     }
144 
ToJsonValue(std::unique_ptr<JsonValue> & json)145     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
146     {
147         Pattern::ToJsonValue(json);
148         auto host = GetHost();
149         CHECK_NULL_VOID(host);
150         auto checkBoxEventHub = host->GetEventHub<NG::CheckBoxEventHub>();
151         auto name = checkBoxEventHub ? checkBoxEventHub->GetName() : "";
152         auto group = checkBoxEventHub ? checkBoxEventHub->GetGroupName() : "";
153         json->Put("name", name.c_str());
154         json->Put("group", group.c_str());
155         json->Put("type", "ToggleType.Checkbox");
156         auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>();
157         auto select = paintProperty->GetCheckBoxSelectValue(false);
158         json->Put("select", select ? "true" : "false");
159     }
160 
161     FocusPattern GetFocusPattern() const override;
162     void UpdateUIStatus(bool check);
163 
164     std::string ProvideRestoreInfo() override;
165     void OnRestoreInfo(const std::string& restoreInfo) override;
166     void OnColorConfigurationUpdate() override;
167 
168 private:
169     void OnAttachToFrameNode() override;
170     void OnDetachFromFrameNode(FrameNode* frameNode) override;
171     void OnModifyDone() override;
172     void InitClickEvent();
173     void InitTouchEvent();
174     void InitMouseEvent();
175     void OnClick();
176     void OnTouchDown();
177     void OnTouchUp();
178     void HandleMouseEvent(bool isHover);
179     void CheckPageNode();
180     void UpdateState();
181     void UpdateUnSelect();
182     void UpdateCheckBoxGroupStatus(const RefPtr<FrameNode>& frameNode,
183         std::unordered_map<std::string, std::list<WeakPtr<FrameNode>>>& checkBoxGroupMap, bool isSelected);
184     void UpdateCheckBoxGroupStatusWhenDetach(const FrameNode* frameNode,
185         std::unordered_map<std::string, std::list<WeakPtr<FrameNode>>>& checkBoxGroupMap);
186     void CheckBoxGroupIsTrue();
187     // Init key event
188     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
189     void GetInnerFocusPaintRect(RoundRect& paintRect);
190     void AddHotZoneRect();
191     void RemoveLastHotZoneRect() const;
192 
193     std::optional<std::string> preName_;
194     std::optional<std::string> preGroup_;
195     int32_t prePageId_ = 0;
196     bool lastSelect_ = false;
197 
198     RefPtr<ClickEvent> clickListener_;
199     RefPtr<TouchEventImpl> touchListener_;
200     RefPtr<InputEvent> mouseEvent_;
201     bool isTouch_ = false;
202     bool isHover_ = false;
203     bool isFirstCreated_ = true;
204     bool isUserSetResponseRegion_ = false;
205     UIStatus uiStatus_ = UIStatus::UNSELECTED;
206     Dimension hotZoneHorizontalPadding_;
207     Dimension hotZoneVerticalPadding_;
208     OffsetF offset_;
209     SizeF size_;
210     OffsetF hotZoneOffset_;
211     SizeF hotZoneSize_;
212     TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE;
213 
214     RefPtr<CheckBoxModifier> checkboxModifier_;
215 
216     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxPattern);
217 };
218 } // namespace OHOS::Ace::NG
219 
220 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_PATTERN_H
221