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