application-window-fa.md 11.2 KB
Newer Older
G
ge-yafang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 管理应用窗口(FA模型)

## 基本概念

窗口沉浸式能力:指对状态栏、导航栏等系统窗口进行控制,减少状态栏导航栏等系统界面的突兀感,从而使用户获得最佳体验的能力。
沉浸式能力只在应用主窗口作为全屏窗口时生效。通常情况下,应用子窗口(弹窗、悬浮窗口等辅助窗口)和处于自由窗口下的应用主窗口无法使用沉浸式能力。

## 场景介绍

在FA模型下,管理应用窗口的典型场景有:

- 设置应用子窗口属性及目标页面

- 体验窗口沉浸式能力

以下分别介绍具体开发方式。


## 接口说明

上述场景涉及的常用接口如下表所示。更多API说明请参见[API参考](../reference/apis/js-apis-window.md)

| 实例名 | 接口名 | 描述 |
| -------- | -------- | -------- |
25 26 27 28 29 30 31 32 33 34 35 36
| window静态方法 | create(id: string, type: WindowType, callback: AsyncCallback&lt;Window&gt;): void | 创建子窗口。<br>此接口仅可在`FA`模型下使用。 |
| window静态方法 | getTopWindow(callback: AsyncCallback&lt;Window&gt;): void | 获取当前应用内最后显示的窗口。<br/>此接口仅可在`FA`模型下使用。 |
| window静态方法 | find(id: string, callback: AsyncCallback&lt;Window&gt;): void | 查找`id`所对应的窗口。 |
| Window | loadContent(path: string, callback: AsyncCallback&lt;void&gt;): void | 为当前窗口加载具体页面内容。 |
| Window | moveTo(x: number, y: number, callback: AsyncCallback&lt;void&gt;): void | 移动当前窗口。 |
| Window | setBackgroundColor(color: string, callback: AsyncCallback&lt;void&gt;): void | 设置窗口的背景色 |
| Window | setBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | 设置屏幕亮度值。 |
| Window | resetSize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | 改变当前窗口大小。 |
| Window | setFullScreen(isFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口是否全屏显示。 |
| Window | setLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback&lt;void&gt;): void | 设置窗口布局是否为全屏布局。 |
| Window | setSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | 设置导航栏、状态栏是否显示。 |
| Window | setSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback&lt;void&gt;): void | 设置窗口内导航栏、状态栏属性。<br/>`systemBarProperties`:导航栏、状态栏的属性集合。 |
G
ge-yafang 已提交
37
| Window | show(callback: AsyncCallback\<void>): void | 显示当前窗口。 |
38 39
| Window | on(type: 'touchOutside', callback: Callback&lt;void&gt;): void | 开启本窗口区域外的点击事件的监听。 |
| Window | destroy(callback: AsyncCallback&lt;void&gt;): void | 销毁当前窗口。 |
G
ge-yafang 已提交
40 41 42 43 44 45 46 47 48 49


## 设置应用子窗口

开发者可以按需创建应用子窗口,如弹窗等,并对其进行属性设置等操作。


### 开发步骤

1. 创建/获取子窗口对象。
G
ge-yafang 已提交
50

G
ge-yafang 已提交
51 52 53 54 55 56 57
   - 可以通过`window.create`接口创建子窗口。
   - 可以通过`window.getTopWindow`来获取最后显示的窗口得到子窗口。
   - 也可以通过`window.find`接口来查找已经创建的窗口从而得到子窗口。

   ```js
   import window from '@ohos.window';
   
58
   let windowClass = null;
G
ge-yafang 已提交
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
   // 1.方式一:创建子窗口。
   window.create("subWindow", window.WindowType.TYPE_APP, (err, data) => {
       if (err.code) {
           console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err));
           return;
       }
       console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data));
       windowClass = data;
   });
   // 1.方式二:获取子窗口。
   window.getTopWindow((err, data) => {
       if (err.code) {
           console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
           return;
       }
       console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data));
       windowClass = data;
   });
   // 1.方式三:查找得到子窗口。
   window.find("subWindow", (err, data) => {
       if (err.code) {
           console.error('Failed to find the subWindow. Cause: ' + JSON.stringify(err));
           return;
       }
       console.info('Succeeded in finding subWindow. Data: ' + JSON.stringify(data));
       windowClass = data;
   });
   ```
   
2. 设置子窗口属性。
G
ge-yafang 已提交
89

G
ge-yafang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
   子窗口创建成功后,可以改变其大小、位置等,还可以根据应用需要设置窗口背景色、亮度等属性。

   
   ```js
   // 2.移动子窗口位置。
   windowClass.moveTo(300, 300, (err, data) => {
     if (err.code) {
       console.error('Failed to move the window. Cause:' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in moving the window. Data: ' + JSON.stringify(data));
   });
   // 2.改变子窗口大小。
   windowClass.resetSize(500, 1000, (err, data) => {
     if (err.code) {
       console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in changing the window size. Data: ' + JSON.stringify(data));
   });
   ```

3. 加载显示子窗口的具体内容。
G
ge-yafang 已提交
113

G
ge-yafang 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
   使用`loadContent``show`接口加载显示子窗口的具体内容。

   
   ```js
   // 3.为子窗口加载对应的目标页面。
   windowClass.loadContent("pages/page2", (err, data) => {
       if (err.code) {
           console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
           return;
       }
       console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data));
       // 3.显示子窗口。
       windowClass.show((err, data) => {
        if (err.code) {
               console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
               return;
           }
           console.info('Succeeded in showing the window. Data: ' + JSON.stringify(data));
       });
   });
   ```
   
