WindowModel.ets
3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
import deviceInfo from '@ohos.deviceInfo'
import display from '@ohos.display';
export class Size {
width: number = 0
height: number = 0
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
}
export class WindowModel {
private windowStage?: window.WindowStage;
static shared: WindowModel = new WindowModel()
static TAG = "WindowModel";
setWindowStage(windowStage: window.WindowStage) {
this.windowStage = windowStage;
}
setMainWindowFullScreen(fullScreen: boolean) {
if (deviceInfo.deviceType != "phone") {
return
}
if (this.windowStage === undefined) {
return;
}
this.windowStage.getMainWindow((err, windowClass: window.Window) => {
if (err.code) {
return;
}
windowClass.setWindowLayoutFullScreen(fullScreen, (err) => {
if (err.code) {
return;
}
});
windowClass.setWindowSystemBarEnable(fullScreen ? [] : ['status', 'navigation'], (err) => {
if (err.code) {
return;
}
});
});
}
getWindowSize(): Promise<Size> {
return new Promise((resolve, reject) => {
if (!this.windowStage) {
let dis = display.getDefaultDisplaySync();
reject(new Size(dis.width, dis.height))
return;
}
let rect = this.windowStage.getMainWindowSync().getWindowProperties().windowRect;
resolve(new Size(rect.width, rect.height));
});
}
setWindowKeepScreenOn(isKeepScreenOn: boolean) {
this.windowStage?.getMainWindow((err, windowClass: window.Window) => {
windowClass.setWindowKeepScreenOn(isKeepScreenOn, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error(WindowModel.TAG +'设置屏幕常亮:' + isKeepScreenOn + ',失败: ' + JSON.stringify(err));
return;
}
console.info(WindowModel.TAG +'设置屏幕常亮:' + isKeepScreenOn + ",成功");
})
})
}
/**
* 设置窗口的显示方向属性
* @param orientation
*/
setPreferredOrientation(orientation: window.Orientation): Promise<void> {
return new Promise((resolve, reject) => {
if (!this.windowStage) {
console.error('Failed, the main window is empty');
reject("Failed, the main window is empty")
return;
}
this.windowStage.getMainWindow((err: BusinessError, windowClass: window.Window) => {
if (err.code) {
console.error('Failed to obtain the main window. Cause: ' + err.message);
reject(err)
return;
}
// 2.设置窗口的显示方向属性,使用Promise异步回调。
windowClass.setPreferredOrientation(orientation).then(() => {
console.info('Succeeded in setting the window orientation.');
resolve()
}).catch((err: BusinessError) => {
console.error('Failed to set the window orientation. Cause: ' + err.message);
reject(err)
});
});
})
}
}