reference material:
https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-window#getwindowproperties9
In the Stage model, typical scenarios for managing application windows include:
- Set the main window properties and target page of the application
- Set application sub window properties and target page
- Immersive ability in the experience window
- Set floating window
- Listening window non interactive and interactive events
The following is an introduction to how to obtain window properties:
Step 1: Get the Window class
getLastWindow(ctx: BaseContext): Promise<Window>
Retrieve the top-level child window within the current application. If there are no application child windows, return to the main application window and use Promise asynchronous callback.
Step 2: Obtain the properties of the current window
getWindowProperties(): WindowProperties
Retrieve the properties of the current window and return WindowProperties.
Explanation of each attribute of WindowProperties
- WindowRect: Window size, which can be obtained during the onPageShow or onForeground stages of the page lifecycle or application lifecycle.
- DrawableRect: The size of the drawable area within the window, where the upper boundary on the left is calculated relative to the window. In the Stage model, this interface needs to be used after calling loadContent() or setUIContent() to load the page content.
- Type: Window type.
- IsFullScreen: Whether it is full screen, default is false. True represents full screen; False indicates non full screen.
- IsLayoutFullScreen: Whether the window is immersive and in full screen mode (not in floating windows, split screens, or other scenes), defaults to false. True means immersive and in full screen mode; False indicates non immersive or non full screen mode.
- Focused: Whether the window can be focused, default is true. True indicates that it can be focused; False means unfocused.
- Touchable: Whether the window is touchable, defaults to true. True means touchable; False means not touchable.
- Brightness: Screen brightness. This parameter is a floating point number, and the adjustable brightness range is [0.0, 1.0]. When taken as 1.0, it represents the maximum brightness value. If the window does not have a brightness value set, it indicates that the brightness follows the system, and the obtained brightness value is -1.
- IsKeepScreenOn: Whether the screen is constantly on, default is false. True indicates constant brightness; False means not frequently lit.
- IsPrivacy Mode: Privacy mode, default to false. True indicates that the mode is enabled; False indicates that the mode is turned off.
- IsTransparent: Whether the window background is transparent. The default is false. True represents transparency; False indicates opacity.
- ID: Window ID, default value is 0, this parameter should be an integer.
- Display ID: The ID of the screen where the window is located, which defaults to the main screen ID. This parameter should be an integer.
Taking the width and height of obtaining window properties as an example, the actual code is as follows:
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct GetWindowPropertiesPage {
@State windowWidth: number = 0
@State windowHeight: number = 0
aboutToAppear(): void {
try {
let context = getContext(this) as common.UIAbilityContext;
let promise = window.getLastWindow(context);
promise.then((data) => {
//获取窗口对象
let windowClass = data;
try {
//获取窗口属性
let properties = windowClass.getWindowProperties();
let rect = properties.windowRect;
//rect.width: 窗口宽度;rect.height: 窗口高度
this.windowWidth = px2vp(rect.width)
this.windowHeight = px2vp(rect.height)
} catch (exception) {
console.error('Failed to obtain the window properties. Cause: ' + JSON.stringify(exception));
}
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
});
} catch (exception) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
}
build() {
Column({ space: 10 }) {
Text('GetWindowProperties Page')
.fontSize(20)
.fontWeight(FontWeight.Bold)
Text(`windowWidth = ${this.windowWidth}`)
Text(`windowHeight = ${this.windowHeight}`)
}
.height('100%')
.width('100%')
}
}
Top comments (0)