Flutter Windows Embedder
flutter::WindowsLifecycleManager Class Reference

#include <windows_lifecycle_manager.h>

Inheritance diagram for flutter::WindowsLifecycleManager:
flutter::testing::MockWindowsLifecycleManager

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 
)
protectedvirtual

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 259 of file windows_lifecycle_manager.cc.

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

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 ( )
protectedvirtual

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 211 of file windows_lifecycle_manager.cc.

212  {
213  // Synthesize an unfocus event when a focused window is hidden.
214  if (event == WindowStateEvent::kHide &&
215  focused_windows_.find(hwnd) != focused_windows_.end()) {
217  }
218 
219  std::lock_guard guard(state_update_lock_);
220  switch (event) {
222  bool first_shown_window = visible_windows_.empty();
223  auto pair = visible_windows_.insert(hwnd);
224  if (first_shown_window && pair.second &&
225  state_ == AppLifecycleState::kHidden) {
227  }
228  break;
229  }
231  bool present = visible_windows_.erase(hwnd);
232  bool empty = visible_windows_.empty();
233  if (present && empty &&
234  (state_ == AppLifecycleState::kResumed ||
235  state_ == AppLifecycleState::kInactive)) {
237  }
238  break;
239  }
241  bool first_focused_window = focused_windows_.empty();
242  auto pair = focused_windows_.insert(hwnd);
243  if (first_focused_window && pair.second &&
244  state_ == AppLifecycleState::kInactive) {
246  }
247  break;
248  }
250  if (focused_windows_.erase(hwnd) && focused_windows_.empty() &&
251  state_ == AppLifecycleState::kResumed) {
253  }
254  break;
255  }
256  }
257 }

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 198 of file windows_lifecycle_manager.cc.

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

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) {
87  } else if (wpar == SIZE_MINIMIZED) {
89  }
90  break;
91 
92  case WM_SHOWWINDOW:
93  if (!wpar) {
95  } else {
97  }
98  break;
99 
100  case WM_DESTROY:
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:
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:528
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:805
flutter::WindowsLifecycleManager::SetLifecycleState
virtual void SetLifecycleState(AppLifecycleState state)
Definition: windows_lifecycle_manager.cc:198
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:211
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