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 */ 15import screen from '@ohos.screen'; 16import { BusinessError } from '@ohos.base'; 17 18export default class CommonFunc { 19 static rect_left:ESObject 20 static rect_top:ESObject; 21 static rect_right:ESObject; 22 static rect_bottom:ESObject; 23 static rect_value:ESObject 24 25 static sleep(time:number) : Promise<void> { 26 return new Promise((resolve)=>{ 27 setTimeout(()=>{ 28 resolve() 29 },time) 30 }) 31 } 32 33 static getComponentRect(key:string) { 34 35 let strJson = getInspectorByKey(key); 36 let obj:ESObject = JSON.parse(strJson); 37 console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj)); 38 let rectInfo:ESObject = JSON.parse('[' + obj.$rect + ']') 39 console.info("[getInspectorByKey] rectInfo is: " + rectInfo); 40 CommonFunc.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0] 41 CommonFunc.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1] 42 CommonFunc.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0] 43 CommonFunc.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1] 44 return CommonFunc.rect_value = { 45 left: CommonFunc.rect_left, 46 top: CommonFunc.rect_top, 47 right: CommonFunc.rect_right, 48 bottom: CommonFunc.rect_bottom 49 } 50 } 51 52 static setScreenOrientation(orientation:screen.Orientation) { 53 // 1 -- 2 | 54 let screenClass: screen.Screen | null = null; 55 screen.getAllScreens().then(data => { 56 screenClass = data[0]; 57 console.log('Succeeded in getting all screens. Data:' + JSON.stringify(data)); 58 59 }).catch((err: BusinessError) => { 60 console.log('Failed to get all screens. Cause: ' + JSON.stringify(err)); 61 }); 62 try { 63 screenClass!.setOrientation(orientation, (err: BusinessError) => { 64 const errCode: number = err.code; 65 if (errCode) { 66 console.error('Failed to set the vertical orientation. Code: ' + JSON.stringify(err)); 67 return; 68 } 69 console.info('Succeeded in setting the vertical orientation.'); 70 }); 71 } catch (exception) { 72 console.error('Failed to set the vertical orientation. Code: ' + JSON.stringify(exception)); 73 }; 74 } 75} 76 77 78 79 80