• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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/js_view_stack_processor.h"
17 
18 #include "bridge/declarative_frontend/engine/bindings.h"
19 #include "bridge/declarative_frontend/engine/js_types.h"
20 #include "bridge/declarative_frontend/jsview/models/view_stack_model_impl.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/common/container.h"
23 #include "core/components_ng/base/view_stack_model.h"
24 #include "core/components_ng/base/view_stack_model_ng.h"
25 #include "core/components_ng/base/view_stack_processor.h"
26 #include "frameworks/core/pipeline/base/element_register.h"
27 #include "foundation/arkui/ace_engine/frameworks/core/common/ace_application_info.h"
28 
29 namespace OHOS::Ace {
30 
31 std::unique_ptr<ViewStackModel> ViewStackModel::instance_ = nullptr;
32 std::mutex ViewStackModel::mutex_;
33 
GetInstance()34 ViewStackModel* ViewStackModel::GetInstance()
35 {
36     if (!instance_) {
37         std::lock_guard<std::mutex> lock(mutex_);
38         if (!instance_) {
39 #ifdef NG_BUILD
40             instance_.reset(new NG::ViewStackModelNG());
41 #else
42             if (Container::IsCurrentUseNewPipeline()) {
43                 instance_.reset(new NG::ViewStackModelNG());
44             } else {
45                 instance_.reset(new Framework::ViewStackModelImpl());
46             }
47 #endif
48         }
49     }
50     return instance_.get();
51 }
52 
53 } // namespace OHOS::Ace
54 namespace OHOS::Ace::Framework {
55 
JSVisualState(const JSCallbackInfo & info)56 void JSViewStackProcessor::JSVisualState(const JSCallbackInfo& info)
57 {
58     if ((info.Length() < 1) || (!info[0]->IsString())) {
59         ViewStackModel::GetInstance()->ClearVisualState();
60         return;
61     }
62 
63     std::string state = info[0]->ToString();
64     VisualState visualState = JSViewStackProcessor::StringToVisualState(state);
65     ViewStackModel::GetInstance()->SetVisualState(visualState);
66 }
67 
68 // public static emthods exposed to JS
JSBind(BindingTarget globalObj)69 void JSViewStackProcessor::JSBind(BindingTarget globalObj)
70 {
71     JSClass<JSViewStackProcessor>::Declare("ViewStackProcessor");
72     MethodOptions opt = MethodOptions::NONE;
73 
74     JSClass<JSViewStackProcessor>::StaticMethod(
75         "AllocateNewElmetIdForNextComponent", &JSViewStackProcessor::JsAllocateNewElmetIdForNextComponent, opt);
76     JSClass<JSViewStackProcessor>::StaticMethod(
77         "StartGetAccessRecordingFor", &JSViewStackProcessor::JsStartGetAccessRecordingFor, opt);
78     JSClass<JSViewStackProcessor>::StaticMethod(
79         "SetElmtIdToAccountFor", &JSViewStackProcessor::JsSetElmtIdToAccountFor, opt);
80     JSClass<JSViewStackProcessor>::StaticMethod(
81         "GetElmtIdToAccountFor", &JSViewStackProcessor::JsGetElmtIdToAccountFor, opt);
82     JSClass<JSViewStackProcessor>::StaticMethod(
83         "StopGetAccessRecording", &JSViewStackProcessor::JsStopGetAccessRecording, opt);
84     JSClass<JSViewStackProcessor>::StaticMethod(
85         "ImplicitPopBeforeContinue", &JSViewStackProcessor::JsImplicitPopBeforeContinue, opt);
86     JSClass<JSViewStackProcessor>::StaticMethod("visualState", JSVisualState, opt);
87     JSClass<JSViewStackProcessor>::StaticMethod("MakeUniqueId", &JSViewStackProcessor::JSMakeUniqueId, opt);
88     JSClass<JSViewStackProcessor>::StaticMethod("UsesNewPipeline", &JSViewStackProcessor::JsUsesNewPipeline, opt);
89     JSClass<JSViewStackProcessor>::StaticMethod("getApiVersion", &JSViewStackProcessor::JsGetApiVersion, opt);
90     JSClass<JSViewStackProcessor>::StaticMethod("GetAndPushFrameNode", &JSViewStackProcessor::JsGetAndPushFrameNode);
91     JSClass<JSViewStackProcessor>::StaticMethod("moveDeletedElmtIds", &JSViewStackProcessor::JsMoveDeletedElmtIds);
92     JSClass<JSViewStackProcessor>::Bind<>(globalObj);
93 }
94 
StringToVisualState(const std::string & stateString)95 VisualState JSViewStackProcessor::StringToVisualState(const std::string& stateString)
96 {
97     if (stateString == "normal") {
98         return VisualState::NORMAL;
99     }
100     if (stateString == "focused") {
101         return VisualState::FOCUSED;
102     }
103     if (stateString == "pressed" || stateString == "clicked") {
104         return VisualState::PRESSED;
105     }
106     if (stateString == "disabled") {
107         return VisualState::DISABLED;
108     }
109     if (stateString == "hover") {
110         return VisualState::HOVER;
111     }
112     if (stateString == "selected") {
113         return VisualState::SELECTED;
114     }
115     LOGE("Unknown visual state \"%{public}s\", resetting to UNDEFINED", stateString.c_str());
116     return VisualState::NOTSET;
117 }
118 
JsStartGetAccessRecordingFor(ElementIdType elmtId)119 void JSViewStackProcessor::JsStartGetAccessRecordingFor(ElementIdType elmtId)
120 {
121     ViewStackModel::GetInstance()->StartGetAccessRecordingFor(elmtId);
122 }
123 
JsGetElmtIdToAccountFor()124 int32_t JSViewStackProcessor::JsGetElmtIdToAccountFor()
125 {
126     return ViewStackModel::GetInstance()->GetElmtIdToAccountFor();
127 }
128 
JsSetElmtIdToAccountFor(ElementIdType elmtId)129 void JSViewStackProcessor::JsSetElmtIdToAccountFor(ElementIdType elmtId)
130 {
131     ViewStackModel::GetInstance()->SetElmtIdToAccountFor(elmtId);
132 }
133 
JsStopGetAccessRecording()134 void JSViewStackProcessor::JsStopGetAccessRecording()
135 {
136     return ViewStackModel::GetInstance()->StopGetAccessRecording();
137 }
138 
JsImplicitPopBeforeContinue()139 void JSViewStackProcessor::JsImplicitPopBeforeContinue()
140 {
141     ViewStackModel::GetInstance()->ImplicitPopBeforeContinue();
142 }
143 
JSMakeUniqueId(const JSCallbackInfo & info)144 void JSViewStackProcessor::JSMakeUniqueId(const JSCallbackInfo& info)
145 {
146     const auto result = ElementRegister::GetInstance()->MakeUniqueId();
147     info.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
148 }
JsMoveDeletedElmtIds(const JSCallbackInfo & info)149 void JSViewStackProcessor::JsMoveDeletedElmtIds(const JSCallbackInfo& info)
150 {
151     auto jsArrInfo = info[0];
152     if (!jsArrInfo->IsArray()) {
153         return;
154     }
155     JSRef<JSArray> jsArr = JSRef<JSArray>::Cast(jsArrInfo);
156 
157     RemovedElementsType removedElements;
158     ElementRegister::GetInstance()->MoveRemovedItems(removedElements);
159     size_t index = jsArr->Length();
160     for (const auto& rmElmtId : removedElements) {
161         jsArr->SetValueAt(index++, JSRef<JSVal>::Make(ToJSValue(rmElmtId)));
162     }
163 }
164 
165 /**
166  * return true of current Container uses new Pipeline
167  */
JsUsesNewPipeline()168 bool JSViewStackProcessor::JsUsesNewPipeline()
169 {
170     auto container = Container::Current();
171     return container ? container->IsUseNewPipeline() : AceApplicationInfo::GetInstance().IsUseNewPipeline();
172 }
173 
174 /**
175  * return the API version specified in the manifest.json
176  */
JsGetApiVersion()177 int32_t JSViewStackProcessor::JsGetApiVersion()
178 {
179     return AceApplicationInfo::GetInstance().GetApiTargetVersion();
180 }
181 
JsGetAndPushFrameNode(const JSCallbackInfo & info)182 void JSViewStackProcessor::JsGetAndPushFrameNode(const JSCallbackInfo& info)
183 {
184     if (info.Length() < 2) {
185         return;
186     }
187     if (!info[0]->IsString() || !info[1]->IsNumber()) {
188         return;
189     }
190     ViewStackModel::GetInstance()->GetAndPushFrameNode(info[0]->ToString(), info[1]->ToNumber<int32_t>());
191 }
192 
193 } // namespace OHOS::Ace::Framework
194