• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) -> bool {
101                 auto stepper = wp.Upgrade();
102                 if (stepper) {
103                     nextFocusNode = stepper->GetFocusNode(step, currFocusNode);
104                 }
105                 return nextFocusNode.Upgrade() != currFocusNode.Upgrade();
106             });
107     }
108 
109     void OnModifyDone() override;
110 
111     void OnColorConfigurationUpdate() override;
112     void OnLanguageConfigurationUpdate() override;
113 
114 private:
115     void OnAttachToFrameNode() override;
116     int32_t TotalCount() const;
117 
118     void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub);
119     void UpdateIndexWithoutMeasure(int32_t index);
120     void UpdateOrCreateLeftButtonNode(int32_t index);
121     void CreateLeftButtonNode();
122     void UpdateLeftButtonNode(int32_t index);
123     void UpdateOrCreateRightButtonNode(int32_t index);
124     void CreateRightButtonNode(int32_t index);
125     void CreateArrowRightButtonNode(int32_t index, bool isDisabled);
126     void CreateArrowlessRightButtonNode(int32_t index, bool isDisabled, const std::string& defaultContent);
127     void CreateWaitingRightButtonNode();
128     void InitButtonClickEvent();
129     void HandlingLeftButtonClickEvent();
130     void HandlingRightButtonClickEvent();
131     void SetAccessibilityAction();
132     static void ButtonSkipColorConfigurationUpdate(const RefPtr<FrameNode>& buttonNode);
133     WeakPtr<FocusHub> GetFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
134     bool isRightToLeft_ = false;
135     RefPtr<FrameNode> leftImage_;
136     RefPtr<FrameNode> rightImage_;
137     int32_t index_ = 0;
138     int32_t maxIndex_ = 0;
139     std::shared_ptr<ChangeEvent> swiperChangeEvent_;
140     ACE_DISALLOW_COPY_AND_MOVE(StepperPattern);
141     bool isLoadingButton_ = false;
142     RefPtr<FocusHub> leftFocusHub_ = nullptr;
143 };
144 
145 } // namespace OHOS::Ace::NG
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STEPPER_STEPPER_PATTERN_H
148