The uiExtension module provides APIs for the EmbeddedUIExtensionAbility (or UIExtensionAbility) to obtain the host application window information or the information about the corresponding EmbeddedComponent (or UIExtensionComponent) component.
NOTE
The initial APIs of this module are supported since API version 12. Updates will be marked with a superscript to indicate their earliest API version.
import { uiExtension } from '@kit.ArkUI'
getWindowAvoidArea(type: window.AvoidAreaType): window.AvoidArea
Obtains the area where this window cannot be displayed, for example, the system bar area, notch, gesture area, and soft keyboard area.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | window.AvoidAreaType | Yes | Type of the area. |
Return value
Type | Description |
---|---|
window.AvoidArea | Area where the window cannot be displayed. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Obtain the information about the area where the window cannot be displayed.
let avoidArea: window.AvoidArea | undefined = extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
console.info(`avoidArea: ${JSON.stringify(avoidArea)}`);
}
}
on(type: 'avoidAreaChange', callback: Callback<AvoidAreaInfo>): void
Subscribes to the event indicating changes to the area where the window cannot be displayed.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'avoidAreaChange', indicating the event of changes to the area where the window cannot be displayed. |
callback | Callback<AvoidAreaInfo> | Yes | Callback used to return the area information. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { uiExtension } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Subscribe to the event indicating changes to the area where the window cannot be displayed.
extensionWindow.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => {
console.info(`The avoid area of the host window is: ${JSON.stringify(info.area)}.`);
});
}
}
off(type: 'avoidAreaChange', callback?: Callback<AvoidAreaInfo>): void
Unsubscribes from the event indicating changes to the area where the window cannot be displayed.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'avoidAreaChange', indicating the event of changes to the area where the window cannot be displayed. |
callback | Callback<AvoidAreaInfo> | No | Callback used for unsubscription. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionDestroy(session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Cancel all subscriptions to the event indicating changes to the area where the window cannot be displayed.
extensionWindow.off('avoidAreaChange');
}
}
on(type: 'windowSizeChange', callback: Callback<window.Size>): void
Subscribes to the window size change event of the host application.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'windowSizeChange', indicating the window size change event. |
callback | Callback<window.Size> | Yes | Callback used to return the window size. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Subscribe to the window size change event of the host application.
extensionWindow.on('windowSizeChange', (size: window.Size) => {
console.info(`The avoid area of the host window is: ${JSON.stringify(size)}.`);
});
}
}
off(type: 'windowSizeChange', callback?: Callback<window.Size>): void
Unsubscribes from the window size change event of the host application.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'windowSizeChange', indicating the window size change event. |
callback | Callback<window.Size> | No | Callback used for unsubscription. If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionDestroy(session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Unsubscribe from the window size change event of the host application.
extensionWindow.off('windowSizeChange');
}
}
on(type: 'rectChange', reasons: number, callback: Callback<RectChangeOptions>): void
Subscribes to changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent). This API can be used only on 2-in-1 devices.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'rectChange', indicating the rectangle change event for the component (EmbeddedComponent or UIExtensionComponent). |
reasons | number | Yes | Reason for the position and size change of the component (EmbeddedComponent or UIExtensionComponent). |
callback | Callback<RectChangeOptions> | Yes | Callback used to return the current rectangle change values and the reason for the change of the component (EmbeddedComponent or UIExtensionComponent). |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. |
801 | Capability not supported. Failed to call the API due to limited device capabilities. |
Example:
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { uiExtension } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Subscribe to changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent).
extensionWindow.on('rectChange', uiExtension.RectChangeReason.HOST_WINDOW_RECT_CHANGE, (data: uiExtension.RectChangeOptions) => {
console.info('Succeeded window rect changes. Data: ' + JSON.stringify(data));
});
}
}
off(type: 'rectChange', callback?: Callback<RectChangeOptions>): void
Unsubscribes from changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent). This API can be used only on 2-in-1 devices.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
type | string | Yes | Event type. The value is fixed at 'rectChange', indicating the rectangle change event for the component (EmbeddedComponent or UIExtensionComponent). |
callback | Callback<RectChangeOptions> | No | Callback used to return the current rectangle change values and the reason for the change of the component (EmbeddedComponent or UIExtensionComponent). If a value is passed in, the corresponding subscription is canceled. If no value is passed in, all subscriptions to the specified event are canceled. |
Error codes
For details about the error codes, see Universal Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible cause: 1. Mandatory parameters are left unspecified; 2. Incorrect parameters types; 3. Parameter verification failed. |
801 | Capability not supported. Failed to call the API due to limited device capabilities. |
Example:
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionDestroy(session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Unsubscribe from changes in the position and size of the component (EmbeddedComponent or UIExtensionComponent).
extensionWindow.off('rectChange');
}
}
properties: WindowProxyProperties
Provides information about the host application window and the component (EmbeddedComponent or UIExtensionComponent).
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Name | Type | Description |
---|---|---|
properties | WindowProxyProperties | Information about the host application window and the component (EmbeddedComponent or UIExtensionComponent). |
Example
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Obtain the position and size information of the component (EmbeddedComponent or UIExtensionComponent).
const rect = extensionWindow.properties.uiExtensionHostWindowProxyRect;
console.log(`Rect Info: ${JSON.stringify(rect)}`);
}
}
createSubWindowWithOptions(name: string, subWindowOptions: window.SubWindowOptions): Promise<window.Window>
Creates a subwindow for this window proxy. This API uses a promise to return the result.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 12.
Model restriction: This API can be used only in the stage model.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
name | string | Yes | Name of the subwindow. |
subWindowOptions | window.SubWindowOptions | Yes | Parameters used for creating the subwindow. |
Return value
Type | Description |
---|---|
Promise<window.Window> | Promise used to return the subwindow created. |
Error codes
For details about the error codes, see Universal Error Codes. and Window Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
801 | Capability not supported. Failed to call the API due to limited device capabilities. |
1300002 | This window state is abnormal. |
1300005 | This window proxy is abnormal. |
Example:
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
const subWindowOpts: window.SubWindowOptions = {
title: 'This is a subwindow',
decorEnabled: true
};
// Create a subwindow.
extensionWindow.createSubWindowWithOptions('subWindowForHost', subWindowOpts)
.then((subWindow: window.Window) => {
subWindow.setUIContent('pages/Index', (err, data) =>{
if (err && err.code != 0) {
return;
}
subWindow?.resize(300, 300, (err, data)=>{
if (err && err.code != 0) {
return;
}
subWindow?.moveWindowTo(100, 100, (err, data)=>{
if (err && err.code != 0) {
return;
}
subWindow?.showWindow((err, data) => {
if (err && err.code == 0) {
console.info(`The subwindow has been shown!`);
} else {
console.error(`Failed to show the subwindow!`);
}
});
});
});
});
}).catch((error: BusinessError) => {
console.error(`Create subwindow failed: ${JSON.stringify(error)}`);
})
}
}
occupyEvents(eventFlags: number): Promise<void>
Sets the events that the component (EmbeddedComponent or UIExtensionComponent) will occupy, preventing the host from responding to these events within the component's area.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 16.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
eventFlags | EventFlag | Yes | Type of events to be occupied by the component. |
Return value
Type | Description |
---|---|
Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes. and Window Error Codes.
ID | Error Message |
---|---|
401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2.Incorrect parameters types; 3. Parameter verification failed. |
1300002 | This window state is abnormal. |
1300003 | This window manager service works abnormally. |
Example:
// ExtensionProvider.ts
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Occupy events.
try {
let promise = extensionWindow.occupyEvents(uiExtension.EventFlag.EVENT_CLICK | uiExtension.EventFlag.EVENT_LONG_PRESS);
promise.then(() => {
console.info('Succeeded in occupy events');
}).catch((err: BusinessError) => {
console.error(`Failed to occupy events. Cause code: ${err.code}, message: ${err.message}`);
});
} catch (e) {
console.error(`Occupy events got exception`);
}
}
}
Enumerates event types.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Value | Description |
---|---|---|
EVENT_NONE | 0x00000000 | None. |
EVENT_PAN_GESTURE_LEFT | 0x00000001 | Swipe left event. |
EVENT_PAN_GESTURE_RIGHT | 0x00000002 | Swipe right event. |
EVENT_PAN_GESTURE_UP | 0x00000004 | Swipe up event. |
EVENT_PAN_GESTURE_DOWN | 0x00000008 | Swipe down event. |
EVENT_CLICK | 0x00000100 | Click event. |
EVENT_LONG_PRESS | 0x00000200 | Long press event. |
Describes the information about the area where the window cannot be displayed.
Atomic service API: This API can be used in atomic services since API version 12.
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Mandatory | Description |
---|---|---|---|
type | window.AvoidAreaType | Yes | Type of the area where the window cannot be displayed. |
area | window.AvoidArea | Yes | Area where the window cannot be displayed. |
Provides information about the host application window and component (EmbeddedComponent or UIExtensionComponent).
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Name | Type | Mandatory | Description |
---|---|---|---|
uiExtensionHostWindowProxyRect | window.Rect | Yes | Position and size of the component (EmbeddedComponent or UIExtensionComponent). |
Enumerates the reasons for changes in the rectangle (position and size) of the component (EmbeddedComponent or UIExtensionComponent).
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Name | Value | Description |
---|---|---|
HOST_WINDOW_RECT_CHANGE | 1 | The rectangle of the host window containing the component changes. |
Provides the values and reasons returned when the rectangle (position and size) of the component (EmbeddedComponent or UIExtensionComponent) changes.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 14.
Name | Type | Readable | Writable | Description |
---|---|---|---|---|
rect | window.Rect | Yes | Yes | New values of the rectangle of the component after the change. |
reason | RectChangeReason | Yes | Yes | Reason for the rectangle change. |
This example shows how to use all the available APIs in the EmbeddedUIExtensionAbility. The bundle name of the sample application is com.example.embeddeddemo, and the EmbeddedUIExtensionAbility to start is ExampleEmbeddedAbility.
The EntryAbility (UIAbility) of the sample application loads the pages/Index.ets file, whose content is as follows:
// The UIAbility loads pages/Index.ets when started.
import { Want } from '@kit.AbilityKit';
@Entry
@Component
struct Index {
@State message: string = 'Message: '
private want: Want = {
bundleName: "com.example.embeddeddemo",
abilityName: "ExampleEmbeddedAbility",
}
build() {
Row() {
Column() {
Text(this.message).fontSize(30)
EmbeddedComponent(this.want, EmbeddedType.EMBEDDED_UI_EXTENSION)
.width('100%')
.height('90%')
.onTerminated((info)=>{
this.message = 'Termination: code = ' + info.code + ', want = ' + JSON.stringify(info.want);
})
.onError((error)=>{
this.message = 'Error: code = ' + error.code;
})
}
.width('100%')
}
.height('100%')
}
}
The EmbeddedUIExtensionAbility to start by the EmbeddedComponent is implemented in the ets/extensionAbility/ExampleEmbeddedAbility file. The file content is as follows:
import { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit';
const TAG: string = '[ExampleEmbeddedAbility]'
export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility {
onCreate() {
console.info(TAG, `onCreate`);
}
onForeground() {
console.info(TAG, `onForeground`);
}
onBackground() {
console.info(TAG, `onBackground`);
}
onDestroy() {
console.info(TAG, `onDestroy`);
}
onSessionCreate(want: Want, session: UIExtensionContentSession) {
console.info(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`);
let param: Record<string, UIExtensionContentSession> = {
'session': session
};
let storage: LocalStorage = new LocalStorage(param);
session.loadContent('pages/extension', storage);
}
}
The entry page file of the EmbeddedUIExtensionAbility is pages/extension.ets, whose content is as follows:
import { UIExtensionContentSession } from '@kit.AbilityKit';
import { uiExtension, window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
let storage = LocalStorage.getShared()
@Entry(storage)
@Component
struct Extension {
@State message: string = 'EmbeddedUIExtensionAbility Index';
private session: UIExtensionContentSession | undefined = storage.get<UIExtensionContentSession>('session');
private extensionWindow: uiExtension.WindowProxy | undefined = this.session?.getUIExtensionWindowProxy();
private subWindow: window.Window | undefined = undefined;
aboutToAppear(): void {
this.extensionWindow?.on('windowSizeChange', (size: window.Size) => {
console.info(`size = ${JSON.stringify(size)}`);
});
this.extensionWindow?.on('rectChange', uiExtension.RectChangeReason.HOST_WINDOW_RECT_CHANGE, (data: uiExtension.RectChangeOptions) => {
console.info('Succeeded window rect changes. Data: ' + JSON.stringify(data));
});
this.extensionWindow?.on('avoidAreaChange', (info: uiExtension.AvoidAreaInfo) => {
console.info(`type = ${JSON.stringify(info.type)}, area = ${JSON.stringify(info.area)}`);
});
}
aboutToDisappear(): void {
this.extensionWindow?.off('windowSizeChange');
this.extensionWindow?.off('rectChange');
this.extensionWindow?.off('avoidAreaChange');
}
build() {
Column() {
Text(this.message)
.fontSize(20)
.fontWeight(FontWeight.Bold)
Button("Obtain Component Size").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => {
let rect = this.extensionWindow?.properties.uiExtensionHostWindowProxyRect;
console.info(`EmbeddedComponent position and size: ${JSON.stringify(rect)}`);
})
Button("Obtain Avoid Area Info").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => {
let avoidArea: window.AvoidArea | undefined = this.extensionWindow?.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
console.info(`System avoid area: ${JSON.stringify(avoidArea)}`);
})
Button("Create Subwindow").width('90%').margin({top: 5, bottom: 5}).fontSize(16).onClick(() => {
let subWindowOpts: window.SubWindowOptions = {
'title': 'This is a subwindow',
decorEnabled: true
};
this.extensionWindow?.createSubWindowWithOptions('subWindowForHost', subWindowOpts)
.then((subWindow: window.Window) => {
this.subWindow = subWindow;
this.subWindow.loadContent('pages/Index', storage, (err, data) =>{
if (err && err.code != 0) {
return;
}
this.subWindow?.resize(300, 300, (err, data)=>{
if (err && err.code != 0) {
return;
}
this.subWindow?.moveWindowTo(100, 100, (err, data)=>{
if (err && err.code != 0) {
return;
}
this.subWindow?.showWindow((err, data) => {
if (err && err.code == 0) {
console.info(`The subwindow has been shown!`);
} else {
console.error(`Failed to show the subwindow!`);
}
});
});
});
});
}).catch((error: BusinessError) => {
console.error(`Create subwindow failed: ${JSON.stringify(error)}`);
})
})
}.width('100%').height('100%')
}
}
Add an item to extensionAbilities in the module.json5 file of the sample application. The details are as follows:
{
"name": "ExampleEmbeddedAbility",
"srcEntry": "./ets/extensionAbility/ExampleEmbeddedAbility.ets",
"type": "embeddedUI"
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。