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
16 #include "sub_page.h"
17 #include "dock/focus_manager.h"
18 #include "log/log.h"
19 #include "scope_guard.h"
20
21 namespace Updater {
22 using namespace OHOS;
SubPage()23 SubPage::SubPage() : basePage_ {}, pageId_ {}, comsId_ {}, isVisible_ {false}, color_ {Color::Black()}
24 {
25 }
26
SubPage(const std::shared_ptr<Page> & basePage,const std::string & pageId)27 SubPage::SubPage(const std::shared_ptr<Page> &basePage, const std::string &pageId)
28 : basePage_ {basePage}, pageId_ {pageId}, comsId_ {}, isVisible_ {false}, color_ {Color::Black()}
29 {
30 }
31
Reset()32 void SubPage::Reset()
33 {
34 isVisible_ = false;
35 focusedView_ = nullptr;
36 }
37
BuildSubPage(UxSubPageInfo & subpageInfo)38 bool SubPage::BuildSubPage(UxSubPageInfo &subpageInfo)
39 {
40 if (!IsValid()) {
41 return false;
42 }
43 Reset();
44 color_ = StrToColor(subpageInfo.bgColor);
45 comsId_ = std::move(subpageInfo.coms);
46 int minY = INT16_MAX;
47 std::string focusedId {};
48 for (auto &id : comsId_) {
49 if (!basePage_->IsValidCom(id)) {
50 LOG(ERROR) << "invalid id for subpage: " << id;
51 return false;
52 }
53 if ((*basePage_)[id]->IsFocusable() && (*basePage_)[id]->GetY() < minY) {
54 minY = (*basePage_)[id]->GetY();
55 focusedId = id;
56 }
57 }
58 if (!focusedId.empty()) {
59 focusedView_ = (*basePage_)[focusedId].As();
60 }
61 return true;
62 }
63
IsPageInfoValid(const UxSubPageInfo & info)64 bool SubPage::IsPageInfoValid(const UxSubPageInfo &info)
65 {
66 if (info.id.empty()) {
67 LOG(ERROR) << "sub page id is empty";
68 return false;
69 }
70
71 if (!CheckColor(info.bgColor)) {
72 LOG(ERROR) << "sub page color not valid, bgcolor: " << info.bgColor;
73 return false;
74 }
75 return true;
76 }
77
GetPageId()78 std::string SubPage::GetPageId()
79 {
80 return pageId_;
81 }
82
SetVisible(bool isVisible)83 void SubPage::SetVisible(bool isVisible)
84 {
85 if (!IsValid()) {
86 return;
87 }
88 isVisible_ = isVisible;
89 const auto &view = basePage_->GetView();
90 if (view == nullptr) {
91 LOG(ERROR) << "basepage's view is nullptr";
92 return;
93 }
94
95 for (const auto &id : comsId_) {
96 (*basePage_)[id]->SetVisible(isVisible);
97 }
98 view->SetVisible(isVisible);
99 if (isVisible) {
100 // change background
101 view->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
102 view->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
103 }
104 UpdateFocus(isVisible);
105 }
106
IsVisible() const107 bool SubPage::IsVisible() const
108 {
109 return isVisible_;
110 }
111
GetView() const112 OHOS::UIViewGroup *SubPage::GetView() const
113 {
114 if (!IsValid()) {
115 return nullptr;
116 }
117 return basePage_->GetView();
118 }
119
IsValid() const120 bool SubPage::IsValid() const
121 {
122 if (basePage_ == nullptr || !basePage_->IsValid()) {
123 LOG(ERROR) << "basepage of subpage is null";
124 return false;
125 }
126 return true;
127 }
128
IsValidCom(const std::string & id) const129 bool SubPage::IsValidCom(const std::string &id) const
130 {
131 if (!IsValid()) {
132 return false;
133 }
134 if (std::find(comsId_.begin(), comsId_.end(), id) == comsId_.end()) {
135 return false;
136 }
137 return basePage_->IsValidCom(id);
138 }
139
operator [](const std::string & id)140 ViewProxy &SubPage::operator[](const std::string &id)
141 {
142 static ViewProxy dummy;
143 if (!IsValid()) {
144 return dummy;
145 }
146 return (*basePage_)[id];
147 }
148 } // namespace Updater