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