feat: entry中使用XComponent指定模块,绘制

This commit is contained in:
🍒cheliangzhao⭐ 2024-01-04 14:50:20 +08:00
parent fc56622ccb
commit b9967dad48
4 changed files with 51 additions and 9 deletions

View File

@ -91,6 +91,15 @@ void PluginRender::RegisterCallback(OH_NativeXComponent *nativeXComponent) {
this->renderCallback_->OnSurfaceCreated = OnSurfaceCreatedCB;
this->renderCallback_->OnSurfaceDestroyed = OnSurfaceDestroyedCB;
// 将 callback 注册给 NativeXComponent
// 将 callback 注册给 NativeXComponent
OH_NativeXComponent_RegisterCallback(nativeXComponent, this->renderCallback_);
// // has bug
// renderCallback_1.DispatchTouchEvent = DispatchTouchEventCB;
// renderCallback_1.OnSurfaceChanged = OnSurfaceChangedCB;
// renderCallback_1.OnSurfaceCreated = OnSurfaceCreatedCB;
// renderCallback_1.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
//
// OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_1);
}

View File

@ -35,6 +35,7 @@ public:
private:
OH_NativeXComponent_Callback *renderCallback_;
OH_NativeXComponent_Callback renderCallback_1;
static std::unordered_map<std::string, PluginRender *> instance_;
};

View File

@ -0,0 +1,4 @@
export interface XComponentContext {
add: (a: number, b: number) => number;
drawPattern: () => void;
}

View File

@ -1,16 +1,44 @@
import hilog from '@ohos.hilog';
import testNapi from 'libentry.so';
import { DrawingDemo } from 'drawing'
import { XComponentContext } from '../interface/XComponentContext';
const TAG = '[Sample_DrawingAPI]'
const TAG = 'DrawingDemo'
@Entry
@Component
struct Index {
export struct Index {
private xComponentAttrs: XComponentAttrs = {
id: 'xcomponentId',
type: XComponentType.SURFACE,
libraryname: 'drawing'
}
@State xComponentContext: XComponentContext | undefined = undefined;
build() {
Column() {
DrawingDemo()
Column({ space: 10 }) {
Row() {
XComponent(this.xComponentAttrs)// .focusable(true)// 可响应键盘事件
.onLoad((xComponentContext: XComponentContext) => {
this.xComponentContext = xComponentContext
const res = xComponentContext.add(2, 3)
hilog.info(0xffff, TAG, `xComponentContext.add: 2 + 3 = ${res}`)
})
.onDestroy(() => {
console.log("onDestroy");
})
.width('100%')
.height('80%')
}
.width('100%')
.justifyContent(FlexAlign.Center)
Row() {
Button('draw')
.onClick(() => {
this.xComponentContext?.drawPattern()
})
}
.width('100%')
.justifyContent(FlexAlign.Center)
}
}
}
@ -19,4 +47,4 @@ interface XComponentAttrs {
id: string;
type: number;
libraryname: string;
}
}