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 "frameworks/bridge/declarative_frontend/jsview/js_keyboard_avoid.h"
17 
18 #include "base/utils/utils.h"
19 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
20 #include "core/components/common/layout/constants.h"
21 #include "core/pipeline_ng/pipeline_context.h"
22 
23 namespace OHOS::Ace::Framework {
24 const std::vector<KeyBoardAvoidMode> KEYBOARD_AVOID_MODES = { KeyBoardAvoidMode::OFFSET, KeyBoardAvoidMode::RESIZE,
25     KeyBoardAvoidMode::OFFSET_WITH_CARET, KeyBoardAvoidMode::RESIZE_WITH_CARET, KeyBoardAvoidMode::NONE };
26 
SetKeyboardAvoidMode(const JSCallbackInfo & info)27 void JSKeyboardAvoid::SetKeyboardAvoidMode(const JSCallbackInfo& info)
28 {
29     auto pipeline = PipelineBase::GetCurrentContext();
30     CHECK_NULL_VOID(pipeline);
31     if (info.Length() < 1) {
32         return;
33     }
34     if (!info[0]->IsNumber()) {
35         pipeline->SetEnableKeyBoardAvoidMode(KeyBoardAvoidMode::OFFSET);
36         return;
37     }
38     auto index = info[0]->ToNumber<int32_t>();
39     if (index < 0 || index >= static_cast<int32_t>(KEYBOARD_AVOID_MODES.size())) {
40         return;
41     }
42     pipeline->SetEnableKeyBoardAvoidMode(KEYBOARD_AVOID_MODES[index]);
43 }
44 
GetKeyboardAvoidMode(const JSCallbackInfo & info)45 void JSKeyboardAvoid::GetKeyboardAvoidMode(const JSCallbackInfo& info)
46 {
47     auto pipeline = PipelineBase::GetCurrentContext();
48     CHECK_NULL_VOID(pipeline);
49     auto mode = pipeline->GetEnableKeyBoardAvoidMode();
50     auto obj = "KeyBoardAvoidMode.OFFSET";
51     switch (mode) {
52         case KeyBoardAvoidMode::OFFSET_WITH_CARET:
53             obj = "KeyBoardAvoidMode.OFFSET_WITH_CARET";
54             break;
55         case KeyBoardAvoidMode::RESIZE:
56             obj = "KeyBoardAvoidMode.RESIZE";
57             break;
58         case KeyBoardAvoidMode::RESIZE_WITH_CARET:
59             obj = "KeyBoardAvoidMode.RESIZE_WITH_CARET";
60             break;
61         case KeyBoardAvoidMode::NONE:
62             obj = "KeyBoardAvoidMode.NONE";
63             break;
64         case KeyBoardAvoidMode::OFFSET:
65         default:
66             break;
67     }
68     auto returnValue = JSVal(ToJSValue(obj));
69     auto returnPtr = JSRef<JSVal>::Make(returnValue);
70     info.SetReturnValue(returnPtr);
71 }
72 
JSBind(BindingTarget globalObj)73 void JSKeyboardAvoid::JSBind(BindingTarget globalObj)
74 {
75     JSClass<JSKeyboardAvoid>::Declare("__KeyboardAvoid__");
76     JSClass<JSKeyboardAvoid>::StaticMethod("setKeyboardAvoid", &JSKeyboardAvoid::SetKeyboardAvoidMode);
77     JSClass<JSKeyboardAvoid>::StaticMethod("getKeyboardAvoid", &JSKeyboardAvoid::GetKeyboardAvoidMode);
78     JSClass<JSKeyboardAvoid>::InheritAndBind<JSViewAbstract>(globalObj);
79 }
80 } // namespace OHOS::Ace::Framework