1a3e0fd82Sopenharmony_ci/*
2a3e0fd82Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3a3e0fd82Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4a3e0fd82Sopenharmony_ci * you may not use this file except in compliance with the License.
5a3e0fd82Sopenharmony_ci * You may obtain a copy of the License at
6a3e0fd82Sopenharmony_ci *
7a3e0fd82Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8a3e0fd82Sopenharmony_ci *
9a3e0fd82Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10a3e0fd82Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11a3e0fd82Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a3e0fd82Sopenharmony_ci * See the License for the specific language governing permissions and
13a3e0fd82Sopenharmony_ci * limitations under the License.
14a3e0fd82Sopenharmony_ci */
15a3e0fd82Sopenharmony_ci
16a3e0fd82Sopenharmony_ci/**
17a3e0fd82Sopenharmony_ci * @addtogroup UI_Components
18a3e0fd82Sopenharmony_ci * @{
19a3e0fd82Sopenharmony_ci *
20a3e0fd82Sopenharmony_ci * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
21a3e0fd82Sopenharmony_ci *
22a3e0fd82Sopenharmony_ci * @since 1.0
23a3e0fd82Sopenharmony_ci * @version 1.0
24a3e0fd82Sopenharmony_ci */
25a3e0fd82Sopenharmony_ci
26a3e0fd82Sopenharmony_ci/**
27a3e0fd82Sopenharmony_ci * @file ui_scroll_view.h
28a3e0fd82Sopenharmony_ci *
29a3e0fd82Sopenharmony_ci * @brief Declares a view group that allows its child views to be displayed as scroll events occur.
30a3e0fd82Sopenharmony_ci *
31a3e0fd82Sopenharmony_ci * @since 1.0
32a3e0fd82Sopenharmony_ci * @version 1.0
33a3e0fd82Sopenharmony_ci */
34a3e0fd82Sopenharmony_ci
35a3e0fd82Sopenharmony_ci#ifndef GRAPHIC_LITE_UI_SCROLL_VIEW_H
36a3e0fd82Sopenharmony_ci#define GRAPHIC_LITE_UI_SCROLL_VIEW_H
37a3e0fd82Sopenharmony_ci
38a3e0fd82Sopenharmony_ci#include "animator/animator.h"
39a3e0fd82Sopenharmony_ci#include "components/ui_abstract_scroll.h"
40a3e0fd82Sopenharmony_ci
41a3e0fd82Sopenharmony_cinamespace OHOS {
42a3e0fd82Sopenharmony_ci/**
43a3e0fd82Sopenharmony_ci * @brief Supports horizontal or vertical scroll of child views. This class is inherited from {@link UIAbstractScroll}.
44a3e0fd82Sopenharmony_ci *
45a3e0fd82Sopenharmony_ci * Horizontal or vertical scroll occurs only when the width or height of the child view is greater than that of the
46a3e0fd82Sopenharmony_ci * <b>UIScrollView</b>.
47a3e0fd82Sopenharmony_ci *
48a3e0fd82Sopenharmony_ci * @since 1.0
49a3e0fd82Sopenharmony_ci * @version 1.0
50a3e0fd82Sopenharmony_ci */
51a3e0fd82Sopenharmony_ciclass UIScrollView : public UIAbstractScroll {
52a3e0fd82Sopenharmony_cipublic:
53a3e0fd82Sopenharmony_ci    /**
54a3e0fd82Sopenharmony_ci     * @brief Represents a listener that contains a callback to be invoked upon scroll state changes. The state can
55a3e0fd82Sopenharmony_ci     *        either be {@link SCROLL_STATE_STOP} or {@link SCROLL_STATE_MOVE}.
56a3e0fd82Sopenharmony_ci     * @since 1.0
57a3e0fd82Sopenharmony_ci     * @version 1.0
58a3e0fd82Sopenharmony_ci     */
59a3e0fd82Sopenharmony_ci    class OnScrollListener : public HeapBase {
60a3e0fd82Sopenharmony_ci    public:
61a3e0fd82Sopenharmony_ci        /**
62a3e0fd82Sopenharmony_ci         * @brief A constructor used to create an <b>OnScrollListener</b> instance with the default scroll state
63a3e0fd82Sopenharmony_ci         *        {@link SCROLL_STATE_STOP}.
64a3e0fd82Sopenharmony_ci         * @since 1.0
65a3e0fd82Sopenharmony_ci         * @version 1.0
66a3e0fd82Sopenharmony_ci         */
67a3e0fd82Sopenharmony_ci        OnScrollListener() : state_(SCROLL_STATE_STOP) {}
68a3e0fd82Sopenharmony_ci
69a3e0fd82Sopenharmony_ci        /**
70a3e0fd82Sopenharmony_ci         * @brief A destructor used to delete the <b>OnScrollListener</b> instance.
71a3e0fd82Sopenharmony_ci         * @since 1.0
72a3e0fd82Sopenharmony_ci         * @version 1.0
73a3e0fd82Sopenharmony_ci         */
74a3e0fd82Sopenharmony_ci        virtual ~OnScrollListener() {}
75a3e0fd82Sopenharmony_ci
76a3e0fd82Sopenharmony_ci        /**
77a3e0fd82Sopenharmony_ci         * @brief Called when a scroll starts.
78a3e0fd82Sopenharmony_ci         *
79a3e0fd82Sopenharmony_ci         * @since 1.0
80a3e0fd82Sopenharmony_ci         * @version 1.0
81a3e0fd82Sopenharmony_ci         */
82a3e0fd82Sopenharmony_ci        virtual void OnScrollStart() {}
83a3e0fd82Sopenharmony_ci
84a3e0fd82Sopenharmony_ci        /**
85a3e0fd82Sopenharmony_ci         * @brief Called when a scroll ends.
86a3e0fd82Sopenharmony_ci         *
87a3e0fd82Sopenharmony_ci         * @since 1.0
88a3e0fd82Sopenharmony_ci         * @version 1.0
89a3e0fd82Sopenharmony_ci         */
90a3e0fd82Sopenharmony_ci        virtual void OnScrollEnd() {}
91a3e0fd82Sopenharmony_ci
92a3e0fd82Sopenharmony_ci        /**
93a3e0fd82Sopenharmony_ci         * @brief Obtains the scroll state of this view.
94a3e0fd82Sopenharmony_ci         *
95a3e0fd82Sopenharmony_ci         * @return Returns the scroll state, either {@link SCROLL_STATE_STOP} or {@link SCROLL_STATE_MOVE}.
96a3e0fd82Sopenharmony_ci         * @since 1.0
97a3e0fd82Sopenharmony_ci         * @version 1.0
98a3e0fd82Sopenharmony_ci         */
99a3e0fd82Sopenharmony_ci        uint8_t GetScrollState() const
100a3e0fd82Sopenharmony_ci        {
101a3e0fd82Sopenharmony_ci            return state_;
102a3e0fd82Sopenharmony_ci        }
103a3e0fd82Sopenharmony_ci
104a3e0fd82Sopenharmony_ci        void SetScrollState(uint8_t state)
105a3e0fd82Sopenharmony_ci        {
106a3e0fd82Sopenharmony_ci            state_ = state;
107a3e0fd82Sopenharmony_ci        }
108a3e0fd82Sopenharmony_ci
109a3e0fd82Sopenharmony_ci        static constexpr uint8_t SCROLL_STATE_STOP = 0;
110a3e0fd82Sopenharmony_ci        static constexpr uint8_t SCROLL_STATE_MOVE = 1;
111a3e0fd82Sopenharmony_ci
112a3e0fd82Sopenharmony_ci    private:
113a3e0fd82Sopenharmony_ci        uint8_t state_;
114a3e0fd82Sopenharmony_ci    };
115a3e0fd82Sopenharmony_ci
116a3e0fd82Sopenharmony_ci    /**
117a3e0fd82Sopenharmony_ci     * @brief A constructor used to create a <b>UIScrollView</b> instance, with both horizontal and vertical scrolls
118a3e0fd82Sopenharmony_ci     *        supported.
119a3e0fd82Sopenharmony_ci     *
120a3e0fd82Sopenharmony_ci     * @since 1.0
121a3e0fd82Sopenharmony_ci     * @version 1.0
122a3e0fd82Sopenharmony_ci     */
123a3e0fd82Sopenharmony_ci    UIScrollView();
124a3e0fd82Sopenharmony_ci
125a3e0fd82Sopenharmony_ci    /**
126a3e0fd82Sopenharmony_ci     * @brief A destructor used to delete the <b>UIScrollView</b> instance.
127a3e0fd82Sopenharmony_ci     *
128a3e0fd82Sopenharmony_ci     * @since 1.0
129a3e0fd82Sopenharmony_ci     * @version 1.0
130a3e0fd82Sopenharmony_ci     */
131a3e0fd82Sopenharmony_ci    virtual ~UIScrollView() {}
132a3e0fd82Sopenharmony_ci
133a3e0fd82Sopenharmony_ci    /**
134a3e0fd82Sopenharmony_ci     * @brief Obtains the view type.
135a3e0fd82Sopenharmony_ci     * @return Returns the view type, as defined in {@link UIViewType}.
136a3e0fd82Sopenharmony_ci     *
137a3e0fd82Sopenharmony_ci     * @since 1.0
138a3e0fd82Sopenharmony_ci     * @version 1.0
139a3e0fd82Sopenharmony_ci     */
140a3e0fd82Sopenharmony_ci    UIViewType GetViewType() const override
141a3e0fd82Sopenharmony_ci    {
142a3e0fd82Sopenharmony_ci        return UI_SCROLL_VIEW;
143a3e0fd82Sopenharmony_ci    }
144a3e0fd82Sopenharmony_ci
145a3e0fd82Sopenharmony_ci#if defined(ENABLE_ROTATE_INPUT) && ENABLE_ROTATE_INPUT
146a3e0fd82Sopenharmony_ci    bool OnRotateEvent(const RotateEvent& event) override;
147a3e0fd82Sopenharmony_ci
148a3e0fd82Sopenharmony_ci    bool OnRotateEndEvent(const RotateEvent& event) override;
149a3e0fd82Sopenharmony_ci#endif
150a3e0fd82Sopenharmony_ci
151a3e0fd82Sopenharmony_ci    bool OnDragEvent(const DragEvent& event) override;
152a3e0fd82Sopenharmony_ci
153a3e0fd82Sopenharmony_ci    bool OnDragEndEvent(const DragEvent& event) override;
154a3e0fd82Sopenharmony_ci
155a3e0fd82Sopenharmony_ci    bool OnPressEvent(const PressEvent& event) override;
156a3e0fd82Sopenharmony_ci
157a3e0fd82Sopenharmony_ci    /**
158a3e0fd82Sopenharmony_ci     * @brief Scrolls the content of this view.
159a3e0fd82Sopenharmony_ci     *
160a3e0fd82Sopenharmony_ci     * @param xDistance Indicates the offset distance by which the content is scrolled on the x-axis.
161a3e0fd82Sopenharmony_ci     * @param yDistance Indicates the offset distance by which the content is scrolled on the y-axis.
162a3e0fd82Sopenharmony_ci     * @since 1.0
163a3e0fd82Sopenharmony_ci     * @version 1.0
164a3e0fd82Sopenharmony_ci     */
165a3e0fd82Sopenharmony_ci    void ScrollBy(int16_t xDistance, int16_t yDistance);
166a3e0fd82Sopenharmony_ci
167a3e0fd82Sopenharmony_ci    /**
168a3e0fd82Sopenharmony_ci     * @brief Sets whether a horizontal scroll is enabled.
169a3e0fd82Sopenharmony_ci     *
170a3e0fd82Sopenharmony_ci     * @param state Specifies whether a horizontal scroll is enabled. <b>true</b> indicates a horizontal scroll is
171a3e0fd82Sopenharmony_ci     *              enabled, and <b>false</b> indicates the opposite case.
172a3e0fd82Sopenharmony_ci     * @since 1.0
173a3e0fd82Sopenharmony_ci     * @version 1.0
174a3e0fd82Sopenharmony_ci     */
175a3e0fd82Sopenharmony_ci    void SetHorizontalScrollState(bool state)
176a3e0fd82Sopenharmony_ci    {
177a3e0fd82Sopenharmony_ci        if (direction_ == VERTICAL || direction_ == HORIZONTAL_AND_VERTICAL) {
178a3e0fd82Sopenharmony_ci            direction_ = state ? HORIZONTAL_AND_VERTICAL : VERTICAL;
179a3e0fd82Sopenharmony_ci        } else {
180a3e0fd82Sopenharmony_ci            direction_ = state ? HORIZONTAL : HORIZONTAL_NOR_VERTICAL;
181a3e0fd82Sopenharmony_ci        }
182a3e0fd82Sopenharmony_ci    }
183a3e0fd82Sopenharmony_ci
184a3e0fd82Sopenharmony_ci    /**
185a3e0fd82Sopenharmony_ci     * @brief Checks whether a horizontal scroll is enabled.
186a3e0fd82Sopenharmony_ci     *
187a3e0fd82Sopenharmony_ci     * @return Returns <b>true</b> if a horizontal scroll is enabled; returns <b>false</b> otherwise.
188a3e0fd82Sopenharmony_ci     * @since 1.0
189a3e0fd82Sopenharmony_ci     * @version 1.0
190a3e0fd82Sopenharmony_ci     */
191a3e0fd82Sopenharmony_ci    bool GetHorizontalScrollState() const
192a3e0fd82Sopenharmony_ci    {
193a3e0fd82Sopenharmony_ci        return (direction_ == HORIZONTAL || direction_ == HORIZONTAL_AND_VERTICAL);
194a3e0fd82Sopenharmony_ci    }
195a3e0fd82Sopenharmony_ci
196a3e0fd82Sopenharmony_ci    /**
197a3e0fd82Sopenharmony_ci     * @brief Sets whether a vertical scroll is enabled.
198a3e0fd82Sopenharmony_ci     *
199a3e0fd82Sopenharmony_ci     * @param state Specifies whether a vertical scroll is enabled. <b>true</b> indicates a vertical scroll is enabled,
200a3e0fd82Sopenharmony_ci     *              and <b>false</b> indicates the opposite case.
201a3e0fd82Sopenharmony_ci     * @since 1.0
202a3e0fd82Sopenharmony_ci     * @version 1.0
203a3e0fd82Sopenharmony_ci     */
204a3e0fd82Sopenharmony_ci    void SetVerticalScrollState(bool state)
205a3e0fd82Sopenharmony_ci    {
206a3e0fd82Sopenharmony_ci        if (direction_ == HORIZONTAL || direction_ == HORIZONTAL_AND_VERTICAL) {
207a3e0fd82Sopenharmony_ci            direction_ = state ? HORIZONTAL_AND_VERTICAL : HORIZONTAL;
208a3e0fd82Sopenharmony_ci        } else {
209a3e0fd82Sopenharmony_ci            direction_ = state ? VERTICAL : HORIZONTAL_NOR_VERTICAL;
210a3e0fd82Sopenharmony_ci        }
211a3e0fd82Sopenharmony_ci    }
212a3e0fd82Sopenharmony_ci
213a3e0fd82Sopenharmony_ci    /**
214a3e0fd82Sopenharmony_ci     * @brief Checks whether a vertical scroll is enabled.
215a3e0fd82Sopenharmony_ci     *
216a3e0fd82Sopenharmony_ci     * @return Returns <b>true</b> if a vertical scroll is enabled, returns <b>false</b> otherwise.
217a3e0fd82Sopenharmony_ci     * @since 1.0
218a3e0fd82Sopenharmony_ci     * @version 1.0
219a3e0fd82Sopenharmony_ci     */
220a3e0fd82Sopenharmony_ci    bool GetVerticalScrollState() const
221a3e0fd82Sopenharmony_ci    {
222a3e0fd82Sopenharmony_ci        return (direction_ == VERTICAL || direction_ == HORIZONTAL_AND_VERTICAL);
223a3e0fd82Sopenharmony_ci    }
224a3e0fd82Sopenharmony_ci
225a3e0fd82Sopenharmony_ci    /**
226a3e0fd82Sopenharmony_ci     * @brief Registers a listener that contains a callback to be invoked upon scroll state changes.
227a3e0fd82Sopenharmony_ci     *
228a3e0fd82Sopenharmony_ci     * @param scrollListener Indicates the listener to register. For details, see {@link OnScrollListener}.
229a3e0fd82Sopenharmony_ci     * @since 1.0
230a3e0fd82Sopenharmony_ci     * @version 1.0
231a3e0fd82Sopenharmony_ci     */
232a3e0fd82Sopenharmony_ci    void RegisterScrollListener(OnScrollListener* scrollListener)
233a3e0fd82Sopenharmony_ci    {
234a3e0fd82Sopenharmony_ci        scrollListener_ = scrollListener;
235a3e0fd82Sopenharmony_ci    }
236a3e0fd82Sopenharmony_ci
237a3e0fd82Sopenharmony_ciprotected:
238a3e0fd82Sopenharmony_ci    void StopAnimator() override;
239a3e0fd82Sopenharmony_ci    bool DragXInner(int16_t distance) override;
240a3e0fd82Sopenharmony_ci    bool DragYInner(int16_t distance) override;
241a3e0fd82Sopenharmony_ci    bool MoveOffset(int16_t offsetX, int16_t offsetY);
242a3e0fd82Sopenharmony_ci
243a3e0fd82Sopenharmony_ciprivate:
244a3e0fd82Sopenharmony_ci    void Drag(const DragEvent& event);
245a3e0fd82Sopenharmony_ci    void CalculateReboundDistance(int16_t& dragDistanceX, int16_t& dragDistanceY) override;
246a3e0fd82Sopenharmony_ci    void RefreshScrollBar();
247a3e0fd82Sopenharmony_ci    OnScrollListener* scrollListener_;
248a3e0fd82Sopenharmony_ci#if defined(ENABLE_VIBRATOR) && ENABLE_VIBRATOR
249a3e0fd82Sopenharmony_ci    int16_t totalRotateLen_;
250a3e0fd82Sopenharmony_ci    int16_t lastVibratorRotateLen_;
251a3e0fd82Sopenharmony_ci    void SetIsEdge(bool& lastIsEdge, Rect childRect);
252a3e0fd82Sopenharmony_ci#endif
253a3e0fd82Sopenharmony_ci};
254a3e0fd82Sopenharmony_ci} // namespace OHOS
255a3e0fd82Sopenharmony_ci#endif // GRAPHIC_LITE_UI_SCROLL_VIEW_H
256