1 /*
2 * Copyright (c) 2023 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/utils/system_properties.h"
17 #include "core/components_ng/pattern/pattern.h"
18 #include "core/components_ng/pattern/scrollable/scrollable_item.h"
19
20 namespace OHOS::Ace::NG {
GetOrCreateScrollableItem(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)21 RefPtr<ScrollableItem> ScrollableItem::GetOrCreateScrollableItem(
22 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
23 {
24 auto node = ElementRegister::GetInstance()->GetSpecificItemById<ScrollableItem>(nodeId);
25 if (node) {
26 if (node->GetTag() == tag) {
27 return node;
28 }
29 ElementRegister::GetInstance()->RemoveItemSilently(nodeId);
30 auto parent = node->GetParent();
31 if (parent) {
32 parent->RemoveChild(node);
33 }
34 }
35
36 auto pattern = patternCreator ? patternCreator() : AceType::MakeRefPtr<Pattern>();
37 node = AceType::MakeRefPtr<ScrollableItem>(tag, nodeId, pattern, false);
38 node->InitializePatternAndContext();
39 ElementRegister::GetInstance()->AddUINode(node);
40 return node;
41 }
42
43 // return true to release the ScrollableItem
44 // return false to deallocate the ScrollableItem
MaybeRelease()45 bool ScrollableItem::MaybeRelease()
46 {
47 if (!SystemProperties::GetEnableScrollableItemPool()) {
48 return true;
49 }
50 deleter_(this);
51 return maybeRelease_;
52 }
53
54 } // namespace OHOS::Ace::NG
55