harmony 鸿蒙@ohos.arkui.uiExtension (uiExtension)
@ohos.arkui.uiExtension (uiExtension)
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.
Modules to Import
import { uiExtension } from '@kit.ArkUI'
WindowProxy
Properties
System capability: SystemCapability.ArkUI.ArkUI.Full
Name | Type | Read Only | Optional | Description |
---|---|---|---|---|
properties14+ | WindowProxyProperties | No | No | Information about the component (EmbeddedComponent or UIExtensionComponent). Atomic service API: This API can be used in atomic services since API version 14. NOTE Due to architecture restrictions, avoid obtaining the value in onSessionCreate. Instead, when possible, obtain the value after receiving the on(‘windowSizeChange’) callback. |
getWindowAvoidArea
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(‘avoidAreaChange’)
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(‘avoidAreaChange’)
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(‘windowSizeChange’)
on(type: ‘windowSizeChange’, callback: Callback
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(‘windowSizeChange’)
off(type: ‘windowSizeChange’, callback?: Callback
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(‘rectChange’)14+
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(‘rectChange’)14+
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');
}
}
createSubWindowWithOptions
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)}`);
})
}
}
occupyEvents18+
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 18.
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
eventFlags | number | Yes | Type of events to occupy. For details about the available values, see EventFlag. |
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 cause: 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 { uiExtension } from '@kit.ArkUI';
export default class EntryAbility extends EmbeddedUIExtensionAbility {
onSessionCreate(want: Want, session: UIExtensionContentSession) {
const extensionWindow = session.getUIExtensionWindowProxy();
// Occupy events.
setTimeout(() => {
try {
extensionWindow.occupyEvents(uiExtension.EventFlag.EVENT_CLICK|uiExtension.EventFlag.EVENT_LONG_PRESS);
} catch (e) {
console.error(`Occupy events got exception code: ${e.code}, message: ${e.message}`);
}
}, 500);
}
}
EventFlag18+
Enumerates event types.
System capability: SystemCapability.ArkUI.ArkUI.Full
Atomic service API: This API can be used in atomic services since API version 18.
Name | Value | Description |
---|---|---|
EVENT_NONE | 0x00000000 | No event. |
EVENT_PAN_GESTURE_LEFT | 0x00000001 | Pan-left event. |
EVENT_PAN_GESTURE_RIGHT | 0x00000002 | Pan-right event. |
EVENT_PAN_GESTURE_UP | 0x00000004 | Pan-up event. |
EVENT_PAN_GESTURE_DOWN | 0x00000008 | Pan-down event. |
EVENT_CLICK | 0x00000100 | Click event. |
EVENT_LONG_PRESS | 0x00000200 | Long press event. |
AvoidAreaInfo
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. |
WindowProxyProperties14+
Provides information about a component.
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). |
RectChangeReason14+
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 | 0x0001 | The rectangle of the host window containing the component changes. |
RectChangeOptions14+
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. |
Example
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:
json { "name": "ExampleEmbeddedAbility", "srcEntry": "./ets/extensionAbility/ExampleEmbeddedAbility.ets", "type": "embeddedUI" }
你可能感兴趣的鸿蒙文章
harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent
harmony 鸿蒙ARKUI_TextPickerRangeContent
harmony 鸿蒙ArkUI_AnimateCompleteCallback
harmony 鸿蒙ArkUI_ContextCallback
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