From dec89d9d7dc9ca7bb1795c5cbfc9e41f8c543e95 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Tue, 26 Dec 2023 10:31:11 +0800 Subject: [PATCH 1/9] =?UTF-8?q?=E6=95=B4=E7=90=86flutterview.ets=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../ets/embedding/ohos/FlutterAbility.ets | 54 ++++++++---------- .../flutter/src/main/ets/view/FlutterView.ets | 56 +++++++++++++++++++ 2 files changed, 80 insertions(+), 30 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index ee499033a1..d194059018 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -37,33 +37,16 @@ import { DVModel, DVModelChildren, DVModelEvents, - DVModelParameters } from '../../view/DynamicView/dynamicView'; + DVModelParameters +} from '../../view/DynamicView/dynamicView'; import StringUtils from '../../util/StringUtils'; import List from '@ohos.util.List'; import ExclusiveAppComponent from './ExclusiveAppComponent'; +import { FlutterView } from '../../view/FlutterView'; const TAG = "FlutterAbility"; const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'; -@Component -struct XComponentStruct { - private context:ESObject; - dvModelParams: DVModelParameters = new DVModelParameters(); - - build() { - XComponent({ id: (this.dvModelParams as Record)["xComponentId"], type: 'texture', libraryname: 'flutter'}) - .onLoad((context) => { - this.context = context; - }) - .onDestroy(() => { - }) - } -} - -@Builder function BuildXComponentStruct(buildParams: BuilderParams) { - XComponentStruct({dvModelParams: buildParams.params}); -} - /** * flutter ohos基础ability,请在让主ability继承自该类。 * 该类主要职责: @@ -76,6 +59,7 @@ export class FlutterAbility extends UIAbility implements Host { private mainWindow?: window.Window; private viewportMetrics = new ViewportMetrics(); private displayInfo?: display.Display; + private flutterView?: FlutterView /** * onCreate @@ -171,12 +155,22 @@ export class FlutterAbility extends UIAbility implements Host { loadContent() { if (this.windowStage != null && this.stillAttachedForEvent("loadContent")) { Log.i(TAG, 'loadContent'); - let params: DVModelParameters = new DVModelParameters(); - (params as Record)["xComponentId"] = - this.delegate?.getFlutterNapi()?.nativeShellHolderId?.toString(); - let xComponentModel: DVModel = - new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); - RootDvModeManager.addDvModel(xComponentModel); + // let params: DVModelParameters = new DVModelParameters(); + // (params as Record)["xComponentId"] = + // this.delegate?.getFlutterNapi()?.nativeShellHolderId?.toString(); + // let xComponentModel: DVModel = + // new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); + // RootDvModeManager.addDvModel(xComponentModel); + + if (!this.flutterView) { + this.flutterView = new FlutterView(); + } + const engine = this.getFlutterEngine(); + if (engine) { + this.flutterView.attachToFlutterEngine(engine); + } else { + throw new Error('this.getFlutterEngine() is null!'); + } this.windowStage?.loadContent('pages/Index', (err, data) => { if (err.code) { @@ -324,7 +318,7 @@ export class FlutterAbility extends UIAbility implements Host { return true; } - shouldRestoreAndSaveState(): boolean{ + shouldRestoreAndSaveState(): boolean { if (this.launchWant.parameters![FlutterAbilityLaunchConfigs.EXTRA_CACHED_ENGINE_ID] != undefined) { return this.launchWant.parameters![FlutterAbilityLaunchConfigs.EXTRA_CACHED_ENGINE_ID] as boolean; } @@ -335,7 +329,7 @@ export class FlutterAbility extends UIAbility implements Host { return true; } - getPlugins(): List{ + getPlugins(): List { // Subclasses should override this function. return new List() } @@ -380,7 +374,7 @@ export class FlutterAbility extends UIAbility implements Host { } } - private onWindowPropertiesUpdated(){ + private onWindowPropertiesUpdated() { if (this.delegate == null || !this.delegate.isAttached) { return; } @@ -416,7 +410,7 @@ export class FlutterAbility extends UIAbility implements Host { } } - onConfigurationUpdated(config: Configuration){ + onConfigurationUpdated(config: Configuration) { Log.i(TAG, 'onConfigurationUpdated config:' + JSON.stringify(config)); this?.delegate?.flutterEngine?.getSettingsChannel()?.startMessage() .setAlwaysUse24HourFormat(I18n.System.is24HourClock()) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index cf38a74159..4f0f92afc6 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -12,7 +12,63 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import util from '@ohos.util'; +import FlutterEngine from '../embedding/engine/FlutterEngine'; +import { RootDvModeManager } from '../plugin/platform/RootDvModelManager'; +import { BuilderParams, DVModel, DVModelChildren, DVModelEvents, DVModelParameters } from './DynamicView/dynamicView'; + + +@Component +struct XComponentStruct { + private context: ESObject; + dvModelParams: DVModelParameters = new DVModelParameters(); + + build() { + XComponent({ + id: (this.dvModelParams as Record)["xComponentId"], + type: 'texture', + libraryname: 'flutter' + }) + .onLoad((context) => { + this.context = context; + }) + .onDestroy(() => { + }) + } +} + +@Builder +function BuildXComponentStruct(buildParams: BuilderParams) { + XComponentStruct({ dvModelParams: buildParams.params }); +} export class FlutterView { + private xcomponentId: number + private flutterEngine?: FlutterEngine | null + + constructor() { + this.xcomponentId = Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER + 1)); + } + + public getUUID(): number { + return this.xcomponentId; + } + + public getFlutterEngine(): FlutterEngine | null { + return this.flutterEngine ? this.flutterEngine : null; + } + + public attachToFlutterEngine(flutterEngine: FlutterEngine) { + this.flutterEngine = flutterEngine; + + let params: DVModelParameters = new DVModelParameters(); + (params as Record)["xComponentId"] = this.xcomponentId.toString() + let xComponentModel: DVModel = + new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); + RootDvModeManager.addDvModel(xComponentModel); + } + public detachFromFlutterEngine() { + this.flutterEngine = null + } } \ No newline at end of file -- Gitee From d2b6cb36ee078ed5d46e9d92076e55c476aa5db8 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Tue, 26 Dec 2023 17:24:08 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0napi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../main/ets/embedding/engine/FlutterNapi.ets | 16 ++++ .../ets/embedding/ohos/FlutterAbility.ets | 6 +- .../flutter/src/main/ets/view/FlutterView.ets | 21 ++++- shell/platform/ohos/library_loader.cpp | 8 ++ .../ohos/napi/platform_view_ohos_napi.cpp | 81 +++++++++++++++++++ .../ohos/napi/platform_view_ohos_napi.h | 6 ++ .../platform/ohos/ohos_xcomponent_adapter.cpp | 66 ++++++++++++--- shell/platform/ohos/ohos_xcomponent_adapter.h | 7 ++ 8 files changed, 195 insertions(+), 16 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets index 11f1b234fb..6a5b75e4bf 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/FlutterNapi.ets @@ -353,6 +353,22 @@ export default class FlutterNapi { } flutter.nativeGetSystemLanguages(this.nativeShellHolderId!, systemLanguages); } + + /** + * xcomponet绑定flutterEngine + * @param xcomponentId + */ + xComponentAttachFlutterEngine(xcomponentId: number) { + flutter.nativeXComponentAttachFlutterEngine(xcomponentId, this.nativeShellHolderId!); + } + + /** + * xcomponet解除绑定flutterEngine + * @param xcomponentId + */ + xComponentDetachFlutterEngine(xcomponentId: number) { + flutter.nativeXComponentDetachFlutterEngine(xcomponentId, this.nativeShellHolderId!); + } } export interface AccessibilityDelegate { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index d194059018..5ecc0e9eeb 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -163,11 +163,13 @@ export class FlutterAbility extends UIAbility implements Host { // RootDvModeManager.addDvModel(xComponentModel); if (!this.flutterView) { - this.flutterView = new FlutterView(); + if (this.delegate?.getFlutterNapi()) { + this.flutterView = new FlutterView(this.delegate?.getFlutterNapi()!); + } } const engine = this.getFlutterEngine(); if (engine) { - this.flutterView.attachToFlutterEngine(engine); + this.flutterView?.attachToFlutterEngine(engine); } else { throw new Error('this.getFlutterEngine() is null!'); } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 4f0f92afc6..46abef268a 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -14,6 +14,7 @@ */ import util from '@ohos.util'; import FlutterEngine from '../embedding/engine/FlutterEngine'; +import FlutterNapi from '../embedding/engine/FlutterNapi'; import { RootDvModeManager } from '../plugin/platform/RootDvModelManager'; import { BuilderParams, DVModel, DVModelChildren, DVModelEvents, DVModelParameters } from './DynamicView/dynamicView'; @@ -45,16 +46,20 @@ function BuildXComponentStruct(buildParams: BuilderParams) { export class FlutterView { private xcomponentId: number private flutterEngine?: FlutterEngine | null + private flutterNapi: FlutterNapi + private isAttach: boolean - constructor() { + constructor(flutterNapi: FlutterNapi) { + this.flutterNapi = flutterNapi + this.isAttach = false this.xcomponentId = Math.floor(Math.random() * (Number.MAX_SAFE_INTEGER + 1)); } - public getUUID(): number { + public getXComponentId(): number { return this.xcomponentId; } - public getFlutterEngine(): FlutterEngine | null { + public getAttachedFlutterEngine(): FlutterEngine | null { return this.flutterEngine ? this.flutterEngine : null; } @@ -66,9 +71,19 @@ export class FlutterView { let xComponentModel: DVModel = new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); RootDvModeManager.addDvModel(xComponentModel); + + //xcomponent绑定engine + this.flutterNapi.xComponentAttachFlutterEngine(this.xcomponentId); + + this.isAttach = true; + } + + public isAttachedToFlutterEngine(): boolean { + return this.isAttach; } public detachFromFlutterEngine() { this.flutterEngine = null + this.isAttach = false } } \ No newline at end of file diff --git a/shell/platform/ohos/library_loader.cpp b/shell/platform/ohos/library_loader.cpp index 7f88815b7b..bad3e98f76 100644 --- a/shell/platform/ohos/library_loader.cpp +++ b/shell/platform/ohos/library_loader.cpp @@ -102,6 +102,14 @@ static napi_value Init(napi_env env, napi_value exports) { "nativeGetSystemLanguages", flutter::PlatformViewOHOSNapi::nativeGetSystemLanguages), + DECLARE_NAPI_FUNCTION( + "nativeXComponentAttachFlutterEngine", + flutter::PlatformViewOHOSNapi::nativeXComponentAttachFlutterEngine), + + DECLARE_NAPI_FUNCTION( + "nativeXComponentDetachFlutterEngine", + flutter::PlatformViewOHOSNapi::nativeXComponentDetachFlutterEngine), + }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); bool ret = flutter::XComponentAdapter::GetInstance()->Export(env, exports); diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp index 3da5999832..e4ea8ffc41 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.cpp +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.cpp @@ -25,6 +25,7 @@ #include "flutter/shell/platform/ohos/ohos_main.h" #include "flutter/shell/platform/ohos/ohos_shell_holder.h" #include "flutter/shell/platform/ohos/surface/ohos_native_window.h" +#include "flutter/shell/platform/ohos/ohos_xcomponent_adapter.h" #include "unicode/uchar.h" #define OHOS_SHELL_HOLDER (reinterpret_cast(shell_holder)) @@ -1418,6 +1419,86 @@ napi_value PlatformViewOHOSNapi::nativeGetSystemLanguages( return nullptr; } +/** + * @brief xcomponent与flutter引擎绑定 + * @note + * @param nativeShellHolderId: number + * @param xcomponentId: number + * @return void + */ +napi_value PlatformViewOHOSNapi::nativeXComponentAttachFlutterEngine( + napi_env env, + napi_callback_info info){ + napi_status ret; + size_t argc = 2; + napi_value args[2] = {nullptr}; + int64_t xcomponent_id; + int64_t shell_holder; + ret = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine napi_get_cb_info error:" + << ret; + return nullptr; + } + ret = napi_get_value_int64(env, args[0], &xcomponent_id); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine xcomponent_id napi_get_value_int64 error"; + return nullptr; + } + ret = napi_get_value_int64(env, args[1], &shell_holder); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine shell_holder napi_get_value_int64 error"; + return nullptr; + } + std::string xcomponent_id_str = std::to_string(xcomponent_id); + std::string shell_holder_str = std::to_string(shell_holder); + + LOGD("nativeXComponentAttachFlutterEngine xcomponent_id: %{public}ld , shell_holder: %{public}ld ", + xcomponent_id, shell_holder); + + XComponentAdapter::GetInstance()->AttachFlutterEngine(xcomponent_id_str, + shell_holder_str); + return nullptr; +} +/** + * @brief xcomponent解除flutter引擎绑定 + * @note + * @param nativeShellHolderId: number + * @param xcomponentId: number + * @return napi_value + */ +napi_value PlatformViewOHOSNapi::nativeXComponentDetachFlutterEngine( + napi_env env, + napi_callback_info info){ + napi_status ret; + size_t argc = 2; + napi_value args[2] = {nullptr}; + int64_t xcomponent_id; + int64_t shell_holder; + ret = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine napi_get_cb_info error:" + << ret; + return nullptr; + } + ret = napi_get_value_int64(env, args[0], &xcomponent_id); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine xcomponent_id napi_get_value_int64 error"; + return nullptr; + } + ret = napi_get_value_int64(env, args[1], &shell_holder); + if (ret != napi_ok) { + FML_DLOG(ERROR) << "nativeXComponentAttachFlutterEngine shell_holder napi_get_value_int64 error"; + return nullptr; + } + std::string xcomponent_id_str = std::to_string(xcomponent_id); + + LOGD("nativeXComponentDetachFlutterEngine xcomponent_id: %{public}ld", + xcomponent_id); + XComponentAdapter::GetInstance()->DetachFlutterEngine(xcomponent_id_str); + return nullptr; +} + int64_t PlatformViewOHOSNapi::GetShellHolder() { return PlatformViewOHOSNapi::shell_holder_value; } diff --git a/shell/platform/ohos/napi/platform_view_ohos_napi.h b/shell/platform/ohos/napi/platform_view_ohos_napi.h index d32a6fe996..2bf7d6eaaf 100644 --- a/shell/platform/ohos/napi/platform_view_ohos_napi.h +++ b/shell/platform/ohos/napi/platform_view_ohos_napi.h @@ -142,6 +142,12 @@ class PlatformViewOHOSNapi { static napi_value nativeGetSystemLanguages( napi_env env, napi_callback_info info); // 应用下发系统语言设置 + static napi_value nativeXComponentAttachFlutterEngine( + napi_env env, + napi_callback_info info); + static napi_value nativeXComponentDetachFlutterEngine( + napi_env env, + napi_callback_info info); // Surface相关,XComponent调用 static void SurfaceCreated(int64_t shell_holder, void* window); diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 29e820b67d..e6fa8813f2 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -76,6 +76,21 @@ void XComponentAdapter::SetNativeXComponent( } } +void XComponentAdapter::AttachFlutterEngine(std::string& id, + std::string& shellholderId) { + auto iter = xcomponetMap_.find(id); + if (iter != xcomponetMap_.end()) { + iter->second->AttachFlutterEngine(shellholderId); + } +} + +void XComponentAdapter::DetachFlutterEngine(std::string& id) { + auto iter = xcomponetMap_.find(id); + if (iter != xcomponetMap_.end()) { + iter->second->DetachFlutterEngine(); + } +} + #include using OHOS_SurfaceBufferUsage = enum { BUFFER_USAGE_CPU_READ = (1ULL << 0), /**< CPU read buffer */ @@ -213,10 +228,11 @@ void XComponentBase::BindXComponentCallback() { callback_.DispatchTouchEvent = DispatchTouchEventCB; } -XComponentBase::XComponentBase(std::string id,OH_NativeXComponent* xcomponet) { +XComponentBase::XComponentBase(std::string id, OH_NativeXComponent* xcomponet) { nativeXComponent_ = xcomponet; if (nativeXComponent_ != nullptr) { id_ = id; + isAttached_ = false; BindXComponentCallback(); OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); } @@ -225,10 +241,18 @@ XComponentBase::XComponentBase(std::string id,OH_NativeXComponent* xcomponet) { XComponentBase::~XComponentBase() { +void XComponentBase::AttachFlutterEngine(std::string shellholderId) { + shellholderId_ = shellholderId; + isAttached_ = true; } -void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, void* window) -{ +void XComponentBase::DetachFlutterEngine() { + shellholderId_ = ""; + isAttached_ = false; +} + +void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, + void* window) { LOGD( "XComponentManger::OnSurfaceCreated window = %{public}p component = " "%{public}p", @@ -248,7 +272,11 @@ void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, void* wind if (ret) { LOGD("SetNativeWindowOpt failed:%{public}d", ret); } - PlatformViewOHOSNapi::SurfaceCreated(std::stoll(id_), window); + if (isAttached_) { + PlatformViewOHOSNapi::SurfaceCreated(std::stoll(shellholderId_), window); + } else { + LOGE("OnSurfaceCreated XComponentBase is not attached"); + } } void XComponentBase::OnSurfaceChanged(OH_NativeXComponent* component, void* window) @@ -260,22 +288,38 @@ void XComponentBase::OnSurfaceChanged(OH_NativeXComponent* component, void* wind LOGD("XComponent Current width:%{public}d,height:%{public}d", static_cast(width_), static_cast(height_)); } - PlatformViewOHOSNapi::SurfaceChanged(std::stoll(id_), width_, height_); + if (isAttached_) { + PlatformViewOHOSNapi::SurfaceChanged(std::stoll(shellholderId_), width_, + height_); + } else { + LOGE("OnSurfaceCreated XComponentBase is not attached"); + } } -void XComponentBase::OnSurfaceDestroyed(OH_NativeXComponent* component, void* window) -{ +void XComponentBase::OnSurfaceDestroyed(OH_NativeXComponent* component, + void* window) { LOGD("XComponentManger::OnSurfaceDestroyed"); - PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(id_)); + if (isAttached_) { + PlatformViewOHOSNapi::SurfaceDestroyed(std::stoll(shellholderId_)); + } else { + LOGE("OnSurfaceCreated OnSurfaceDestroyed is not attached"); + } } -void XComponentBase::OnDispatchTouchEvent(OH_NativeXComponent* component, void* window) -{ +void XComponentBase::OnDispatchTouchEvent(OH_NativeXComponent* component, + void* window) { LOGD("XComponentManger::DispatchTouchEvent"); int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent_); if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - ohosTouchProcessor_.HandleTouchEvent(std::stoll(id_), component, &touchEvent_); + if (isAttached_) { + ohosTouchProcessor_.HandleTouchEvent(std::stoll(shellholderId_), + component, &touchEvent_); + } else { + LOGE( + "XComponentManger::DispatchTouchEvent XComponentBase is not " + "attached"); + } } } diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.h b/shell/platform/ohos/ohos_xcomponent_adapter.h index 210483e392..f800fa82a1 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.h +++ b/shell/platform/ohos/ohos_xcomponent_adapter.h @@ -32,6 +32,9 @@ public: XComponentBase(std::string id_, OH_NativeXComponent* xcomponet); ~XComponentBase(); + void AttachFlutterEngine(std::string shellholderId); + void DetachFlutterEngine(); + // Callback, called by ACE XComponent void OnSurfaceCreated(OH_NativeXComponent* component, void* window); void OnSurfaceChanged(OH_NativeXComponent* component, void* window); @@ -41,6 +44,8 @@ public: OH_NativeXComponent_TouchEvent touchEvent_; OH_NativeXComponent_Callback callback_; std::string id_; + std::string shellholderId_; + bool isAttached_; OH_NativeXComponent* nativeXComponent_; uint64_t width_; uint64_t height_; @@ -56,6 +61,8 @@ class XComponentAdapter { bool Export(napi_env env, napi_value exports); void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent); + void AttachFlutterEngine(std::string& id, std::string& shellholderId); + void DetachFlutterEngine(std::string& id); public: std::map xcomponetMap_; -- Gitee From 623a690d3c689a13efc0c04a35041a6f0c455925 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Tue, 26 Dec 2023 19:14:31 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0napi=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../platform/ohos/ohos_xcomponent_adapter.cpp | 48 +++++++++++++------ shell/platform/ohos/ohos_xcomponent_adapter.h | 3 +- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index e6fa8813f2..51891a471d 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -70,17 +70,28 @@ void XComponentAdapter::SetNativeXComponent( std::string& id, OH_NativeXComponent* nativeXComponent) { auto iter = xcomponetMap_.find(id); - if(iter == xcomponetMap_.end()) { - XComponentBase* xcomponet = new XComponentBase(id, nativeXComponent); + if (iter == xcomponetMap_.end()) { + XComponentBase* xcomponet = new XComponentBase(id); xcomponetMap_[id] = xcomponet; } + + iter = xcomponetMap_.find(id); + if (iter != xcomponetMap_.end()) { + iter->second->SetNativeXComponent(nativeXComponent); + } } void XComponentAdapter::AttachFlutterEngine(std::string& id, std::string& shellholderId) { auto iter = xcomponetMap_.find(id); - if (iter != xcomponetMap_.end()) { - iter->second->AttachFlutterEngine(shellholderId); + if (iter == xcomponetMap_.end()) { + XComponentBase* xcomponet = new XComponentBase(id); + xcomponetMap_[id] = xcomponet; + } + + auto findIter = xcomponetMap_.find(id); + if (findIter != xcomponetMap_.end()) { + findIter->second->AttachFlutterEngine(shellholderId); } } @@ -228,20 +239,18 @@ void XComponentBase::BindXComponentCallback() { callback_.DispatchTouchEvent = DispatchTouchEventCB; } -XComponentBase::XComponentBase(std::string id, OH_NativeXComponent* xcomponet) { - nativeXComponent_ = xcomponet; - if (nativeXComponent_ != nullptr) { - id_ = id; - isAttached_ = false; - BindXComponentCallback(); - OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); - } +XComponentBase::XComponentBase(std::string id){ + id_ = id; + isAttached_ = false; } -XComponentBase::~XComponentBase() -{ +XComponentBase::~XComponentBase() {} void XComponentBase::AttachFlutterEngine(std::string shellholderId) { + LOGD( + "XComponentManger::AttachFlutterEngine xcomponentId:%{public}s, " + "shellholderId:%{public}s", + id_.c_str(), shellholderId.c_str()); shellholderId_ = shellholderId; isAttached_ = true; } @@ -249,6 +258,15 @@ void XComponentBase::AttachFlutterEngine(std::string shellholderId) { void XComponentBase::DetachFlutterEngine() { shellholderId_ = ""; isAttached_ = false; + nativeXComponent_ = nullptr; +} + +void XComponentBase::SetNativeXComponent(OH_NativeXComponent* nativeXComponent){ + nativeXComponent_ = nativeXComponent; + if (nativeXComponent_ != nullptr) { + BindXComponentCallback(); + OH_NativeXComponent_RegisterCallback(nativeXComponent_, &callback_); + } } void XComponentBase::OnSurfaceCreated(OH_NativeXComponent* component, @@ -292,7 +310,7 @@ void XComponentBase::OnSurfaceChanged(OH_NativeXComponent* component, void* wind PlatformViewOHOSNapi::SurfaceChanged(std::stoll(shellholderId_), width_, height_); } else { - LOGE("OnSurfaceCreated XComponentBase is not attached"); + LOGE("OnSurfaceChanged XComponentBase is not attached"); } } diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.h b/shell/platform/ohos/ohos_xcomponent_adapter.h index f800fa82a1..a888a7a1f4 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.h +++ b/shell/platform/ohos/ohos_xcomponent_adapter.h @@ -29,11 +29,12 @@ private: void BindXComponentCallback(); public: - XComponentBase(std::string id_, OH_NativeXComponent* xcomponet); + XComponentBase(std::string id); ~XComponentBase(); void AttachFlutterEngine(std::string shellholderId); void DetachFlutterEngine(); + void SetNativeXComponent(OH_NativeXComponent* nativeXComponent); // Callback, called by ACE XComponent void OnSurfaceCreated(OH_NativeXComponent* component, void* window); -- Gitee From 5baf60b9164c23998e3de34c62531743def68359 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Tue, 26 Dec 2023 19:22:22 +0800 Subject: [PATCH 4/9] =?UTF-8?q?FlutterAbility=E6=9A=B4=E9=9C=B2getFlutterV?= =?UTF-8?q?iew=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../flutter/src/main/ets/embedding/ohos/FlutterAbility.ets | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index 5ecc0e9eeb..053c525fa9 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -442,6 +442,10 @@ export class FlutterAbility extends UIAbility implements Host { getFlutterEngine(): FlutterEngine | null { return this.delegate?.flutterEngine || null; } + + getFlutterView(): FlutterView | null { + return this.flutterView ? this.flutterView : null; + } } export class ViewportMetrics { -- Gitee From b610a9723562fa5be807e4510cf15e50eb7cfd9c Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Tue, 26 Dec 2023 19:35:24 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../flutter/src/main/ets/view/FlutterView.ets | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 46abef268a..8927c1a358 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -66,15 +66,16 @@ export class FlutterView { public attachToFlutterEngine(flutterEngine: FlutterEngine) { this.flutterEngine = flutterEngine; + // 1.给XComponent赋值id let params: DVModelParameters = new DVModelParameters(); (params as Record)["xComponentId"] = this.xcomponentId.toString() let xComponentModel: DVModel = new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); RootDvModeManager.addDvModel(xComponentModel); - //xcomponent绑定engine + //2.xcomponent绑定engine + // 将shellHolder传递到C++侧,用于将Xcomponent和FlutterEngine绑定 this.flutterNapi.xComponentAttachFlutterEngine(this.xcomponentId); - this.isAttach = true; } @@ -83,6 +84,7 @@ export class FlutterView { } public detachFromFlutterEngine() { + this.flutterNapi.xComponentDetachFlutterEngine(this.xcomponentId); this.flutterEngine = null this.isAttach = false } -- Gitee From eb68f1e1d9cb981150b7834be50fe7daa673a9b0 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Wed, 27 Dec 2023 18:37:22 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../flutter/src/main/ets/embedding/ohos/FlutterAbility.ets | 7 ------- 1 file changed, 7 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index 053c525fa9..bc55ed2886 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -155,13 +155,6 @@ export class FlutterAbility extends UIAbility implements Host { loadContent() { if (this.windowStage != null && this.stillAttachedForEvent("loadContent")) { Log.i(TAG, 'loadContent'); - // let params: DVModelParameters = new DVModelParameters(); - // (params as Record)["xComponentId"] = - // this.delegate?.getFlutterNapi()?.nativeShellHolderId?.toString(); - // let xComponentModel: DVModel = - // new DVModel("xComponent", params, new DVModelEvents(), new DVModelChildren(), BuildXComponentStruct); - // RootDvModeManager.addDvModel(xComponentModel); - if (!this.flutterView) { if (this.delegate?.getFlutterNapi()) { this.flutterView = new FlutterView(this.delegate?.getFlutterNapi()!); -- Gitee From 51754280b06ae6a92a8a4688dfb491b065efafab Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Thu, 28 Dec 2023 11:05:29 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=E6=8A=8Aflutterview=E7=9A=84=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E9=80=BB=E8=BE=91=E8=BF=81=E7=A7=BB=E5=88=B0delegte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../ets/embedding/ohos/FlutterAbility.ets | 12 +++------ .../embedding/ohos/FlutterAbilityDelegate.ets | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index bc55ed2886..a472497f7b 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -59,7 +59,7 @@ export class FlutterAbility extends UIAbility implements Host { private mainWindow?: window.Window; private viewportMetrics = new ViewportMetrics(); private displayInfo?: display.Display; - private flutterView?: FlutterView + private flutterView?: FlutterView | null /** * onCreate @@ -156,16 +156,10 @@ export class FlutterAbility extends UIAbility implements Host { if (this.windowStage != null && this.stillAttachedForEvent("loadContent")) { Log.i(TAG, 'loadContent'); if (!this.flutterView) { - if (this.delegate?.getFlutterNapi()) { - this.flutterView = new FlutterView(this.delegate?.getFlutterNapi()!); + if (this.delegate) { + this.flutterView = this.delegate?.onCreateView(); } } - const engine = this.getFlutterEngine(); - if (engine) { - this.flutterView?.attachToFlutterEngine(engine); - } else { - throw new Error('this.getFlutterEngine() is null!'); - } this.windowStage?.loadContent('pages/Index', (err, data) => { if (err.code) { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityDelegate.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityDelegate.ets index ec1657e4d2..5585ab56d0 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityDelegate.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityDelegate.ets @@ -37,6 +37,7 @@ import Settings from './Settings'; import FlutterNapi from '../engine/FlutterNapi'; import List from '@ohos.util.List'; import GeneratedPluginRegister from '../engine/plugins/util/GeneratedPluginRegister'; +import { FlutterView } from '../../view/FlutterView'; const TAG = "FlutterAbilityDelegate"; const PLUGINS_RESTORATION_BUNDLE_KEY = "plugins"; @@ -57,6 +58,7 @@ class FlutterAbilityDelegate implements ExclusiveAppComponent { private engineGroup?: FlutterEngineGroup; private settings?: Settings; private isHost:boolean = false; + private flutterView?: FlutterView; constructor(host?: Host) { this.host = host; @@ -386,6 +388,30 @@ class FlutterAbilityDelegate implements ExclusiveAppComponent { } } + onCreateView(): FlutterView | null { + if (this.getFlutterNapi()) { + this.flutterView = new FlutterView(this.getFlutterNapi()!); + if (this.flutterEngine) { + this.flutterView?.attachToFlutterEngine(this.flutterEngine!) + }else{ + throw new Error('this.flutterEngine() is null!'); + } + return this.flutterView; + } else { + return null; + } + } + + /** + * Invoke this from {@code FlutterAbility#onDestroy()}. + * + * */ + onDestroyView() { + Log.i(TAG, "onDestroyView()"); + this.ensureAlive(); + this.flutterView?.detachFromFlutterEngine() + } + onSaveState(reason: AbilityConstant.StateType, wantParam: Record): AbilityConstant.OnSaveResult { Log.i(TAG, "onSaveInstanceState. Giving framework and plugins an opportunity to save state."); this.ensureAlive(); -- Gitee From b65e505ddfe11ab7e91ea27fc25f4d92e382c58c Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Thu, 28 Dec 2023 11:13:28 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=8A=8Aflutterview=E9=94=80=E6=AF=81?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../flutter/src/main/ets/embedding/ohos/FlutterAbility.ets | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index a472497f7b..8a428a3ac8 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -95,6 +95,7 @@ export class FlutterAbility extends UIAbility implements Host { onDestroy() { this.context.eventHub.off(EVENT_BACK_PRESS); if (this.stillAttachedForEvent("onDestroy")) { + this?.delegate?.onCreateView(); this?.delegate?.onDestroy(); } } -- Gitee From d7fb3cdb2f0ab046aacbca6d21789bfe057fb0a0 Mon Sep 17 00:00:00 2001 From: xiaoxiang Date: Thu, 28 Dec 2023 12:35:35 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E9=94=99=E8=AF=AFcreateview=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaoxiang --- .../flutter/src/main/ets/embedding/ohos/FlutterAbility.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index 8a428a3ac8..b1aeda4bb5 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -95,7 +95,7 @@ export class FlutterAbility extends UIAbility implements Host { onDestroy() { this.context.eventHub.off(EVENT_BACK_PRESS); if (this.stillAttachedForEvent("onDestroy")) { - this?.delegate?.onCreateView(); + this?.delegate?.onDestroyView(); this?.delegate?.onDestroy(); } } -- Gitee