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/base/inspector_filter.h" 25 #include "core/components_ng/event/event_hub.h" 26 #include "core/components_ng/pattern/toggle/toggle_model_ng.h" 27 #include "core/components_ng/pattern/checkbox/checkbox_accessibility_property.h" 28 #include "core/components_ng/pattern/checkbox/checkbox_event_hub.h" 29 #include "core/components_ng/pattern/checkbox/checkbox_layout_algorithm.h" 30 #include "core/components_ng/pattern/checkbox/checkbox_model_ng.h" 31 #include "core/components_ng/pattern/checkbox/checkbox_paint_method.h" 32 #include "core/components_ng/pattern/checkbox/checkbox_paint_property.h" 33 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_paint_property.h" 34 #include "core/components_ng/pattern/pattern.h" 35 #include "core/pipeline_ng/pipeline_context.h" 36 37 namespace OHOS::Ace::NG { 38 class CheckBoxPattern : public Pattern { 39 DECLARE_ACE_TYPE(CheckBoxPattern, Pattern); 40 41 public: 42 CheckBoxPattern() = default; 43 ~CheckBoxPattern() override = default; 44 IsAtomicNode()45 bool IsAtomicNode() const override 46 { 47 return true; 48 } 49 CreatePaintProperty()50 RefPtr<PaintProperty> CreatePaintProperty() override 51 { 52 return MakeRefPtr<CheckBoxPaintProperty>(); 53 } 54 CreateLayoutAlgorithm()55 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 56 { 57 return MakeRefPtr<CheckBoxLayoutAlgorithm>(); 58 } 59 CreateNodePaintMethod()60 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 61 { 62 auto host = GetHost(); 63 CHECK_NULL_RETURN(host, nullptr); 64 auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>(); 65 paintProperty->SetHost(host); 66 if (!paintMethod_) { 67 paintMethod_ = MakeRefPtr<CheckBoxPaintMethod>(); 68 } 69 CheckBoxStyle checkboxStyle = CheckBoxStyle::CIRCULAR_STYLE; 70 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_ELEVEN)) { 71 checkboxStyle = CheckBoxStyle::CIRCULAR_STYLE; 72 } else { 73 checkboxStyle = CheckBoxStyle::SQUARE_STYLE; 74 } 75 if (paintProperty->HasCheckBoxSelectedStyle()) { 76 checkboxStyle = paintProperty->GetCheckBoxSelectedStyleValue(CheckBoxStyle::CIRCULAR_STYLE); 77 } 78 paintMethod_->SetCheckboxStyle(checkboxStyle); 79 paintMethod_->SetUseContentModifier(UseContentModifier()); 80 paintMethod_->SetHasBuilder(builder_.has_value()); 81 host->SetCheckboxFlag(true); 82 auto eventHub = host->GetEventHub<EventHub>(); 83 CHECK_NULL_RETURN(eventHub, nullptr); 84 auto enabled = eventHub->IsEnabled(); 85 paintMethod_->SetEnabled(enabled); 86 paintMethod_->SetTouchHoverAnimationType(touchHoverType_); 87 return paintMethod_; 88 } 89 OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)90 bool OnDirtyLayoutWrapperSwap( 91 const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override 92 { 93 auto geometryNode = dirty->GetGeometryNode(); 94 offset_ = geometryNode->GetContentOffset(); 95 size_ = geometryNode->GetContentSize(); 96 if (!isUserSetResponseRegion_) { 97 AddHotZoneRect(); 98 } 99 return true; 100 } 101 CreateEventHub()102 RefPtr<EventHub> CreateEventHub() override 103 { 104 return MakeRefPtr<CheckBoxEventHub>(); 105 } 106 CreateAccessibilityProperty()107 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 108 { 109 return MakeRefPtr<CheckBoxAccessibilityProperty>(); 110 } 111 GetPreName()112 const std::optional<std::string>& GetPreName() 113 { 114 return preName_; 115 } 116 GetPreGroup()117 const std::optional<std::string>& GetPreGroup() 118 { 119 return preGroup_; 120 } 121 GetPrePageId()122 int32_t GetPrePageId() const 123 { 124 return prePageId_; 125 } 126 SetPreName(const std::string & name)127 void SetPreName(const std::string& name) 128 { 129 preName_ = name; 130 } 131 SetPreGroup(const std::string & group)132 void SetPreGroup(const std::string& group) 133 { 134 preGroup_ = group; 135 } 136 SetPrePageId(int32_t pageId)137 void SetPrePageId(int32_t pageId) 138 { 139 prePageId_ = pageId; 140 } 141 142 void MarkIsSelected(bool isSelected); SetLastSelect(bool select)143 void SetLastSelect(bool select) 144 { 145 lastSelect_ = select; 146 } 147 SetBuilderFunc(CheckBoxMakeCallback && makeFunc)148 void SetBuilderFunc(CheckBoxMakeCallback&& makeFunc) 149 { 150 if (makeFunc == nullptr) { 151 makeFunc_ = std::nullopt; 152 OnModifyDone(); 153 return; 154 } 155 makeFunc_ = std::move(makeFunc); 156 } 157 GetContentModifierNode()158 RefPtr<FrameNode> GetContentModifierNode() 159 { 160 return contentModifierNode_; 161 } 162 SetToggleBuilderFunc(SwitchMakeCallback && toggleMakeFunc)163 void SetToggleBuilderFunc(SwitchMakeCallback&& toggleMakeFunc) 164 { 165 if (toggleMakeFunc == nullptr) { 166 toggleMakeFunc_ = std::nullopt; 167 OnModifyDone(); 168 return; 169 } 170 toggleMakeFunc_ = std::move(toggleMakeFunc); 171 } 172 UseContentModifier()173 bool UseContentModifier() 174 { 175 return contentModifierNode_ != nullptr; 176 } 177 178 void SetCheckBoxSelect(bool value); 179 SetIsUserSetResponseRegion(bool isUserSetResponseRegion)180 void SetIsUserSetResponseRegion(bool isUserSetResponseRegion) 181 { 182 isUserSetResponseRegion_ = isUserSetResponseRegion; 183 } 184 SetIndicatorBuilder(const std::optional<std::function<void ()>> & buildFunc)185 void SetIndicatorBuilder(const std::optional<std::function<void()>>& buildFunc) 186 { 187 builder_ = buildFunc; 188 } 189 ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter)190 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override 191 { 192 Pattern::ToJsonValue(json, filter); 193 /* no fixed attr below, just return */ 194 if (filter.IsFastFilter()) { 195 return; 196 } 197 auto host = GetHost(); 198 CHECK_NULL_VOID(host); 199 auto checkBoxEventHub = host->GetEventHub<NG::CheckBoxEventHub>(); 200 auto name = checkBoxEventHub ? checkBoxEventHub->GetName() : ""; 201 auto group = checkBoxEventHub ? checkBoxEventHub->GetGroupName() : ""; 202 json->PutExtAttr("name", name.c_str(), filter); 203 json->PutExtAttr("group", group.c_str(), filter); 204 json->PutExtAttr("type", "ToggleType.Checkbox", filter); 205 auto paintProperty = host->GetPaintProperty<CheckBoxPaintProperty>(); 206 auto select = paintProperty->GetCheckBoxSelectValue(false); 207 json->PutExtAttr("select", select ? "true" : "false", filter); 208 } 209 SetOriginalCheckboxStyle(OriginalCheckBoxStyle style)210 void SetOriginalCheckboxStyle(OriginalCheckBoxStyle style) 211 { 212 originalStyle_ = style; 213 } 214 GetOriginalCheckboxStyle()215 OriginalCheckBoxStyle GetOriginalCheckboxStyle() 216 { 217 return originalStyle_; 218 } 219 220 FocusPattern GetFocusPattern() const override; 221 void UpdateUIStatus(bool check); 222 223 std::string ProvideRestoreInfo() override; 224 void OnRestoreInfo(const std::string& restoreInfo) override; 225 void OnColorConfigurationUpdate() override; 226 void OnAttachToMainTree() override; 227 void StartCustomNodeAnimation(bool select); 228 RefPtr<GroupManager> GetGroupManager(); 229 230 private: 231 void OnAttachToFrameNode() override; 232 void OnDetachFromFrameNode(FrameNode* frameNode) override; 233 void OnModifyDone() override; 234 void OnAfterModifyDone() override; 235 void InitClickEvent(); 236 void InitTouchEvent(); 237 void InitMouseEvent(); 238 void OnClick(); 239 void OnTouchDown(); 240 void OnTouchUp(); 241 void HandleMouseEvent(bool isHover); 242 void CheckPageNode(); 243 void LoadBuilder(); 244 void UpdateIndicator(); 245 void SetBuilderNodeHidden(); 246 void StartEnterAnimation(); 247 void StartExitAnimation(); 248 void UpdateState(); 249 void UpdateUnSelect(); 250 void CheckBoxGroupIsTrue(); 251 void SetPrePageIdToLastPageId(); 252 // Init key event 253 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 254 void GetInnerFocusPaintRect(RoundRect& paintRect); 255 void AddHotZoneRect(); 256 void RemoveLastHotZoneRect() const; 257 void SetAccessibilityAction(); 258 void UpdateSelectStatus(bool isSelected); 259 void ChangeSelfStatusAndNotify(const RefPtr<CheckBoxPaintProperty>& paintProperty); 260 void ChangeGroupStatusAndNotify(const RefPtr<FrameNode>& checkBoxGroupNode, const std::vector<std::string>& vec, 261 bool haveCheckBoxSelected, bool isAllCheckBoxSelected); 262 std::string GetGroupNameWithNavId(); 263 void FireBuilder(); 264 RefPtr<FrameNode> BuildContentModifierNode(); 265 void InitCheckBoxStatusByGroup(RefPtr<FrameNode> checkBoxGroupNode, 266 const RefPtr<CheckBoxGroupPaintProperty>& groupPaintProperty, const std::list<RefPtr<FrameNode>>& list); 267 void UpdateCheckBoxGroupStatus(RefPtr<FrameNode> checkBoxGroupNode, const std::list<RefPtr<FrameNode>>& list); 268 269 std::optional<CheckBoxMakeCallback> makeFunc_; 270 std::optional<SwitchMakeCallback> toggleMakeFunc_; 271 RefPtr<FrameNode> contentModifierNode_; 272 std::optional<std::string> preName_; 273 std::optional<std::string> preGroup_; 274 int32_t prePageId_ = 0; 275 bool lastSelect_ = false; 276 std::optional<std::string> currentNavId_ = std::nullopt; 277 278 RefPtr<ClickEvent> clickListener_; 279 RefPtr<TouchEventImpl> touchListener_; 280 RefPtr<InputEvent> mouseEvent_; 281 bool isTouch_ = false; 282 bool isHover_ = false; 283 bool isFirstCreated_ = true; 284 bool isUserSetResponseRegion_ = false; 285 UIStatus uiStatus_ = UIStatus::UNSELECTED; 286 Dimension hotZoneHorizontalPadding_; 287 Dimension hotZoneVerticalPadding_; 288 OffsetF offset_; 289 SizeF size_; 290 OffsetF hotZoneOffset_; 291 SizeF hotZoneSize_; 292 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 293 OriginalCheckBoxStyle originalStyle_ = OriginalCheckBoxStyle::CIRCULAR_STYLE; 294 RefPtr<FrameNode> builderNode_; 295 std::optional<std::function<void()>> builder_; 296 297 RefPtr<CheckBoxPaintMethod> paintMethod_; 298 WeakPtr<GroupManager> groupManager_; 299 300 ACE_DISALLOW_COPY_AND_MOVE(CheckBoxPattern); 301 }; 302 } // namespace OHOS::Ace::NG 303 304 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_PATTERN_H 305