WindowModel.ets
4.45 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import window from '@ohos.window';
import { AsyncCallback, BusinessError } from '@ohos.base';
import deviceInfo from '@ohos.deviceInfo'
import display from '@ohos.display';
interface SystemBarProperties {
statusBarColor?: string;
isStatusBarLightIcon?: boolean;
statusBarContentColor?: string;
navigationBarColor?: string;
isNavigationBarLightIcon?: boolean;
navigationBarContentColor?: string;
}
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;
private windowClass?: window.Window;
private isFullScreen: boolean = false
static shared: WindowModel = new WindowModel()
static TAG = "WindowModel";
setWindowStage(windowStage: window.WindowStage) {
this.windowStage = windowStage;
this.windowClass = windowStage.getMainWindowSync();
}
getWindowStage(): window.WindowStage {
return this.windowStage as window.WindowStage
}
getWindowClass(): window.Window {
return this.windowClass as window.Window
}
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)
});
});
})
}
/**
* 设置窗口全屏模式时窗口内导航栏、状态栏的属性,使用callback异步回调。
* @param systemBarProperties
* @param callback
*/
setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback?: AsyncCallback<void>): void {
this.windowClass?.setWindowSystemBarProperties(systemBarProperties, (err: BusinessError) => {
callback && callback(err)
})
}
setWindowSystemBarEnable(names: Array<'status' | 'navigation'>) {
this.windowClass?.setWindowSystemBarEnable(names)
}
getWindowProperties() {
return this.windowClass?.getWindowProperties()
}
setWindowLayoutFullScreen(isFullScreen: boolean) {
this.isFullScreen = isFullScreen
this.windowClass?.setWindowLayoutFullScreen(isFullScreen)
}
getIsFullScreen(): boolean {
return this.isFullScreen
}
}