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    let strJson = getInspectorByKey(key);
35    let obj:ESObject = JSON.parse(strJson);
36    console.info("[getInspectorByKey] current component obj is: " + JSON.stringify(obj));
37    let rectInfo:ESObject = JSON.parse('[' + obj.$rect + ']')
38    console.info("[getInspectorByKey] rectInfo is: " + rectInfo);
39    CommonFunc.rect_left = JSON.parse('[' + rectInfo[0] + ']')[0]
40    CommonFunc.rect_top = JSON.parse('[' + rectInfo[0] + ']')[1]
41    CommonFunc.rect_right = JSON.parse('[' + rectInfo[1] + ']')[0]
42    CommonFunc.rect_bottom = JSON.parse('[' + rectInfo[1] + ']')[1]
43    return CommonFunc.rect_value = {
44      left: CommonFunc.rect_left,
45      top: CommonFunc.rect_top,
46      right: CommonFunc.rect_right,
47      bottom: CommonFunc.rect_bottom
48    }
49  }
50
51  static setScreenOrientation(orientation:screen.Orientation) {
52      // 1 -- 2 |
53       let screenClass: screen.Screen | null = null;
54       screen.getAllScreens().then(data => {
55           screenClass = data[0];
56          console.log('Succeeded in getting all screens. Data:' + JSON.stringify(data));
57             
58       }).catch((err: BusinessError) => {
59             console.log('Failed to get all screens. Cause: ' + JSON.stringify(err));
60       }); 
61       try {
62          screenClass!.setOrientation(orientation, (err: BusinessError) => {
63           const errCode: number = err.code;
64           if (errCode) {
65               console.error('Failed to set the vertical orientation. Code: ' + JSON.stringify(err));
66              return;
67            }
68          console.info('Succeeded in setting the vertical orientation.');
69          });
70       } catch (exception) {
71        console.error('Failed to set the vertical orientation. Code: ' + JSON.stringify(exception));
72     };
73  }
74}