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