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

261 lines
7.3 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiPageManager.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2025-06-04
*--------------------------------------------------------------------------*/
#include "TjdUiPageManager.h"
#include "TjdUiPageTransition.h"
#include "common/screen.h"
#include <vector>
namespace TJD {
class UIPageManager::PageInfo
{
public:
PageInfo(UIPageBase *view) : view(view) {}
virtual ~PageInfo()
{
if (view != nullptr) {
view->UnLoad();
delete view;
view = nullptr;
isInit = false;
}
}
UIPageBase *view{nullptr};
bool isInit{false};
};
class UIPageManager::EffectAnimatorCallback : public OHOS::AnimatorCallback
{
public:
EffectAnimatorCallback()
{
targetImage_.SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
curImage_.SetPosition(0, 0, OHOS::Screen::GetInstance().GetWidth(), OHOS::Screen::GetInstance().GetHeight());
}
void SetType(PageTransitionType type)
{
type_ = type;
translation_ = UIPageTranslationManager::GetInstance().GetTranslation(type);
}
void Callback(OHOS::UIView *view) override
{
if (animator_ == nullptr) {
return;
}
if (translation_ == nullptr) {
animator_->Stop();
return;
}
if (!isInit_) {
isInit_ = true;
InitImageView();
translation_->Init(&targetImage_, &curImage_, dynamic_cast<OHOS::UIViewGroup *>(target_->GetParent()),
animator_->GetTime());
}
translation_->ApplyAnimation(animator_->GetRunTime());
if (animator_->GetRunTime() >= animator_->GetTime()) {
animator_->Stop();
}
}
void OnStop(OHOS::UIView &view) override
{
if (translation_) {
translation_->OnStop();
target_->SetVisible(true);
current_->SetVisible(false);
current_->OnHide();
translation_ = nullptr;
}
isInit_ = false;
OHOS::ImageCacheFree(targetImageInfo_);
OHOS::ImageCacheFree(curImageInfo_);
target_ = nullptr;
current_ = nullptr;
if (onStopCallback_) {
onStopCallback_();
}
}
void SetView(UIPageBase *targetView, UIPageBase *currentView)
{
target_ = targetView;
current_ = currentView;
}
void SetAnimator(OHOS::Animator *animator) { animator_ = animator; }
void RegisterOnStopCallback(std::function<void()> callback) { onStopCallback_ = std::move(callback); }
private:
void InitImageView()
{
target_->SetVisible(true);
current_->SetVisible(true);
target_->GetBitmap(targetImageInfo_);
current_->GetBitmap(curImageInfo_);
targetImage_.SetSrc(&targetImageInfo_);
curImage_.SetSrc(&curImageInfo_);
target_->SetVisible(false);
current_->SetVisible(false);
}
UIPageTranslation *translation_ = nullptr;
OHOS::Animator *animator_{nullptr};
UIPageBase *target_{nullptr};
UIPageBase *current_{nullptr};
OHOS::UIImageView targetImage_;
OHOS::UIImageView curImage_;
OHOS::ImageInfo targetImageInfo_;
OHOS::ImageInfo curImageInfo_;
PageTransitionType type_{PageTransitionType::PAGE_TRANSITION_INVALID};
bool isInit_{false};
std::function<void()> onStopCallback_{nullptr};
};
UIPageManager::~UIPageManager()
{
for (auto kv : map_) {
delete kv.second;
}
if (animator_ != nullptr) {
animator_->Stop();
delete animator_;
animator_ = nullptr;
}
if (animatorCallback_ != nullptr) {
delete animatorCallback_;
animatorCallback_ = nullptr;
}
}
void UIPageManager::PreLoadAllPages()
{
if (baseView == nullptr) {
return;
}
for (auto &kv : map_) {
if (kv.second != nullptr && kv.second->view != nullptr) {
if (kv.second->isInit == false) {
kv.second->view->PreLoad();
kv.second->isInit = true;
}
kv.second->view->SetVisible(false);
baseView->Add(kv.second->view);
}
}
}
bool UIPageManager::RegisterPage(int16_t pageId, UIPageBase *page)
{
if (baseView == nullptr) {
return false;
}
if (map_[pageId] != nullptr) {
return false;
}
map_[pageId] = new PageInfo(page);
page->SetPosition(0, 0, OHOS::Screen::GetInstance().GetHeight(), OHOS::Screen::GetInstance().GetWidth());
page->SetVisible(false);
baseView->Add(page);
return true;
}
void UIPageManager::ShowView(int16_t pageId, PageTransitionType type)
{
if (curPageId == -1) {
curPageId = pageId;
if (map_[curPageId] != nullptr) {
PreLoadView(map_[curPageId]);
ShowView(map_[curPageId]);
}
return;
}
if (curPageId == pageId || map_[pageId] == nullptr) {
return;
}
PreLoadView(map_[pageId]);
if (type != PageTransitionType::PAGE_TRANSITION_INVALID) {
StartAnimatorChangedView(pageId, type);
return;
}
if (map_[curPageId] != nullptr) {
HideView(map_[curPageId]);
}
if (map_[pageId] != nullptr) {
ShowView(map_[pageId]);
}
curPageId = pageId;
}
UIPageBase *UIPageManager::GetPage(int16_t pageId)
{
if (map_.find(pageId) != map_.end()) {
return map_[pageId]->view;
}
return nullptr;
}
void UIPageManager::StartAnimatorChangedView(int16_t pageId, PageTransitionType type)
{
/* 动画期间不允许切换页面 */
if (animator_ != nullptr && animator_->GetState() != OHOS::Animator::STOP) {
return;
}
/* 让两个view都设置为可见状态 */
if (map_[curPageId] != nullptr) {
map_[curPageId]->view->SetVisible(true);
map_[curPageId]->view->SetX(0);
}
if (map_[pageId] != nullptr) {
ShowView(map_[pageId]);
map_[pageId]->view->SetX(0);
}
if (animator_ == nullptr) {
animatorCallback_ = new EffectAnimatorCallback();
animatorCallback_->RegisterOnStopCallback(std::bind(&UIPageManager::AnimatorStopCallback, this));
animator_ = new OHOS::Animator(animatorCallback_, nullptr, 500, true);
animatorCallback_->SetAnimator(animator_);
}
animatorCallback_->SetType(type);
animatorCallback_->SetView(map_[pageId]->view, map_[curPageId]->view);
changePageId = pageId;
animator_->Start();
}
void UIPageManager::AnimatorStopCallback(void) { curPageId = changePageId; }
void UIPageManager::PreLoadView(PageInfo *pageInfo)
{
if (pageInfo == nullptr || pageInfo->view == nullptr) {
return;
}
if (pageInfo->isInit == false) {
pageInfo->view->PreLoad();
pageInfo->isInit = true;
}
}
void UIPageManager::ShowView(PageInfo *pageInfo)
{
if (pageInfo == nullptr || pageInfo->view == nullptr) {
return;
}
pageInfo->view->SetVisible(true);
pageInfo->view->OnShow();
}
void UIPageManager::HideView(PageInfo *pageInfo)
{
if (pageInfo == nullptr || pageInfo->view == nullptr) {
return;
}
pageInfo->view->SetVisible(false);
pageInfo->view->OnHide();
}
} // namespace TJD