harmony 鸿蒙Application Window Development (FA Model)
Application Window Development (FA Model)
Basic Concepts
Immersive window: System windows such as the status bar and navigation bar are controlled in such a way that they are unobtrusive and natural looking in the entire screen, thereby engaging users more deeply with the content displayed.
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).
When to Use
In the FA model, you can perform the following operations during application window development:
Setting the properties and content of the subwindow of an application
Experiencing the immersive window feature
Available APIs
The table below lists the common APIs used for application window development. For details about more APIs, see Window.
Instance | API | Description |
---|---|---|
Window static method | createWindow(config: Configuration, callback: AsyncCallback<Window>): void | Creates a subwindow. config specifies the parameters used for creating the window. |
Window static method | findWindow(name: string): Window | Finds a window based on the name. |
Window | setUIContent(path: string, callback: AsyncCallback<void>): void | Loads the page content to this window. |
Window | moveWindowTo(x: number, y: number, callback: AsyncCallback<void>): void | Moves this window. |
Window | setWindowBackgroundColor(color: string, callback: AsyncCallback<void>): void | Sets the background color for this window. |
Window | setWindowBrightness(brightness: number, callback: AsyncCallback<void>): void | Sets the brightness for this window. |
Window | resize(width: number, height: number, callback: AsyncCallback<void>): void | Changes the window size. |
Window | setWindowLayoutFullScreen(isLayoutFullScreen: boolean, callback: AsyncCallback<void>): void | Sets whether to enable the full-screen mode for the window layout. |
Window | setWindowSystemBarEnable(names: Array<‘status’|‘navigation’>): Promise<void> | Sets whether to display the status bar and navigation bar in this window. |
Window | setWindowSystemBarProperties(systemBarProperties: SystemBarProperties, callback: AsyncCallback<void>): void | Sets the properties of the status bar and navigation bar in this window. systemBarProperties: properties of the status bar and navigation bar. |
Window | showWindow(callback: AsyncCallback<void>): void | Shows this window. |
Window | on(type: ‘touchOutside’, callback: Callback<void>): void | Enables listening for click events outside this window. |
Window | destroyWindow(callback: AsyncCallback<void>): void | Destroys this window. |
Setting the Subwindow of an Application
You can create a subwindow, such as a dialog box, and set its properties.
How to Develop
Create or obtain a subwindow.
- Call window.createWindow to create a subwindow.
- Call window.findWindow to find an available subwindow.
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
let windowClass: window.Window|null = null;
// Method 1: Create a subwindow.
let config: window.Configuration = { name: "subWindow", windowType: window.WindowType.TYPE_APP };
window.createWindow(config, (err: BusinessError, data) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to create the subWindow. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in creating subWindow. Data: ' + JSON.stringify(data));
windowClass = data;
});
// Method 2: Find a subwindow.
try {
windowClass = window.findWindow('subWindow');
} catch (exception) {
console.error('Failed to find the Window. Cause: ' + JSON.stringify(exception));
}
- Set the properties of the subwindow.
After the subwindow is created, you can set its properties, such as the size, position, background color, and brightness.
// Move the subwindow.
let windowClass: window.Window = window.findWindow("test");
windowClass.moveWindowTo(300, 300, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to move the window. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in moving the window.');
});
// Change the size of the subwindow.
windowClass.resize(500, 500, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in changing the window size.');
});
- Load content for the subwindow and show it.
Call setUIContent and showWindow to load and display the content in the subwindow.
// Load the page content to the subwindow.
let windowClass: window.Window = window.findWindow("test");
windowClass.setUIContent("pages/page2", (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// Show the subwindow.
windowClass.showWindow((err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in showing the window.');
});
});
- Destroy the subwindow.
When the subwindow is no longer needed, you can call destroyWindow to destroy it.
// Call destroy() to destroy the subwindow when it is no longer needed.
let windowClass: window.Window = window.findWindow("test");
windowClass.destroyWindow((err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to destroy the subwindow. Cause:' + JSON.stringify(err));
return;
}
console.info('Succeeded in destroying the subwindow.');
});
Experiencing the Immersive Window Feature
To 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. 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, the component module controls the immersive full-screen layout of the status bar and navigation bar. If false is passed in, the component module controls the non-immersive full-screen layout of the status bar and navigation bar.
How to Develop
- Obtain the main window.
NOTE
The immersive window feature can be implemented only after the main window is obtained.
Ensure that the top window of the application is the main window. You can use window.getLastWindow to obtain the main window.
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
let mainWindowClass: window.Window|null = null;
// Obtain the main window.
class BaseContext {
stageMode: boolean = false;
}
let context: BaseContext = { stageMode: false };
window.getLastWindow(context, (err: BusinessError, data) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to get the subWindow. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in getting subWindow. Data: ' + JSON.stringify(data));
mainWindowClass = data;
});
Implement the immersive effect. You can use either of the following methods:
- Method 1: When the main window of the application is a full-screen window, call setWindowSystemBarEnable to hide the status bar and navigation bar.
- 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.
// Use method 1 to implement the immersive effect.
let names: Array<'status'|'navigation'> = [];
let mainWindowClass: window.Window = window.findWindow("test");
mainWindowClass.setWindowSystemBarEnable(names, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
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.');
});
// Use method 2 to implement the immersive effect.
let isLayoutFullScreen: boolean = true;
mainWindowClass.setWindowLayoutFullScreen(isLayoutFullScreen, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
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.');
});
let sysBarProps: window.SystemBarProperties = {
statusBarColor: '#ff00ff',
navigationBarColor: '#00ff00',
// The following properties are supported since API version 8.
statusBarContentColor: '#ffffff',
navigationBarContentColor: '#ffffff'
};
mainWindowClass.setWindowSystemBarProperties(sysBarProps, (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to set the system bar properties. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the system bar properties.');
});
- Load content for the immersive window and show it.
Call setUIContent and showWindow to load and display the content in the immersive window.
// Load the page content to the immersive window.
let mainWindowClass: window.Window = window.findWindow("test");
mainWindowClass.setUIContent("pages/page3", (err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to load the content. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in loading the content.');
// Show the immersive window.
mainWindowClass.showWindow((err: BusinessError) => {
let errCode: number = err.code;
if (errCode) {
console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in showing the window.');
});
});
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Application Window Development (Stage Mode)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