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 "label_btn_adapter.h"
16 #include "dock/focus_manager.h"
17 #include "log/log.h"
18 #include "page/view_proxy.h"
19 #include "updater_ui_const.h"
20
21 namespace Updater {
22 struct LabelBtnAdapter::LabelBtnOnFocusListener : public OHOS::UIView::OnFocusListener {
23 DISALLOW_COPY_MOVE(LabelBtnOnFocusListener);
24 public:
LabelBtnOnFocusListenerUpdater::LabelBtnAdapter::LabelBtnOnFocusListener25 LabelBtnOnFocusListener(const OHOS::ColorType &focusedTxtColor, const OHOS::ColorType &unfocusedTxtcolor,
26 const OHOS::ColorType &focusedBgColor, const OHOS::ColorType &unfocusedBgcolor)
27 : focusedTxtcolor_(focusedTxtColor), unfocusedTxtcolor_(unfocusedTxtcolor), focusedBgcolor_(focusedBgColor),
28 unfocusedBgcolor_(unfocusedBgcolor)
29 {}
30
~LabelBtnOnFocusListenerUpdater::LabelBtnAdapter::LabelBtnOnFocusListener31 ~LabelBtnOnFocusListener() {}
32
OnFocusUpdater::LabelBtnAdapter::LabelBtnOnFocusListener33 bool OnFocus(OHOS::UIView &view) override
34 {
35 LabelBtnAdapter *button = nullptr;
36 if (view.GetViewType() != OHOS::UI_LABEL_BUTTON) {
37 return false;
38 }
39 button = static_cast<LabelBtnAdapter *>(&view);
40 LOG(DEBUG) << "key OnFocus";
41 button->SetLabelStyle(OHOS::STYLE_TEXT_COLOR, focusedTxtcolor_.full);
42 button->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, focusedBgcolor_.full);
43 button->Invalidate();
44 return true;
45 }
46
OnBlurUpdater::LabelBtnAdapter::LabelBtnOnFocusListener47 bool OnBlur(OHOS::UIView &view) override
48 {
49 LabelBtnAdapter *button = nullptr;
50 if (view.GetViewType() != OHOS::UI_LABEL_BUTTON) {
51 return false;
52 }
53 button = static_cast<LabelBtnAdapter *>(&view);
54 LOG(DEBUG) << "key OnBlur";
55 button->SetLabelStyle(OHOS::STYLE_TEXT_COLOR, unfocusedTxtcolor_.full);
56 button->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, unfocusedBgcolor_.full);
57 button->Invalidate();
58 return true;
59 }
60 private:
61 OHOS::ColorType focusedTxtcolor_;
62 OHOS::ColorType unfocusedTxtcolor_;
63 OHOS::ColorType focusedBgcolor_;
64 OHOS::ColorType unfocusedBgcolor_;
65 };
66
67 LabelBtnAdapter::LabelBtnAdapter() = default;
68
69 LabelBtnAdapter::~LabelBtnAdapter() = default;
70
LabelBtnAdapter(const UxViewInfo & info)71 LabelBtnAdapter::LabelBtnAdapter(const UxViewInfo &info)
72 {
73 const UxViewCommonInfo &common = info.commonInfo;
74 const UxLabelBtnInfo &spec = std::get<UxLabelBtnInfo>(info.specificInfo);
75 viewId_ = common.id;
76 this->SetPosition(common.x, common.y);
77 this->SetWidth(common.w);
78 this->SetHeight(common.h);
79 this->SetVisible(common.visible);
80 this->SetViewId(viewId_.c_str());
81 this->SetText(TranslateText(spec.text).c_str());
82 this->SetFont(DEFAULT_FONT_FILENAME, spec.fontSize);
83 auto txtColor = StrToColor(spec.txtColor);
84 this->SetLabelStyle(OHOS::STYLE_TEXT_COLOR, txtColor.full);
85 this->SetLabelStyle(OHOS::STYLE_TEXT_OPA, txtColor.alpha);
86 auto bgColor = StrToColor(spec.bgColor);
87 this->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
88 this->SetStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
89 if (spec.focusable) {
90 LOG(INFO) << "init focus listener for " << viewId_;
91 InitFocus(txtColor, bgColor, StrToColor(spec.focusedTxtColor),
92 StrToColor(spec.focusedBgColor));
93 }
94 }
95
IsValid(const UxLabelBtnInfo & info)96 bool LabelBtnAdapter::IsValid(const UxLabelBtnInfo &info)
97 {
98 if (info.fontSize > MAX_FONT_SIZE) {
99 LOG(ERROR) << "label viewinfo check failed, fontSize: " << info.fontSize;
100 return false;
101 }
102
103 if (!CheckColor(info.bgColor) || !CheckColor(info.focusedBgColor) ||
104 !CheckColor(info.txtColor) || !CheckColor(info.focusedTxtColor)) {
105 LOG(ERROR) << "label viewinfo check failed, bgColor:" << info.bgColor <<
106 " focusedBgColor:" << info.focusedBgColor <<
107 " txtColor:" << info.txtColor <<
108 " focusedTxtColor: " << info.focusedTxtColor;
109 return false;
110 }
111
112 return true;
113 }
114
SetText(const std::string & txt)115 void LabelBtnAdapter::SetText(const std::string &txt)
116 {
117 /**
118 * if argument txt is "*", then won't change the content of this label,
119 * ignore this txt and keep label button text as before. "*" is normally
120 * used as DEFAULT_STRING, which can ignore unused string
121 */
122 if (txt == "*") {
123 return;
124 }
125 OHOS::UILabelButton::SetText(txt.c_str());
126 }
127
OnPressEvent(const OHOS::PressEvent & event)128 bool LabelBtnAdapter::OnPressEvent(const OHOS::PressEvent &event)
129 {
130 OHOS::FocusManager::GetInstance()->ClearFocus();
131 OHOS::FocusManager::GetInstance()->RequestFocus(this);
132 return UIButton::OnPressEvent(event);
133 }
134
InitFocus(const OHOS::ColorType & txtColor,const OHOS::ColorType & bgColor,const OHOS::ColorType & focusedTxtColor,const OHOS::ColorType & focusedBgColor)135 void LabelBtnAdapter::InitFocus(const OHOS::ColorType &txtColor, const OHOS::ColorType &bgColor,
136 const OHOS::ColorType &focusedTxtColor, const OHOS::ColorType &focusedBgColor)
137 {
138 focusListener_ = std::make_unique<LabelBtnOnFocusListener>(focusedTxtColor, txtColor, focusedBgColor, bgColor);
139 this->SetFocusable(true);
140 this->SetOnFocusListener(focusListener_.get());
141 }
142 } // namespace Updater
143