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 SetKeyboardAvoidMode(const JSCallbackInfo & info)26void JSKeyboardAvoid::SetKeyboardAvoidMode(const JSCallbackInfo& info) 27 { 28 auto pipeline = PipelineBase::GetCurrentContext(); 29 CHECK_NULL_VOID(pipeline); 30 if (info.Length() < 1) { 31 return; 32 } 33 if (!info[0]->IsNumber()) { 34 pipeline->SetEnableKeyBoardAvoidMode(false); 35 return; 36 } 37 auto index = info[0]->ToNumber<int32_t>(); 38 if (index < 0 || index >= static_cast<int32_t>(KEYBOARD_AVOID_MODES.size())) { 39 return; 40 } 41 if (KEYBOARD_AVOID_MODES[index] == KeyBoardAvoidMode::RESIZE) { 42 pipeline->SetEnableKeyBoardAvoidMode(true); 43 } else { 44 pipeline->SetEnableKeyBoardAvoidMode(false); 45 } 46 } 47 GetKeyboardAvoidMode(const JSCallbackInfo & info)48void JSKeyboardAvoid::GetKeyboardAvoidMode(const JSCallbackInfo& info) 49 { 50 auto pipeline = PipelineBase::GetCurrentContext(); 51 CHECK_NULL_VOID(pipeline); 52 auto obj = "KeyBoardAvoidMode.OFFSET"; 53 if (pipeline->IsEnableKeyBoardAvoidMode()) { 54 obj = "KeyBoardAvoidMode.RESIZE"; 55 } 56 auto returnValue = JSVal(ToJSValue(obj)); 57 auto returnPtr = JSRef<JSVal>::Make(returnValue); 58 info.SetReturnValue(returnPtr); 59 } 60 JSBind(BindingTarget globalObj)61void JSKeyboardAvoid::JSBind(BindingTarget globalObj) 62 { 63 JSClass<JSKeyboardAvoid>::Declare("__KeyboardAvoid__"); 64 JSClass<JSKeyboardAvoid>::StaticMethod("setKeyboardAvoid", &JSKeyboardAvoid::SetKeyboardAvoidMode); 65 JSClass<JSKeyboardAvoid>::StaticMethod("getKeyboardAvoid", &JSKeyboardAvoid::GetKeyboardAvoidMode); 66 JSClass<JSKeyboardAvoid>::InheritAndBind<JSViewAbstract>(globalObj); 67 } 68 } // namespace OHOS::Ace::Framework