ui_swipe_view.h 9.8 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @addtogroup UI_Components
 * @{
 *
 * @brief Defines UI components such as buttons, texts, images, lists, and progress bars.
 *
 * @since 1.0
 * @version 1.0
 */

/**
 * @file ui_swipe_view.h
 *
 * @brief Defines the attributes and common functions of a swipe view.
 *
 * Each swipe view consists of multiple child views, which can be navigated through swiping. The child views can be
 * either horizontal or vertical.
 *
 * @since 1.0
 * @version 1.0
 */

#ifndef GRAPHIC_LITE_UI_SWIPE_VIEW_H
#define GRAPHIC_LITE_UI_SWIPE_VIEW_H

#include "animator/animator.h"
#include "components/ui_abstract_scroll.h"

namespace OHOS {
/**
 * @brief Represents a swipe view.
 *
 * Each swipe view consists of multiple child views, which can be navigated through swiping. The child views can be
 * either horizontal or vertical.
 *
 * @see UIAbstractScroll
 * @since 1.0
 * @version 1.0
 */
class UISwipeView : public UIAbstractScroll {
public:
    /**
     * @brief Represents a listener for changes of the swipe view.
     *
     * This is an inner class of <b>UISwipeView</b>. It contains a callback function to be invoked when the swipe view
     * state changes.
     *
     * @since 1.0
     * @version 1.0
     */
    class OnSwipeListener : public HeapBase {
    public:
        virtual void OnSwipe(UISwipeView& view) = 0;
        virtual ~OnSwipeListener() {}
    };

72
    enum AlignMode : uint8_t { ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT };
M
mamingshuai 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    /**
     * @brief A constructor used to create a <b>UISwipeView</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    UISwipeView(uint8_t direction = HORIZONTAL);

    /**
     * @brief A destructor used to delete the <b>UISwipeView</b> instance.
     *
     * @since 1.0
     * @version 1.0
     */
    virtual ~UISwipeView();

    /**
     * @brief Obtains the component type.
     *
     * @return Returns the component type, as defined in {@link UIViewType}.
     * @since 1.0
     * @version 1.0
     */
    UIViewType GetViewType() const override
    {
        return UI_SWIPE_VIEW;
    }

    /**
     * @brief Sets the dragging direction.
     *
     * @param direction Indicates the dragging direction, either {@link HORIZONTAL} or {@link VERTICAL}.
     * @since 1.0
     * @version 1.0
     */
    void SetDirection(uint8_t direction)
    {
        direction_ = direction;
    }

    /**
     * @brief Obtains the dragging direction.
     *
     * @return Returns the dragging direction.
     * @since 1.0
     * @version 1.0
     */
    uint8_t GetDirection() const
    {
        return direction_;
    }

    /**
     * @brief Adds a view.
     *
     * @param view Indicates the view to add.
     * @since 1.0
     * @version 1.0
     */
    void Add(UIView* view) override;

    /**
     * @brief Inserts a view.
     *
     * @param prevView Indicates the previous view.
     * @param insertView Indicates the view to insert.
     * @since 1.0
     * @version 1.0
     */
    void Insert(UIView* prevView, UIView* insertView) override;

    /**
     * @brief Deletes a view.
     *
     * @param view Indicates the view to delete.
     * @since 1.0
     * @version 1.0
     */
    virtual void Remove(UIView* view) override;

    /**
     * @brief Sets the index for the current tab.
     *
     * @param index Indicates the index of a view.
     * @param needAnimator Specifies whether a flip animation is needed. <b>false</b> (default value) indicates a flip
     * animation is not needed, and <b>true</b> indicates the opposite case.
     * @since 1.0
     * @version 1.0
     */
    void SetCurrentPage(uint16_t index, bool needAnimator = false);

    /**
     * @brief Obtains the current tab index.
     *
     * @return Returns the current tab index.
     * @since 1.0
     * @version 1.0
     */
    uint16_t GetCurrentPage() const
    {
        return curIndex_;
    }

    /**
     * @brief Obtains the current view.
     *
     * @return Returns the current view.
     * @since 1.0
     * @version 1.0
     */
    UIView* GetCurrentView() const
    {
        return curView_;
    }

    /**
     * @brief Sets a blank size, as defined in {@link DEFAULT_BLANK_SIZE}
     *
     * @param size Indicates the blank size to set.
     * @since 1.0
     * @version 1.0
     */
    void SetBlankSize(uint16_t size)
    {
        blankSize_ = size;
    }

    /**
     * @fn void OnDragEvent(const DragEvent& event) override
     *
     * @brief revice drag event, Switch to specified view when drag
     *
     * @param event The drag event
     */
    bool OnDragEvent(const DragEvent& event) override;

    bool OnDragEndEvent(const DragEvent& event) override;

#if ENABLE_ROTATE_INPUT
Y
YueBiang 已提交
213 214
    bool OnRotateStartEvent(const RotateEvent& event) override;

215 216
    bool OnRotateEvent(const RotateEvent& event) override;

Y
YueBiang 已提交
217
    bool OnRotateEndEvent(const RotateEvent& event) override;
M
mamingshuai 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#endif

    /**
     * @brief Sets the time for the page being animated. The page will go beyond the blank during this time.
     *
     * @param time Indicates the time of the page being animated.
     * @since 1.0
     * @version 1.0
     */
    void SetAnimatorTime(uint16_t time);

    /**
     * @brief Sets whether the swipe view supports a cycle swipe.
     *
     * @param loop Indicates the cycle swipe flag. <b>true</b> indicates the cycle swipe is supported, and <b>false</b>
     * indicates the opposite case.
     * @since 1.0
     * @version 1.0
     */
    void SetLoopState(bool loop)
    {
        loop_ = loop;
    }

    /**
     * @brief Obtains a view based on its index.
     *
     * @param Indicates the index of a view.
     * @return Returns the view.
     * @since 1.0
     * @version 1.0
     */
    UIView* GetViewByIndex(uint16_t index) const;

    /**
     * @brief Obtains the listener set for swipe events.
     *
     * @return Returns the swipe event listener.
     * @since 1.0
     * @version 1.0
     */
    OnSwipeListener*& GetOnSwipeListener()
    {
        return swipeListener_;
    }

    /**
     * @brief Sets the listener that contains a callback to be invoked upon a swipe event.
     *
     * @param onSwipeListener Indicates the listener to set.
     * @since 1.0
     * @version 1.0
     */
    void SetOnSwipeListener(OnSwipeListener* onSwipeListener)
    {
        swipeListener_ = onSwipeListener;
    }

    /**
     * @brief Sets the alignment mode for child components of <b>UISwipeView</b>.
     *
     * @param alignMode Indicates the alignment mode to set, as enumerated in {@link AlignMode}.
     * The default value is <b>ALIGN_CENTER</b>.
     * @since 1.0
     * @version 1.0
     */
    void SetAlignMode(AlignMode alignMode = ALIGN_CENTER)
    {
        alignMode_ = alignMode;
    }

    /**
     * @brief Obtains the alignment mode of child components of <b>UISwipeView</b>.
     *
     * @return Returns the alignment mode. For details, see {@link AlignMode}.
     * @since 1.0
     * @version 1.0
     */
    AlignMode GetAlignMode()
    {
        return alignMode_;
    }

    /**
     * @brief Indicates the horizontal direction.
     *
     * @since 1.0
     * @version 1.0
     */
    static constexpr uint8_t HORIZONTAL = 0;

    /**
     * @brief Indicates the vertical direction.
     *
     * @since 1.0
     * @version 1.0
     */
    static constexpr uint8_t VERTICAL = 1;

Y
YueBiang 已提交
317 318 319 320
    void SetXScrollBarVisible(bool visible) = delete;

    void SetYScrollBarVisible(bool visible) = delete;

321 322 323
    void SetScrollBarSide(uint8_t side) = delete;

    void SetScrollBarCenter(Point center) = delete;
324

M
mamingshuai 已提交
325 326 327 328 329 330 331
protected:
    bool DragXInner(int16_t distance) override;
    bool DragYInner(int16_t distance) override;
    void SortChild();
    void StopAnimator() override;
    virtual void SwitchToPage(int16_t dst, bool needAnimator = true);
    void MoveChildByOffset(int16_t xOffset, int16_t yOffset) override;
L
liqiang 已提交
332
    void MoveHeadOrTailChild();
M
mamingshuai 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

    /**
     * @brief Indicates that the animation duration is 12 ticks.
     *
     * @since 1.0
     * @version 1.0
     */
    constexpr static uint16_t ANIMATOR_TIME = 12;

    /**
     * @brief Indicates the maximum distance of an invalid dragging. Dragging is not triggered if the distance is less
     * than this value.
     *
     * @since 1.0
     * @version 1.0
     */
    constexpr static uint16_t STOP_DISTANCE = 5;

    /**
     * @brief Indicates the maximum distance between the first and the last tab when the current view is not in a cycle
     * swipe mode. The page can be rebound after the setting.
     *
     * @since 1.0
     * @version 1.0
     */
    constexpr static uint16_t DEFAULT_BLANK_SIZE = 30;
    uint16_t tickTime_;
    OnSwipeListener* swipeListener_;
    uint16_t curIndex_;
    uint16_t blankSize_;
    UIView* curView_;
    AlignMode alignMode_ = ALIGN_CENTER;
    bool loop_;

private:
368 369 370 371 372
    void RefreshCurrentViewByPosition(int16_t (UIView::*pfnGetXOrY)() const, int16_t (UIView::*pfnGetWidthOrHeight)());
    void RefreshCurrentViewByThrow(int16_t distance,
                                   uint8_t dragDirection,
                                   int16_t (UIView::*pfnGetXOrY)() const,
                                   int16_t (UIView::*pfnGetWidthOrHeight)());
373

374
    bool IsNeedLoop();
M
mamingshuai 已提交
375 376 377
    void MoveFirstChildToLast();
    void MoveLastChildToFirst();
    void CalculateInvalidate();
378 379 380
    void CurrentIndexInc();
    void CurrentIndexDec();
    void Vibrator();
M
mamingshuai 已提交
381 382 383
};
} // namespace OHOS
#endif // GRAPHIC_LITE_UI_SWIPE_VIEW_H