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