| Flutter Windows Embedder |

Public Member Functions | Protected Member Functions | List of all members
flutter::WindowsLifecycleManager Class Reference

`#include <windows_lifecycle_manager.h>`

Inheritance diagram for flutter::WindowsLifecycleManager:

## Public Member Functions
| WindowsLifecycleManager (FlutterWindowsEngine *engine)
virtual | ~WindowsLifecycleManager ()
virtual void | Quit (std::optional< HWND > window, std::optional< WPARAM > wparam, std::optional< LPARAM > lparam, UINT exit_code)
bool | WindowProc (HWND hwnd, UINT msg, WPARAM w, LPARAM l, LRESULT *result)
virtual void | BeginProcessingLifecycle ()
virtual void | BeginProcessingExit ()
virtual void | SetLifecycleState (AppLifecycleState state)
virtual void | OnWindowStateEvent (HWND hwnd, WindowStateEvent event)
AppLifecycleState | GetLifecycleState ()
std::optional< LRESULT > | ExternalWindowMessage (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)

## Protected Member Functions
virtual bool | IsLastWindowOfProcess ()
virtual void | DispatchMessage (HWND window, UINT msg, WPARAM wparam, LPARAM lparam)

## Detailed Description

A manager for lifecycle events of the top-level windows.

WndProc is called for window messages of the top-level Flutter window. ExternalWindowMessage is called for non-flutter top-level window messages. OnWindowStateEvent is called when the visibility or focus state of a window is changed, including the FlutterView window.

Definition at line 37 of file windows_lifecycle_manager.h.

## Constructor & Destructor Documentation

## ◆ WindowsLifecycleManager()

flutter::WindowsLifecycleManager::WindowsLifecycleManager | ( | FlutterWindowsEngine * | engine | ) |

Definition at line 16 of file windows_lifecycle_manager.cc.

17 : engine_(engine) {}

## ◆ ~WindowsLifecycleManager()

flutter::WindowsLifecycleManager::~WindowsLifecycleManager | ( |  | ) |
| virtual

Definition at line 19 of file windows_lifecycle_manager.cc.

19 {}

## Member Function Documentation

## ◆ BeginProcessingExit()

void flutter::WindowsLifecycleManager::BeginProcessingExit | ( |  | ) |
| virtual

Definition at line 191 of file windows_lifecycle_manager.cc.

191 {
192 process_exit_ = true;
193 }

## ◆ BeginProcessingLifecycle()

void flutter::WindowsLifecycleManager::BeginProcessingLifecycle | ( |  | ) |
| virtual

Reimplemented in flutter::testing::MockWindowsLifecycleManager.

Definition at line 187 of file windows_lifecycle_manager.cc.

187 {
188 process_lifecycle_ = true;
189 }

Referenced by flutter::testing::MockWindowsLifecycleManager::BeginProcessingLifecycle().

## ◆ DispatchMessage()

void flutter::WindowsLifecycleManager::DispatchMessage | ( | HWND | window,
|  | UINT | msg,
|  | WPARAM | wparam,
|  | LPARAM | lparam
| ) |  |
| protected virtual

Definition at line 34 of file windows_lifecycle_manager.cc.

37 {
38 PostMessage(hwnd, message, wparam, lparam);
39 }

References message.

Referenced by Quit().

## ◆ ExternalWindowMessage()

std::optional< LRESULT > flutter::WindowsLifecycleManager::ExternalWindowMessage | ( | HWND | hwnd,
|  | UINT | message,
|  | WPARAM | wparam,
|  | LPARAM | lparam
| ) |  |

Definition at line 256 of file windows_lifecycle_manager.cc.

