• 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 "text_label_adapter.h"
16 #include "language/language_ui.h"
17 #include "log/log.h"
18 #include "updater_ui_const.h"
19 
20 namespace Updater {
TextLabelAdapter(const UxViewInfo & info)21 TextLabelAdapter::TextLabelAdapter(const UxViewInfo &info)
22 {
23     const UxViewCommonInfo &common = info.commonInfo;
24     const UxLabelInfo &spec = std::get<UxLabelInfo>(info.specificInfo);
25     viewId_ = common.id;
26     this->SetPosition(common.x, common.y, common.w, common.h);
27     this->SetVisible(common.visible);
28     this->SetViewId(viewId_.c_str());
29     this->SetAlign(GetAlign(spec.align), OHOS::TEXT_ALIGNMENT_CENTER);
30     this->SetText(TranslateText(spec.text).c_str());
31     this->SetFont(DEFAULT_FONT_FILENAME, spec.fontSize);
32     auto fontColor = StrToColor(spec.fontColor);
33     this->SetStyle(OHOS::STYLE_TEXT_COLOR, fontColor.full);
34     this->SetStyle(OHOS::STYLE_TEXT_OPA, fontColor.alpha);
35     auto bgColor = StrToColor(spec.bgColor);
36     this->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
37     this->SetStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
38 }
39 
IsValid(const UxLabelInfo & info)40 bool TextLabelAdapter::IsValid(const UxLabelInfo &info)
41 {
42     if (info.fontSize > MAX_FONT_SIZE) {
43         LOG(ERROR) << "label viewinfo check failed, fontSize: " << info.fontSize;
44         return false;
45     }
46 
47     if (!CheckColor(info.bgColor) || !CheckColor(info.fontColor)) {
48         LOG(ERROR) << "label viewinfo check failed, bgColor:" << info.bgColor <<
49             " fontColor:" << info.fontColor;
50         return false;
51     }
52     return true;
53 }
54 
SetText(const std::string & txt)55 void TextLabelAdapter::SetText(const std::string &txt)
56 {
57     // usage of "*" is same as label button's SetText
58     if (txt == "*") {
59         return;
60     }
61     OHOS::UILabel::SetText(txt.c_str());
62 }
63 } // namespace Updater
64