From b9967dad4866d6876a8f09ae3e7fc61c1d1a2c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=8D=92cheliangzhao=E2=AD=90?= Date: Thu, 4 Jan 2024 14:50:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20entry=E4=B8=AD=E4=BD=BF=E7=94=A8XCompon?= =?UTF-8?q?ent=E6=8C=87=E5=AE=9A=E6=A8=A1=E5=9D=97=EF=BC=8C=E7=BB=98?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drawing/src/main/cpp/plugin/plugin_render.cpp | 11 ++++- drawing/src/main/cpp/plugin/plugin_render.h | 1 + .../main/ets/interface/XComponentContext.ets | 4 ++ entry/src/main/ets/pages/Index.ets | 44 +++++++++++++++---- 4 files changed, 51 insertions(+), 9 deletions(-) create mode 100644 entry/src/main/ets/interface/XComponentContext.ets diff --git a/drawing/src/main/cpp/plugin/plugin_render.cpp b/drawing/src/main/cpp/plugin/plugin_render.cpp index c92d9f6..e2879b7 100644 --- a/drawing/src/main/cpp/plugin/plugin_render.cpp +++ b/drawing/src/main/cpp/plugin/plugin_render.cpp @@ -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); } diff --git a/drawing/src/main/cpp/plugin/plugin_render.h b/drawing/src/main/cpp/plugin/plugin_render.h index fbe1a98..0a94144 100644 --- a/drawing/src/main/cpp/plugin/plugin_render.h +++ b/drawing/src/main/cpp/plugin/plugin_render.h @@ -35,6 +35,7 @@ public: private: OH_NativeXComponent_Callback *renderCallback_; + OH_NativeXComponent_Callback renderCallback_1; static std::unordered_map instance_; }; diff --git a/entry/src/main/ets/interface/XComponentContext.ets b/entry/src/main/ets/interface/XComponentContext.ets new file mode 100644 index 0000000..fce9680 --- /dev/null +++ b/entry/src/main/ets/interface/XComponentContext.ets @@ -0,0 +1,4 @@ +export interface XComponentContext { + add: (a: number, b: number) => number; + drawPattern: () => void; +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 3c6b446..9c20859 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -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; -} \ No newline at end of file +}