1# Application Window Development (FA Model) 2 3## Basic Concepts 4 5Immersive window: a window display mode where the system windows (generally the status bar and navigation bar) are hidden to allow users to fully engage with the content. 6 7The immersive window feature is applicable only to the main window of an application in full-screen mode. It does not apply to a main window in any other mode or a subwindow (for example, a dialog box or a floating window). 8 9> **NOTE** 10> 11> Currently, immersive UI development supports window-level configuration, but not page-level configuration. If page redirection is required, you can set the immersive mode at the beginning of the page lifecycle, for example, in the **onPageShow** callback, and then restore the default settings when the page exits, for example, in the **onPageHide** callback. 12 13## When to Use 14 15In the FA model, you can perform the following operations during application window development: 16 17- Setting the properties and content of the subwindow of an application 18 19- Experiencing the immersive window feature 20 21## Available APIs 22 23The table below lists the common APIs used for application window development. For details about more APIs, see [Window](../reference/apis-arkui/js-apis-window.md). 24 25| Instance | API | Description | 26| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 27| Window static method| createWindow(config: Configuration, callback: AsyncCallback\<Window>): void | Creates a subwindow.<br>**config**: parameters used for creating the window. | 28| Window static method| findWindow(name: string): Window | Finds a window based on the name. | 29| Window | setUIContent(path: string, callback: AsyncCallback<void>): void | Loads the content of a page, with its path in the current project specified, to this window.<br>**path**: path of the page from which the content will be loaded. The path is configured in the **config.json** file of the project in the FA model. | 30| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void | Moves this window. | 31| Window | setWindowBrightness(brightness: number, callback: AsyncCallback<void>): void | Sets the brightness for this window. | 32| Window | resize(width: number, height: number, callback: AsyncCallback<void>): void | Changes the window size. | 33| Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean): Promise<void> | Sets whether to enable the full-screen mode for the window layout. | 34| Window | setWindowSystemBarEnable(names: Array<'status'\|'navigation'>): Promise<void> | Sets whether to display the status bar and navigation bar in this window. | 35| Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise<void> | Sets the properties of the status bar and navigation bar in this window.<br>**systemBarProperties**: properties of the status bar and navigation bar.| 36| Window | showWindow(callback: AsyncCallback\<void>): void | Shows this window. | 37| Window | on(type: 'touchOutside', callback: Callback<void>): void | Enables listening for touch events outside this window. | 38| Window | destroyWindow(callback: AsyncCallback<void>): void | Destroys this window. | 39 40 41## Setting the Subwindow of an Application 42 43You can create a subwindow, such as a dialog box, and set its properties. 44 45 46### How to Develop 47 481. Create or obtain a subwindow. 49 50 - Call **window.createWindow** to create a subwindow. 51 - Call **window.findWindow** to find an available subwindow. 52 53 ```ts 54 import { window } from '@kit.ArkUI'; 55 import { BusinessError } from '@kit.BasicServicesKit'; 56 57 let windowClass: window.Window | null = null; 58 // Method 1: Create a subwindow. 59 let config: window.Configuration = { name: "subWindow", windowType: window.WindowType.TYPE_APP }; 60 window.createWindow(config, (err: BusinessError, data) => { 61 let errCode: number = err.code; 62 if (errCode) { 63 console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err)); 64 return; 65 } 66 console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data)); 67 windowClass = data; 68 }); 69 // Method 2: Find a subwindow. 70 try { 71 windowClass = window.findWindow('subWindow'); 72 } catch (exception) { 73 console.error('Failed to find the Window. Cause: ' + JSON.stringify(exception)); 74 } 75 ``` 76 772. Set the properties of the subwindow. 78 79 After the subwindow is created, you can set its properties, such as the size, position, background color, and brightness. 80 81 ```ts 82 // Move the subwindow. 83 let windowClass: window.Window = window.findWindow("test"); 84 windowClass.moveWindowTo(300, 300, (err: BusinessError) => { 85 let errCode: number = err.code; 86 if (errCode) { 87 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 88 return; 89 } 90 console.info('Succeeded in moving the window.'); 91 }); 92 // Change the size of the subwindow. 93 windowClass.resize(500, 500, (err: BusinessError) => { 94 let errCode: number = err.code; 95 if (errCode) { 96 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 97 return; 98 } 99 console.info('Succeeded in changing the window size.'); 100 }); 101 ``` 102 1033. Load content to and show the subwindow. 104 105 Call **setUIContent** to load content to the subwindow and **showWindow** to show the subwindow. 106 107 ```ts 108 // Load content to the subwindow. 109 let windowClass: window.Window = window.findWindow("test"); 110 windowClass.setUIContent("pages/page2", (err: BusinessError) => { 111 let errCode: number = err.code; 112 if (errCode) { 113 console.error('Failed to load the content. Cause: ' + JSON.stringify(err)); 114 return; 115 } 116 console.info('Succeeded in loading the content.'); 117 // Show the subwindow. 118 windowClass.showWindow((err: BusinessError) => { 119 let errCode: number = err.code; 120 if (errCode) { 121 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 122 return; 123 } 124 console.info('Succeeded in showing the window.'); 125 }); 126 }); 127 ``` 128 1294. Destroy the subwindow. 130 131 When the subwindow is no longer needed, you can call **destroyWindow** to destroy it. 132 133 ```ts 134 // Call destroy() to destroy the subwindow when it is no longer needed. 135 let windowClass: window.Window = window.findWindow("test"); 136 windowClass.destroyWindow((err: BusinessError) => { 137 let errCode: number = err.code; 138 if (errCode) { 139 console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err)); 140 return; 141 } 142 console.info('Succeeded in destroying the subwindow.'); 143 }); 144 ``` 145 146## Experiencing the Immersive Window Feature 147 148To create a better video watching and gaming experience, you can use the immersive window feature to hide the status bar and navigation bar. This feature is available only for the main window of an application. Since API version 10, the immersive window has the same size as the full screen by default; its layout is controlled by the component module; the background color of its status bar and navigation bar is transparent, and the text color is black. When an application window calls **setWindowLayoutFullScreen**, with **true** passed in, an immersive window layout is used. If **false** is passed in, a non-immersive window layout is used. 149 150 151### How to Develop 152 1531. Obtain the main window. 154 155 > **NOTE** 156 > 157 > The immersive window feature can be implemented only after the main window is obtained. 158 > 159 > Ensure that the top window of the application is the main window. You can use **window.getLastWindow** to obtain the main window. 160 161 ```ts 162 import { window } from '@kit.ArkUI'; 163 import { BusinessError } from '@kit.BasicServicesKit'; 164 165 let mainWindowClass: window.Window | null = null; 166 167 // Obtain the main window. 168 class BaseContext { 169 stageMode: boolean = false; 170 } 171 172 let context: BaseContext = { stageMode: false }; 173 window.getLastWindow(context, (err: BusinessError, data) => { 174 let errCode: number = err.code; 175 if (errCode) { 176 console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err)); 177 return; 178 } 179 console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data)); 180 mainWindowClass = data; 181 }); 182 ``` 183 1842. Implement the immersive effect. You can use either of the following methods: 185 186 - Method 1: When the main window of the application is a full-screen window, call **setWindowSystemBarEnable** to hide the status bar and navigation bar. 187 - Method 2: Call **setWindowLayoutFullScreen** to enable the full-screen mode for the main window layout. Call **setWindowSystemBarProperties** to set the opacity, background color, text color, and highlighted icon of the status bar and navigation bar to create a display effect consistent with that of the main window. 188 189 ```ts 190 // Implement the immersive effect by hiding the status bar and navigation bar. 191 let names: Array<'status' | 'navigation'> = []; 192 let mainWindowClass: window.Window = window.findWindow("test"); 193 mainWindowClass.setWindowSystemBarEnable(names) 194 .then(() => { 195 console.info('Succeeded in setting the system bar to be visible.'); 196 }) 197 .catch((err: BusinessError) => { 198 console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); 199 }); 200 // Implement the immersive effect by setting the properties of the status bar and navigation bar. 201 202 let isLayoutFullScreen: boolean = true; 203 mainWindowClass.setWindowLayoutFullScreen(isLayoutFullScreen) 204 .then(() => { 205 console.info('Succeeded in setting the window layout to full-screen mode.'); 206 }) 207 .catch((err: BusinessError) => { 208 console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); 209 }); 210 let sysBarProps: window.SystemBarProperties = { 211 statusBarColor: '#ff00ff', 212 navigationBarColor: '#00ff00', 213 // The following properties are supported since API version 8. 214 statusBarContentColor: '#ffffff', 215 navigationBarContentColor: '#ffffff' 216 }; 217 mainWindowClass.setWindowSystemBarProperties(sysBarProps) 218 .then(() => { 219 console.info('Succeeded in setting the system bar properties.'); 220 }) 221 .catch((err: BusinessError) => { 222 console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); 223 }); 224 ``` 225 2263. Load content to and show the immersive window. 227 228 Call **setUIContent** to load content to the immersive window and **showWindow** to show the window. 229 230 ```ts 231 // Load content to the immersive window. 232 let mainWindowClass: window.Window = window.findWindow("test"); 233 mainWindowClass.setUIContent("pages/page3", (err: BusinessError) => { 234 let errCode: number = err.code; 235 if (errCode) { 236 console.error('Failed to load the content. Cause: ' + JSON.stringify(err)); 237 return; 238 } 239 console.info('Succeeded in loading the content.'); 240 // Show the immersive window. 241 mainWindowClass.showWindow((err: BusinessError) => { 242 let errCode: number = err.code; 243 if (errCode) { 244 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 245 return; 246 } 247 console.info('Succeeded in showing the window.'); 248 }); 249 }); 250 ``` 251