diff --git a/windows/webview_windows_plugin.cc b/windows/webview_windows_plugin.cc index 43ae06f..b2497e8 100644 --- a/windows/webview_windows_plugin.cc +++ b/windows/webview_windows_plugin.cc @@ -55,7 +55,12 @@ class WebviewWindowsPlugin : public flutter::Plugin { virtual ~WebviewWindowsPlugin(); private: - std::unique_ptr platform_; + // CreateDispatcherQueueController can only be called once per thread. + // In the desktop_mult_window plugin, the plugin creates multiple instances, + // which can cause issues due to this constraint + // Therefore, we made it a singleton + static std::unique_ptr platform_; + static int plugin_count_ = 0; std::unique_ptr webview_host_; std::unordered_map> instances_; @@ -73,6 +78,8 @@ class WebviewWindowsPlugin : public flutter::Plugin { std::unique_ptr> result); }; +std::unique_ptr WebviewWindowsPlugin::platform_; + // static void WebviewWindowsPlugin::RegisterWithRegistrar( flutter::PluginRegistrarWindows* registrar) { @@ -97,12 +104,20 @@ WebviewWindowsPlugin::WebviewWindowsPlugin(flutter::TextureRegistrar* textures, : textures_(textures), messenger_(messenger) { window_class_.lpszClassName = L"FlutterWebviewMessage"; window_class_.lpfnWndProc = &DefWindowProc; - RegisterClass(&window_class_); + plugin_count_ ++; + if (plugin_count_ == 1){ + RegisterClass(&window_class_); + } } WebviewWindowsPlugin::~WebviewWindowsPlugin() { instances_.clear(); - UnregisterClass(window_class_.lpszClassName, nullptr); + plugin_count_ --; + // To prevent the accidental creation of other windows caused by deregistration + // in multi-plugin-instance environments like desktop_mult_window + if (plugin_count_ == 0){ + UnregisterClass(window_class_.lpszClassName, nullptr); + } } void WebviewWindowsPlugin::HandleMethodCall(