Handle external window's WM_CLOSE in lifecycle manager (flutter/engine#45840)
Handle the close messages from non-Flutter windows similarly to how we handle our own, for graceful app exit purposes. https://github.com/flutter/flutter/issues/131497 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [ ] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat
This commit is contained in:
@@ -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<MockWindowBindingHandler>>();
|
||||
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<MockWindowsLifecycleManager>(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()};
|
||||
|
||||
|
||||
@@ -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<LRESULT> 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()) {
|
||||
|
||||
@@ -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<std::tuple<HWND, WPARAM, LPARAM>, int> sent_close_messages_;
|
||||
|
||||
Reference in New Issue
Block a user