1 /*
2  * Copyright (c) 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_SWIPER_INDICATOR_SWIPER_ARROW_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_ARROW_PATTERN_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/utils/utils.h"
21 #include "core/components_ng/base/frame_node.h"
22 #include "core/components_ng/pattern/button/button_pattern.h"
23 #include "core/components_ng/pattern/pattern.h"
24 #include "core/components_ng/pattern/swiper/swiper_pattern.h"
25 #include "core/components_ng/pattern/swiper_indicator/indicator_common/swiper_arrow_layout_algorithm.h"
26 #include "core/components_ng/pattern/swiper_indicator/indicator_common/swiper_arrow_layout_property.h"
27 
28 namespace OHOS::Ace::NG {
29 class SwiperArrowPattern : public Pattern {
30     DECLARE_ACE_TYPE(SwiperArrowPattern, Pattern);
31 
32 public:
33     SwiperArrowPattern() = default;
34     ~SwiperArrowPattern() override = default;
CreateLayoutProperty()35     RefPtr<LayoutProperty> CreateLayoutProperty() override
36     {
37         return MakeRefPtr<SwiperArrowLayoutProperty>();
38     }
39 
CreateLayoutAlgorithm()40     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
41     {
42         return MakeRefPtr<SwiperArrowLayoutAlgorithm>();
43     }
44 
GetSwiperNode()45     RefPtr<FrameNode> GetSwiperNode() const
46     {
47         auto host = GetHost();
48         CHECK_NULL_RETURN(host, nullptr);
49         auto swiperNode = host->GetParent();
50         CHECK_NULL_RETURN(swiperNode, nullptr);
51         return DynamicCast<FrameNode>(swiperNode);
52     }
53 
GetFocusPattern()54     FocusPattern GetFocusPattern() const override
55     {
56         auto pipelineContext = PipelineBase::GetCurrentContext();
57         CHECK_NULL_RETURN(pipelineContext, FocusPattern());
58         auto swiperTheme = pipelineContext->GetTheme<SwiperIndicatorTheme>();
59         CHECK_NULL_RETURN(swiperTheme, FocusPattern());
60         FocusPaintParam paintParam;
61         paintParam.SetPaintColor(swiperTheme->GetFocusedColor());
62         paintParam.SetPaintWidth(swiperTheme->GetFocusedBorderWidth());
63         return { FocusType::NODE, true, FocusStyleType::INNER_BORDER, paintParam };
64     }
65 
GetSwiperArrowLayoutProperty()66     RefPtr<SwiperArrowLayoutProperty> GetSwiperArrowLayoutProperty() const
67     {
68         auto host = GetHost();
69         CHECK_NULL_RETURN(host, nullptr);
70         auto swiperArrowLayoutProperty = host->GetLayoutProperty<SwiperArrowLayoutProperty>();
71         CHECK_NULL_RETURN(swiperArrowLayoutProperty, nullptr);
72         return swiperArrowLayoutProperty;
73     }
74 
75     void ButtonOnHover(RefPtr<FrameNode> buttonNode, bool isHovered);
76     void SetButtonVisible(bool visible);
77     void DumpAdvanceInfo() override;
78 private:
79     void OnModifyDone() override;
80     void InitNavigationArrow();
81     void InitSwiperChangeEvent(const RefPtr<SwiperEventHub>& swiperEventHub);
82     void InitEvent();
83     void UpdateButtonNode(int32_t index);
84     void ButtonTouchEvent(RefPtr<FrameNode> buttonNode, TouchType touchType);
85     void ButtonClickEvent();
86     void UpdateArrowContent();
87     void InitOnKeyEvent();
88     bool OnKeyEvent(const KeyEvent& event);
89     void OnClick() const;
90 
91     int32_t TotalCount() const;
92     RefPtr<ClickEvent> buttonClickListener_;
93     RefPtr<TouchEventImpl> buttonTouchListener_;
94     RefPtr<InputEvent> buttonOnHoverListener_;
95     std::shared_ptr<ChangeEvent> swiperChangeEvent_;
96 
97     int32_t index_ = 0;
98     bool isFirstCreate_ = true;
99     Color hoverBeginColor_ = Color::TRANSPARENT;
100     Color backgroundColor_ = Color::TRANSPARENT;
101     bool isTouch_ = false;
102     bool isHover_ = false;
103     bool isVisible_ = false;
104     bool hoverOnClickFlag_ = false;
105     ACE_DISALLOW_COPY_AND_MOVE(SwiperArrowPattern);
106 };
107 } // namespace OHOS::Ace::NG
108 
109 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_INDICATOR_SWIPER_ARROW_PATTERN_H
110