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