• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 child window (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 child window 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/arkts-apis-window.md).
24
25| Instance        | API                                                      | Description                                                        |
26| -------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
27| Window static method| createWindow(config: Configuration, callback: AsyncCallback\<Window>): void | Creates a child window.<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&lt;void&gt;): 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&lt;void&gt;): void | Moves this window.                                              |
31| Window         | setWindowBrightness(brightness: number, callback: AsyncCallback&lt;void&gt;): void | Sets the brightness for this window.                                            |
32| Window         | resize(width: number, height: number, callback: AsyncCallback&lt;void&gt;): void | Changes the window size.                                          |
33| Window         | setWindowLayoutFullScreen(isLayoutFullScreen: boolean): Promise&lt;void&gt; | Sets whether to enable the full-screen mode for the window layout.                                 |
34| Window         | setWindowSystemBarEnable(names: Array&lt;'status'\|'navigation'&gt;): Promise&lt;void&gt; | Sets whether to display the status bar and navigation bar in this window.                                |
35| Window         | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties): Promise&lt;void&gt; | 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&lt;void&gt;): void | Enables listening for touch events outside this window.                          |
38| Window         | destroyWindow(callback: AsyncCallback&lt;void&gt;): void     | Destroys this window.                                              |
39
40
41## Setting the Child Window of an Application
42
43You can create a child window, such as a dialog box, and set its properties.
44
45> **NOTE**
46>
47> In the following scenarios, you are not advised to use child windows. Instead, consider using the [overlay](../reference/apis-arkui/arkui-ts/ts-universal-attributes-overlay.md) capability of components first.
48> - On mobile devices (tablets in non-freeform mode and phones), child windows cannot extend beyond the boundaries of the main window when it is in floating or split-screen mode, just like components.
49> - In split-screen or freeform window mode, components, when compared with child windows, offer better real-time adaptability to changes in the main window's position and size.
50> - On certain platforms, system configurations may restrict child windows to default system animations and rounded shadows, offering no customization options for applications and thereby limiting their versatility.
51
52### How to Develop
53
541. Create or obtain a child window.
55
56   - Call **window.createWindow** to create a child window.
57   - Call **window.findWindow** to find an available child window.
58
59   ```ts
60   import { window } from '@kit.ArkUI';
61   import { BusinessError } from '@kit.BasicServicesKit';
62
63   let windowClass: window.Window | null = null;
64   // Method 1: Create a child window.
65   let config: window.Configuration = { name: "subWindow", windowType: window.WindowType.TYPE_APP };
66   window.createWindow(config, (err: BusinessError, data) => {
67     let errCode: number = err.code;
68     if (errCode) {
69       console.error(`Failed to create the subWindow. Code:${err.code}, message:${err.message}`);
70       return;
71     }
72     console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data));
73     windowClass = data;
74     if (!windowClass) {
75      console.error('windowClass is null');
76      return;
77     }
78   });
79   // Method 2: Find a child window.
80   try {
81     windowClass = window.findWindow('subWindow');
82   } catch (exception) {
83     console.error('Failed to find the Window. Cause: ' + JSON.stringify(exception));
84   }
85   ```
86
872. Set the properties of the child window.
88
89   After the child window is created, you can set its properties, such as the size, position, background color, and brightness.
90
91   ```ts
92   // Move the child window.
93   windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
94     let errCode: number = err.code;
95     if (errCode) {
96       console.error('Failed to move the window. Cause:' + JSON.stringify(err));
97       return;
98     }
99     console.info('Succeeded in moving the window.');
100   });
101   // Change the size of the child window.
102   windowClass.resize(500, 500, (err: BusinessError) => {
103     let errCode: number = err.code;
104     if (errCode) {
105       console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
106       return;
107     }
108     console.info('Succeeded in changing the window size.');
109   });
110   ```
111
1123. Load content to and show the child window.
113
114   Call **setUIContent** to load content to the child window and **showWindow** to show the child window.
115
116   ```ts
117   // Load content to the child window.
118   windowClass.setUIContent("pages/page2", (err: BusinessError) => {
119     let errCode: number = err.code;
120     if (errCode) {
121       console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
122       return;
123     }
124     console.info('Succeeded in loading the content.');
125     if (!windowClass) {
126       console.error('windowClass is null');
127       return;
128     }
129     // Show the child window.
130     windowClass.showWindow((err: BusinessError) => {
131       let errCode: number = err.code;
132       if (errCode) {
133         console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
134         return;
135       }
136       console.info('Succeeded in showing the window.');
137     });
138   });
139   ```
140
1414. Destroy the child window.
142
143   When the child window is no longer needed, you can call **destroyWindow** to destroy it.
144
145   ```ts
146   // Call destroy() to destroy the child window when it is no longer needed.
147   windowClass.destroyWindow((err: BusinessError) => {
148     let errCode: number = err.code;
149     if (errCode) {
150       console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err));
151       return;
152     }
153     console.info('Succeeded in destroying the subwindow.');
154   });
155   ```
156
157## Experiencing the Immersive Window Feature
158
159To 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.
160
161
162### How to Develop
163
1641. Obtain the main window.
165
166   > **NOTE**
167   >
168   > The immersive window feature can be implemented only after the main window is obtained.
169   >
170   > Ensure that the top window of the application is the main window. You can use **window.getLastWindow** to obtain the main window.
171
172   ```ts
173   import { window } from '@kit.ArkUI';
174   import { BusinessError } from '@kit.BasicServicesKit';
175
176   let mainWindowClass: window.Window | null = null;
177
178   // Obtain the main window.
179   class BaseContext {
180     stageMode: boolean = false;
181   }
182
183   let context: BaseContext = { stageMode: false };
184   window.getLastWindow(context, (err: BusinessError, data) => {
185     let errCode: number = err.code;
186     if (errCode) {
187       console.error('Failed to get the mainWindow. Cause: ' + JSON.stringify(err));
188       return;
189     }
190     console.info('Succeeded in getting mainWindow. Data: ' + JSON.stringify(data));
191     mainWindowClass = data;
192     if (!mainWindowClass) {
193      console.error('mainWindowClass is null');
194      return;
195     }
196   });
197   ```
198
1992. Implement the immersive effect. You can use either of the following methods:
200
201   - Method 1: When the main window of the application is a full-screen window, call **setWindowSystemBarEnable** to hide the status bar and navigation bar.
202   - 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.
203
204   ```ts
205   // Implement the immersive effect by hiding the status bar and navigation bar.
206   let names: Array<'status' | 'navigation'> = [];
207   mainWindowClass.setWindowSystemBarEnable(names)
208    .then(() => {
209      console.info('Succeeded in setting the system bar to be visible.');
210    })
211    .catch((err: BusinessError) => {
212      console.error('Failed to set the system bar to be visible. Cause:' + JSON.stringify(err));
213    });
214   // Implement the immersive effect by setting the properties of the status bar and navigation bar.
215
216   let isLayoutFullScreen: boolean = true;
217   mainWindowClass.setWindowLayoutFullScreen(isLayoutFullScreen)
218    .then(() => {
219      console.info('Succeeded in setting the window layout to full-screen mode.');
220    })
221    .catch((err: BusinessError) => {
222      console.error('Failed to set the window layout to full-screen mode. Cause:' + JSON.stringify(err));
223    });
224   let sysBarProps: window.SystemBarProperties = {
225     statusBarColor: '#ff00ff',
226     navigationBarColor: '#00ff00',
227     // The following properties are supported since API version 8.
228     statusBarContentColor: '#ffffff',
229     navigationBarContentColor: '#ffffff'
230   };
231   mainWindowClass.setWindowSystemBarProperties(sysBarProps)
232    .then(() => {
233      console.info('Succeeded in setting the system bar properties.');
234    })
235    .catch((err: BusinessError) => {
236      console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
237    });
238   ```
239
2403. Load content to and show the immersive window.
241
242   Call **setUIContent** to load content to the immersive window and **showWindow** to show the window.
243
244   ```ts
245   // Load content to the immersive window.
246   mainWindowClass.setUIContent("pages/page3", (err: BusinessError) => {
247     let errCode: number = err.code;
248     if (errCode) {
249       console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
250       return;
251     }
252     console.info('Succeeded in loading the content.');
253     if (!mainWindowClass) {
254      console.error('mainWindowClass is null');
255      return;
256     }
257     // Show the immersive window.
258     mainWindowClass.showWindow((err: BusinessError) => {
259       let errCode: number = err.code;
260       if (errCode) {
261         console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
262         return;
263       }
264       console.info('Succeeded in showing the window.');
265     });
266   });
267   ```
268