260 {
261 std::optional<flutter::WindowStateEvent> event = std::nullopt;
262
263 // TODO (schectman): Handle WM_CLOSE messages.
264 // https://github.com/flutter/flutter/issues/131497
265 switch (message) {
266 case WM_SHOWWINDOW:
267 event = wparam ? flutter::WindowStateEvent::kShow
268 : flutter::WindowStateEvent::kHide;
269 break;
270 case WM_SIZE:
271 switch (wparam) {
272 case SIZE_MINIMIZED:
273 event = flutter::WindowStateEvent::kHide;
274 break;
275 case SIZE_RESTORED:
276 case SIZE_MAXIMIZED:
277 event = flutter::WindowStateEvent::kShow;
278 break;
279 }
280 break;
281 case WM_SETFOCUS:
282 event = flutter::WindowStateEvent::kFocus;
283 break;
284 case WM_KILLFOCUS:
285 event = flutter::WindowStateEvent::kUnfocus;
286 break;
287 case WM_DESTROY:
288 event = flutter::WindowStateEvent::kHide;
289 break;
290 case WM_CLOSE:
291 if (HandleCloseMessage(hwnd, wparam, lparam)) {
292 return NULL;
293 }
294 break;
295 }
296
297 if (event.has_value()) {
298 OnWindowStateEvent(hwnd, *event);
299 }
300
301 return std::nullopt;
302 }

References flutter::kFocus, flutter::kHide, flutter::kShow, flutter::kUnfocus, and message.

## ◆ GetLifecycleState()

AppLifecycleState flutter::WindowsLifecycleManager::GetLifecycleState | ( |  | ) |
| inline

Definition at line 75 of file windows_lifecycle_manager.h.

75 { return state_; }

Referenced by flutter::testing::TEST_F().

## ◆ IsLastWindowOfProcess()

bool flutter::WindowsLifecycleManager::IsLastWindowOfProcess | ( |  | ) |
| protected virtual

Definition at line 164 of file windows_lifecycle_manager.cc.

164 {
165 DWORD pid = ::GetCurrentProcessId();
166 ThreadSnapshot thread_snapshot;
167 std::optional<THREADENTRY32> first_thread = thread_snapshot.GetFirstThread();
168 if (!first_thread.has_value()) {
169 FML_LOG(ERROR) << "No first thread found";
170 return true;
171 }
172
173 int num_windows = 0;
174 THREADENTRY32 thread = *first_thread;
175 do {
176 if (thread.th32OwnerProcessID == pid) {
177 num_windows += NumWindowsForThread(thread);
178 if (num_windows > 1) {
179 return false;
180 }
181 }
182 } while (thread_snapshot.GetNextThread(thread));
183
184 return num_windows <= 1;
185 }

References flutter::ThreadSnapshot::GetFirstThread(), flutter::ThreadSnapshot::GetNextThread(), and flutter::NumWindowsForThread().

## ◆ OnWindowStateEvent()

void flutter::WindowsLifecycleManager::OnWindowStateEvent | ( | HWND | hwnd,
|  | WindowStateEvent | event
| ) |  |
| virtual

Definition at line 208 of file windows_lifecycle_manager.cc.

209 {
210 // Synthesize an unfocus event when a focused window is hidden.
211 if (event == WindowStateEvent::kHide &&
212 focused_windows_.find(hwnd) != focused_windows_.end()) {
213 OnWindowStateEvent(hwnd, WindowStateEvent::kUnfocus);
214 }
215
216 std::lock_guard guard(state_update_lock_);
217 switch (event) {
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);
224 }
225 break;
226 }
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);
234 }
235 break;
236 }
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);
243 }
244 break;
245 }
246 case WindowStateEvent::kUnfocus: {
247 if (focused_windows_.erase(hwnd) && focused_windows_.empty() &&
248 state_ == AppLifecycleState::kResumed) {
249 SetLifecycleState(AppLifecycleState::kInactive);
250 }
251 break;
252 }
253 }
254 }

Referenced by flutter::testing::TEST_F(), and WindowProc().

## ◆ Quit()

void flutter::WindowsLifecycleManager::Quit | ( | std::optional< HWND > | window,
|  | std::optional< WPARAM > | wparam,
|  | std::optional< LPARAM > | lparam,
|  | UINT | exit_code
| ) |  |
| virtual

Definition at line 21 of file windows_lifecycle_manager.cc.

24 {
25 if (!hwnd.has_value()) {
26 ::PostQuitMessage(exit_code);
27 } else {
28 BASE_CHECK(wparam.has_value() && lparam.has_value());
29 sent_close_messages_[std::make_tuple(*hwnd, *wparam, *lparam)]++;
30 DispatchMessage(*hwnd, WM_CLOSE, *wparam, *lparam);
31 }
32 }

References DispatchMessage().

## ◆ SetLifecycleState()

void flutter::WindowsLifecycleManager::SetLifecycleState | ( | AppLifecycleState | state | ) |
| virtual

