Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a28f689

Browse files
Tie lifecycle to message
1 parent e8225ea commit a28f689

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

shell/platform/windows/fixtures/main.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ void exitTestCancel() async {
127127
await exited.future;
128128
}
129129

130+
@pragma('vm:entry-point')
131+
void enableLifecycleTest() async {
132+
final Completer<ByteData?> finished = Completer<ByteData?>();
133+
ui.channelBuffers.setListener('flutter/lifecycle', (ByteData? data, ui.PlatformMessageResponseCallback callback) async {
134+
if (data != null) {
135+
ui.PlatformDispatcher.instance.sendPlatformMessage(
136+
'flutter/unittest',
137+
data,
138+
(ByteData? reply) {
139+
finished.complete();
140+
});
141+
}
142+
});
143+
await finished.future;
144+
}
145+
130146
@pragma('vm:entry-point')
131147
void customEntrypoint() {}
132148

shell/platform/windows/flutter_windows_engine.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ void FlutterWindowsEngine::OnDwmCompositionChanged() {
792792
}
793793

794794
void FlutterWindowsEngine::OnApplicationLifecycleEnabled() {
795-
lifecycle_manager_->BeginProcessingClose();
795+
lifecycle_manager_->BeginProcessingLifecycle();
796796
}
797797

798798
void FlutterWindowsEngine::OnWindowStateEvent(HWND hwnd,

shell/platform/windows/flutter_windows_engine_unittests.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,5 +1002,56 @@ TEST_F(FlutterWindowsEngineTest, InnerWindowHidden) {
10021002
AppLifecycleState::kInactive);
10031003
}
10041004

1005+
TEST_F(FlutterWindowsEngineTest, EnableLifecycleState) {
1006+
FlutterWindowsEngineBuilder builder{GetContext()};
1007+
builder.SetDartEntrypoint("enableLifecycleTest");
1008+
bool finished = false;
1009+
1010+
auto window_binding_handler =
1011+
std::make_unique<::testing::NiceMock<MockWindowBindingHandler>>();
1012+
MockFlutterWindowsView view(std::move(window_binding_handler));
1013+
view.SetEngine(builder.Build());
1014+
FlutterWindowsEngine* engine = view.GetEngine();
1015+
1016+
EngineModifier modifier(engine);
1017+
modifier.embedder_api().RunsAOTCompiledDartCode = []() { return false; };
1018+
auto handler = std::make_unique<MockWindowsLifecycleManager>(engine);
1019+
ON_CALL(*handler, SetLifecycleState)
1020+
.WillByDefault([handler_ptr = handler.get()](AppLifecycleState state) {
1021+
handler_ptr->WindowsLifecycleManager::SetLifecycleState(state);
1022+
});
1023+
modifier.SetLifecycleManager(std::move(handler));
1024+
1025+
auto binary_messenger =
1026+
std::make_unique<BinaryMessengerImpl>(engine->messenger());
1027+
// Mark the test only as completed on receiving an inactive state message.
1028+
binary_messenger->SetMessageHandler(
1029+
"flutter/unittest", [&finished](const uint8_t* message,
1030+
size_t message_size, BinaryReply reply) {
1031+
std::string contents(message, message + message_size);
1032+
EXPECT_NE(contents.find("AppLifecycleState.inactive"),
1033+
std::string::npos);
1034+
finished = true;
1035+
char response[] = "";
1036+
reply(reinterpret_cast<uint8_t*>(response), 0);
1037+
});
1038+
1039+
engine->Run();
1040+
1041+
// Test that setting the state before enabling lifecycle does nothing.
1042+
HWND hwnd = reinterpret_cast<HWND>(1);
1043+
view.OnWindowStateEvent(hwnd, WindowStateEvent::kShow);
1044+
view.OnWindowStateEvent(hwnd, WindowStateEvent::kHide);
1045+
EXPECT_FALSE(finished);
1046+
1047+
// Test that we can set the state afterwards.
1048+
engine->OnApplicationLifecycleEnabled();
1049+
view.OnWindowStateEvent(hwnd, WindowStateEvent::kShow);
1050+
1051+
while (!finished) {
1052+
engine->task_runner()->ProcessTasks();
1053+
}
1054+
}
1055+
10051056
} // namespace testing
10061057
} // namespace flutter

shell/platform/windows/windows_lifecycle_manager.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
namespace flutter {
1515

1616
WindowsLifecycleManager::WindowsLifecycleManager(FlutterWindowsEngine* engine)
17-
: engine_(engine), process_close_(false) {}
17+
: engine_(engine) {}
1818

1919
WindowsLifecycleManager::~WindowsLifecycleManager() {}
2020

@@ -49,7 +49,7 @@ bool WindowsLifecycleManager::WindowProc(HWND hwnd,
4949
// is, we re-dispatch a new WM_CLOSE message. In order to allow the new
5050
// message to reach other delegates, we ignore it here.
5151
case WM_CLOSE: {
52-
if (!process_close_) {
52+
if (!process_lifecycle_) {
5353
return false;
5454
}
5555
auto key = std::make_tuple(hwnd, wpar, lpar);
@@ -179,16 +179,16 @@ bool WindowsLifecycleManager::IsLastWindowOfProcess() {
179179
return num_windows <= 1;
180180
}
181181

182-
void WindowsLifecycleManager::BeginProcessingClose() {
183-
process_close_ = true;
182+
void WindowsLifecycleManager::BeginProcessingLifecycle() {
183+
process_lifecycle_ = true;
184184
}
185185

186186
void WindowsLifecycleManager::SetLifecycleState(AppLifecycleState state) {
187187
if (state_ == state) {
188188
return;
189189
}
190190
state_ = state;
191-
if (engine_) {
191+
if (engine_ && process_lifecycle_) {
192192
const char* state_name = AppLifecycleStateToString(state);
193193
engine_->SendPlatformMessage("flutter/lifecycle",
194194
reinterpret_cast<const uint8_t*>(state_name),

shell/platform/windows/windows_lifecycle_manager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ class WindowsLifecycleManager {
5252
// update the application lifecycle.
5353
bool WindowProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT* result);
5454

55-
// Signal to start consuming WM_CLOSE messages.
56-
void BeginProcessingClose();
55+
// Signal to start consuming WM_CLOSE messages and sending lifecycle state
56+
// update messages.
57+
void BeginProcessingLifecycle();
5758

5859
// Update the app lifecycle state in response to a change in window state.
5960
// When the app lifecycle state actually changes, this sends a platform
@@ -100,7 +101,7 @@ class WindowsLifecycleManager {
100101

101102
std::map<std::tuple<HWND, WPARAM, LPARAM>, int> sent_close_messages_;
102103

103-
bool process_close_;
104+
bool process_lifecycle_ = false;
104105

105106
std::set<HWND> visible_windows_;
106107

0 commit comments

Comments
 (0)