1 /*
2 * Copyright (c) 2022-2024 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/list_item_model_impl.h"
17
18 #include "base/memory/referenced.h"
19 #include "bridge/declarative_frontend/jsview/js_interactable_view.h"
20 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
21 #include "bridge/declarative_frontend/jsview/models/view_abstract_model_impl.h"
22 #include "core/components_v2/list/list_item_component.h"
23
24 namespace OHOS::Ace::Framework {
25
Create(bool isCreateArc)26 void ListItemModelImpl::Create(bool isCreateArc)
27 {
28 auto listItemComponent = AceType::MakeRefPtr<V2::ListItemComponent>();
29 ViewStackProcessor::GetInstance()->ClaimElementId(listItemComponent);
30 ViewStackProcessor::GetInstance()->Push(listItemComponent);
31 JSInteractableView::SetFocusable(true);
32 JSInteractableView::SetFocusNode(true);
33 }
34
Create(std::function<void (int32_t)> && deepRenderFunc,V2::ListItemStyle listItemStyle,bool isCreateArc)35 void ListItemModelImpl::Create(
36 std::function<void(int32_t)>&& deepRenderFunc, V2::ListItemStyle listItemStyle, bool isCreateArc)
37 {
38 auto listItemComponent = AceType::MakeRefPtr<V2::ListItemComponent>();
39 ViewStackProcessor::GetInstance()->ClaimElementId(listItemComponent);
40 V2::DeepRenderFunc listItemDeepRenderFunc = [jsDeepRenderFunc = std::move(deepRenderFunc),
41 elmtId = listItemComponent->GetElementId()]() -> RefPtr<Component> {
42 ACE_SCOPED_TRACE("JSListItem::ExecuteDeepRender");
43
44 jsDeepRenderFunc(elmtId);
45 RefPtr<Component> component = ViewStackProcessor::GetInstance()->Finish();
46 ACE_DCHECK(AceType::DynamicCast<V2::ListItemComponent>(component) != nullptr);
47 return component;
48 }; // listItemDeepRenderFunc lambda
49
50 listItemComponent->SetDeepRenderFunc(listItemDeepRenderFunc);
51 ViewStackProcessor::GetInstance()->Push(listItemComponent);
52 JSInteractableView::SetFocusable(true);
53 JSInteractableView::SetFocusNode(true);
54 }
55
SetBorderRadius(const Dimension & borderRadius)56 void ListItemModelImpl::SetBorderRadius(const Dimension& borderRadius)
57 {
58 JSViewSetProperty(&V2::ListItemComponent::SetBorderRadius, borderRadius);
59 }
60
SetType(const std::string & type)61 void ListItemModelImpl::SetType(const std::string& type)
62 {
63 JSViewSetProperty(&V2::ListItemComponent::SetType, type);
64 }
65
SetIsLazyCreating(bool isLazy)66 void ListItemModelImpl::SetIsLazyCreating(bool isLazy)
67 {
68 JSViewSetProperty(&V2::ListItemComponent::SetIsLazyCreating, isLazy);
69 }
70
SetSticky(V2::StickyMode stickyMode)71 void ListItemModelImpl::SetSticky(V2::StickyMode stickyMode)
72 {
73 JSViewSetProperty(&V2::ListItemComponent::SetSticky, stickyMode);
74 }
75
SetEditMode(uint32_t editMode)76 void ListItemModelImpl::SetEditMode(uint32_t editMode)
77 {
78 JSViewSetProperty(&V2::ListItemComponent::SetEditMode, editMode);
79 if ((V2::EditMode::MOVABLE & editMode) == 0) {
80 auto* stack = ViewStackProcessor::GetInstance();
81 auto box = stack->GetBoxComponent();
82 box->SetEnableDragStart(false);
83 }
84 }
85
SetSelectable(bool selectable)86 void ListItemModelImpl::SetSelectable(bool selectable)
87 {
88 JSViewSetProperty(&V2::ListItemComponent::SetSelectable, selectable);
89 }
90
SetSwiperAction(std::function<void ()> && startAction,std::function<void ()> && endAction,OnOffsetChangeFunc && onOffsetChangeFunc,V2::SwipeEdgeEffect edgeEffect,NG::FrameNode * node)91 void ListItemModelImpl::SetSwiperAction(std::function<void()>&& startAction, std::function<void()>&& endAction,
92 [[maybe_unused]] OnOffsetChangeFunc&& onOffsetChangeFunc, V2::SwipeEdgeEffect edgeEffect, NG::FrameNode* node)
93 {
94 auto listItem = AceType::DynamicCast<V2::ListItemComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
95 if (!listItem) {
96 LOGW("Failed to get '%{public}s' in view stack", AceType::TypeName<V2::ListItemComponent>());
97 return;
98 }
99 if (startAction) {
100 ScopedViewStackProcessor builderViewStackProcessor;
101 startAction();
102 RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
103 listItem->SetSwiperStartComponent(customComponent);
104 }
105 if (endAction) {
106 ScopedViewStackProcessor builderViewStackProcessor;
107 endAction();
108 RefPtr<Component> customComponent = ViewStackProcessor::GetInstance()->Finish();
109 listItem->SetSwiperEndComponent(customComponent);
110 }
111 listItem->SetEdgeEffect(edgeEffect);
112 }
113
SetSelectCallback(OnSelectFunc && selectCallback)114 void ListItemModelImpl::SetSelectCallback(OnSelectFunc&& selectCallback)
115 {
116 JSViewSetProperty(&V2::ListItemComponent::SetOnSelectId, std::move(selectCallback));
117 }
118
SetDeleteArea(std::function<void ()> && builderAction,OnDeleteEvent && onDelete,OnEnterDeleteAreaEvent && onEnterDeleteArea,OnExitDeleteAreaEvent && onExitDeleteArea,OnStateChangedEvent && onStateChange,const Dimension & length,bool isStartArea,NG::FrameNode * node)119 void ListItemModelImpl::SetDeleteArea(std::function<void()>&& builderAction, OnDeleteEvent&& onDelete,
120 OnEnterDeleteAreaEvent&& onEnterDeleteArea, OnExitDeleteAreaEvent&& onExitDeleteArea,
121 OnStateChangedEvent&& onStateChange, const Dimension& length, bool isStartArea, NG::FrameNode* node) {};
122
SetOnDragStart(NG::OnDragStartFunc && onDragStart)123 void ListItemModelImpl::SetOnDragStart(NG::OnDragStartFunc&& onDragStart)
124 {
125 auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
126 box->SetOnDragStartId(ViewAbstractModelImpl::ToDragFunc(std::move(onDragStart)));
127 JSViewSetProperty(&V2::ListItemComponent::MarkIsDragStart, true);
128 }
129
SetDeleteAreaWithFrameNode(const RefPtr<NG::UINode> & builderComponent,OnDeleteEvent && onDelete,OnEnterDeleteAreaEvent && onEnterDeleteArea,OnExitDeleteAreaEvent && onExitDeleteArea,OnStateChangedEvent && onStateChange,const Dimension & length,bool isStartArea,NG::FrameNode * node)130 void ListItemModelImpl::SetDeleteAreaWithFrameNode(const RefPtr<NG::UINode>& builderComponent, OnDeleteEvent&& onDelete,
131 OnEnterDeleteAreaEvent&& onEnterDeleteArea, OnExitDeleteAreaEvent&& onExitDeleteArea,
132 OnStateChangedEvent&& onStateChange, const Dimension& length, bool isStartArea, NG::FrameNode* node) {};
133
134 } // namespace OHOS::Ace::Framework
135