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 "frameworks/bridge/declarative_frontend/jsview/js_camera.h"
17 
18 #include "base/log/ace_trace.h"
19 #include "core/components/camera/camera_component.h"
20 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
21 
22 namespace OHOS::Ace::Framework {
23 
Create(const JSCallbackInfo & info)24 void JSCamera::Create(const JSCallbackInfo& info)
25 {
26     RefPtr<CameraComponent> cameraComponent = AceType::MakeRefPtr<OHOS::Ace::CameraComponent>();
27     if (info.Length() > 0 && info[0]->IsObject()) {
28         JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
29         JSRef<JSVal> idValue = sizeObj->GetProperty("id");
30         if (!idValue->IsNull() && !idValue->IsEmpty()) {
31             cameraComponent->SetCameraId(idValue->ToString());
32         }
33 
34         JSRef<JSVal> resolutionWidthValue = sizeObj->GetProperty("resolutionWidth");
35         int32_t resolutionWidth = -1;
36         if (!resolutionWidthValue->IsNull() && !resolutionWidthValue->IsEmpty()
37             && resolutionWidthValue->IsNumber()) {
38             resolutionWidth = resolutionWidthValue->ToNumber<int32_t>();
39         }
40 
41         JSRef<JSVal> resolutionHeightValue = sizeObj->GetProperty("resolutionHeight");
42         int32_t resolutionHeight = -1;
43         if (!resolutionHeightValue->IsNull() && !resolutionHeightValue->IsEmpty() &&
44             resolutionHeightValue->IsNumber()) {
45             resolutionHeight = resolutionHeightValue->ToNumber<int32_t>();
46         }
47 
48         if (resolutionWidth != -1 && resolutionHeight != -1) {
49             cameraComponent->SetResolutionWidth(resolutionWidth);
50             cameraComponent->SetResolutionHeight(resolutionHeight);
51             cameraComponent->SignSetResolution(true);
52         }
53     }
54 
55     ViewStackProcessor::GetInstance()->Push(cameraComponent);
56     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
57     box->SetColor(Color::BLACK);
58 }
59 
JsDevicePosition(int32_t value)60 void JSCamera::JsDevicePosition(int32_t value)
61 {
62     auto stack = ViewStackProcessor::GetInstance();
63     auto cameraComponent = AceType::DynamicCast<CameraComponent>(stack->GetMainComponent());
64     if (!cameraComponent) {
65         return;
66     }
67     cameraComponent->SetDevicePosition(static_cast<DevicePosition>(value));
68 }
69 
JSBind(BindingTarget globalObj)70 void JSCamera::JSBind(BindingTarget globalObj)
71 {
72     JSClass<JSCamera>::Declare("Camera");
73     MethodOptions opt = MethodOptions::NONE;
74     JSClass<JSCamera>::StaticMethod("create", &JSCamera::Create, opt);
75     JSClass<JSCamera>::StaticMethod("devicePosition", &JSCamera::JsDevicePosition, opt);
76 
77     JSClass<JSCamera>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
78     JSClass<JSCamera>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
79     JSClass<JSCamera>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
80     JSClass<JSCamera>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
81     JSClass<JSCamera>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
82 
83     JSClass<JSCamera>::InheritAndBind<JSViewAbstract>(globalObj);
84 }
85 
86 } // namespace OHOS::Ace::Framework
87