• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "dock/focus_manager.h"
17 #include "language/language_ui.h"
18 #include "log/log.h"
19 #include "page/view_proxy.h"
20 #include "updater_ui_const.h"
21 #include "view_api.h"
22 namespace Updater {
23 struct TextLabelAdapter::TextLabelOnFocusListener : public OHOS::UIView::OnFocusListener {
24     DISALLOW_COPY_MOVE(TextLabelOnFocusListener);
25 public:
TextLabelOnFocusListenerUpdater::TextLabelAdapter::TextLabelOnFocusListener26     TextLabelOnFocusListener(const OHOS::ColorType &focusedFontColor, const OHOS::ColorType &unfocusedFontcolor,
27         const OHOS::ColorType &focusedBgColor, const OHOS::ColorType &unfocusedBgcolor)
28         : focusedFontcolor_(focusedFontColor), unfocusedFontcolor_(unfocusedFontcolor), focusedBgcolor_(focusedBgColor),
29           unfocusedBgcolor_(unfocusedBgcolor)
30     {}
31 
~TextLabelOnFocusListenerUpdater::TextLabelAdapter::TextLabelOnFocusListener32     ~TextLabelOnFocusListener() {}
33 
OnFocusUpdater::TextLabelAdapter::TextLabelOnFocusListener34     bool OnFocus(OHOS::UIView &view) override
35     {
36         TextLabelAdapter *label = nullptr;
37         if (view.GetViewType() != OHOS::UI_LABEL) {
38             return false;
39         }
40         label = static_cast<TextLabelAdapter *>(&view);
41         LOG(DEBUG) << "key OnFocus";
42         label->SetStyle(OHOS::STYLE_TEXT_COLOR, focusedFontcolor_.full);
43         label->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, focusedBgcolor_.full);
44         label->Invalidate();
45         return true;
46     }
47 
OnBlurUpdater::TextLabelAdapter::TextLabelOnFocusListener48     bool OnBlur(OHOS::UIView &view) override
49     {
50         TextLabelAdapter *label = nullptr;
51         if (view.GetViewType() != OHOS::UI_LABEL) {
52             return false;
53         }
54         label = static_cast<TextLabelAdapter *>(&view);
55         LOG(DEBUG) << "key OnBlur";
56         label->SetStyle(OHOS::STYLE_TEXT_COLOR, unfocusedFontcolor_.full);
57         label->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, unfocusedBgcolor_.full);
58         label->Invalidate();
59         return true;
60     }
61 private:
62     OHOS::ColorType focusedFontcolor_;
63     OHOS::ColorType unfocusedFontcolor_;
64     OHOS::ColorType focusedBgcolor_;
65     OHOS::ColorType unfocusedBgcolor_;
66 };
67 
68 TextLabelAdapter::TextLabelAdapter() = default;
69 
70 TextLabelAdapter::~TextLabelAdapter() = default;
71 
TextLabelAdapter(const UxViewInfo & info)72 TextLabelAdapter::TextLabelAdapter(const UxViewInfo &info)
73 {
74     const UxLabelInfo &spec = AsSpecific(info.specificInfo);
75     SetViewCommonInfo(info.commonInfo);
76     this->SetAlign(GetAlign(spec.align), OHOS::TEXT_ALIGNMENT_CENTER);
77     this->SetText(TranslateText(spec.text).c_str());
78     this->SetFont((spec.style == "bold") ? BOLD_FONT_FILENAME : DEFAULT_FONT_FILENAME, spec.fontSize);
79     this->SetStyle(OHOS::STYLE_LINE_HEIGHT, UPDATER_UI_FONT_HEIGHT_RATIO * spec.fontSize);
80     auto fontColor = StrToColor(spec.fontColor);
81     this->SetStyle(OHOS::STYLE_TEXT_COLOR, fontColor.full);
82     this->SetStyle(OHOS::STYLE_TEXT_OPA, fontColor.alpha);
83     auto bgColor = StrToColor(spec.bgColor);
84     this->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
85     this->SetStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
86     this->SetTouchable(spec.touchable);
87     if (spec.focusInfo.focusable) {
88         LOG(DEBUG) << "init focus listener for " << viewId_;
89         InitFocus(fontColor, bgColor, StrToColor(spec.focusInfo.focusedFontColor),
90             StrToColor(spec.focusInfo.focusedBgColor));
91         this->SetTouchable(true);
92     } else {
93         this->SetFocusable(false);
94     }
95 
96     if (spec.lineBreakMode == "marquee") {
97         this->SetLineBreakMode(OHOS::UILabel::LINE_BREAK_MARQUEE);
98         this->SetRollSpeed(80); // 80: label roll speed
99     }
100 }
101 
IsValid(const UxLabelInfo & info)102 bool TextLabelAdapter::IsValid(const UxLabelInfo &info)
103 {
104     if (info.fontSize > MAX_FONT_SIZE) {
105         LOG(ERROR) << "label viewinfo check failed, fontSize: " << static_cast<uint32_t>(info.fontSize);
106         return false;
107     }
108 
109     if (!CheckColor(info.bgColor) || !CheckColor(info.fontColor)) {
110         LOG(ERROR) << "label viewinfo check failed, bgColor:" << info.bgColor <<
111             " fontColor:" << info.fontColor;
112         return false;
113     }
114     return true;
115 }
116 
SetText(const std::string & txt)117 void TextLabelAdapter::SetText(const std::string &txt)
118 {
119     // usage of "*" is same as label button's SetText
120     if (txt == "*") {
121         return;
122     }
123     OHOS::UILabel::SetText(txt.c_str());
124 }
125 
OnPressEvent(const OHOS::PressEvent & event)126 bool TextLabelAdapter::OnPressEvent(const OHOS::PressEvent &event)
127 {
128     OHOS::FocusManager::GetInstance()->ClearFocus();
129     OHOS::FocusManager::GetInstance()->RequestFocus(this);
130     return UIView::OnPressEvent(event);
131 }
132 
InitFocus(const OHOS::ColorType & fontColor,const OHOS::ColorType & bgColor,const OHOS::ColorType & focusedFontColor,const OHOS::ColorType & focusedBgColor)133 void TextLabelAdapter::InitFocus(const OHOS::ColorType &fontColor, const OHOS::ColorType &bgColor,
134     const OHOS::ColorType &focusedFontColor, const OHOS::ColorType &focusedBgColor)
135 {
136     focusListener_ = std::make_unique<TextLabelOnFocusListener>(focusedFontColor, fontColor, focusedBgColor, bgColor);
137     this->SetFocusable(true);
138     this->SetOnFocusListener(focusListener_.get());
139 }
140 } // namespace Updater
141