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_RADIO_RADIO_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RADIO_RADIO_PATTERN_H 18 19 #include "base/memory/referenced.h" 20 #include "core/components_ng/event/event_hub.h" 21 #include "core/components_ng/pattern/pattern.h" 22 #include "core/components_ng/pattern/radio/radio_accessibility_property.h" 23 #include "core/components_ng/pattern/radio/radio_event_hub.h" 24 #include "core/components_ng/pattern/radio/radio_layout_algorithm.h" 25 #include "core/components_ng/pattern/radio/radio_paint_method.h" 26 #include "core/components_ng/pattern/radio/radio_paint_property.h" 27 28 namespace OHOS::Ace::NG { 29 30 class RadioPattern : public Pattern { 31 DECLARE_ACE_TYPE(RadioPattern, Pattern); 32 33 public: 34 RadioPattern() = default; 35 ~RadioPattern() override = default; 36 IsAtomicNode()37 bool IsAtomicNode() const override 38 { 39 return true; 40 } 41 CreatePaintProperty()42 RefPtr<PaintProperty> CreatePaintProperty() override 43 { 44 return MakeRefPtr<RadioPaintProperty>(); 45 } 46 CreateLayoutAlgorithm()47 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 48 { 49 return MakeRefPtr<RadioLayoutAlgorithm>(); 50 } 51 CreateNodePaintMethod()52 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 53 { 54 if (!radioModifier_) { 55 radioModifier_ = AceType::MakeRefPtr<RadioModifier>(); 56 } 57 auto paintMethod = MakeRefPtr<RadioPaintMethod>(radioModifier_); 58 paintMethod->SetTotalScale(totalScale_); 59 paintMethod->SetPointScale(pointScale_); 60 paintMethod->SetRingPointScale(ringPointScale_); 61 paintMethod->SetUIStatus(uiStatus_); 62 auto host = GetHost(); 63 CHECK_NULL_RETURN(host, nullptr); 64 auto eventHub = host->GetEventHub<EventHub>(); 65 CHECK_NULL_RETURN(eventHub, nullptr); 66 auto enabled = eventHub->IsEnabled(); 67 paintMethod->SetEnabled(enabled); 68 paintMethod->SetIsOnAnimationFlag(isOnAnimationFlag_); 69 paintMethod->SetTouchHoverAnimationType(touchHoverType_); 70 paintMethod->SetIsFirstCreated(isFirstCreated_); 71 isFirstCreated_ = false; 72 return paintMethod; 73 } 74 75 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& /*config*/) override; 76 CreateEventHub()77 RefPtr<EventHub> CreateEventHub() override 78 { 79 return MakeRefPtr<RadioEventHub>(); 80 } 81 CreateAccessibilityProperty()82 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 83 { 84 return MakeRefPtr<RadioAccessibilityProperty>(); 85 } 86 GetPreValue()87 const std::optional<std::string>& GetPreValue() 88 { 89 return preValue_; 90 } 91 GetPreGroup()92 const std::optional<std::string>& GetPreGroup() 93 { 94 return preGroup_; 95 } 96 GetPrePageId()97 int32_t GetPrePageId() const 98 { 99 return prePageId_; 100 } 101 SetPreValue(const std::string & value)102 void SetPreValue(const std::string& value) 103 { 104 preValue_ = value; 105 } 106 SetPreGroup(const std::string & group)107 void SetPreGroup(const std::string& group) 108 { 109 preGroup_ = group; 110 } 111 SetPrePageId(int32_t pageId)112 void SetPrePageId(int32_t pageId) 113 { 114 prePageId_ = pageId; 115 } 116 SetIsUserSetResponseRegion(bool isUserSetResponseRegion)117 void SetIsUserSetResponseRegion(bool isUserSetResponseRegion) 118 { 119 isUserSetResponseRegion_ = isUserSetResponseRegion; 120 } 121 122 FocusPattern GetFocusPattern() const override; 123 124 void UpdateUncheckStatus(const RefPtr<FrameNode>& frameNode); 125 ToJsonValue(std::unique_ptr<JsonValue> & json)126 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override 127 { 128 Pattern::ToJsonValue(json); 129 auto host = GetHost(); 130 CHECK_NULL_VOID(host); 131 auto radioEventHub = host->GetEventHub<NG::RadioEventHub>(); 132 auto value = radioEventHub ? radioEventHub->GetValue() : ""; 133 auto group = radioEventHub ? radioEventHub->GetGroup() : ""; 134 json->Put("value", value.c_str()); 135 json->Put("group", group.c_str()); 136 } 137 std::string ProvideRestoreInfo() override; 138 void OnRestoreInfo(const std::string& restoreInfo) override; 139 140 private: 141 void OnAttachToFrameNode() override; 142 void OnDetachFromFrameNode(FrameNode* frameNode) override; 143 void OnModifyDone() override; 144 void InitClickEvent(); 145 void InitTouchEvent(); 146 void InitMouseEvent(); 147 void OnClick(); 148 void UpdateState(); 149 void UpdateGroupCheckStatus(const RefPtr<FrameNode>& frameNode, const RefPtr<FrameNode>& pageNode, bool check); 150 void OnTouchDown(); 151 void OnTouchUp(); 152 void CheckPageNode(); 153 void HandleMouseEvent(bool isHover); 154 void UpdateUIStatus(bool check); 155 // Init key event 156 void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub); 157 void GetInnerFocusPaintRect(RoundRect& paintRect); 158 void AddHotZoneRect(); 159 void RemoveLastHotZoneRect() const; 160 161 RefPtr<ClickEvent> clickListener_; 162 RefPtr<TouchEventImpl> touchListener_; 163 RefPtr<InputEvent> mouseEvent_; 164 165 bool isFirstCreated_ = true; 166 bool preCheck_ = false; 167 std::optional<std::string> preValue_; 168 std::optional<std::string> preGroup_; 169 int32_t prePageId_ = 0; 170 bool isTouch_ = false; 171 bool isHover_ = false; 172 float totalScale_ = 1.0f; 173 float pointScale_ = 0.5f; 174 float ringPointScale_ = 0.0f; 175 UIStatus uiStatus_ = UIStatus::UNSELECTED; 176 Dimension hotZoneHorizontalPadding_; 177 Dimension hotZoneVerticalPadding_; 178 OffsetF offset_; 179 SizeF size_; 180 OffsetF hotZoneOffset_; 181 SizeF hotZoneSize_; 182 bool isGroupChanged_ = false; 183 TouchHoverAnimationType touchHoverType_ = TouchHoverAnimationType::NONE; 184 bool isOnAnimationFlag_ = false; 185 bool isUserSetResponseRegion_ = false; 186 187 RefPtr<RadioModifier> radioModifier_; 188 ACE_DISALLOW_COPY_AND_MOVE(RadioPattern); 189 }; 190 } // namespace OHOS::Ace::NG 191 192 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RADIO_RADIO_PATTERN_H 193