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 
UsResRegion()44     bool UsResRegion() override
45     {
46         return false;
47     }
48 
CreateLayoutProperty()49     RefPtr<LayoutProperty> CreateLayoutProperty() override
50     {
51         return MakeRefPtr<StepperLayoutProperty>();
52     }
53 
CreateLayoutAlgorithm()54     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
55     {
56         return MakeRefPtr<StepperLayoutAlgorithm>(index_ != 0);
57     }
58 
CreateAccessibilityProperty()59     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
60     {
61         return MakeRefPtr<StepperAccessibilityProperty>();
62     }
63 
CreateEventHub()64     RefPtr<EventHub> CreateEventHub() override
65     {
66         return MakeRefPtr<StepperEventHub>();
67     }
68 
GetFocusPattern()69     FocusPattern GetFocusPattern() const override
70     {
71         return { FocusType::SCOPE, true };
72     }
73 
GetTheme()74     static RefPtr<StepperTheme> GetTheme()
75     {
76         static RefPtr<StepperTheme> stepperTheme;
77         if (!stepperTheme) {
78             auto pipeline = PipelineContext::GetCurrentContext();
79             CHECK_NULL_RETURN(pipeline, nullptr);
80             stepperTheme = pipeline->GetTheme<StepperTheme>();
81             CHECK_NULL_RETURN(stepperTheme, nullptr);
82         }
83         return stepperTheme;
84     }
85 
GetIsLoadingButton()86     bool GetIsLoadingButton() const
87     {
88         return isLoadingButton_;
89     }
90 
GetCurrentIndex()91     int32_t GetCurrentIndex() const
92     {
93         return index_;
94     }
95 
GetScopeFocusAlgorithm()96     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override
97     {
98         return ScopeFocusAlgorithm(true, true, ScopeType::OTHERS,
99             [wp = WeakClaim(this)](
100                 FocusStep step, const WeakPtr<FocusHub>& currFocusNode, WeakPtr<FocusHub>& nextFocusNode) {
101                 auto stepper = wp.Upgrade();
102                 if (stepper) {
103                     nextFocusNode = stepper->GetFocusNode(step, currFocusNode);
104                 }
105             });
106     }
107 
108     void OnModifyDone() override;
109 
110     void OnColorConfigurationUpdate() override;
111     void OnLanguageConfigurationUpdate() override;
112 
113 private:
114     void OnAttachToFrameNode() override;
115     int32_t TotalCount() const;
116 
117     void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub);
118     void UpdateIndexWithoutMeasure(int32_t index);
119     void UpdateOrCreateLeftButtonNode(int32_t index);
120     void CreateLeftButtonNode();
121     void UpdateLeftButtonNode(int32_t index);
122     void UpdateOrCreateRightButtonNode(int32_t index);
123     void CreateRightButtonNode(int32_t index);
124     void CreateArrowRightButtonNode(int32_t index, bool isDisabled);
125     void CreateArrowlessRightButtonNode(int32_t index, bool isDisabled, const std::string& defaultContent);
126     void CreateWaitingRightButtonNode();
127     void InitButtonClickEvent();
128     void HandlingLeftButtonClickEvent();
129     void HandlingRightButtonClickEvent();
130     void SetAccessibilityAction();
131     static void ButtonSkipColorConfigurationUpdate(const RefPtr<FrameNode>& buttonNode);
132     WeakPtr<FocusHub> GetFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
133     bool isRightToLeft_ = false;
134     RefPtr<FrameNode> leftImage_;
135     RefPtr<FrameNode> rightImage_;
136     int32_t index_ = 0;
137     int32_t maxIndex_ = 0;
138     std::shared_ptr<ChangeEvent> swiperChangeEvent_;
139     ACE_DISALLOW_COPY_AND_MOVE(StepperPattern);
140     bool isLoadingButton_ = false;
141     RefPtr<FocusHub> leftFocusHub_ = nullptr;
142 };
143 
144 } // namespace OHOS::Ace::NG
145 
146 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STEPPER_STEPPER_PATTERN_H
147