diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index d27c78c705..7b29748453 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -909,6 +909,28 @@ TEST_F(FlutterWindowsEngineTest, EnableApplicationLifecycle) { 0); } +TEST_F(FlutterWindowsEngineTest, ApplicationLifecycleExternalWindow) { + FlutterWindowsEngineBuilder builder{GetContext()}; + + auto window_binding_handler = + std::make_unique<::testing::NiceMock>(); + MockFlutterWindowsView view(std::move(window_binding_handler)); + view.SetEngine(builder.Build()); + FlutterWindowsEngine* engine = view.GetEngine(); + + EngineModifier modifier(engine); + modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; }; + auto handler = std::make_unique(engine); + ON_CALL(*handler, IsLastWindowOfProcess).WillByDefault([]() { + return false; + }); + EXPECT_CALL(*handler, IsLastWindowOfProcess).Times(1); + modifier.SetLifecycleManager(std::move(handler)); + engine->lifecycle_manager()->BeginProcessingExit(); + + engine->lifecycle_manager()->ExternalWindowMessage(0, WM_CLOSE, 0, 0); +} + TEST_F(FlutterWindowsEngineTest, AppStartsInResumedState) { FlutterWindowsEngineBuilder builder{GetContext()}; diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc index a41b1617ba..90d52f300b 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.cc @@ -38,6 +38,30 @@ void WindowsLifecycleManager::DispatchMessage(HWND hwnd, PostMessage(hwnd, message, wparam, lparam); } +bool WindowsLifecycleManager::HandleCloseMessage(HWND hwnd, + WPARAM wparam, + LPARAM lparam) { + if (!process_exit_) { + return false; + } + auto key = std::make_tuple(hwnd, wparam, lparam); + auto itr = sent_close_messages_.find(key); + if (itr != sent_close_messages_.end()) { + if (itr->second == 1) { + sent_close_messages_.erase(itr); + } else { + sent_close_messages_[key]--; + } + return false; + } + if (IsLastWindowOfProcess()) { + engine_->RequestApplicationQuit(hwnd, wparam, lparam, + AppExitType::cancelable); + return true; + } + return false; +} + bool WindowsLifecycleManager::WindowProc(HWND hwnd, UINT msg, WPARAM wpar, @@ -48,27 +72,8 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd, // send a request to the framework to see if the app should exit. If it // is, we re-dispatch a new WM_CLOSE message. In order to allow the new // message to reach other delegates, we ignore it here. - case WM_CLOSE: { - if (!process_exit_) { - return false; - } - auto key = std::make_tuple(hwnd, wpar, lpar); - auto itr = sent_close_messages_.find(key); - if (itr != sent_close_messages_.end()) { - if (itr->second == 1) { - sent_close_messages_.erase(itr); - } else { - sent_close_messages_[key]--; - } - return false; - } - if (IsLastWindowOfProcess()) { - engine_->RequestApplicationQuit(hwnd, wpar, lpar, - AppExitType::cancelable); - return true; - } - break; - } + case WM_CLOSE: + return HandleCloseMessage(hwnd, wpar, lpar); // DWM composition can be disabled on Windows 7. // Notify the engine as this can result in screen tearing. @@ -285,6 +290,11 @@ std::optional WindowsLifecycleManager::ExternalWindowMessage( case WM_DESTROY: event = flutter::WindowStateEvent::kHide; break; + case WM_CLOSE: + if (HandleCloseMessage(hwnd, wparam, lparam)) { + return NULL; + } + break; } if (event.has_value()) { diff --git a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h index c56bfc306b..fc20b38246 100644 --- a/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h +++ b/engine/src/flutter/shell/platform/windows/windows_lifecycle_manager.h @@ -99,6 +99,13 @@ class WindowsLifecycleManager { LPARAM lparam); private: + // Pass top-level window close notifications to the application lifecycle + // logic. If the last window of the process receives WM_CLOSE and a listener + // is registered for WidgetsBindingObserver.didRequestAppExit, the message is + // sent to the framework to query whether the application should be allowed + // to quit. + bool HandleCloseMessage(HWND hwnd, WPARAM wparam, LPARAM lparam); + FlutterWindowsEngine* engine_; std::map, int> sent_close_messages_;