| 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 ()
bool | IsUpdateStateScheduled () const
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 }
message
Win32Message message
Definition: keyboard_unittests.cc:137

References message.

Referenced by Quit().

## ◆ ExternalWindowMessage()

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

Definition at line 255 of file windows_lifecycle_manager.cc.

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

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

## ◆ GetLifecycleState()

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

Definition at line 71 of file windows_lifecycle_manager.h.

71 { return state_; }

## ◆ 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 }
flutter::NumWindowsForThread
static int64_t NumWindowsForThread(const THREADENTRY32 &thread)
Definition: windows_lifecycle_manager.cc:149

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

## ◆ IsUpdateStateScheduled()

bool flutter::WindowsLifecycleManager::IsUpdateStateScheduled | ( |  | ) | const
| inline

Definition at line 74 of file windows_lifecycle_manager.h.

74 { return update_state_scheduled_; }

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

## ◆ OnWindowStateEvent()

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

Definition at line 218 of file windows_lifecycle_manager.cc.

219 {
220 // Instead of updating the state immediately, remember all
221 // changes to individual window and then update the state in next run loop
222 // turn. Otherwise the application would be temporarily deactivated when
223 // switching focus between windows for example.
224 if (!update_state_scheduled_) {
225 update_state_scheduled_ = true;
226 // Task runner will be destroyed together with engine so it is safe
227 // to keep reference to it.
228 engine_->task_runner()->PostTask([this]() {
229 update_state_scheduled_ = false;
230 UpdateState();
231 });
232 }
233
234 switch (event) {
235 case WindowStateEvent::kShow: {
236 visible_windows_.insert(hwnd);
237 break;
238 }
239 case WindowStateEvent::kHide: {
240 visible_windows_.erase(hwnd);
241 focused_windows_.erase(hwnd);
242 break;
243 }
244 case WindowStateEvent::kFocus: {
245 focused_windows_.insert(hwnd);
246 break;
247 }
248 case WindowStateEvent::kUnfocus: {
249 focused_windows_.erase(hwnd);
250 break;
251 }
252 }
253 }
flutter::FlutterWindowsEngine::task_runner
TaskRunner * task_runner()
Definition: flutter_windows_engine.h:161
flutter::TaskRunner::PostTask
void PostTask(TaskClosure task)
Definition: task_runner.cc:88

Referenced by 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 }
flutter::WindowsLifecycleManager::DispatchMessage
virtual void DispatchMessage(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
Definition: windows_lifecycle_manager.cc:34

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 }
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:724
flutter::AppLifecycleStateToString
constexpr const char * AppLifecycleStateToString(AppLifecycleState state)
Definition: app_lifecycle_state.h:72

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 }
flutter::FlutterWindowsEngine::OnDwmCompositionChanged
void OnDwmCompositionChanged()
Definition: flutter_windows_engine.cc:1017

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