• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "box_progress_adapter.h"
16 #include "graphic_engine.h"
17 #include "img_view_adapter.h"
18 #include "log/log.h"
19 #include "scope_guard.h"
20 #include "updater/updater_const.h"
21 #include "view_api.h"
22 
23 namespace Updater {
BoxProgressAdapter(const UxViewInfo & info)24 BoxProgressAdapter::BoxProgressAdapter(const UxViewInfo &info)
25 {
26     SetViewCommonInfo(info.commonInfo);
27     const UxBoxProgressInfo &spec = std::get<UxBoxProgressInfo>(info.specificInfo);
28     this->SetRange(progressWidth_ - 1, 0);
29     this->SetValue(spec.defaultValue);
30 
31     auto bgColor = StrToColor(spec.bgColor);
32     this->SetBackgroundStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
33     this->SetBackgroundStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
34 
35     auto fgColor = StrToColor(spec.fgColor);
36     this->SetForegroundStyle(OHOS::STYLE_BACKGROUND_COLOR, fgColor.full);
37     this->SetForegroundStyle(OHOS::STYLE_BACKGROUND_OPA, fgColor.alpha);
38     hasEp_ = spec.hasEp;
39     epId_ = spec.endPoint;
40 }
41 
IsValid(const UxBoxProgressInfo & info)42 bool BoxProgressAdapter::IsValid(const UxBoxProgressInfo &info)
43 {
44     if (info.defaultValue > MAX_PROGRESS_VALUE) {
45         LOG(ERROR) << "progress viewinfo check failed, defaultValue: " << info.defaultValue;
46         return false;
47     }
48 
49     if (!CheckColor(info.bgColor) || !CheckColor(info.fgColor)) {
50         LOG(ERROR) << "progress viewinfo check failed, bgColor:" << info.bgColor <<
51             " fgColor:" << info.fgColor;
52         return false;
53     }
54 
55     if (info.hasEp && info.endPoint.empty()) {
56         LOG(ERROR) << "has end point but id is empty, please check your config file";
57         return false;
58     }
59 
60     return true;
61 }
62 
SetValue(float value)63 void BoxProgressAdapter::SetValue(float value)
64 {
65 #ifndef UPDATER_UT
66     ON_SCOPE_EXIT(flush) {
67         GraphicEngine::GetInstance().Flush(GetRect());
68     };
69 #endif
70     OHOS::UIBoxProgress::SetValue(static_cast<int>((value / FULL_PERCENT_PROGRESS) * (progressWidth_ - 1)));
71     if (!hasEp_ || ep_ == nullptr || !ep_->IsVisible()) {
72         return;
73     }
74     auto pos = GetPosOfEp();
75     ep_->SetVisible(false);
76     ep_->SetPosition(pos.x, pos.y);
77     ep_->SetVisible(true);
78 }
79 
InitEp()80 bool BoxProgressAdapter::InitEp()
81 {
82     if (!hasEp_) {
83         return true;
84     }
85     if (this->GetParent() == nullptr) {
86         LOG(ERROR) << "box progress's parent is nullptr";
87         return false;
88     }
89     auto child = this->GetParent()->GetChildById(epId_.c_str());
90     if (child == nullptr || child->GetViewType() != OHOS::UI_IMAGE_VIEW) {
91         LOG(ERROR) << "box progress has not an end point children or is not a img view";
92         return false;
93     }
94     ep_ = static_cast<ImgViewAdapter *>(child);
95     return true;
96 }
97 
GetPosOfEp()98 OHOS::Point BoxProgressAdapter::GetPosOfEp()
99 {
100     float rate = static_cast<float>(GetValue()) / (progressWidth_ - 1);
101     constexpr float halfDivisor = 2.0;
102     return OHOS::Point {
103         static_cast<int16_t>(GetX() - ep_->GetWidth() / halfDivisor + GetWidth() * rate),
104         static_cast<int16_t>(GetY() - ep_->GetHeight() / halfDivisor + GetHeight() / halfDivisor)
105     };
106 }
107 
SetVisible(bool isVisible)108 void BoxProgressAdapter::SetVisible(bool isVisible)
109 {
110     OHOS::UIBoxProgress::SetVisible(isVisible);
111     if (!hasEp_ || ep_ == nullptr) {
112         return;
113     }
114     isVisible ?  ep_->Start() : ep_->Stop();
115     ep_->SetVisible(isVisible);
116     ep_->Invalidate();
117 }
118 } // namespace Updater
119