1# Application Window Development (Stage Mode) 2 3 4## Basic Concepts 5 6- Immersive window: System windows such as the status bar and navigation bar are controlled in such a way that they are unobtrusive on the entire screen, thereby engaging users more deeply with the content displayed. 7 The immersive window feature is valid only when the main window of an application is displayed in full-screen mode. It does not apply to a main window in free window mode or a subwindow (for example, a dialog box or a floating window). 8 9- Floating window: a special application window that can still be displayed in the foreground when the main window and corresponding ability are running the background. 10 The floating window can be used to continue playing a video in a small window after the application returns to the background, or offer a quick entry (for example, bubbles) to a specific application. Before creating a floating window, an application must apply for the required permission. 11 12 13## When to Use 14 15In the stage model, you can perform the following operations during application window development: 16 17- Setting the properties and content of the main window of an application 18 19- Setting the properties and content of the subwindow of an application 20 21- Experiencing the immersive window feature 22 23- Setting a floating window 24 25 26## Available APIs 27 28The table below lists the common APIs used for application window development. For details about more APIs, see [Window](../reference/apis/js-apis-window.md). 29| Instance | API | Description | 30| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | 31| WindowStage | getMainWindow(callback: AsyncCallback<Window>): void | Obtains the main window of this window stage.<br>This API can be used only in the stage model.| 32| WindowStage | loadContent(path: string, callback: AsyncCallback<void>): void | Loads the page content to the main window in this window stage.<br>This API can be used only in the stage model.| 33| WindowStage | createSubWindow(name: string, callback: AsyncCallback<Window>): void | Creates a subwindow.<br>This API can be used only in the stage model. | 34| Window static method| createWindow(config: Configuration, callback: AsyncCallback\<Window>): void | Creates a system window.<br>**config** specifies the parameters used for creating the window. | 35| Window | setUIContent(path: string, callback: AsyncCallback<void>): void | Loads the page content to this window. | 36| Window | setWindowBackgroundColor(color: string, callback: AsyncCallback<void>): void | Sets the background color for this window. | 37| Window | setWindowBrightness(brightness: number, callback: AsyncCallback<void>): void | Sets the brightness for this window. | 38| Window | setWindowTouchable(isTouchable: boolean, callback: AsyncCallback<void>): void | Sets whether this window is touchable. | 39| Window | moveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void | Moves this window. | 40| Window | resize(width: number, height: number, callback: AsyncCallback<void>): void | Changes the window size. | 41| Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback<void>): void | Sets whether to enable the full-screen mode for the window layout. | 42| Window | setWindowSystemBarEnable(names: Array<'status'\|'navigation'>): Promise<void> | Sets whether to display the status bar and navigation bar in this window. | 43| Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void | Sets the properties of the status bar and navigation bar in this window.<br>**systemBarProperties**: properties of the status bar and navigation bar.| 44| Window | showWindow(callback: AsyncCallback\<void>): void | Shows this window. | 45| Window | on(type: 'touchOutside', callback: Callback<void>): void | Enables listening for click events outside this window. | 46| Window | destroyWindow(callback: AsyncCallback<void>): void | Destroys this window. | 47 48 49## Setting the Main Window of an Application 50 51In the stage model, the main window of an application is created and maintained by a **UIAbility** instance. In the **onWindowStageCreate** callback of the **UIAbility** instance, use **WindowStage** to obtain the main window of the application and set its properties. You can also set the properties (for example, **maxWindowWidth**) in the [module.json5 file](../quick-start/module-configuration-file.md#abilities). 52 53### How to Develop 54 551. Obtain the main window. 56 57 Call **getMainWindow** to obtain the main window of the application. 58 592. Set the properties of the main window. 60 61 You can set multiple properties of the main window, such as the background color, brightness, and whether the main window is touchable. The code snippet below uses the **touchable** property as an example. 62 633. Load content for the main window. 64 65 Call **loadContent** to load the page content to the main window. 66 67```ts 68import UIAbility from '@ohos.app.ability.UIAbility'; 69 70export default class EntryAbility extends UIAbility { 71 onWindowStageCreate(windowStage) { 72 // 1. Obtain the main window of the application. 73 let windowClass = null; 74 windowStage.getMainWindow((err, data) => { 75 if (err.code) { 76 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 77 return; 78 } 79 windowClass = data; 80 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 81 // 2. Set the touchable property of the main window. 82 let isTouchable = true; 83 windowClass.setWindowTouchable(isTouchable, (err) => { 84 if (err.code) { 85 console.error('Failed to set the window to be touchable. Cause:' + JSON.stringify(err)); 86 return; 87 } 88 console.info('Succeeded in setting the window to be touchable.'); 89 }) 90 }) 91 // 3. Load the page content to the main window. 92 windowStage.loadContent("pages/page2", (err) => { 93 if (err.code) { 94 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 95 return; 96 } 97 console.info('Succeeded in loading the content.'); 98 }); 99 } 100}; 101``` 102 103## Setting a Subwindow of an Application 104 105You can create an application subwindow, such as a dialog box, and set its properties. 106 107### How to Develop 108 1091. Create a subwindow. 110 111 Call **createSubWindow** to create a subwindow. 112 1132. Set the properties of the subwindow. 114 115 After the subwindow is created, you can set its properties, such as the size, position, background color, and brightness. 116 1173. Load content for the subwindow and show it. 118 119 Call **setUIContent** and **showWindow** to load and display the content in the subwindow. 120 1214. Destroy the subwindow. 122 123 When the subwindow is no longer needed, you can call **destroyWindow** to destroy it. 124 125```ts 126import UIAbility from '@ohos.app.ability.UIAbility'; 127 128let windowStage_ = null; 129let sub_windowClass = null; 130 131export default class EntryAbility extends UIAbility { 132 showSubWindow() { 133 // 1. Create a subwindow. 134 windowStage_.createSubWindow("mySubWindow", (err, data) => { 135 if (err.code) { 136 console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err)); 137 return; 138 } 139 sub_windowClass = data; 140 console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data)); 141 // 2. Set the position, size, and other properties of the subwindow. 142 sub_windowClass.moveWindowTo(300, 300, (err) => { 143 if (err.code) { 144 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 145 return; 146 } 147 console.info('Succeeded in moving the window.'); 148 }); 149 sub_windowClass.resize(500, 500, (err) => { 150 if (err.code) { 151 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 152 return; 153 } 154 console.info('Succeeded in changing the window size.'); 155 }); 156 // 3. Load the page content to the subwindow. 157 sub_windowClass.setUIContent("pages/page3", (err) => { 158 if (err.code) { 159 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 160 return; 161 } 162 console.info('Succeeded in loading the content.'); 163 // 3. Show the subwindow. 164 sub_windowClass.showWindow((err) => { 165 if (err.code) { 166 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 167 return; 168 } 169 console.info('Succeeded in showing the window.'); 170 }); 171 }); 172 }) 173 } 174 175 destroySubWindow() { 176 // 4. Destroy the subwindow when it is no longer needed (depending on the service logic). 177 sub_windowClass.destroyWindow((err) => { 178 if (err.code) { 179 console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); 180 return; 181 } 182 console.info('Succeeded in destroying the window.'); 183 }); 184 } 185 186 onWindowStageCreate(windowStage) { 187 windowStage_ = windowStage; 188 // Create a subwindow when it is needed, for example, when a click event occurs in the main window. Calling onWindowStageCreate is not always necessary. The code here is for reference only. 189 this.showSubWindow(); 190 } 191 192 onWindowStageDestroy() { 193 // Destroy the subwindow when it is no longer needed, for example, when the Close button in the subwindow is clicked. Calling onWindowStageDestroy is not always necessary. The code here is for reference only. 194 this.destroySubWindow(); 195 } 196}; 197``` 198 199## Experiencing the Immersive Window Feature 200 201To create a better video watching and gaming experience, you can use the immersive window feature to hide the system windows, including the status bar and navigation bar. This feature is available only for the main window of an application. 202 203 204### How to Develop 205 2061. Obtain the main window. 207 208 Call **getMainWindow** to obtain the main window of the application. 209 2102. Implement the immersive effect. You can use either of the following methods: 211 212 - Method 1: Call **setWindowSystemBarEnable** to hide the status bar and navigation bar. 213 214 - 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 ensure that their display effect is consistent with that of the main window. 215 2163. Load content for the immersive window and show it. 217 218 Call **loadContent** to load the content to the immersive window. 219 220```ts 221import UIAbility from '@ohos.app.ability.UIAbility'; 222 223export default class EntryAbility extends UIAbility { 224 onWindowStageCreate(windowStage) { 225 // 1. Obtain the main window of the application. 226 let windowClass = null; 227 windowStage.getMainWindow((err, data) => { 228 if (err.code) { 229 console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err)); 230 return; 231 } 232 windowClass = data; 233 console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data)); 234 235 // 2. Use method 1 to implement the immersive effect. 236 let names = []; 237 windowClass.setWindowSystemBarEnable(names, (err) => { 238 if (err.code) { 239 console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err)); 240 return; 241 } 242 console.info('Succeeded in setting the system bar to be visible.'); 243 }); 244 // 2. Use method 2 to implement the immersive effect. 245 let isLayoutFullScreen = true; 246 windowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err) => { 247 if (err.code) { 248 console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err)); 249 return; 250 } 251 console.info('Succeeded in setting the window layout to full-screen mode.'); 252 }); 253 let sysBarProps = { 254 statusBarColor: '#ff00ff', 255 navigationBarColor: '#00ff00', 256 // The following properties are supported since API version 8. 257 statusBarContentColor: '#ffffff', 258 navigationBarContentColor: '#ffffff' 259 }; 260 windowClass.setWindowSystemBarProperties(sysBarProps, (err) => { 261 if (err.code) { 262 console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err)); 263 return; 264 } 265 console.info('Succeeded in setting the system bar properties.'); 266 }); 267 }) 268 // 3. Load the page content to the immersive window. 269 windowStage.loadContent("pages/page2", (err) => { 270 if (err.code) { 271 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 272 return; 273 } 274 console.info('Succeeded in loading the content.'); 275 }); 276 } 277}; 278``` 279 280## Setting a Floating Window 281 282A floating window is created based on an existing task. It is always displayed in the foreground, even if the task used for creating the floating window is switched to the background. Generally, the floating window is above all application windows. You can create a floating window and set its properties. 283 284 285### How to Develop 286 287**Prerequisites**: To create a floating window (a window of the type **WindowType.TYPE_FLOAT**), you must request the **ohos.permission.SYSTEM_FLOAT_WINDOW** permission. For details, see [Declaring Permissions in the Configuration File](../security/accesstoken-guidelines.md#declaring-permissions-in-the-configuration-file). 288 2891. Create a floating window. 290 291 Call **window.createWindow** to create a floating window. 292 2932. Set properties for the floating window. 294 295 After the floating window is created, you can set its properties, such as the size, position, background color, and brightness. 296 2973. Load content for the floating window and show it. 298 299 Call **setUIContent** and **showWindow** to load and display the content in the floating window. 300 3014. Destroy the floating window. 302 303 When the floating window is no longer needed, you can call **destroyWindow** to destroy it. 304 305```ts 306import UIAbility from '@ohos.app.ability.UIAbility'; 307import window from '@ohos.window'; 308 309export default class EntryAbility extends UIAbility { 310 onWindowStageCreate(windowStage) { 311 // 1. Create a floating window. 312 let windowClass = null; 313 let config = {name: "floatWindow", windowType: window.WindowType.TYPE_FLOAT, ctx: this.context}; 314 window.createWindow(config, (err, data) => { 315 if (err.code) { 316 console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err)); 317 return; 318 } 319 console.info('Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data)); 320 windowClass = data; 321 // 2. Set the position, size, and properties of the floating window. 322 windowClass.moveWindowTo(300, 300, (err) => { 323 if (err.code) { 324 console.error('Failed to move the window. Cause:' + JSON.stringify(err)); 325 return; 326 } 327 console.info('Succeeded in moving the window.'); 328 }); 329 windowClass.resize(500, 500, (err) => { 330 if (err.code) { 331 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 332 return; 333 } 334 console.info('Succeeded in changing the window size.'); 335 }); 336 // 3. Load the page content to the floating window. 337 windowClass.setUIContent("pages/page4", (err) => { 338 if (err.code) { 339 console.error('Failed to load the content. Cause:' + JSON.stringify(err)); 340 return; 341 } 342 console.info('Succeeded in loading the content.'); 343 // 3. Show the floating window. 344 windowClass.showWindow((err) => { 345 if (err.code) { 346 console.error('Failed to show the window. Cause: ' + JSON.stringify(err)); 347 return; 348 } 349 console.info('Succeeded in showing the window.'); 350 }); 351 }); 352 // 4. Destroy the floating window when it is no longer needed (depending on the service logic). 353 windowClass.destroyWindow((err) => { 354 if (err.code) { 355 console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err)); 356 return; 357 } 358 console.info('Succeeded in destroying the window.'); 359 }); 360 }); 361 } 362}; 363``` 364 365