mcu_hi3321_watch/tjd/ui/app/shutdown/TjdUiAppShutdownView.cpp
2025-05-26 20:15:20 +08:00

232 lines
8.2 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2025. All rights reserved.
*
* Description: TjdUiAppShutdownView.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2024-06-13
*--------------------------------------------------------------------------*/
#include "TjdUiAppShutdownView.h"
#include "TjdUiImageIds.h"
#include "TjdUiMemManage.h"
#include "TjdUiMultiLanguageExt.h"
#include "animator/animator_manager.h"
#include "common/image_cache_manager.h"
#include "sys_config.h"
namespace TJD {
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_info(...) sys_ui_log_i(__VA_ARGS__) // 一般信息打印宏控制
#define static_print_warn(...) sys_ui_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_ui_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_ui_log_d(__VA_ARGS__) // 调试信息打印一般常开
#else
#define static_print_info(...)
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
ShutdownViewIndex TjdUiAppShutdownView::currentViewIndex_ = ShutdownViewIndex::SHUTDOWN_UNKNOWN;
#define IMAGE_SHUTDOWN_BIN_PATH TJD_IMAGE_PATH "img_shutdown.bin"
static inline int16_t HorizontalCenter(int16_t width, int16_t parentWidth) { return (parentWidth - width) / 2; }
// clang-format off
static inline void InitLabelHorCenter(OHOS::UILabel &label, uint8_t size, int16_t y, int16_t target, const char *text)
{
label.SetFont(TJD_VECTOR_FONT_FILENAME, size);
label.SetText(text);
label.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT);
label.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
label.SetPosition(HorizontalCenter(label.GetWidth(), target), y);
}
// clang-format on
static TjdUiAppShutdownView *g_shutdownView = nullptr;
TjdUiAppShutdownView::TjdUiAppShutdownView() { g_shutdownView = this; }
TjdUiAppShutdownView::~TjdUiAppShutdownView()
{
g_shutdownView = nullptr;
if (mainView_ != nullptr) {
mainView_->RemoveAll();
delete mainView_;
mainView_ = nullptr;
}
for (int i = 0; i < ShutdownViewIndex::SHUTDOWN_UNKNOWN; i++) {
if (viewManager_[i] != nullptr) {
delete viewManager_[i];
viewManager_[i] = nullptr;
}
}
OHOS::ImageCacheManager::GetInstance().UnloadAllInMultiRes(IMAGE_SHUTDOWN_BIN_PATH);
}
TjdUiAppShutdownView *TjdUiAppShutdownView::GetInstance(void) { return g_shutdownView; }
OHOS::UIScrollView *TjdUiAppShutdownView::InitView()
{
OHOS::ImageCacheManager::GetInstance().LoadAllInMultiRes(IMAGE_SHUTDOWN_BIN_PATH);
if (mainView_ == nullptr) {
mainView_ = new OHOS::UIScrollView();
}
mainView_->SetPosition(0, 0, OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
mainView_->SetOnDragListener(TjdUiAppShutdownPresenter::GetInstance());
ShowView(ShutdownViewIndex::CHOOSE_SHUTDOWN_VIEW);
return mainView_;
}
void TjdUiAppShutdownView::InitTargetView(ShutdownViewIndex index)
{
if (viewManager_[index] != nullptr) {
return;
}
// clang-format off
switch (index) {
case CHOOSE_SHUTDOWN_VIEW: viewManager_[index] = new ChooseShutdownView(); break;
case RESTART_VIEW: viewManager_[index] = new RestartView(); break;
case SHUTDOWN_VIEW: viewManager_[index] = new ShutdownView(); break;
default: break;
}
// clang-format on
if (viewManager_[index] == nullptr) {
return;
}
viewManager_[index]->SetPosition(0, 0);
viewManager_[index]->SetVisible(false);
mainView_->Add(viewManager_[index]);
}
void TjdUiAppShutdownView::ShowView(ShutdownViewIndex showIndex)
{
if (showIndex < 0 || showIndex >= ShutdownViewIndex::SHUTDOWN_UNKNOWN) {
return;
}
InitTargetView(showIndex);
if (currentViewIndex_ >= 0 && currentViewIndex_ < ShutdownViewIndex::SHUTDOWN_UNKNOWN &&
viewManager_[currentViewIndex_] != nullptr) {
viewManager_[currentViewIndex_]->HideView();
}
if (viewManager_[showIndex] != nullptr) {
viewManager_[showIndex]->ShowView();
}
currentViewIndex_ = showIndex;
}
#pragma region 关机主页面
ChooseShutdownView::ChooseShutdownView()
{
OHOS::ImageCacheManager &imgManager = OHOS::ImageCacheManager::GetInstance();
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
const float iconSize = 117.0;
OHOS::Vector2<float> scale = {};
restart_.SetPosition(89, 161, 117, 117);
restart_.SetViewId(BTN_RESTART_VIEW_ID);
restart_.SetTouchable(true);
restart_.SetOnClickListener(TjdUiAppShutdownPresenter::GetInstance());
auto restartRes = imgManager.LoadOneInMultiRes(IMG_SHUTDOWN_RESTART, IMAGE_SHUTDOWN_BIN_PATH);
if (restartRes != nullptr) {
restart_.SetSrc(restartRes);
}
scale.x_ = iconSize / restartRes->header.width;
scale.y_ = iconSize / restartRes->header.height;
restart_.Scale(scale, {0, 0});
restartDesc_.SetFont(TJD_VECTOR_FONT_FILENAME, 34);
restartDesc_.SetPosition(80, 300, 134, 34);
restartDesc_.SetText("重启");
restartDesc_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
restartDesc_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
restartDesc_.SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.6);
restartDesc_.AlignHorCenterToSibling(restart_.GetViewId());
shutdown_.SetPosition(260, 161, 117, 117);
shutdown_.SetViewId(BTN_SHUTDOWN_VIEW_ID);
shutdown_.SetTouchable(true);
shutdown_.SetOnClickListener(TjdUiAppShutdownPresenter::GetInstance());
auto shutdownRes = imgManager.LoadOneInMultiRes(IMG_SHUTDOWN_SHUTDOWN, IMAGE_SHUTDOWN_BIN_PATH);
if (shutdownRes != nullptr) {
shutdown_.SetSrc(shutdownRes);
}
scale.x_ = iconSize / shutdownRes->header.width;
scale.y_ = iconSize / shutdownRes->header.height;
shutdown_.Scale(scale, {0, 0});
shutdownDesc_.SetFont(TJD_VECTOR_FONT_FILENAME, 34);
shutdownDesc_.SetPosition(256, 300, 134, 34);
shutdownDesc_.SetText("关机");
shutdownDesc_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP);
shutdownDesc_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER);
shutdownDesc_.SetStyle(OHOS::STYLE_TEXT_OPA, OHOS::OPA_OPAQUE * 0.6);
shutdownDesc_.AlignHorCenterToSibling(shutdown_.GetViewId());
Add(&restart_);
Add(&restartDesc_);
Add(&shutdown_);
Add(&shutdownDesc_);
}
#pragma endregion
#pragma region 重启页面
RestartView::RestartView()
{
OHOS::ImageCacheManager &imgManager = OHOS::ImageCacheManager::GetInstance();
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
restart_.SetPosition(HorizontalCenter(201, 466), 95, 201, 201);
restart_.SetTouchable(true);
restart_.SetViewId(BTN_RESTART_ENTER_VIEW_ID);
restart_.SetOnClickListener(TjdUiAppShutdownPresenter::GetInstance());
auto restartRes = imgManager.LoadOneInMultiRes(IMG_SHUTDOWN_RESTART, IMAGE_SHUTDOWN_BIN_PATH);
if (restartRes != nullptr) {
restart_.SetSrc(restartRes);
}
InitLabelHorCenter(restartDesc_, 36, 317, 466, "再次点击确认重启");
Add(&restart_);
Add(&restartDesc_);
}
#pragma endregion
#pragma region 关机页面
ShutdownView::ShutdownView()
{
OHOS::ImageCacheManager &imgManager = OHOS::ImageCacheManager::GetInstance();
Resize(OHOS::HORIZONTAL_RESOLUTION, OHOS::VERTICAL_RESOLUTION);
shutdown.SetPosition(HorizontalCenter(201, 466), 95, 201, 201);
shutdown.SetTouchable(true);
shutdown.SetViewId(BTN_SHUTDOWN_ENTER_VIEW_ID);
shutdown.SetOnClickListener(TjdUiAppShutdownPresenter::GetInstance());
auto restartRes = imgManager.LoadOneInMultiRes(IMG_SHUTDOWN_SHUTDOWN, IMAGE_SHUTDOWN_BIN_PATH);
if (restartRes != nullptr) {
shutdown.SetSrc(restartRes);
}
InitLabelHorCenter(shutdownDesc_, 36, 317, 466, "再次点击确认关机");
Add(&shutdown);
Add(&shutdownDesc_);
}
#pragma endregion
} // namespace TJD