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
16 #include "base_page.h"
17 #include "component/component_factory.h"
18 #include "dock/focus_manager.h"
19 #include "log/log.h"
20
21 namespace Updater {
22 using namespace OHOS;
BasePage()23 BasePage::BasePage()
24 : width_ {}, height_ {}, root_ {std::make_unique<OHOS::UIViewGroup>()},
25 coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()}
26 {
27 }
28
BasePage(int16_t width,int16_t height)29 BasePage::BasePage(int16_t width, int16_t height)
30 : width_ {width}, height_ {height}, root_ {std::make_unique<OHOS::UIViewGroup>()},
31 coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()}
32 {
33 }
34
IsPageInfoValid(const UxPageInfo & pageInfo)35 bool BasePage::IsPageInfoValid(const UxPageInfo &pageInfo)
36 {
37 if (pageInfo.id.empty()) {
38 LOG(ERROR) << "page id is empty";
39 return false;
40 }
41 if (!CheckColor(pageInfo.bgColor)) {
42 LOG(ERROR) << "page color not valid, bgcolor: " << pageInfo.bgColor;
43 return false;
44 }
45 return true;
46 }
47
Reset()48 void BasePage::Reset()
49 {
50 root_->RemoveAll();
51 coms_.clear();
52 comsMap_.clear();
53 focusedView_ = nullptr;
54 }
55
BuildPage(const UxPageInfo & pageInfo)56 bool BasePage::BuildPage(const UxPageInfo &pageInfo)
57 {
58 Reset();
59 pageId_ = pageInfo.id;
60 color_ = StrToColor(pageInfo.bgColor);
61 root_->SetViewId(pageId_.c_str());
62 root_->SetPosition(0, 0, width_, height_);
63 root_->SetVisible(true);
64 root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
65 root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
66 return BuildComs(pageInfo);
67 }
68
BuildComs(const UxPageInfo & pageInfo)69 bool BasePage::BuildComs(const UxPageInfo &pageInfo)
70 {
71 const auto &infos = pageInfo.viewInfos;
72 coms_.reserve(infos.size());
73 int minY = INT_MAX;
74 for (auto &info : infos) {
75 if (!BuildCom(info, minY)) {
76 return false;
77 }
78 }
79 return true;
80 }
81
BuildCom(const UxViewInfo & viewInfo,int & minY)82 bool BasePage::BuildCom(const UxViewInfo &viewInfo, int &minY)
83 {
84 if (viewInfo.specificInfo == nullptr) {
85 LOG(ERROR) << "component (" << viewInfo.commonInfo.id
86 << ") specific info is null, please check your page config json";
87 return false;
88 }
89 if (!viewInfo.specificInfo->IsValid()) {
90 LOG(ERROR) << "component (" << viewInfo.commonInfo.id << ") is invalid, please check your page config json";
91 return false;
92 }
93
94 std::unique_ptr<ComponentInterface> viewPtr =
95 ComponentFactory::CreateView(viewInfo.specificInfo->GetType(), viewInfo);
96 if (viewPtr == nullptr) {
97 LOG(ERROR) << "create component view failed";
98 return false;
99 }
100 std::string message = std::string("component id:") + viewInfo.commonInfo.id + ", ";
101 auto upView = std::make_unique<ViewProxy>(std::move(viewPtr), message);
102
103 auto &viewObj = *upView;
104 OHOS::UIView *view = viewObj.operator->();
105 if (view == nullptr) {
106 LOG(ERROR) << "view is nullptr";
107 return false;
108 }
109
110 if (view->GetViewId() == nullptr) {
111 LOG(ERROR) << "id is nullptr";
112 return false;
113 }
114 if (view->IsFocusable() && viewInfo.commonInfo.y < minY) {
115 minY = viewInfo.commonInfo.y;
116 focusedView_ = view;
117 }
118 coms_.push_back(std::move(upView));
119 root_->Add(view);
120 // empty id is allowed. id is needed only when get specific view by id.
121 if (std::string(view->GetViewId()).empty()) {
122 return true; // skip this view. build com success, but not save in map
123 }
124 // only map non-empty id
125 if (!comsMap_.emplace(view->GetViewId(), coms_.back().get()).second) {
126 LOG(ERROR) << "view id duplicated:" << view->GetViewId();
127 }
128 return true;
129 }
130
operator [](const std::string & id)131 ViewProxy &BasePage::operator[](const std::string &id)
132 {
133 static ViewProxy dummy;
134 auto it = comsMap_.find(id);
135 if (it != comsMap_.end() && it->second != nullptr) {
136 return *it->second;
137 }
138 return dummy;
139 }
140
IsValidCom(const std::string & id) const141 bool BasePage::IsValidCom(const std::string &id) const
142 {
143 return comsMap_.find(id) != comsMap_.end();
144 }
145
IsValid() const146 bool BasePage::IsValid() const
147 {
148 return root_ != nullptr;
149 }
150
GetPageId()151 std::string BasePage::GetPageId()
152 {
153 return pageId_;
154 }
155
SetVisible(bool isVisible)156 void BasePage::SetVisible(bool isVisible)
157 {
158 if (root_ == nullptr) {
159 LOG(ERROR) << "root is null";
160 return;
161 }
162 root_->SetVisible(isVisible);
163 if (isVisible) {
164 // change background
165 root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
166 root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
167 }
168 UpdateFocus(isVisible);
169 }
170
IsVisible() const171 bool BasePage::IsVisible() const
172 {
173 if (root_ == nullptr) {
174 LOG(ERROR) << "root is null";
175 return false;
176 }
177 return root_->IsVisible();
178 }
179
GetView() const180 OHOS::UIViewGroup *BasePage::GetView() const
181 {
182 return root_.get();
183 }
184 } // namespace Updater
185