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/common/layout/constants.h" 23 #include "core/components_ng/base/inspector_filter.h" 24 #include "core/components_ng/event/event_hub.h" 25 #include "core/components_ng/pattern/toggle/toggle_model_ng.h" 26 #include "core/components_ng/pattern/overlay/group_manager.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/pattern.h" 34 #include "core/components_ng/pattern/picker/picker_type_define.h" 35 36 namespace OHOS::Ace::NG { 37 class CheckBoxGroupPaintProperty; 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 60 RefPtr<NodePaintMethod> CreateNodePaintMethod() override; 61 62 bool OnDirtyLayoutWrapperSwap( 63 const RefPtr<LayoutWrapper>& dirty, bool /*skipMeasure*/, bool /*skipLayout*/) override; 64 CreateEventHub()65 RefPtr<EventHub> CreateEventHub() override 66 { 67 return MakeRefPtr<CheckBoxEventHub>(); 68 } 69 CreateAccessibilityProperty()70 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 71 { 72 return MakeRefPtr<CheckBoxAccessibilityProperty>(); 73 } 74 GetPreName()75 const std::optional<std::string>& GetPreName() 76 { 77 return preName_; 78 } 79 GetPreGroup()80 const std::optional<std::string>& GetPreGroup() 81 { 82 return preGroup_; 83 } 84 GetPrePageId()85 int32_t GetPrePageId() const 86 { 87 return prePageId_; 88 } 89 SetPreName(const std::string & name)90 void SetPreName(const std::string& name) 91 { 92 preName_ = name; 93 } 94 SetPreGroup(const std::string & group)95 void SetPreGroup(const std::string& group) 96 { 97 preGroup_ = group; 98 } 99 SetPrePageId(int32_t pageId)100 void SetPrePageId(int32_t pageId) 101 { 102 prePageId_ = pageId; 103 } 104 105 void MarkIsSelected(bool isSelected); SetLastSelect(bool select)106 void SetLastSelect(bool select) 107 { 108 lastSelect_ = select; 109 } 110 111 void SetBuilderFunc(CheckBoxMakeCallback&& makeFunc); 112 GetContentModifierNode()113 RefPtr<FrameNode> GetContentModifierNode() 114 { 115 return contentModifierNode_; 116 } 117 118 void SetToggleBuilderFunc(SwitchMakeCallback&& toggleMakeFunc); 119 UseContentModifier()120 bool UseContentModifier() 121 { 122 return contentModifierNode_ != nullptr; 123 } 124 125 void SetCheckBoxSelect(bool value); 126 SetIsUserSetResponseRegion(bool isUserSetResponseRegion)127 void SetIsUserSetResponseRegion(bool isUserSetResponseRegion) 128 { 129 isUserSetResponseRegion_ = isUserSetResponseRegion; 130 } 131 SetIndicatorBuilder(const std::optional<std::function<void ()>> & buildFunc)132 void SetIndicatorBuilder(const std::optional<std::function<void()>>& buildFunc) 133 { 134 builder_ = buildFunc; 135 } 136 137 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 138 139 void ToTreeJson(std::unique_ptr<JsonValue>& json, const InspectorConfig& config) const override; 140 SetOriginalCheckboxStyle(OriginalCheckBoxStyle style)141 void SetOriginalCheckboxStyle(OriginalCheckBoxStyle style) 142 { 143 originalStyle_ = style; 144 } 145 GetOriginalCheckboxStyle()146 OriginalCheckBoxStyle GetOriginalCheckboxStyle() 147 { 148 return originalStyle_; 149 } 150 151 FocusPattern GetFocusPattern() const override; 152 void UpdateUIStatus(bool check); 153 154 std::string ProvideRestoreInfo() override; 155 void OnRestoreInfo(const std::string& restoreInfo) override; 156 void OnColorConfigurationUpdate() override; 157 void OnAttachToMainTree() override; 158 void OnAttachToMainTreeMultiThread(const RefPtr<FrameNode>& frameNode); 159 void StartCustomNodeAnimation(bool select); 160 RefPtr<GroupManager> GetGroupManager(); 161 SaveCheckboxSettingData(const CheckboxSettingData & checkboxSettingData)162 void SaveCheckboxSettingData(const CheckboxSettingData& checkboxSettingData) 163 { 164 checkboxSettingData_ = checkboxSettingData; 165 } 166 167 bool OnThemeScopeUpdate(int32_t themeScopeId) override; 168 169 void DumpInfo() override; SetIsUserSetMargin(bool isUserSetMargin)170 void SetIsUserSetMargin(bool isUserSetMargin) 171 { 172 isUserSetMargin_ = isUserSetMargin; 173 } 174 IsEnableMatchParent()175 bool IsEnableMatchParent() override 176 { 177 return true; 178 } 179 180 private: 181 void OnAttachToFrameNode() override; 182 void OnAttachToFrameNodeMultiThread(const RefPtr<FrameNode>& frameNode); 183 void OnDetachFromFrameNode(FrameNode* frameNode) override; 184 void OnDetachFromFrameNodeMultiThread(); 185 void OnDetachFromMainTree() override; 186 void OnDetachFromMainTreeMultiThread(const RefPtr<FrameNode>& frameNode); 187 void OnModifyDone() override; 188 void OnAfterModifyDone() override; 189 void InitClickEvent(); 190 void InitTouchEvent(); 191 void InitMouseEvent(); 192 void InitFocusEvent(); 193 void OnClick(); 194 void OnTouchDown(); 195 void OnTouchUp(); 196 void HandleMouseEvent(bool isHover); 197 void HandleFocusEvent(); 198 void HandleBlurEvent(); 199 void AddIsFocusActiveUpdateEvent(); 200 void RemoveIsFocusActiveUpdateEvent(); 201 void RegisterVisibleAreaChange(); 202 void OnIsFocusActiveUpdate(bool isFocusAcitve); 203 void CheckPageNode(); 204 void LoadBuilder(); 205 void UpdateIndicator(); 206 void SetBuilderNodeHidden(); 207 void StartEnterAnimation(); 208 void StartExitAnimation(); 209 void UpdateState(); 210 void UpdateUnSelect(); 211 void UpdateGroupStatus(FrameNode* frameNode); 212 void CheckBoxGroupIsTrue(); 213 void SetPrePageIdToLastPageId(); 214 // Init key event 215 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 216 bool IsSquareStyleBox(); 217 bool OnKeyEvent(const KeyEvent& event); 218 void GetInnerFocusPaintRect(RoundRect& paintRect); 219 void AddHotZoneRect(); 220 void RemoveLastHotZoneRect() const; 221 void SetAccessibilityAction(); 222 void UpdateSelectStatus(bool isSelected); 223 void ChangeSelfStatusAndNotify(const RefPtr<CheckBoxPaintProperty>& paintProperty); 224 void ChangeGroupStatusAndNotify(const RefPtr<FrameNode>& checkBoxGroupNode, const std::vector<std::string>& vec, 225 bool haveCheckBoxSelected, bool isAllCheckBoxSelected); 226 std::string GetGroupNameWithNavId(); 227 void FireBuilder(); 228 RefPtr<FrameNode> BuildContentModifierNode(); 229 void InitCheckBoxStatusByGroup(RefPtr<FrameNode> checkBoxGroupNode, 230 const RefPtr<CheckBoxGroupPaintProperty>& groupPaintProperty, const std::list<RefPtr<FrameNode>>& list); 231 void UpdateCheckBoxGroupStatus(RefPtr<FrameNode> checkBoxGroupNode, const std::list<RefPtr<FrameNode>>& list); 232 void UpdatePaintPropertyBySettingData(RefPtr<CheckBoxPaintProperty> paintProp); 233 void SetNeedAnimation(bool needAnimation); 234 void InitDefaultMargin(); 235 void ResetDefaultMargin(); 236 void UpdateNavIdAndState(const RefPtr<FrameNode>& host); 237 238 CheckboxSettingData checkboxSettingData_; 239 240 std::optional<CheckBoxMakeCallback> makeFunc_; 241 std::optional<SwitchMakeCallback> toggleMakeFunc_; 242 RefPtr<FrameNode> contentModifierNode_; 243 std::optional<std::string> preName_; 244 std::optional<std::string> preGroup_; 245 int32_t prePageId_ = 0; 246 bool lastSelect_ = false; 247 std::optional<std::string> currentNavId_ = std::nullopt; 248 249 RefPtr<ClickEvent> clickListener_; 250 RefPtr<TouchEventImpl> touchListener_; 251 RefPtr<InputEvent> mouseEvent_; 252 bool isTouch_ = false; 253 bool isHover_ = false; 254 bool isFirstCreated_ = true; 255 bool isUserSetResponseRegion_ = false; 256 bool focusEventInitialized_ = false; 257 bool visible_ = true; 258 bool isUserSetMargin_ = false; 259 bool hasVisibleChangeRegistered_ = false; 260 UIStatus uiStatus_ = UIStatus::UNSELECTED; 261 Dimension hotZoneHorizontalPadding_; 262 Dimension hotZoneVerticalPadding_; 263 OffsetF offset_; 264 SizeF size_; 265 OffsetF hotZoneOffset_; 266 SizeF hotZoneSize_; 267 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 268 OriginalCheckBoxStyle originalStyle_ = OriginalCheckBoxStyle::CIRCULAR_STYLE; 269 RefPtr<FrameNode> builderNode_; 270 std::optional<std::function<void()>> builder_; 271 std::function<void(bool)> isFocusActiveUpdateEvent_; 272 273 RefPtr<CheckBoxPaintMethod> paintMethod_; 274 WeakPtr<GroupManager> groupManager_; 275 ACE_DISALLOW_COPY_AND_MOVE(CheckBoxPattern); 276 }; 277 } // namespace OHOS::Ace::NG 278 279 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CHECKBOX_CHECKBOX_PATTERN_H 280