4. 销毁子窗口。
G
ge-yafang 已提交
137

G
ge-yafang 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
   当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用`destroy`接口销毁子窗口。

   
   ```js
   // 4.销毁子窗口。当不再需要某些子窗口时,可根据场景的具体实现逻辑,使用destroy接口销毁子窗口,此处以监听窗口区域外的点击事件实现子窗口的销毁。
   windowClass.on('touchOutside', () => {
       console.info('touch outside');
       windowClass.destroy((err, data) => {
           if (err.code) {
               console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err));
               return;
           }
           console.info('Succeeded in destroying the subwindow. Data: ' + JSON.stringify(data));
       });
   });
   ```


## 体验窗口沉浸式能力

在看视频、玩游戏等场景下,用户往往希望隐藏状态栏、导航栏等不必要的系统窗口,从而获得更佳的沉浸式体验。此时可以借助窗口沉浸式能力(窗口沉浸式能力都是针对应用主窗口而言的),达到预期效果。


### 开发步骤

1. 获取主窗口对象。
G
ge-yafang 已提交
164

165 166 167 168 169
   > **说明:** 
   >
   > 沉浸式能力需要在成功获取应用主窗口对象的前提下进行。
   >
   > 确保应用内最后显示的窗口为主窗口,然后再使用`window.getTopWindow`接口来获取得到主窗口。
G
ge-yafang 已提交
170 171 172 173 174

   
   ```js
   import window from '@ohos.window';
   
175
   let mainWindowClass = null;
G
ge-yafang 已提交
176 177 178 179 180 181 182 183 184 185 186 187
   // 1.获取主窗口
   window.getTopWindow((err, data) => {
     if (err.code) {
       console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data));
     mainWindowClass = data;
   });
   ```

2. 实现沉浸式效果。有以下三种方式:
G
ge-yafang 已提交
188

G
ge-yafang 已提交
189 190 191 192 193 194
   - 方式一:调用`setFullScreen`接口,设置应用主窗口为全屏显示,此时导航栏、状态栏将隐藏,从而达到沉浸式效果。
   - 方式二:调用`setSystemBarEnable`接口,设置导航栏、状态栏不显示,从而达到沉浸式效果。
   - 方式三:调用`setLayoutFullScreen`接口,设置应用主窗口为全屏布局;然后调用`setSystemPropertites`接口,设置导航栏、状态栏的透明度、背景/文字颜色以及高亮图标等属性,使之保持与主窗口显示协调一致,从而达到沉浸式效果。

   ```js
   // 2.实现沉浸式效果。方式一:设置窗口全屏显示。
195
   let isFullScreen = true;
G
ge-yafang 已提交
196 197 198 199 200 201 202 203
   mainWindowClass.setFullScreen(isFullScreen, (err, data) => {
     if (err.code) {
       console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data));
   });
   // 2.实现沉浸式效果。方式二:设置导航栏、状态栏不显示。
204
   let names = [];
G
ge-yafang 已提交
205 206 207 208 209 210 211 212
   mainWindowClass.setSystemBarEnable(names, (err, data) => {
     if (err.code) {
       console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in setting the system bar to be visible. Data: ' + JSON.stringify(data));
   });
   // 2.实现沉浸式效果。
213
   // 方式三:设置窗口为全屏布局,配合设置状态栏、导航栏的透明度、背景/文字颜色及高亮图标等属性,与主窗口显示保持协调一致。
214
   let isLayoutFullScreen = true;
G
ge-yafang 已提交
215 216 217 218 219 220 221
   mainWindowClass.setLayoutFullScreen(isLayoutFullScreen, (err, data) => {
     if (err.code) {
       console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in setting the window layout to full-screen mode. Data: ' + JSON.stringify(data));
   });
222
   let sysBarProps = {
G
ge-yafang 已提交
223 224
     statusBarColor: '#ff00ff',
     navigationBarColor: '#00ff00',
225
     // 以下两个属性从API Version7开始支持
G
ge-yafang 已提交
226 227
     isStatusBarLightIcon: false,
     isNavigationBarLightIcon: false,
228
     // 以下两个属性从API Version8开始支持
G
ge-yafang 已提交
229 230 231
     statusBarContentColor: '#ffffff',
     navigationBarContentColor: '#ffffff'
   };
232
   mainWindowClass.setSystemBarProperties(sysBarProps, (err, data) => {
G
ge-yafang 已提交
233 234 235 236 237 238 239 240 241
     if (err.code) {
       console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
       return;
     }
     console.info('Succeeded in setting the system bar properties. Data: ' + JSON.stringify(data));
   });
   ```
   
3. 加载显示沉浸式窗口的具体内容。
G
ge-yafang 已提交
242

G
ge-yafang 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
   使用`loadContent``show`接口加载显示沉浸式窗口的具体内容。

   
   ```js
   // 3.为沉浸式窗口加载对应的目标页面。
   mainWindowClass.loadContent("pages/page3", (err, data) => {
       if (err.code) {
           console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
           return;
       }
       console.info('Succeeded in loading the content. Data: ' + JSON.stringify(data));
       // 3.显示沉浸式窗口。
       mainWindowClass.show((err, data) => {
           if (err.code) {
               console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
               return;
           }
           console.info('Succeeded in showing the window. Data: ' + JSON.stringify(data));
       });
   });
263
   ```