22 std::optional<WPARAM> wparam,
23 std::optional<LPARAM> lparam,
25 if (!hwnd.has_value()) {
26 ::PostQuitMessage(exit_code);
28 BASE_CHECK(wparam.has_value() && lparam.has_value());
29 sent_close_messages_[std::make_tuple(*hwnd, *wparam, *lparam)]++;
38 PostMessage(hwnd,
message, wparam, lparam);
41 bool WindowsLifecycleManager::HandleCloseMessage(HWND hwnd,
47 auto key = std::make_tuple(hwnd, wparam, lparam);
48 auto itr = sent_close_messages_.find(
key);
49 if (itr != sent_close_messages_.end()) {
50 if (itr->second == 1) {
51 sent_close_messages_.erase(itr);
53 sent_close_messages_[
key]--;
76 return HandleCloseMessage(hwnd, wpar, lpar);
80 case WM_DWMCOMPOSITIONCHANGED:
85 if (wpar == SIZE_MAXIMIZED || wpar == SIZE_RESTORED) {
87 }
else if (wpar == SIZE_MINIMIZED) {
110 thread_snapshot_ = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
113 if (thread_snapshot_ != INVALID_HANDLE_VALUE) {
114 ::CloseHandle(thread_snapshot_);
119 if (thread_snapshot_ == INVALID_HANDLE_VALUE) {
120 FML_LOG(ERROR) <<
"Failed to get thread snapshot";
123 THREADENTRY32 thread;
124 thread.dwSize =
sizeof(thread);
125 if (!::Thread32First(thread_snapshot_, &thread)) {
126 DWORD error_num = ::GetLastError();
129 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
130 error_num, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msg, 256,
132 FML_LOG(ERROR) <<
"Failed to get thread(" << error_num <<
"): " << msg;
139 if (thread_snapshot_ == INVALID_HANDLE_VALUE) {
142 return ::Thread32Next(thread_snapshot_, &thread);
146 HANDLE thread_snapshot_;
150 int64_t num_windows = 0;
153 [](HWND hwnd, LPARAM lparam) {
154 int64_t* windows_ptr = reinterpret_cast<int64_t*>(lparam);
155 if (::GetParent(hwnd) == nullptr) {
158 return *windows_ptr <= 1 ? TRUE : FALSE;
160 reinterpret_cast<LPARAM
>(&num_windows));
164 bool WindowsLifecycleManager::IsLastWindowOfProcess() {
165 DWORD pid = ::GetCurrentProcessId();
167 std::optional<THREADENTRY32> first_thread = thread_snapshot.
GetFirstThread();
168 if (!first_thread.has_value()) {
169 FML_LOG(ERROR) <<
"No first thread found";
174 THREADENTRY32 thread = *first_thread;
176 if (thread.th32OwnerProcessID == pid) {
178 if (num_windows > 1) {
184 return num_windows <= 1;
187 void WindowsLifecycleManager::BeginProcessingLifecycle() {
188 process_lifecycle_ =
true;
191 void WindowsLifecycleManager::BeginProcessingExit() {
192 process_exit_ =
true;
196 if (state_ == state) {
200 if (engine_ && process_lifecycle_) {
202 engine_->SendPlatformMessage(
"flutter/lifecycle",
203 reinterpret_cast<const uint8_t*
>(state_name),
204 strlen(state_name),
nullptr,
nullptr);
208 void WindowsLifecycleManager::OnWindowStateEvent(HWND hwnd,
211 if (event == WindowStateEvent::kHide &&
212 focused_windows_.find(hwnd) != focused_windows_.end()) {
213 OnWindowStateEvent(hwnd, WindowStateEvent::kUnfocus);
216 std::lock_guard guard(state_update_lock_);
218 case WindowStateEvent::kShow: {
219 bool first_shown_window = visible_windows_.empty();
220 auto pair = visible_windows_.insert(hwnd);
221 if (first_shown_window && pair.second &&
222 state_ == AppLifecycleState::kHidden) {
223 SetLifecycleState(AppLifecycleState::kInactive);
227 case WindowStateEvent::kHide: {
228 bool present = visible_windows_.erase(hwnd);
229 bool empty = visible_windows_.empty();
230 if (present && empty &&
231 (state_ == AppLifecycleState::kResumed ||
232 state_ == AppLifecycleState::kInactive)) {
233 SetLifecycleState(AppLifecycleState::kHidden);
237 case WindowStateEvent::kFocus: {
238 bool first_focused_window = focused_windows_.empty();
239 auto pair = focused_windows_.insert(hwnd);
240 if (first_focused_window && pair.second &&
241 state_ == AppLifecycleState::kInactive) {
242 SetLifecycleState(AppLifecycleState::kResumed);
246 case WindowStateEvent::kUnfocus: {
247 if (focused_windows_.erase(hwnd) && focused_windows_.empty() &&
248 state_ == AppLifecycleState::kResumed) {
249 SetLifecycleState(AppLifecycleState::kInactive);
256 std::optional<LRESULT> WindowsLifecycleManager::ExternalWindowMessage(
261 std::optional<flutter::WindowStateEvent>
event = std::nullopt;
291 if (HandleCloseMessage(hwnd, wparam, lparam)) {
297 if (event.has_value()) {
298 OnWindowStateEvent(hwnd, *event);