• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 管理应用窗口(FA模型)
2
3## 基本概念
4
5窗口沉浸式能力:指对状态栏、导航栏等系统窗口进行控制,减少状态栏导航栏等系统界面的突兀感,从而使用户获得最佳体验的能力。
6沉浸式能力只在应用主窗口作为全屏窗口时生效。通常情况下,应用子窗口(弹窗、悬浮窗口等辅助窗口)和处于自由窗口下的应用主窗口无法使用沉浸式能力。
7
8## 场景介绍
9
10在FA模型下,管理应用窗口的典型场景有:
11
12- 设置应用子窗口属性及目标页面
13
14- 体验窗口沉浸式能力
15
16以下分别介绍具体开发方式。
17
18
19## 接口说明
20
21上述场景涉及的常用接口如下表所示。更多API说明请参见[API参考](../reference/apis/js-apis-window.md)。
22
23| 实例名         | 接口名                                                       | 描述                                                         |
24| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
25| window静态方法 | createWindow(config: Configuration, callback: AsyncCallback\<Window>): void | 创建子窗口。<br/>-`config`:创建窗口时的参数。               |
26| window静态方法 | findWindow(name: string): Window                             | 查找`name`所对应的窗口。                                     |
27| Window         | setUIContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 根据当前工程中某个页面的路径为窗口加载具体的页面内容。<br>其中path为要加载到窗口中的页面内容的路径,在FA模型下该路径需添加到工程的config.json文件中。                                 |
28| Window         | moveWindowTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口。                                               |
29| Window         | setWindowBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。                                             |
30| Window         | resize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。                                           |
31| Window         | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。                                 |
32| Window         | setWindowSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。                                 |
33| Window         | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
34| Window         | showWindow(callback: AsyncCallback\<void>): void             | 显示当前窗口。                                               |
35| Window         | on(type: 'touchOutside', callback: Callback&lt;void&gt;): void | 开启本窗口区域外的点击事件的监听。                           |
36| Window         | destroyWindow(callback: AsyncCallback&lt;void&gt;): void     | 销毁当前窗口。                                               |
37
38
39## 设置应用子窗口
40
41开发者可以按需创建应用子窗口,如弹窗等,并对其进行属性设置等操作。
42
43
44### 开发步骤
45
461. 创建/获取子窗口对象。
47
48   - 可以通过`window.createWindow`接口创建子窗口。
49   - 也可以通过`window.findWindow`接口来查找已经创建的窗口从而得到子窗口。
50
51   ```ts
52   import window from '@ohos.window';
53   import { BusinessError } from '@ohos.base';
54
55   let windowClass: window.Window | null = null;
56   // 方式一:创建子窗口。
57   let config: window.Configuration = { name: "subWindow", windowType: window.WindowType.TYPE_APP };
58   window.createWindow(config, (err: BusinessError, data) => {
59     let errCode: number = err.code;
60     if (errCode) {
61       console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err));
62       return;
63     }
64     console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data));
65     windowClass = data;
66   });
67   // 方式二:查找得到子窗口。
68   try {
69     windowClass = window.findWindow('subWindow');
70   } catch (exception) {
71     console.error('Failed to find the Window. Cause: ' + JSON.stringify(exception));
72   }
73   ```
74
752. 设置子窗口属性。
76
77   子窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置窗口背景色、亮度等属性。
78
79   ```ts
80   // 移动子窗口位置。
81   let windowClass: window.Window = window.findWindow("test");
82   windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
83     let errCode: number = err.code;
84     if (errCode) {
85       console.error('Failed to move the window. Cause:' + JSON.stringify(err));
86       return;
87     }
88     console.info('Succeeded in moving the window.');
89   });
90   // 改变子窗口大小。
91   windowClass.resize(500, 500, (err: BusinessError) => {
92     let errCode: number = err.code;
93     if (errCode) {
94       console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
95       return;
96     }
97     console.info('Succeeded in changing the window size.');
98   });
99   ```
100
1013. 加载显示子窗口的具体内容。
102
103   使用`setUIContent`和`showWindow`接口加载显示子窗口的具体内容。
104
105   ```ts
106   // 为子窗口加载对应的目标页面。
107   let windowClass: window.Window = window.findWindow("test");
108   windowClass.setUIContent("pages/page2", (err: BusinessError) => {
109     let errCode: number = err.code;
110     if (errCode) {
111       console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
112       return;
113     }
114     console.info('Succeeded in loading the content.');
115     // 显示子窗口。
116     windowClass.showWindow((err: BusinessError) => {
117       let errCode: number = err.code;
118       if (errCode) {
119         console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
120         return;
121       }
122       console.info('Succeeded in showing the window.');
123     });
124   });
125   ```
126
1274. 销毁子窗口。
128
129   当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用`destroyWindow`接口销毁子窗口。
130
131   ```ts
132   // 销毁子窗口。当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用destroy接口销毁子窗口。
133   let windowClass: window.Window = window.findWindow("test");
134   windowClass.destroyWindow((err: BusinessError) => {
135     let errCode: number = err.code;
136     if (errCode) {
137       console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err));
138       return;
139     }
140     console.info('Succeeded in destroying the subwindow.');
141   });
142   ```
143
144## 体验窗口沉浸式能力
145
146在看视频、玩游戏等场景下,用户往往希望隐藏状态栏、导航栏等不必要的系统窗口,从而获得更佳的沉浸式体验。此时可以借助窗口沉浸式能力(窗口沉浸式能力都是针对应用主窗口而言的),达到预期效果。从API version 10开始,沉浸式窗口默认配置为全屏大小并由组件模块控制布局,状态栏、导航栏背景颜色为透明,文字颜色为黑色;应用窗口调用`setWindowLayoutFullScreen`接口,设置为true表示由组件模块控制忽略状态栏、导航栏的沉浸式全屏布局,设置为false表示由组件模块控制避让状态栏、导航栏的非沉浸式全屏布局。
147
148
149### 开发步骤
150
1511. 获取主窗口对象。
152
153   > **说明:**
154   >
155   > 沉浸式能力需要在成功获取应用主窗口对象的前提下进行。
156   >
157   > 确保应用内最后显示的窗口为主窗口,然后再使用`window.getLastWindow`接口来获取得到主窗口。
158
159   ```ts
160   import window from '@ohos.window';
161   import { BusinessError } from '@ohos.base';
162
163   let mainWindowClass: window.Window | null = null;
164
165   // 获取主窗口。
166   class BaseContext {
167     stageMode: boolean = false;
168   }
169
170   let context: BaseContext = { stageMode: false };
171   window.getLastWindow(context, (err: BusinessError, data) => {
172     let errCode: number = err.code;
173     if (errCode) {
174       console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
175       return;
176     }
177     console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data));
178     mainWindowClass = data;
179   });
180   ```
181
1822. 实现沉浸式效果。有以下两种方式:
183
184   - 方式一:应用主窗口为全屏窗口时,调用`setWindowSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
185   - 方式二:调用`setWindowLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setWindowSystemBarProperties`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。
186
187   ```ts
188   // 实现沉浸式效果。方式一:设置导航栏、状态栏不显示。
189   let names: Array<'status' | 'navigation'> = [];
190   let mainWindowClass: window.Window = window.findWindow("test");
191   mainWindowClass.setWindowSystemBarEnable(names, (err: BusinessError) => {
192     let errCode: number = err.code;
193     if (errCode) {
194       console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
195       return;
196     }
197     console.info('Succeeded in setting the system bar to be visible.');
198   });
199   // 实现沉浸式效果。
200   // 方式二:设置窗口为全屏布局,配合设置状态栏、导航栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
201   let isLayoutFullScreen: boolean = true;
202   mainWindowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => {
203     let errCode: number = err.code;
204     if (errCode) {
205       console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
206       return;
207     }
208     console.info('Succeeded in setting the window layout to full-screen mode.');
209   });
210   let sysBarProps: window.SystemBarProperties = {
211     statusBarColor: '#ff00ff',
212     navigationBarColor: '#00ff00',
213     // 以下两个属性从API Version8开始支持。
214     statusBarContentColor: '#ffffff',
215     navigationBarContentColor: '#ffffff'
216   };
217   mainWindowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
218     let errCode: number = err.code;
219     if (errCode) {
220       console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
221       return;
222     }
223     console.info('Succeeded in setting the system bar properties.');
224   });
225   ```
226
2273. 加载显示沉浸式窗口的具体内容。
228
229   使用`setUIContent`和`showWindow`接口加载显示沉浸式窗口的具体内容。
230
231   ```ts
232   // 为沉浸式窗口加载对应的目标页面。
233   let mainWindowClass: window.Window = window.findWindow("test");
234   mainWindowClass.setUIContent("pages/page3", (err: BusinessError) => {
235     let errCode: number = err.code;
236     if (errCode) {
237       console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
238       return;
239     }
240     console.info('Succeeded in loading the content.');
241     // 显示沉浸式窗口。
242     mainWindowClass.showWindow((err: BusinessError) => {
243       let errCode: number = err.code;
244       if (errCode) {
245         console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
246         return;
247       }
248       console.info('Succeeded in showing the window.');
249     });
250   });
251   ```