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 "bridge/declarative_frontend/jsview/models/view_full_update_model_impl.h" 17 18 namespace OHOS::Ace::Framework { 19 CreateNode(NodeInfo && info)20RefPtr<AceType> ViewFullUpdateModelImpl::CreateNode(NodeInfo&& info) 21 { 22 ACE_SCOPED_TRACE("JSView::CreateSpecializedComponent"); 23 // create component, return new something, need to set proper ID 24 25 std::string key = ViewStackProcessor::GetInstance()->ProcessViewId(info.viewId); 26 auto composedComponent = AceType::MakeRefPtr<ComposedComponent>(key, "view"); 27 auto isStatic = info.isStatic; 28 29 auto elementFunction = [nodeInfo = std::move(info)](const RefPtr<ComposedElement>& element) mutable { 30 if (nodeInfo.appearFunc) { 31 nodeInfo.appearFunc(); 32 } 33 34 if (nodeInfo.updateNodeFunc) { 35 nodeInfo.updateNodeFunc(element); 36 } 37 38 // add render function callback to element. when the element rebuilds due 39 // to state update it will call this callback to get the new child component. 40 if (element) { 41 auto renderFunction = [renderFunc = nodeInfo.renderFunc]( 42 const RefPtr<Component>& component) -> RefPtr<Component> { 43 if (!renderFunc) { 44 return nullptr; 45 } 46 auto node = renderFunc(); 47 return AceType::DynamicCast<Component>(node); 48 }; 49 element->SetRenderFunction(std::move(renderFunction)); 50 auto removeFunc = nodeInfo.removeFunc; 51 element->SetRemoveFunction(std::move(removeFunc)); 52 if (nodeInfo.pageTransitionFunc) { 53 auto pageTransitionFunction = [transitionFunc = nodeInfo.pageTransitionFunc]() -> RefPtr<Component> { 54 transitionFunc(); 55 auto pageTransitionComponent = ViewStackProcessor::GetInstance()->GetPageTransitionComponent(); 56 ViewStackProcessor::GetInstance()->ClearPageTransitionComponent(); 57 return pageTransitionComponent; 58 }; 59 element->SetPageTransitionFunction(std::move(pageTransitionFunction)); 60 } 61 } 62 }; 63 64 composedComponent->SetElementFunction(std::move(elementFunction)); 65 66 if (isStatic) { 67 composedComponent->SetStatic(); 68 } 69 return composedComponent; 70 } 71 MarkNeedUpdate(const WeakPtr<AceType> & node)72bool ViewFullUpdateModelImpl::MarkNeedUpdate(const WeakPtr<AceType>& node) 73 { 74 ACE_SCOPED_TRACE("JSView::MarkNeedUpdate"); 75 auto weakElement = AceType::DynamicCast<ComposedElement>(node); 76 if (weakElement.Invalid()) { 77 LOGE("Invalid Element weak ref, internal error"); 78 return false; 79 } 80 auto element = weakElement.Upgrade(); 81 if (element) { 82 element->MarkDirty(); 83 } 84 return true; 85 } 86 87 } // namespace OHOS::Ace::Framework 88