/*---------------------------------------------------------------------------- * Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved. * * Description: TjdUiUtils.cpp * * Author: luziquan@ss-tjd.com * * Create: 2024-11-07 *--------------------------------------------------------------------------*/ #include "TjdUiUtils.h" #include "TjdUiMultiLanguageExt.h" #include "cmsis_os2.h" #include "components/root_view.h" #include "components/ui_label.h" #include "components/ui_scroll_view.h" #include "dock/focus_manager.h" #include "graphic_service.h" #include "power_display_service.h" #include namespace TJD { constexpr static uint32_t MSG_SHOW_DEFAULT_TIMEOUT = 2000; constexpr uint8_t REBOUND_SIZE = 200; int16_t TjdUiUtils::CalculateBottomHidePos(OHOS::UIView &screenBottomView) { const int16_t lastItemBottom = screenBottomView.GetY() + screenBottomView.GetHeight(); return (lastItemBottom + (OHOS::VERTICAL_RESOLUTION / 2) - (screenBottomView.GetHeight() / 2)); } void TjdUiUtils::SetDefaultRollingEffect(OHOS::UIScrollView &view) { view.SetThrowDrag(true); view.SetReboundSize(REBOUND_SIZE); view.SetYScrollBarVisible(true); view.SetSwipeACCLevel(10); view.SetDragACCLevel(15); } void TjdUiUtils::ScrollByTop(OHOS::UIScrollView &view) { view.SetReboundSize(0); if (view.GetChildrenHead() != nullptr) { view.ScrollBy(0, MATH_ABS(view.GetChildrenHead()->GetRect().GetY())); } else { view.ScrollBy(0, view.GetHeight()); } view.SetReboundSize(REBOUND_SIZE); } void TjdUiUtils::ViewRequestFocus(OHOS::UIView &view) { OHOS::FocusManager::GetInstance()->ClearFocus(); view.RequestFocus(); } class TjdUiMsgBox : public OHOS::UIViewGroup { public: TjdUiMsgBox(const char *msg, TjdUiUtils::MsgBoxPosition position, uint32_t color, uint32_t opa); virtual ~TjdUiMsgBox(); private: OHOS::UILabel msg_; osTimerId_t outTimeTimerId_ = nullptr; }; static TjdUiMsgBox *g_msgBox = nullptr; void TjdUiUtils::ShowMsgBox(const char *msg, TjdUiUtils::MsgBoxPosition position, uint32_t color, uint32_t opa) { GraphicService::GetInstance()->PostGraphicEvent([msg, position, color, opa]() { if (g_msgBox != nullptr) { OHOS::RootView::GetInstance()->RemoveSystemView(g_msgBox); delete g_msgBox; g_msgBox = nullptr; } g_msgBox = new TjdUiMsgBox(msg, position, color, opa); OHOS::RootView::GetInstance()->AddSystemView(g_msgBox); g_msgBox->Invalidate(); }); } TjdUiMsgBox::TjdUiMsgBox(const char *msg, TjdUiUtils::MsgBoxPosition position, uint32_t color, uint32_t opa) { SetHeight(110); SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color); SetStyle(OHOS::STYLE_BORDER_RADIUS, GetHeight() >> 1); SetStyle(OHOS::STYLE_BACKGROUND_OPA, opa); msg_.SetFont(TJD_VECTOR_FONT_FILENAME, 30); msg_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_ADAPT); msg_.SetAlign(OHOS::TEXT_ALIGNMENT_CENTER, OHOS::TEXT_ALIGNMENT_CENTER); msg_.SetText(msg); switch (position) { case TjdUiUtils::MsgBoxPosition::MSG_BOX_CENTER: SetY((466 / 2) - (GetHeight() / 2)); if (msg_.GetWidth() * 1.2 > OHOS::HORIZONTAL_RESOLUTION) { msg_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP); msg_.Resize(385, 110); } break; case TjdUiUtils::MsgBoxPosition::MSG_BOX_BOTTOM: SetY(300); if (msg_.GetWidth() * 1.2 > OHOS::HORIZONTAL_RESOLUTION * 0.6) { msg_.SetLineBreakMode(OHOS::UILabel::LINE_BREAK_WRAP); msg_.Resize(310, 110); } break; } msg_.SetY(TjdUiUtils::VerticalCenter(msg_.GetHeight(), GetHeight())); SetWidth(msg_.GetWidth() * 1.2); SetX(TjdUiUtils::HorizontalCenter(GetWidth(), 466)); msg_.SetX(TjdUiUtils::HorizontalCenter(msg_.GetWidth(), GetWidth())); outTimeTimerId_ = osTimerNew( [](void *arg) { GraphicService::GetInstance()->PostGraphicEvent([]() { OHOS::RootView::GetInstance()->RemoveSystemView(g_msgBox); delete g_msgBox; g_msgBox = nullptr; }); }, osTimerOnce, this, nullptr); Add(&msg_); osTimerStart(outTimeTimerId_, MSG_SHOW_DEFAULT_TIMEOUT); } void TjdUiUtils::AutoAlignHorizontalCenter(OHOS::UIView &view, int16_t parentWidth) { view.SetX(TjdUiUtils::HorizontalCenter(view.GetWidth(), parentWidth)); } void TjdUiUtils::AutoAlignVerticalCenter(OHOS::UIView &view, int16_t parentHeight) { view.SetY(TjdUiUtils::VerticalCenter(view.GetHeight(), parentHeight)); } void TjdUiUtils::AdjustViewOffset(OHOS::UIView &curView, OHOS::UIView &targetView, int16_t offset, OffsetDirection direction) { int16_t targetViewX; int16_t targetViewY; switch (direction) { case OffsetDirection::OFFSET_LEFT: case OffsetDirection::OFFSET_RIGHT: targetViewX = targetView.GetX(); switch (direction) { case OffsetDirection::OFFSET_LEFT: curView.SetX(targetViewX - offset - curView.GetWidth()); break; case OffsetDirection::OFFSET_RIGHT: curView.SetX(targetViewX + offset + targetView.GetWidth()); break; } break; case OffsetDirection::OFFSET_TOP: case OffsetDirection::OFFSET_BOTTOM: targetViewY = targetView.GetY(); switch (direction) { case OffsetDirection::OFFSET_TOP: curView.SetY(targetViewY - offset - curView.GetHeight()); break; case OffsetDirection::OFFSET_BOTTOM: curView.SetY(targetViewY + offset + targetView.GetHeight()); break; } break; default: break; } curView.Invalidate(); } TjdUiMsgBox::~TjdUiMsgBox() { RemoveAll(); osTimerStop(outTimeTimerId_); osTimerDelete(outTimeTimerId_); } }; // namespace TJD