• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "core/components/checkable/checkable_theme.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components_ng/event/event_hub.h"
24 #include "core/components_ng/pattern/checkbox/checkbox_event_hub.h"
25 #include "core/components_ng/pattern/checkbox/checkbox_layout_algorithm.h"
26 #include "core/components_ng/pattern/checkbox/checkbox_paint_method.h"
27 #include "core/components_ng/pattern/checkbox/checkbox_paint_property.h"
28 #include "core/components_ng/pattern/pattern.h"
29 #include "core/pipeline_ng/pipeline_context.h"
30 
31 namespace OHOS::Ace::NG {
32 
33 class CheckBoxPattern : public Pattern {
34     DECLARE_ACE_TYPE(CheckBoxPattern, Pattern);
35 
36 public:
37     CheckBoxPattern() = default;
38     ~CheckBoxPattern() override = default;
39 
IsAtomicNode()40     bool IsAtomicNode() const override
41     {
42         return true;
43     }
44 
CreatePaintProperty()45     RefPtr<PaintProperty> CreatePaintProperty() override
46     {
47         return MakeRefPtr<CheckBoxPaintProperty>();
48     }
49 
CreateLayoutAlgorithm()50     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
51     {
52         return MakeRefPtr<CheckBoxLayoutAlgorithm>();
53     }
54 
CreateNodePaintMethod()55     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
56     {
57         auto host = GetHost();
58         CHECK_NULL_RETURN(host, nullptr);
59         auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>();
60         auto isSelect = paintProperty->GetCheckBoxSelectValue(false);
61         if (!checkboxModifier_) {
62             auto pipeline = PipelineBase::GetCurrentContext();
63             auto checkBoxTheme = pipeline->GetTheme<CheckboxTheme>();
64             auto boardColor = isSelect ? paintProperty->GetCheckBoxSelectedColorValue(checkBoxTheme->GetActiveColor())
65                                        : checkBoxTheme->GetInactivePointColor();
66             auto checkColor = isSelect ? checkBoxTheme->GetPointColor() : Color::TRANSPARENT;
67             auto borderColor = isSelect ? Color::TRANSPARENT : checkBoxTheme->GetInactiveColor();
68             auto shadowColor = isSelect ? checkBoxTheme->GetShadowColor() : Color::TRANSPARENT;
69             checkboxModifier_ =
70                 AceType::MakeRefPtr<CheckBoxModifier>(isSelect, boardColor, checkColor, borderColor, shadowColor);
71         }
72         auto paintMethod = MakeRefPtr<CheckBoxPaintMethod>(checkboxModifier_);
73         auto eventHub = host->GetEventHub<EventHub>();
74         CHECK_NULL_RETURN(eventHub, nullptr);
75         auto enabled = eventHub->IsEnabled();
76         paintMethod->SetEnabled(enabled);
77         paintMethod->SetIsHover(isHover_);
78         return paintMethod;
79     }
80 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)81     bool OnDirtyLayoutWrapperSwap(
82         const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override
83     {
84         auto geometryNode = dirty->GetGeometryNode();
85         auto offset = geometryNode->GetContentOffset();
86         auto size = geometryNode->GetContentSize();
87         if (offset != offset_ || size != size_) {
88             offset_ = offset;
89             size_ = size;
90             AddHotZoneRect();
91         }
92         return true;
93     }
94 
CreateEventHub()95     RefPtr<EventHub> CreateEventHub() override
96     {
97         return MakeRefPtr<CheckBoxEventHub>();
98     }
99 
GetPreName()100     const std::optional<std::string>& GetPreName()
101     {
102         return preName_;
103     }
104 
GetPreGroup()105     const std::optional<std::string>& GetPreGroup()
106     {
107         return preGroup_;
108     }
109 
SetPreName(const std::string & name)110     void SetPreName(const std::string& name)
111     {
112         preName_ = name;
113     }
114 
SetPreGroup(const std::string & group)115     void SetPreGroup(const std::string& group)
116     {
117         preGroup_ = group;
118     }
119 
SetLastSelect(bool select)120     void SetLastSelect(bool select)
121     {
122         lastSelect_ = select;
123     }
124 
ToJsonValue(std::unique_ptr<JsonValue> & json)125     void ToJsonValue(std::unique_ptr<JsonValue>& json) const override
126     {
127         Pattern::ToJsonValue(json);
128         auto host = GetHost();
129         CHECK_NULL_VOID(host);
130         auto checkBoxEventHub = host->GetEventHub<NG::CheckBoxEventHub>();
131         auto name = checkBoxEventHub ? checkBoxEventHub->GetName() : "";
132         auto group = checkBoxEventHub ? checkBoxEventHub->GetGroupName() : "";
133         json->Put("name", name.c_str());
134         json->Put("group", group.c_str());
135         json->Put("type", "ToggleType.Checkbox");
136         auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>();
137         auto select = paintProperty->GetCheckBoxSelectValue(false);
138         json->Put("select", select ? "true" : "false");
139     }
140 
141     FocusPattern GetFocusPattern() const override;
142     void UpdateUIStatus(bool check);
143 
144 private:
145     void OnAttachToFrameNode() override;
146     void OnDetachFromFrameNode(FrameNode* frameNode) override;
147     void OnModifyDone() override;
148     void InitClickEvent();
149     void InitTouchEvent();
150     void InitMouseEvent();
151     void OnClick();
152     void OnTouchDown();
153     void OnTouchUp();
154     void HandleMouseEvent(bool isHover);
155     void UpdateState();
156     void UpdateUnSelect();
157     void UpdateCheckBoxGroupStatus(const RefPtr<FrameNode>& frameNode,
158         std::unordered_map<std::string, std::list<WeakPtr<FrameNode>>>& checkBoxGroupMap, bool isSelected);
159     void CheckBoxGroupIsTrue();
160     // Init key event
161     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
162     bool OnKeyEvent(const KeyEvent& event);
163     void GetInnerFocusPaintRect(RoundRect& paintRect);
164     void AddHotZoneRect();
165 
166     std::optional<std::string> preName_;
167     std::optional<std::string> preGroup_;
168     bool lastSelect_ = false;
169 
170     RefPtr<ClickEvent> clickListener_;
171     RefPtr<TouchEventImpl> touchListener_;
172     RefPtr<InputEvent> mouseEvent_;
173     bool isTouch_ = false;
174     bool isHover_ = false;
175     bool isFirstCreated_ = true;
176     UIStatus uiStatus_ = UIStatus::UNSELECTED;
177     Dimension hotZoneHorizontalPadding_;
178     Dimension hotZoneVerticalPadding_;
179     OffsetF offset_;
180     SizeF size_;
181     OffsetF hotZoneOffset_;
182     SizeF hotZoneSize_;
183 
184     RefPtr<CheckBoxModifier> checkboxModifier_;
185 
186     ACE_DISALLOW_COPY_AND_MOVE(CheckBoxPattern);
187 };
188 } // namespace OHOS::Ace::NG
189 
190 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_PATTERN_H
191