1 /* 2 * Copyright (c) 2022-2024 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_STEPPER_STEPPER_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STEPPER_STEPPER_PATTERN_H 18 19 #include "core/components/stepper/stepper_theme.h" 20 #include "core/components_ng/pattern/pattern.h" 21 #include "core/components_ng/pattern/stepper/stepper_accessibility_property.h" 22 #include "core/components_ng/pattern/stepper/stepper_event_hub.h" 23 #include "core/components_ng/pattern/stepper/stepper_layout_algorithm.h" 24 #include "core/components_ng/pattern/stepper/stepper_layout_property.h" 25 #include "core/components_ng/pattern/swiper/swiper_event_hub.h" 26 #include "core/pipeline_ng/pipeline_context.h" 27 28 namespace OHOS::Ace::NG { 29 30 class StepperPattern : public Pattern { 31 DECLARE_ACE_TYPE(StepperPattern, Pattern); 32 33 public: 34 using ChangeEvent = std::function<void(int32_t)>; 35 36 StepperPattern() = default; 37 ~StepperPattern() override = default; 38 IsAtomicNode()39 bool IsAtomicNode() const override 40 { 41 return false; 42 } 43 IsEnableMatchParent()44 bool IsEnableMatchParent() override 45 { 46 return true; 47 } 48 IsEnableFix()49 bool IsEnableFix() override 50 { 51 return true; 52 } 53 IsEnableChildrenMatchParent()54 bool IsEnableChildrenMatchParent() override 55 { 56 return true; 57 } 58 UsResRegion()59 bool UsResRegion() override 60 { 61 return false; 62 } 63 CreateLayoutProperty()64 RefPtr<LayoutProperty> CreateLayoutProperty() override 65 { 66 return MakeRefPtr<StepperLayoutProperty>(); 67 } 68 CreateLayoutAlgorithm()69 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 70 { 71 return MakeRefPtr<StepperLayoutAlgorithm>(index_ != 0); 72 } 73 CreateAccessibilityProperty()74 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 75 { 76 return MakeRefPtr<StepperAccessibilityProperty>(); 77 } 78 CreateEventHub()79 RefPtr<EventHub> CreateEventHub() override 80 { 81 return MakeRefPtr<StepperEventHub>(); 82 } 83 GetFocusPattern()84 FocusPattern GetFocusPattern() const override 85 { 86 return { FocusType::SCOPE, true }; 87 } 88 GetTheme()89 static RefPtr<StepperTheme> GetTheme() 90 { 91 auto pipeline = PipelineContext::GetCurrentContextSafely(); 92 CHECK_NULL_RETURN(pipeline, nullptr); 93 return pipeline->GetTheme<StepperTheme>(); 94 } 95 GetIsLoadingButton()96 bool GetIsLoadingButton() const 97 { 98 return isLoadingButton_; 99 } 100 GetCurrentIndex()101 int32_t GetCurrentIndex() const 102 { 103 return index_; 104 } 105 GetScopeFocusAlgorithm()106 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 107 { 108 return ScopeFocusAlgorithm(true, true, ScopeType::OTHERS, 109 [wp = WeakClaim(this)]( 110 FocusStep step, const WeakPtr<FocusHub>& currFocusNode, WeakPtr<FocusHub>& nextFocusNode) -> bool { 111 auto stepper = wp.Upgrade(); 112 if (stepper) { 113 nextFocusNode = stepper->GetFocusNode(step, currFocusNode); 114 } 115 return nextFocusNode.Upgrade() != currFocusNode.Upgrade(); 116 }); 117 } 118 119 void OnModifyDone() override; 120 121 void OnColorConfigurationUpdate() override; 122 void OnLanguageConfigurationUpdate() override; 123 124 private: 125 void OnAttachToFrameNode() override; 126 int32_t TotalCount() const; 127 128 void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub); 129 void UpdateIndexWithoutMeasure(int32_t index); 130 void UpdateOrCreateLeftButtonNode(int32_t index); 131 void CreateLeftButtonNode(); 132 void UpdateLeftButtonNode(int32_t index); 133 void UpdateOrCreateRightButtonNode(int32_t index); 134 void CreateRightButtonNode(int32_t index); 135 void CreateArrowRightButtonNode(int32_t index, bool isDisabled); 136 void CreateArrowlessRightButtonNode(int32_t index, bool isDisabled, const std::string& defaultContent); 137 void CreateWaitingRightButtonNode(); 138 void InitButtonClickEvent(); 139 void HandlingLeftButtonClickEvent(); 140 void HandlingRightButtonClickEvent(); 141 void SetAccessibilityAction(); 142 static void ButtonSkipColorConfigurationUpdate(const RefPtr<FrameNode>& buttonNode); 143 WeakPtr<FocusHub> GetFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode); 144 bool isRightToLeft_ = false; 145 RefPtr<FrameNode> leftImage_; 146 RefPtr<FrameNode> rightImage_; 147 int32_t index_ = 0; 148 int32_t maxIndex_ = 0; 149 std::shared_ptr<ChangeEvent> swiperChangeEvent_; 150 ACE_DISALLOW_COPY_AND_MOVE(StepperPattern); 151 bool isLoadingButton_ = false; 152 RefPtr<FocusHub> leftFocusHub_ = nullptr; 153 }; 154 155 } // namespace OHOS::Ace::NG 156 157 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STEPPER_STEPPER_PATTERN_H 158