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