mcu_hi3321_watch/tjd/ui/common/TjdUiPageManager.h
2025-06-06 15:13:55 +08:00

121 lines
3.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiPageManager.h
*
* Author: luziquan@ss-tjd.com
*
* Create: 2025-06-04
*--------------------------------------------------------------------------*/
#ifndef TJD_UI_PAGE_MANAGER_H
#define TJD_UI_PAGE_MANAGER_H
#include "animator/animator.h"
#include "animator/easing_equation.h"
#include "components/ui_image_view.h"
#include "components/ui_view_group.h"
#include <unordered_map>
namespace TJD {
class UIPageBase : public OHOS::UIViewGroup
{
public:
virtual ~UIPageBase() = default;
/**
* @brief 加载页面资源
*/
virtual void PreLoad() {}
/**
* @brief 卸载页面资源
*/
virtual void UnLoad() {}
/**
* @brief 页面显示事件
*/
virtual void OnShow() {}
/**
* @brief 页面隐藏事件
*/
virtual void OnHide() {}
};
enum class PageTransitionType
{
PAGE_TRANSITION_INVALID = 0,
PAGE_TRANSITION_TRANSLATION_ENTER, //平移进入
PAGE_TRANSITION_TRANSLATION_EXIT, //平移退出
PAGE_TRANSITION_TRANSLATION_MAX,
};
class UIPageManager
{
public:
UIPageManager() {}
UIPageManager(OHOS::UIViewGroup *base) : baseView(base) {}
/**
* @brief 析构所有注册的页面对象,并调用页面的 UnLoad 方法来释放资源
*/
virtual ~UIPageManager();
/**
* @brief 设置基础视图,所有页面都添加到该视图上
* @param base
*/
void SetBaseView(OHOS::UIViewGroup *base) { baseView = base; }
/**
* @brief 预加载所有页面,调用每个页面的 PreLoad 方法来初始化
*/
void PreLoadAllPages();
/**
* @brief 注册一个页面,会调用页面的 PreLoad 方法来初始化
* @param pageId 页面ID
* @param page 页面对象指针
* @return true
* @return false
*/
bool RegisterPage(int16_t pageId, UIPageBase *page);
/**
* @brief 显示一个页面
* @param pageId 页面ID
* @param type 页面切换动画类型,默认为无动画
* @note 如果当前页面已经是该页面,则不做任何操作,
* 如果第一次调用则直接显示该页面显示后调用页面的OnShow方法
* 如果当前页面不是该页面则先调用当前页面的OnHide方法然后显示该页面并调用其OnShow方法。
* 如果没有动画, CurPage->OnHide() -> TargetPage->OnShow()
* 如果有动画, TargetPage->OnShow() -> 动画 -> CurPage->OnHide()
*/
void ShowView(int16_t pageId, PageTransitionType type = PageTransitionType::PAGE_TRANSITION_INVALID);
int16_t GetCurPageId() { return curPageId; }
UIPageBase *GetPage(int16_t pageId);
private:
class PageInfo;
class EffectAnimatorCallback;
void StartAnimatorChangedView(int16_t pageId, PageTransitionType type);
void AnimatorStopCallback(void);
void PreLoadView(PageInfo *pageInfo);
void ShowView(PageInfo *pageInfo);
void HideView(PageInfo *pageInfo);
EffectAnimatorCallback *animatorCallback_{nullptr};
OHOS::Animator *animator_{nullptr};
std::unordered_map<int16_t, PageInfo *> map_;
OHOS::UIViewGroup *baseView{nullptr};
int16_t changePageId{-1};
int16_t curPageId{-1};
};
} // namespace TJD
#endif