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 "bridge/declarative_frontend/jsview/js_folder_stack.h"
17 
18 #include <algorithm>
19 
20 #include "base/log/ace_scoring_log.h"
21 #include "base/log/ace_trace.h"
22 #include "base/log/log_wrapper.h"
23 #include "core/common/container.h"
24 #include "core/common/display_info.h"
25 #include "core/components_ng/pattern/folder_stack/folder_stack_event_info.h"
26 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
27 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
28 #include "frameworks/bridge/declarative_frontend/jsview/models/stack_model_impl.h"
29 #include "frameworks/core/components_ng/pattern/folder_stack/folder_stack_model_ng.h"
30 
31 namespace OHOS::Ace {
32 std::unique_ptr<FolderStackModel> FolderStackModel::instance_ = nullptr;
33 std::mutex FolderStackModel::mutex_;
34 
GetInstance()35 FolderStackModel* FolderStackModel::GetInstance()
36 {
37     if (!instance_) {
38         std::lock_guard<std::mutex> lock(mutex_);
39         if (!instance_) {
40 #ifdef NG_BUILD
41             instance_.reset(new NG::FolderStackModelNG());
42 #else
43             if (Container::IsCurrentUseNewPipeline()) {
44                 instance_.reset(new NG::FolderStackModelNG());
45             }
46 #endif
47         }
48     }
49     return instance_.get();
50 }
51 } // namespace OHOS::Ace
52 namespace OHOS::Ace::Framework {
53 const static std::array<Alignment, 9> ALIGNMENT_ARR { Alignment::TOP_LEFT, Alignment::TOP_CENTER, Alignment::TOP_RIGHT,
54     Alignment::CENTER_LEFT, Alignment::CENTER, Alignment::CENTER_RIGHT, Alignment::BOTTOM_LEFT,
55     Alignment::BOTTOM_CENTER, Alignment::BOTTOM_RIGHT };
56 
FolderStackEventToJSValue(const NG::FolderEventInfo & eventInfo)57 JSRef<JSVal> FolderStackEventToJSValue(const NG::FolderEventInfo& eventInfo)
58 {
59     JSRef<JSObject> obj = JSRef<JSObject>::New();
60     obj->SetProperty("foldStatus", static_cast<int32_t>(eventInfo.GetFolderState()));
61     return JSRef<JSVal>::Cast(obj);
62 }
63 
HoverStatusChangeEventToJSValue(const NG::FolderEventInfo & eventInfo)64 JSRef<JSVal> HoverStatusChangeEventToJSValue(const NG::FolderEventInfo& eventInfo)
65 {
66     JSRef<JSObject> obj = JSRef<JSObject>::New();
67     obj->SetProperty("foldStatus", static_cast<int32_t>(eventInfo.GetFolderState()));
68     obj->SetProperty("isHoverMode", static_cast<int32_t>(eventInfo.IsHoverMode()));
69     obj->SetProperty("appRotation", static_cast<int32_t>(eventInfo.GetRotation()));
70     obj->SetProperty("windowStatusType",
71         static_cast<int32_t>(JSFolderStack::NotifyWindowStatusChange(eventInfo.GetWindowMode())));
72     return JSRef<JSVal>::Cast(obj);
73 }
74 
Create(const JSCallbackInfo & info)75 void JSFolderStack::Create(const JSCallbackInfo& info)
76 {
77     if (info.Length() > 0 && info[0]->IsObject()) {
78         JSRef<JSArray> params = JSRef<JSArray>::Cast(info[0]);
79         auto upperId = params->GetProperty("upperItems");
80         if (upperId->IsNull() || upperId->IsUndefined() || !upperId->IsArray()) {
81             FolderStackModel::GetInstance()->Create();
82             return;
83         }
84         auto upperIdParams = JSRef<JSArray>::Cast(upperId);
85         auto upperItemLength = upperIdParams->Length();
86         std::vector<std::string> upperItems(upperItemLength);
87         if (upperId->IsArray()) {
88             for (size_t i = 0; i < upperItemLength; i++) {
89                 upperItems[i] = upperIdParams->GetValueAt(i)->ToString();
90             }
91         }
92         FolderStackModel::GetInstance()->Create(upperItems);
93     } else {
94         FolderStackModel::GetInstance()->Create();
95     }
96 }
97 
SetAlignContent(const JSCallbackInfo & info)98 void JSFolderStack::SetAlignContent(const JSCallbackInfo& info)
99 {
100     if (info.Length() != 1 || !info[0]->IsNumber()) {
101         FolderStackModel::GetInstance()->SetAlignment(Alignment::CENTER);
102         return;
103     }
104     auto value = info[0]->ToNumber<size_t>();
105     if (value < 0 || static_cast<int32_t>(value) >= static_cast<int32_t>(ALIGNMENT_ARR.size())) {
106         FolderStackModel::GetInstance()->SetAlignment(Alignment::CENTER);
107         return;
108     }
109     FolderStackModel::GetInstance()->SetAlignment(ALIGNMENT_ARR[value]);
110 }
111 
JsEnableAnimation(const JSCallbackInfo & info)112 void JSFolderStack::JsEnableAnimation(const JSCallbackInfo& info)
113 {
114     if (!info[0]->IsBoolean()) {
115         FolderStackModel::GetInstance()->SetEnableAnimation(true);
116         return;
117     }
118     auto isEnableAnimation = info[0]->ToBoolean();
119     FolderStackModel::GetInstance()->SetEnableAnimation(isEnableAnimation);
120 }
121 
NotifyWindowStatusChange(WindowMode mode)122 WindowStatus JSFolderStack::NotifyWindowStatusChange(WindowMode mode)
123 {
124     auto WindowStatus = WindowStatus::WINDOW_STATUS_UNDEFINED;
125     if (mode == WindowMode::WINDOW_MODE_FLOATING) {
126         WindowStatus = WindowStatus::WINDOW_STATUS_FLOATING;
127     } else if (mode == WindowMode::WINDOW_MODE_SPLIT_PRIMARY || mode == WindowMode::WINDOW_MODE_SPLIT_SECONDARY) {
128         WindowStatus = WindowStatus::WINDOW_STATUS_SPLITSCREEN;
129     }
130     if (mode == WindowMode::WINDOW_MODE_FULLSCREEN) {
131         WindowStatus = WindowStatus::WINDOW_STATUS_FULLSCREEN;
132     }
133     return WindowStatus;
134 }
135 
SetAutoHalfFold(const JSCallbackInfo & info)136 void JSFolderStack::SetAutoHalfFold(const JSCallbackInfo& info)
137 {
138     if (!info[0]->IsBoolean()) {
139         FolderStackModel::GetInstance()->SetAutoHalfFold(true);
140         return;
141     }
142     auto IsAutoHalfFold = info[0]->ToBoolean();
143     FolderStackModel::GetInstance()->SetAutoHalfFold(IsAutoHalfFold);
144 }
145 
JSOnFolderStateChange(const JSCallbackInfo & info)146 void JSFolderStack::JSOnFolderStateChange(const JSCallbackInfo& info)
147 {
148     if (!info[0]->IsFunction()) {
149         return;
150     }
151     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NG::FolderEventInfo, 1>>(
152         JSRef<JSFunc>::Cast(info[0]), FolderStackEventToJSValue);
153     auto onFolderStateChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
154                                    const NG::FolderEventInfo& value) {
155         ACE_SCORING_EVENT("FolderStack.onFolderStateChange");
156         func->Execute(value);
157     };
158     FolderStackModel::GetInstance()->SetOnFolderStateChange(std::move(onFolderStateChange));
159 }
160 
JSOnHoverStatusChange(const JSCallbackInfo & info)161 void JSFolderStack::JSOnHoverStatusChange(const JSCallbackInfo& info)
162 {
163     if (!info[0]->IsFunction()) {
164         return;
165     }
166     auto jsFunc = AceType::MakeRefPtr<JsEventFunction<NG::FolderEventInfo, 1>>(
167         JSRef<JSFunc>::Cast(info[0]), HoverStatusChangeEventToJSValue);
168     auto onHoverStatusChange = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
169                                    const NG::FolderEventInfo& value) {
170         ACE_SCORING_EVENT("FolderStack.onHoverStatusChange");
171         func->Execute(value);
172     };
173     FolderStackModel::GetInstance()->SetOnHoverStatusChange(std::move(onHoverStatusChange));
174 }
175 
JSBind(BindingTarget globalObj)176 void JSFolderStack::JSBind(BindingTarget globalObj)
177 {
178     JSClass<JSFolderStack>::Declare("FolderStack");
179     JSClass<JSFolderStack>::StaticMethod("create", &JSFolderStack::Create);
180     JSClass<JSFolderStack>::StaticMethod("alignContent", &JSFolderStack::SetAlignContent);
181     JSClass<JSFolderStack>::StaticMethod("enableAnimation", &JSFolderStack::JsEnableAnimation);
182     JSClass<JSFolderStack>::StaticMethod("autoHalfFold", &JSFolderStack::SetAutoHalfFold);
183     JSClass<JSFolderStack>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
184     JSClass<JSFolderStack>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
185     JSClass<JSFolderStack>::StaticMethod("onFolderStateChange", &JSFolderStack::JSOnFolderStateChange);
186     JSClass<JSFolderStack>::StaticMethod("onHoverStatusChange", &JSFolderStack::JSOnHoverStatusChange);
187     JSClass<JSFolderStack>::InheritAndBind<JSContainerBase>(globalObj);
188 }
189 } // namespace OHOS::Ace::Framework
190