• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
GetCurrentIndex()86     int32_t GetCurrentIndex() const
87     {
88         return index_;
89     }
90 
GetScopeFocusAlgorithm()91     ScopeFocusAlgorithm GetScopeFocusAlgorithm() override
92     {
93         return ScopeFocusAlgorithm(true, true, ScopeType::OTHERS,
94             [wp = WeakClaim(this)](
95                 FocusStep step, const WeakPtr<FocusHub>& currFocusNode, WeakPtr<FocusHub>& nextFocusNode) {
96                 auto stepper = wp.Upgrade();
97                 if (stepper) {
98                     nextFocusNode = stepper->GetFocusNode(step, currFocusNode);
99                 }
100             });
101     }
102 
103     void OnModifyDone() override;
104 
105     void OnColorConfigurationUpdate() override;
106 
107 private:
108     void OnAttachToFrameNode() override;
109     int32_t TotalCount() const;
110 
111     void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub);
112     void UpdateIndexWithoutMeasure(int32_t index);
113     void UpdateOrCreateLeftButtonNode(int32_t index);
114     void CreateLeftButtonNode();
115     void UpdateLeftButtonNode(int32_t index);
116     void UpdateOrCreateRightButtonNode(int32_t index);
117     void CreateRightButtonNode(int32_t index);
118     void CreateArrowRightButtonNode(int32_t index, bool isDisabled);
119     void CreateArrowlessRightButtonNode(int32_t index, const std::string& defaultContent);
120     void CreateWaitingRightButtonNode();
121     void UpdateRightButtonNode(int32_t index);
122     void InitButtonClickEvent();
123     void HandlingLeftButtonClickEvent();
124     void HandlingRightButtonClickEvent();
125     void InitButtonOnHoverEvent(RefPtr<FrameNode> buttonNode, bool isLeft);
126     void ButtonOnHover(RefPtr<FrameNode> buttonNode, bool isHover, bool isLeft);
127     void ButtonHoverInAnimation(RefPtr<FrameNode> buttonNode);
128     void ButtonHoverOutAnimation(RefPtr<FrameNode> buttonNode);
129     void InitButtonTouchEvent(RefPtr<FrameNode> buttonNode);
130     void ButtonOnTouch(RefPtr<FrameNode> buttonNode, TouchType touchType);
131     void ButtonTouchDownAnimation(RefPtr<FrameNode> buttonNode);
132     void ButtonTouchUpAnimation(RefPtr<FrameNode> buttonNode);
133     void SetAccessibilityAction();
134     void ButtonSkipColorConfigurationUpdate(RefPtr<FrameNode> buttonNode);
135     WeakPtr<FocusHub> GetFocusNode(FocusStep step, const WeakPtr<FocusHub>& currentFocusNode);
136 
137     int32_t index_ = 0;
138     int32_t maxIndex_ = 0;
139     std::shared_ptr<ChangeEvent> swiperChangeEvent_;
140     RefPtr<InputEvent> buttonOnHoverListenr_;
141     RefPtr<TouchEventImpl> buttonTouchListenr_;
142     bool leftIsHover_ = false;
143     bool rightIsHover_ = false;
144     ACE_DISALLOW_COPY_AND_MOVE(StepperPattern);
145     bool isRightLabelDisable_ = false;
146     RefPtr<FocusHub> leftFocusHub_ = nullptr;
147 };
148 
149 } // namespace OHOS::Ace::NG
150 
151 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STEPPER_STEPPER_PATTERN_H
152