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
22namespace Updater {
23struct LabelBtnAdapter::LabelBtnOnFocusListener : public OHOS::UIView::OnFocusListener {
24    DISALLOW_COPY_MOVE(LabelBtnOnFocusListener);
25public:
26    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
32    ~LabelBtnOnFocusListener() {}
33
34    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
48    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    }
61private:
62    OHOS::ColorType focusedTxtcolor_;
63    OHOS::ColorType unfocusedTxtcolor_;
64    OHOS::ColorType focusedBgcolor_;
65    OHOS::ColorType unfocusedBgcolor_;
66};
67
68LabelBtnAdapter::LabelBtnAdapter() = default;
69
70LabelBtnAdapter::~LabelBtnAdapter() = default;
71
72LabelBtnAdapter::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    this->SetLabelStyle(OHOS::STYLE_LINE_HEIGHT, UPDATER_UI_FONT_HEIGHT_RATIO * spec.fontSize);
79    auto txtColor = StrToColor(spec.txtColor);
80    this->SetLabelStyle(OHOS::STYLE_TEXT_COLOR, txtColor.full);
81    this->SetLabelStyle(OHOS::STYLE_TEXT_OPA, txtColor.alpha);
82    auto bgColor = StrToColor(spec.bgColor);
83    this->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, bgColor.full);
84    this->SetStyle(OHOS::STYLE_BACKGROUND_OPA, bgColor.alpha);
85    if (spec.focusable) {
86        LOG(DEBUG) << "init focus listener for " << viewId_;
87        InitFocus(txtColor, bgColor, StrToColor(spec.focusedTxtColor),
88            StrToColor(spec.focusedBgColor));
89    }
90}
91
92bool LabelBtnAdapter::IsValid(const UxLabelBtnInfo &info)
93{
94    if (info.fontSize > MAX_FONT_SIZE) {
95        LOG(ERROR) << "label viewinfo check failed, fontSize: " << info.fontSize;
96        return false;
97    }
98
99    if (!CheckColor(info.bgColor) || !CheckColor(info.focusedBgColor) ||
100        !CheckColor(info.txtColor) || !CheckColor(info.focusedTxtColor)) {
101        LOG(ERROR) << "label viewinfo check failed, bgColor:" << info.bgColor <<
102            " focusedBgColor:" << info.focusedBgColor <<
103            " txtColor:" << info.txtColor <<
104            " focusedTxtColor: " << info.focusedTxtColor;
105        return false;
106    }
107
108    return true;
109}
110
111void LabelBtnAdapter::SetText(const std::string &txt)
112{
113    /**
114     * if argument txt is "*", then won't change the content of this label,
115     * ignore this txt and keep label button text as before. "*" is normally
116     * used as DEFAULT_STRING, which can ignore unused string
117     */
118    if (txt == "*") {
119        return;
120    }
121    OHOS::UILabelButton::SetText(txt.c_str());
122}
123
124bool LabelBtnAdapter::OnPressEvent(const OHOS::PressEvent &event)
125{
126    OHOS::FocusManager::GetInstance()->ClearFocus();
127    OHOS::FocusManager::GetInstance()->RequestFocus(this);
128    return UIButton::OnPressEvent(event);
129}
130
131void LabelBtnAdapter::InitFocus(const OHOS::ColorType &txtColor, const OHOS::ColorType &bgColor,
132    const OHOS::ColorType &focusedTxtColor, const OHOS::ColorType &focusedBgColor)
133{
134    focusListener_ = std::make_unique<LabelBtnOnFocusListener>(focusedTxtColor, txtColor, focusedBgColor, bgColor);
135    this->SetFocusable(true);
136    this->SetOnFocusListener(focusListener_.get());
137}
138} // namespace Updater
139