Definition at line 195 of file windows_lifecycle_manager.cc.

195 {
196 if (state_ == state) {
197 return;
198 }
199 state_ = state;
200 if (engine_ && process_lifecycle_) {
201 const char* state_name = AppLifecycleStateToString(state);
202 engine_->SendPlatformMessage("flutter/lifecycle",
203 reinterpret_cast<const uint8_t*>(state_name),
204 strlen(state_name), nullptr, nullptr);
205 }
206 }

Referenced by flutter::testing::TEST_F().

## ◆ WindowProc()

bool flutter::WindowsLifecycleManager::WindowProc | ( | HWND | hwnd,
|  | UINT | msg,
|  | WPARAM | w,
|  | LPARAM | l,
|  | LRESULT * | result
| ) |  |

Definition at line 65 of file windows_lifecycle_manager.cc.

69 {
70 switch (msg) {
71 // When WM_CLOSE is received from the final window of an application, we
72 // send a request to the framework to see if the app should exit. If it
73 // is, we re-dispatch a new WM_CLOSE message. In order to allow the new
74 // message to reach other delegates, we ignore it here.
75 case WM_CLOSE:
76 return HandleCloseMessage(hwnd, wpar, lpar);
77
78 // DWM composition can be disabled on Windows 7.
79 // Notify the engine as this can result in screen tearing.
80 case WM_DWMCOMPOSITIONCHANGED:
81 engine_->OnDwmCompositionChanged();
82 break;
83
84 case WM_SIZE:
85 if (wpar == SIZE_MAXIMIZED || wpar == SIZE_RESTORED) {
86 OnWindowStateEvent(hwnd, WindowStateEvent::kShow);
87 } else if (wpar == SIZE_MINIMIZED) {
88 OnWindowStateEvent(hwnd, WindowStateEvent::kHide);
89 }
90 break;
91
92 case WM_SHOWWINDOW:
93 if (!wpar) {
94 OnWindowStateEvent(hwnd, WindowStateEvent::kHide);
95 } else {
96 OnWindowStateEvent(hwnd, WindowStateEvent::kShow);
97 }
98 break;
99
100 case WM_DESTROY:
101 OnWindowStateEvent(hwnd, WindowStateEvent::kHide);
102 break;
103 }
104 return false;
105 }

References flutter::kHide, flutter::kShow, flutter::FlutterWindowsEngine::OnDwmCompositionChanged(), and OnWindowStateEvent().

The documentation for this class was generated from the following files:

- shell/platform/windows/windows_lifecycle_manager.h
- shell/platform/windows/windows_lifecycle_manager.cc

flutter::AppLifecycleState::kHidden
@ kHidden
flutter::WindowStateEvent::kHide
@ kHide
flutter::NumWindowsForThread
static int64_t NumWindowsForThread(const THREADENTRY32 &thread)
Definition: windows_lifecycle_manager.cc:149
flutter::FlutterWindowsEngine::SendPlatformMessage
bool SendPlatformMessage(const char *channel, const uint8_t *message, const size_t message_size, const FlutterDesktopBinaryReply reply, void *user_data)
Definition: flutter_windows_engine.cc:699
flutter::WindowsLifecycleManager::DispatchMessage
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
Definition: windows_lifecycle_manager.cc:34
flutter::FlutterWindowsEngine::OnDwmCompositionChanged
void OnDwmCompositionChanged()
Definition: flutter_windows_engine.cc:953
flutter::WindowsLifecycleManager::SetLifecycleState
virtual void SetLifecycleState(AppLifecycleState state)
Definition: windows_lifecycle_manager.cc:195
flutter::WindowStateEvent::kFocus
@ kFocus
flutter::AppLifecycleState::kInactive
@ kInactive
flutter::WindowStateEvent::kShow
@ kShow
flutter::WindowsLifecycleManager::OnWindowStateEvent
virtual void OnWindowStateEvent(HWND hwnd, WindowStateEvent event)
Definition: windows_lifecycle_manager.cc:208
flutter::AppLifecycleState::kResumed
@ kResumed
flutter::WindowStateEvent::kUnfocus
@ kUnfocus
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter::AppLifecycleStateToString
constexpr const char * AppLifecycleStateToString(AppLifecycleState state)
Definition: app_lifecycle_state.h:72