1 /*
2  * Copyright (c) 2021 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 "frameworks/bridge/js_frontend/engine/jsi/jsi_list_bridge.h"
17 
18 #include "frameworks/bridge/common/dom/dom_list.h"
19 
20 namespace OHOS::Ace::Framework {
21 
JsGetCurrentOffset(const shared_ptr<JsRuntime> & runtime,NodeId nodeId)22 shared_ptr<JsValue> JsiListBridge::JsGetCurrentOffset(const shared_ptr<JsRuntime>& runtime, NodeId nodeId)
23 {
24     if (!runtime) {
25         LOGE("JsGetCurrentOffset failed. runtime is null.");
26         return nullptr;
27     }
28     auto engine = static_cast<JsiEngineInstance*>(runtime->GetEmbedderData());
29     if (!engine) {
30         LOGE("JsGetCurrentOffset failed. engine is null.");
31         return runtime->NewUndefined();
32     }
33     auto page = engine->GetRunningPage();
34     if (!page) {
35         LOGE("JsGetCurrentOffset failed. page is null.");
36         return runtime->NewUndefined();
37     }
38 
39     double offsetX = 0.0;
40     double offsetY = 0.0;
41     auto task = [nodeId, page, &offsetX, &offsetY]() {
42         auto domDoc = page->GetDomDocument();
43         if (!domDoc) {
44             return;
45         }
46 
47         auto domList = AceType::DynamicCast<DOMList>(domDoc->GetDOMNodeById(nodeId));
48         if (!domList) {
49             return;
50         }
51         auto offset = domList->GetCurrentOffset();
52         offsetX = offset.GetX();
53         offsetY = offset.GetY();
54     };
55 
56     auto delegate = engine->GetFrontendDelegate();
57     if (!delegate) {
58         LOGE("JsGetCurrentOffset failed. delegate is null.");
59         return runtime->NewUndefined();
60     }
61     delegate->PostSyncTaskToPage(task, "ArkUIListGetCurrentOffset");
62     shared_ptr<JsValue> offsetContext = runtime->NewObject();
63     offsetContext->SetProperty(runtime, "x", runtime->NewNumber(offsetX));
64     offsetContext->SetProperty(runtime, "y", runtime->NewNumber(offsetY));
65     return offsetContext;
66 }
67 
68 } // namespace OHOS::Ace::Framework
69