fix: 监听函数无法触发,因为C++对象的生命周期问题
This commit is contained in:
parent
7f9ae9cb3b
commit
fc56622ccb
|
@ -70,7 +70,7 @@ void DispatchTouchEventCB(OH_NativeXComponent *component, void *window) {
|
||||||
|
|
||||||
std::unordered_map<std::string, PluginRender *> PluginRender::instance_;
|
std::unordered_map<std::string, PluginRender *> PluginRender::instance_;
|
||||||
|
|
||||||
PluginRender::~PluginRender() {}
|
PluginRender::~PluginRender() = default;
|
||||||
|
|
||||||
PluginRender *PluginRender::GetInstance(std::string &id) {
|
PluginRender *PluginRender::GetInstance(std::string &id) {
|
||||||
if (PluginRender::instance_.find(id) == PluginRender::instance_.end()) {
|
if (PluginRender::instance_.find(id) == PluginRender::instance_.end()) {
|
||||||
|
@ -81,15 +81,16 @@ PluginRender *PluginRender::GetInstance(std::string &id) {
|
||||||
return PluginRender::instance_[id];
|
return PluginRender::instance_[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void PluginRender::Export(napi_env env, napi_value value) {}
|
void PluginRender::Export(napi_env env, napi_value value) {}
|
||||||
|
|
||||||
void PluginRender::RegisterCallback(OH_NativeXComponent *nativeXComponent) {
|
void PluginRender::RegisterCallback(OH_NativeXComponent *nativeXComponent) {
|
||||||
this->renderCallback_.DispatchTouchEvent = DispatchTouchEventCB;
|
this->renderCallback_ = new OH_NativeXComponent_Callback();
|
||||||
this->renderCallback_.OnSurfaceChanged = OnSurfaceChangedCB;
|
|
||||||
this->renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB;
|
this->renderCallback_->DispatchTouchEvent = DispatchTouchEventCB;
|
||||||
this->renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
|
this->renderCallback_->OnSurfaceChanged = OnSurfaceChangedCB;
|
||||||
|
this->renderCallback_->OnSurfaceCreated = OnSurfaceCreatedCB;
|
||||||
|
this->renderCallback_->OnSurfaceDestroyed = OnSurfaceDestroyedCB;
|
||||||
|
|
||||||
// 将 callback 注册给 NativeXComponent
|
// 将 callback 注册给 NativeXComponent
|
||||||
OH_NativeXComponent_RegisterCallback(nativeXComponent, &this->renderCallback_);
|
OH_NativeXComponent_RegisterCallback(nativeXComponent, this->renderCallback_);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,19 +24,19 @@
|
||||||
#define HARMONY_DEMO_NAPI_DRAWING_PLUGIN_RENDER_H
|
#define HARMONY_DEMO_NAPI_DRAWING_PLUGIN_RENDER_H
|
||||||
|
|
||||||
class PluginRender {
|
class PluginRender {
|
||||||
private:
|
|
||||||
~PluginRender();
|
|
||||||
|
|
||||||
OH_NativeXComponent_Callback renderCallback_;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static std::unordered_map<std::string, PluginRender *> instance_;
|
~PluginRender();
|
||||||
|
|
||||||
static PluginRender *GetInstance(std::string &id);
|
static PluginRender *GetInstance(std::string &id);
|
||||||
|
|
||||||
void Export(napi_env env, napi_value);
|
void Export(napi_env env, napi_value);
|
||||||
// 注册监听XComponent组件的回调,包括生命周期函数,手势监听函数等。
|
// 注册监听XComponent组件的回调,包括生命周期函数,手势监听函数等。
|
||||||
void RegisterCallback(OH_NativeXComponent *nativeXComponent);
|
void RegisterCallback(OH_NativeXComponent *nativeXComponent);
|
||||||
|
|
||||||
|
private:
|
||||||
|
OH_NativeXComponent_Callback *renderCallback_;
|
||||||
|
|
||||||
|
static std::unordered_map<std::string, PluginRender *> instance_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HARMONY_DEMO_NAPI_DRAWING_PLUGIN_RENDER_H
|
#endif // HARMONY_DEMO_NAPI_DRAWING_PLUGIN_RENDER_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user