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