1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include "base_page.h" 17fb299fa2Sopenharmony_ci#include "component/component_factory.h" 18fb299fa2Sopenharmony_ci#include "dock/focus_manager.h" 19fb299fa2Sopenharmony_ci#include "log/log.h" 20fb299fa2Sopenharmony_ci 21fb299fa2Sopenharmony_cinamespace Updater { 22fb299fa2Sopenharmony_ciusing namespace OHOS; 23fb299fa2Sopenharmony_ciBasePage::BasePage() 24fb299fa2Sopenharmony_ci : width_ {}, height_ {}, root_ {std::make_unique<OHOS::UIViewGroup>()}, 25fb299fa2Sopenharmony_ci coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()} 26fb299fa2Sopenharmony_ci{ 27fb299fa2Sopenharmony_ci} 28fb299fa2Sopenharmony_ci 29fb299fa2Sopenharmony_ciBasePage::BasePage(int16_t width, int16_t height) 30fb299fa2Sopenharmony_ci : width_ {width}, height_ {height}, root_ {std::make_unique<OHOS::UIViewGroup>()}, 31fb299fa2Sopenharmony_ci coms_ {}, comsMap_ {}, pageId_ {}, color_ {Color::Black()} 32fb299fa2Sopenharmony_ci{ 33fb299fa2Sopenharmony_ci} 34fb299fa2Sopenharmony_ci 35fb299fa2Sopenharmony_cibool BasePage::IsPageInfoValid(const UxPageInfo &pageInfo) 36fb299fa2Sopenharmony_ci{ 37fb299fa2Sopenharmony_ci if (pageInfo.id.empty()) { 38fb299fa2Sopenharmony_ci LOG(ERROR) << "page id is empty"; 39fb299fa2Sopenharmony_ci return false; 40fb299fa2Sopenharmony_ci } 41fb299fa2Sopenharmony_ci if (!CheckColor(pageInfo.bgColor)) { 42fb299fa2Sopenharmony_ci LOG(ERROR) << "page color not valid, bgcolor: " << pageInfo.bgColor; 43fb299fa2Sopenharmony_ci return false; 44fb299fa2Sopenharmony_ci } 45fb299fa2Sopenharmony_ci return true; 46fb299fa2Sopenharmony_ci} 47fb299fa2Sopenharmony_ci 48fb299fa2Sopenharmony_civoid BasePage::Reset() 49fb299fa2Sopenharmony_ci{ 50fb299fa2Sopenharmony_ci root_->RemoveAll(); 51fb299fa2Sopenharmony_ci coms_.clear(); 52fb299fa2Sopenharmony_ci comsMap_.clear(); 53fb299fa2Sopenharmony_ci focusedView_ = nullptr; 54fb299fa2Sopenharmony_ci} 55fb299fa2Sopenharmony_ci 56fb299fa2Sopenharmony_cibool BasePage::BuildPage(const UxPageInfo &pageInfo) 57fb299fa2Sopenharmony_ci{ 58fb299fa2Sopenharmony_ci Reset(); 59fb299fa2Sopenharmony_ci pageId_ = pageInfo.id; 60fb299fa2Sopenharmony_ci color_ = StrToColor(pageInfo.bgColor); 61fb299fa2Sopenharmony_ci root_->SetViewId(pageId_.c_str()); 62fb299fa2Sopenharmony_ci root_->SetPosition(0, 0, width_, height_); 63fb299fa2Sopenharmony_ci root_->SetVisible(true); 64fb299fa2Sopenharmony_ci root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full); 65fb299fa2Sopenharmony_ci root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha); 66fb299fa2Sopenharmony_ci return BuildComs(pageInfo); 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_cibool BasePage::BuildComs(const UxPageInfo &pageInfo) 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci const auto &infos = pageInfo.viewInfos; 72fb299fa2Sopenharmony_ci coms_.reserve(infos.size()); 73fb299fa2Sopenharmony_ci int minY = INT_MAX; 74fb299fa2Sopenharmony_ci for (auto &info : infos) { 75fb299fa2Sopenharmony_ci if (!BuildCom(info, minY)) { 76fb299fa2Sopenharmony_ci return false; 77fb299fa2Sopenharmony_ci } 78fb299fa2Sopenharmony_ci } 79fb299fa2Sopenharmony_ci return true; 80fb299fa2Sopenharmony_ci} 81fb299fa2Sopenharmony_ci 82fb299fa2Sopenharmony_cibool BasePage::BuildCom(const UxViewInfo &viewInfo, int &minY) 83fb299fa2Sopenharmony_ci{ 84fb299fa2Sopenharmony_ci if (!ComponentFactory::CheckUxComponent(viewInfo)) { 85fb299fa2Sopenharmony_ci LOG(ERROR) << "component (" << viewInfo.commonInfo.id 86fb299fa2Sopenharmony_ci << ") is invalid, please check your page config json"; 87fb299fa2Sopenharmony_ci return false; 88fb299fa2Sopenharmony_ci } 89fb299fa2Sopenharmony_ci auto upView = std::make_unique<ViewProxy>(ComponentFactory::CreateUxComponent(viewInfo), 90fb299fa2Sopenharmony_ci std::string("component id:") + viewInfo.commonInfo.id + ", "); 91fb299fa2Sopenharmony_ci auto &view = *upView; 92fb299fa2Sopenharmony_ci if (view->GetViewId() == nullptr) { 93fb299fa2Sopenharmony_ci LOG(ERROR) << "id is nullptr"; 94fb299fa2Sopenharmony_ci return false; 95fb299fa2Sopenharmony_ci } 96fb299fa2Sopenharmony_ci if (view->IsFocusable() && viewInfo.commonInfo.y < minY) { 97fb299fa2Sopenharmony_ci minY = viewInfo.commonInfo.y; 98fb299fa2Sopenharmony_ci focusedView_ = view.operator->(); 99fb299fa2Sopenharmony_ci } 100fb299fa2Sopenharmony_ci coms_.push_back(std::move(upView)); 101fb299fa2Sopenharmony_ci root_->Add(view.operator->()); 102fb299fa2Sopenharmony_ci // empty id is allowed. id is needed only when get specific view by id. 103fb299fa2Sopenharmony_ci if (std::string(view->GetViewId()).empty()) { 104fb299fa2Sopenharmony_ci return true; // skip this view. build com success, but not save in map 105fb299fa2Sopenharmony_ci } 106fb299fa2Sopenharmony_ci // only map non-empty id 107fb299fa2Sopenharmony_ci if (!comsMap_.emplace(view->GetViewId(), coms_.back().get()).second) { 108fb299fa2Sopenharmony_ci LOG(ERROR) << "view id duplicated:" << view->GetViewId(); 109fb299fa2Sopenharmony_ci } 110fb299fa2Sopenharmony_ci return true; 111fb299fa2Sopenharmony_ci} 112fb299fa2Sopenharmony_ci 113fb299fa2Sopenharmony_ciViewProxy &BasePage::operator[](const std::string &id) 114fb299fa2Sopenharmony_ci{ 115fb299fa2Sopenharmony_ci static ViewProxy dummy; 116fb299fa2Sopenharmony_ci auto it = comsMap_.find(id); 117fb299fa2Sopenharmony_ci if (it != comsMap_.end() && it->second != nullptr) { 118fb299fa2Sopenharmony_ci return *it->second; 119fb299fa2Sopenharmony_ci } 120fb299fa2Sopenharmony_ci return dummy; 121fb299fa2Sopenharmony_ci} 122fb299fa2Sopenharmony_ci 123fb299fa2Sopenharmony_cibool BasePage::IsValidCom(const std::string &id) const 124fb299fa2Sopenharmony_ci{ 125fb299fa2Sopenharmony_ci return comsMap_.find(id) != comsMap_.end(); 126fb299fa2Sopenharmony_ci} 127fb299fa2Sopenharmony_ci 128fb299fa2Sopenharmony_cibool BasePage::IsValid() const 129fb299fa2Sopenharmony_ci{ 130fb299fa2Sopenharmony_ci return root_ != nullptr; 131fb299fa2Sopenharmony_ci} 132fb299fa2Sopenharmony_ci 133fb299fa2Sopenharmony_cistd::string BasePage::GetPageId() 134fb299fa2Sopenharmony_ci{ 135fb299fa2Sopenharmony_ci return pageId_; 136fb299fa2Sopenharmony_ci} 137fb299fa2Sopenharmony_ci 138fb299fa2Sopenharmony_civoid BasePage::SetVisible(bool isVisible) 139fb299fa2Sopenharmony_ci{ 140fb299fa2Sopenharmony_ci if (root_ == nullptr) { 141fb299fa2Sopenharmony_ci LOG(ERROR) << "root is null"; 142fb299fa2Sopenharmony_ci return; 143fb299fa2Sopenharmony_ci } 144fb299fa2Sopenharmony_ci root_->SetVisible(isVisible); 145fb299fa2Sopenharmony_ci if (isVisible) { 146fb299fa2Sopenharmony_ci // change background 147fb299fa2Sopenharmony_ci root_->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full); 148fb299fa2Sopenharmony_ci root_->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha); 149fb299fa2Sopenharmony_ci } 150fb299fa2Sopenharmony_ci UpdateFocus(isVisible); 151fb299fa2Sopenharmony_ci} 152fb299fa2Sopenharmony_ci 153fb299fa2Sopenharmony_cibool BasePage::IsVisible() const 154fb299fa2Sopenharmony_ci{ 155fb299fa2Sopenharmony_ci if (root_ == nullptr) { 156fb299fa2Sopenharmony_ci LOG(ERROR) << "root is null"; 157fb299fa2Sopenharmony_ci return false; 158fb299fa2Sopenharmony_ci } 159fb299fa2Sopenharmony_ci return root_->IsVisible(); 160fb299fa2Sopenharmony_ci} 161fb299fa2Sopenharmony_ci 162fb299fa2Sopenharmony_ciOHOS::UIViewGroup *BasePage::GetView() const 163fb299fa2Sopenharmony_ci{ 164fb299fa2Sopenharmony_ci return root_.get(); 165fb299fa2Sopenharmony_ci} 166fb299fa2Sopenharmony_ci} // namespace Updater 167