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 "log/log.h"
18 #include "scope_guard.h"
19 #include "updater/updater_const.h"
20
21 namespace Updater {
BoxProgressAdapter(const UxViewInfo & info)22 BoxProgressAdapter::BoxProgressAdapter(const UxViewInfo &info)
23 {
24 const UxViewCommonInfo &common = info.commonInfo;
25 const UxBoxProgressInfo &spec = std::get<UxBoxProgressInfo>(info.specificInfo);
26 viewId_ = common.id;
27 this->SetPosition(common.x, common.y, common.w, common.h);
28 this->SetVisible(common.visible);
29 this->SetViewId(viewId_.c_str());
30 this->SetRange(progressWidth_ - 1, 0);
31 this->SetValue(spec.defaultValue);
32
33 auto bgColor = StrToColor(spec.bgColor);
34 this->SetBackgroundStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
35 this->SetBackgroundStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
36
37 auto fgColor = StrToColor(spec.fgColor);
38 this->SetForegroundStyle(OHOS::STYLE_BACKGROUND_COLOR, fgColor.full);
39 this->SetForegroundStyle(OHOS::STYLE_BACKGROUND_OPA, fgColor.alpha);
40 hasEp_ = spec.hasEp;
41 epId_ = spec.endPoint;
42 }
43
IsValid(const UxBoxProgressInfo & info)44 bool BoxProgressAdapter::IsValid(const UxBoxProgressInfo &info)
45 {
46 if (info.defaultValue > MAX_PROGRESS_VALUE) {
47 LOG(ERROR) << "progress viewinfo check failed, defaultValue: " << info.defaultValue;
48 return false;
49 }
50
51 if (!CheckColor(info.bgColor) || !CheckColor(info.fgColor)) {
52 LOG(ERROR) << "progress viewinfo check failed, bgColor:" << info.bgColor <<
53 " fgColor:" << info.fgColor;
54 return false;
55 }
56
57 if (info.hasEp && info.endPoint.empty()) {
58 LOG(ERROR) << "has end point but id is empty, please check your config file";
59 return false;
60 }
61
62 return true;
63 }
64
SetValue(float value)65 void BoxProgressAdapter::SetValue(float value)
66 {
67 #ifndef UPDATER_UT
68 ON_SCOPE_EXIT(flush) {
69 GraphicEngine::GetInstance().Flush(GetRect());
70 };
71 #endif
72 OHOS::UIBoxProgress::SetValue(static_cast<int>((value / FULL_PERCENT_PROGRESS) * (progressWidth_ - 1)));
73 if (!hasEp_ || ep_ == nullptr) {
74 return;
75 }
76 auto pos = GetPosOfEp();
77 ep_->SetVisible(false);
78 ep_->SetPosition(pos.x, pos.y);
79 ep_->SetVisible(true);
80 }
81
InitEp()82 bool BoxProgressAdapter::InitEp()
83 {
84 if (!hasEp_) {
85 return true;
86 }
87 if (this->GetParent() == nullptr) {
88 LOG(ERROR) << "box progress's parent is nullptr";
89 return false;
90 }
91 auto child = this->GetParent()->GetChildById(epId_.c_str());
92 if (child == nullptr || child->GetViewType() != OHOS::UI_IMAGE_VIEW) {
93 LOG(ERROR) << "box progress has not an end point children or is not a img view";
94 return false;
95 }
96 ep_ = static_cast<ImgViewAdapter *>(child);
97 return true;
98 }
99
GetPosOfEp()100 OHOS::Point BoxProgressAdapter::GetPosOfEp()
101 {
102 float rate = static_cast<float>(GetValue()) / (progressWidth_ - 1);
103 constexpr float halfDivisor = 2.0;
104 return OHOS::Point {
105 static_cast<int16_t>(GetX() - ep_->GetWidth() / halfDivisor + GetWidth() * rate),
106 static_cast<int16_t>(GetY() - ep_->GetHeight() / halfDivisor + GetHeight() / halfDivisor)
107 };
108 }
109
SetVisible(bool isVisible)110 void BoxProgressAdapter::SetVisible(bool isVisible)
111 {
112 OHOS::UIBoxProgress::SetVisible(isVisible);
113 if (!hasEp_ || ep_ == nullptr) {
114 return;
115 }
116 isVisible ? ep_->Start() : ep_->Stop();
117 ep_->SetVisible(isVisible);
118 ep_->Invalidate();
119 }
120 } // namespace Updater
121