Flutter Windows Embedder
flutter::HostWindow Class Reference

#include <host_window.h>

Inheritance diagram for flutter::HostWindow:
flutter::HostWindowSized flutter::HostWindowDialog flutter::HostWindowPopup flutter::HostWindowRegular flutter::HostWindowSatellite flutter::HostWindowTooltip

Classes

struct  HostWindowInitializationParams
 
struct  SavedWindowInfo
 

Public Member Functions

virtual ~HostWindow ()
 
HWND GetWindowHandle () const
 
HWND GetFlutterViewWindowHandle () const
 
void SetContentSize (const WindowSizeRequest &size)
 
void SetConstraints (const WindowConstraints &constraints)
 
virtual void SetFullscreen (bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
 
virtual bool GetFullscreen () const
 
HostWindow * GetOwnerWindow () const
 
void UpdateModalStateLayer ()
 

Static Public Member Functions

static std::unique_ptr< HostWindow > CreateRegularWindow (WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title, bool sized_to_content, bool resizable)
 
static std::unique_ptr< HostWindow > CreateDialogWindow (WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title, HWND parent, bool sized_to_content, bool resizable)
 
static std::unique_ptr< HostWindow > CreateTooltipWindow (WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
 
static std::unique_ptr< HostWindow > CreatePopupWindow (WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
 
static std::unique_ptr< HostWindow > CreateSatelliteWindow (WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent, LPCWSTR title, bool sized_to_content, bool resizable)
 
static HostWindow * GetThisFromHandle (HWND hwnd)
 
static ActualWindowSize GetWindowContentSize (HWND hwnd)
 

Protected Member Functions

WindowArchetype GetArchetype () const
 
void InitializeFlutterView (HostWindowInitializationParams const &params)
 
 HostWindow (WindowManager *window_manager, FlutterWindowsEngine *engine)
 
void AlignOriginWithFrame ()
 
virtual void OnFirstFrame ()
 
virtual LRESULT HandleMessage (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
 
void HandleWindowActivation (HWND hwnd, WPARAM wparam)
 
void EnableRecursively (bool enable)
 
HostWindow * FindFirstEnabledDescendant () const
 
std::vector< HostWindow * > GetOwnedWindows () const
 
void DisableRecursively ()
 
 FML_DISALLOW_COPY_AND_ASSIGN (HostWindow)
 

Static Protected Member Functions

static std::optional< Size > GetWindowSizeForClientSize (WindowsProcTable const &win32, Size const &client_size, std::optional< Size > smallest, std::optional< Size > biggest, DWORD window_style, DWORD extended_window_style, std::optional< HWND > const &owner_hwnd)
 
static std::optional< Size > GetInitialWindowSize (FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const BoxConstraints &constraints, DWORD window_style, DWORD extended_window_style, std::optional< HWND > const &owner_window, bool sized_to_content)
 
static void FocusRootViewOf (HostWindow *window)
 
static LRESULT WndProc (HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
 

Protected Attributes

friend WindowManager
 
WindowManager *const window_manager_ = nullptr
 
FlutterWindowsEngine * engine_
 
std::unique_ptr< FlutterWindowsViewController > view_controller_
 
std::shared_ptr< int > alive_ = std::make_shared<int>(0)
 
WindowArchetype archetype_ = WindowArchetype::kRegular
 
HWND window_handle_
 
BoxConstraints box_constraints_
 
bool is_being_destroyed_ = false
 
bool is_fullscreen_ = false
 
SavedWindowInfo saved_window_info_
 
Microsoft::WRL::ComPtr< ITaskbarList2 > task_bar_list_
 

Detailed Description

Definition at line 29 of file host_window.h.

Constructor & Destructor Documentation

◆ ~HostWindow()

flutter::HostWindow::~HostWindow ( )
virtual

Definition at line 351 of file host_window.cc.

351  {
352  if (view_controller_) {
353  // Unregister the window class. Fail silently if other windows are still
354  // using the class, as only the last window can successfully unregister it.
355  if (!UnregisterClass(kWindowClassName, GetModuleHandle(nullptr))) {
356  // Clear the error state after the failed unregistration.
357  SetLastError(ERROR_SUCCESS);
358  }
359  }
360 }
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:314

◆ HostWindow()

flutter::HostWindow::HostWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine 
)
protected

Definition at line 245 of file host_window.cc.

247  : window_manager_(window_manager), engine_(engine) {}
FlutterWindowsEngine * engine_
Definition: host_window.h:309
WindowManager *const window_manager_
Definition: host_window.h:306

Member Function Documentation

◆ AlignOriginWithFrame()

void flutter::HostWindow::AlignOriginWithFrame ( )
protected

Definition at line 328 of file host_window.cc.

328  {
329  RECT frame_rect;
330  if (FAILED(DwmGetWindowAttribute(window_handle_, DWMWA_EXTENDED_FRAME_BOUNDS,
331  &frame_rect, sizeof(frame_rect)))) {
332  return;
333  }
334  RECT window_rect;
335  if (!GetWindowRect(window_handle_, &window_rect)) {
336  return;
337  }
338 
339  LONG const left_dropshadow_width = frame_rect.left - window_rect.left;
340  LONG const top_dropshadow_height = window_rect.top - frame_rect.top;
341  if (left_dropshadow_width == 0 && top_dropshadow_height == 0) {
342  return;
343  }
344 
345  SetWindowPos(window_handle_, nullptr,
346  window_rect.left - left_dropshadow_width,
347  window_rect.top - top_dropshadow_height, 0, 0,
348  SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
349 }

Referenced by InitializeFlutterView().

◆ CreateDialogWindow()

std::unique_ptr< HostWindow > flutter::HostWindow::CreateDialogWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine,
const WindowSizeRequest &  preferred_size,
const WindowConstraints &  preferred_constraints,
LPCWSTR  title,
HWND  parent,
bool  sized_to_content,
bool  resizable 
)
static

Definition at line 192 of file host_window.cc.

200  {
201  return std::unique_ptr<HostWindow>(new HostWindowDialog(
202  window_manager, engine, preferred_size,
203  FromWindowConstraints(preferred_constraints), title,
204  parent ? parent : std::optional<HWND>(), sized_to_content, resizable));
205 }

Referenced by flutter::WindowManager::CreateDialogWindow().

◆ CreatePopupWindow()

std::unique_ptr< HostWindow > flutter::HostWindow::CreatePopupWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine,
const WindowConstraints &  preferred_constraints,
GetWindowPositionCallback  get_position_callback,
HWND  parent 
)
static

Definition at line 218 of file host_window.cc.

223  {
224  return std::unique_ptr<HostWindowPopup>(new HostWindowPopup(
225  window_manager, engine, FromWindowConstraints(preferred_constraints),
226  get_position_callback, parent));
227 }

Referenced by flutter::WindowManager::CreatePopupWindow().

◆ CreateRegularWindow()

std::unique_ptr< HostWindow > flutter::HostWindow::CreateRegularWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine,
const WindowSizeRequest &  preferred_size,
const WindowConstraints &  preferred_constraints,
LPCWSTR  title,
bool  sized_to_content,
bool  resizable 
)
static

Definition at line 178 of file host_window.cc.

185  {
186  return std::unique_ptr<HostWindow>(
187  new HostWindowRegular(window_manager, engine, preferred_size,
188  FromWindowConstraints(preferred_constraints), title,
189  sized_to_content, resizable));
190 }

Referenced by flutter::WindowManager::CreateRegularWindow().

◆ CreateSatelliteWindow()

std::unique_ptr< HostWindow > flutter::HostWindow::CreateSatelliteWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine,
const WindowSizeRequest &  preferred_size,
const WindowConstraints &  preferred_constraints,
GetWindowPositionCallback  get_position_callback,
HWND  parent,
LPCWSTR  title,
bool  sized_to_content,
bool  resizable 
)
static

Definition at line 229 of file host_window.cc.

238  {
239  return std::unique_ptr<HostWindowSatellite>(new HostWindowSatellite(
240  window_manager, engine, preferred_size,
241  FromWindowConstraints(preferred_constraints), get_position_callback,
242  parent, title, sized_to_content, resizable));
243 }

Referenced by flutter::WindowManager::CreateSatelliteWindow().

◆ CreateTooltipWindow()

std::unique_ptr< HostWindow > flutter::HostWindow::CreateTooltipWindow ( WindowManager *  window_manager,
FlutterWindowsEngine *  engine,
const WindowConstraints &  preferred_constraints,
GetWindowPositionCallback  get_position_callback,
HWND  parent 
)
static

Definition at line 207 of file host_window.cc.

212  {
213  return std::unique_ptr<HostWindowTooltip>(new HostWindowTooltip(
214  window_manager, engine, FromWindowConstraints(preferred_constraints),
215  get_position_callback, parent));
216 }

Referenced by flutter::WindowManager::CreateTooltipWindow().

◆ DisableRecursively()

void flutter::HostWindow::DisableRecursively ( )
protected

Definition at line 930 of file host_window.cc.

930  {
931  // Disable the window itself.
932  EnableWindow(window_handle_, false);
933 
934  for (HostWindow* const owned : GetOwnedWindows()) {
935  owned->DisableRecursively();
936  }
937 }
HostWindow(WindowManager *window_manager, FlutterWindowsEngine *engine)
Definition: host_window.cc:245
std::vector< HostWindow * > GetOwnedWindows() const
Definition: host_window.cc:900

◆ EnableRecursively()

void flutter::HostWindow::EnableRecursively ( bool  enable)
protected

Definition at line 878 of file host_window.cc.

878  {
879  EnableWindow(window_handle_, enable);
880 
881  for (HostWindow* const owned : GetOwnedWindows()) {
882  owned->EnableRecursively(enable);
883  }
884 }

◆ FindFirstEnabledDescendant()

HostWindow * flutter::HostWindow::FindFirstEnabledDescendant ( ) const
protected

Definition at line 886 of file host_window.cc.

886  {
887  if (IsWindowEnabled(window_handle_)) {
888  return const_cast<HostWindow*>(this);
889  }
890 
891  for (HostWindow* const owned : GetOwnedWindows()) {
892  if (HostWindow* const result = owned->FindFirstEnabledDescendant()) {
893  return result;
894  }
895  }
896 
897  return nullptr;
898 }

References FindFirstEnabledDescendant().

Referenced by FindFirstEnabledDescendant().

◆ FML_DISALLOW_COPY_AND_ASSIGN()

flutter::HostWindow::FML_DISALLOW_COPY_AND_ASSIGN ( HostWindow  )
protected

◆ FocusRootViewOf()

void flutter::HostWindow::FocusRootViewOf ( HostWindow *  window)
staticprotected

Definition at line 385 of file host_window.cc.

385  {
386  auto child_content = window->view_controller_->view()->GetWindowHandle();
387  if (window != nullptr && child_content != nullptr) {
388  SetFocus(child_content);
389  }
390 };

References view_controller_.

Referenced by flutter::HostWindowDialog::HandleMessage().

◆ GetArchetype()

WindowArchetype flutter::HostWindow::GetArchetype ( ) const
inlineprotected

Definition at line 177 of file host_window.h.

177 { return archetype_; }
WindowArchetype archetype_
Definition: host_window.h:322

References archetype_.

Referenced by flutter::WindowManager::GetSatelliteWindowFromHandle().

◆ GetFlutterViewWindowHandle()

HWND flutter::HostWindow::GetFlutterViewWindowHandle ( ) const

Definition at line 381 of file host_window.cc.

381  {
382  return view_controller_->view()->GetWindowHandle();
383 }

References GetWindowHandle().

Referenced by flutter::WindowManager::HandleMessage().

◆ GetFullscreen()

bool flutter::HostWindow::GetFullscreen ( ) const
virtual

Reimplemented in flutter::HostWindowDialog.

Definition at line 788 of file host_window.cc.

788  {
789  return is_fullscreen_;
790 }

Referenced by InternalFlutterWindows_WindowManager_GetFullscreen().

◆ GetInitialWindowSize()

std::optional< Size > flutter::HostWindow::GetInitialWindowSize ( FlutterWindowsEngine *  engine,
const WindowSizeRequest &  preferred_size,
const BoxConstraints &  constraints,
DWORD  window_style,
DWORD  extended_window_style,
std::optional< HWND > const &  owner_window,
bool  sized_to_content 
)
staticprotected

Definition at line 852 of file host_window.cc.

859  {
860  double client_width;
861  double client_height;
862  if (sized_to_content) {
863  // Use the minimum constraint as the initial window size. The window will
864  // be resized to match the rendered content after the first frame.
865  client_width = std::max(1.0, constraints.smallest().width());
866  client_height = std::max(1.0, constraints.smallest().height());
867  } else {
868  client_width = preferred_size.preferred_view_width;
869  client_height = preferred_size.preferred_view_height;
870  }
871 
873  *engine->windows_proc_table(), Size(client_width, client_height),
874  constraints.smallest(), constraints.biggest(), window_style,
875  extended_window_style, owner_window);
876 }
static std::optional< Size > GetWindowSizeForClientSize(WindowsProcTable const &win32, Size const &client_size, std::optional< Size > smallest, std::optional< Size > biggest, DWORD window_style, DWORD extended_window_style, std::optional< HWND > const &owner_hwnd)
Definition: host_window.cc:805

References flutter::WindowSizeRequest::preferred_view_height, flutter::WindowSizeRequest::preferred_view_width, and flutter::FlutterWindowsEngine::windows_proc_table().

◆ GetOwnedWindows()

std::vector< HostWindow * > flutter::HostWindow::GetOwnedWindows ( ) const
protected

Definition at line 900 of file host_window.cc.

900  {
901  std::vector<HostWindow*> owned_windows;
902  struct EnumData {
903  HWND owner_window_handle;
904  std::vector<HostWindow*>* owned_windows;
905  } data{window_handle_, &owned_windows};
906 
907  EnumWindows(
908  [](HWND hwnd, LPARAM lparam) -> BOOL {
909  auto* const data = reinterpret_cast<EnumData*>(lparam);
910  if (GetWindow(hwnd, GW_OWNER) == data->owner_window_handle) {
911  HostWindow* const window = GetThisFromHandle(hwnd);
912  if (window && !window->is_being_destroyed_) {
913  data->owned_windows->push_back(window);
914  }
915  }
916  return TRUE;
917  },
918  reinterpret_cast<LPARAM>(&data));
919 
920  return owned_windows;
921 }
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:362

References is_being_destroyed_.

◆ GetOwnerWindow()

HostWindow * flutter::HostWindow::GetOwnerWindow ( ) const

Definition at line 923 of file host_window.cc.

923  {
924  if (HWND const owner_window_handle = GetWindow(GetWindowHandle(), GW_OWNER)) {
925  return GetThisFromHandle(owner_window_handle);
926  }
927  return nullptr;
928 };
HWND GetWindowHandle() const
Definition: host_window.cc:377

Referenced by flutter::HostWindowDialog::HandleMessage(), and flutter::testing::TEST_F().

◆ GetThisFromHandle()

HostWindow * flutter::HostWindow::GetThisFromHandle ( HWND  hwnd)
static

Definition at line 362 of file host_window.cc.

362  {
363  wchar_t class_name[256];
364  if (!GetClassName(hwnd, class_name, sizeof(class_name) / sizeof(wchar_t))) {
365  FML_LOG(ERROR) << "Failed to get class name for window handle " << hwnd
366  << ": " << GetLastErrorAsString();
367  return nullptr;
368  }
369  // Ignore window handles that do not match the expected class name.
370  if (wcscmp(class_name, kWindowClassName) != 0) {
371  return nullptr;
372  }
373 
374  return reinterpret_cast<HostWindow*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
375 }

Referenced by flutter::WindowManager::GetSatelliteWindowFromHandle(), flutter::WindowManager::HandleMessage(), InternalFlutterWindows_WindowManager_GetFullscreen(), InternalFlutterWindows_WindowManager_SetFullscreen(), InternalFlutterWindows_WindowManager_SetWindowConstraints(), InternalFlutterWindows_WindowManager_SetWindowSize(), InternalFlutterWindows_WindowManager_UpdatePopupPosition(), InternalFlutterWindows_WindowManager_UpdateTooltipPosition(), and flutter::testing::TEST_F().

◆ GetWindowContentSize()

ActualWindowSize flutter::HostWindow::GetWindowContentSize ( HWND  hwnd)
static

Definition at line 792 of file host_window.cc.

792  {
793  RECT rect;
794  GetClientRect(hwnd, &rect);
795  double const dpr = FlutterDesktopGetDpiForHWND(hwnd) /
796  static_cast<double>(USER_DEFAULT_SCREEN_DPI);
797  double const width = rect.right / dpr;
798  double const height = rect.bottom / dpr;
799  return {
800  .width = rect.right / dpr,
801  .height = rect.bottom / dpr,
802  };
803 }
UINT FlutterDesktopGetDpiForHWND(HWND hwnd)

References FlutterDesktopGetDpiForHWND().

Referenced by InternalFlutterWindows_WindowManager_GetWindowContentSize().

◆ GetWindowHandle()

HWND flutter::HostWindow::GetWindowHandle ( ) const

Definition at line 377 of file host_window.cc.

377  {
378  return window_handle_;
379 }

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

◆ GetWindowSizeForClientSize()

std::optional< Size > flutter::HostWindow::GetWindowSizeForClientSize ( WindowsProcTable const &  win32,
Size const &  client_size,
std::optional< Size >  smallest,
std::optional< Size >  biggest,
DWORD  window_style,
DWORD  extended_window_style,
std::optional< HWND > const &  owner_hwnd 
)
staticprotected

Definition at line 805 of file host_window.cc.

812  {
813  UINT const dpi = GetDpiForHWND(owner_hwnd ? *owner_hwnd : nullptr);
814  double const scale_factor =
815  static_cast<double>(dpi) / USER_DEFAULT_SCREEN_DPI;
816  RECT rect = {
817  .right = static_cast<LONG>(client_size.width() * scale_factor),
818  .bottom = static_cast<LONG>(client_size.height() * scale_factor)};
819 
820  if (!win32.AdjustWindowRectExForDpi(&rect, window_style, FALSE,
821  extended_window_style, dpi)) {
822  FML_LOG(ERROR) << "Failed to run AdjustWindowRectExForDpi: "
823  << GetLastErrorAsString();
824  return std::nullopt;
825  }
826 
827  double width = static_cast<double>(rect.right - rect.left);
828  double height = static_cast<double>(rect.bottom - rect.top);
829 
830  // Apply size constraints.
831  double const non_client_width = width - (client_size.width() * scale_factor);
832  double const non_client_height =
833  height - (client_size.height() * scale_factor);
834  if (smallest) {
835  flutter::Size min_physical_size = ClampToVirtualScreen(
836  flutter::Size(smallest->width() * scale_factor + non_client_width,
837  smallest->height() * scale_factor + non_client_height));
838  width = std::max(width, min_physical_size.width());
839  height = std::max(height, min_physical_size.height());
840  }
841  if (biggest) {
842  flutter::Size max_physical_size = ClampToVirtualScreen(
843  flutter::Size(biggest->width() * scale_factor + non_client_width,
844  biggest->height() * scale_factor + non_client_height));
845  width = std::min(width, max_physical_size.width());
846  height = std::min(height, max_physical_size.height());
847  }
848 
849  return flutter::Size{width, height};
850 }
UINT GetDpiForHWND(HWND hwnd)
Definition: dpi_utils.cc:128

References flutter::WindowsProcTable::AdjustWindowRectExForDpi(), and flutter::GetDpiForHWND().

Referenced by flutter::HostWindowSized::ApplyContentSize().

◆ HandleMessage()

LRESULT flutter::HostWindow::HandleMessage ( HWND  hwnd,
UINT  message,
WPARAM  wparam,
LPARAM  lparam 
)
protectedvirtual

Reimplemented in flutter::HostWindowTooltip, flutter::HostWindowSatellite, and flutter::HostWindowDialog.

Definition at line 432 of file host_window.cc.

435  {
437  window_handle_, message, wparam, lparam);
438  if (result) {
439  return *result;
440  }
441 
442  switch (message) {
443  case WM_DESTROY:
444  is_being_destroyed_ = true;
445  break;
446 
447  case WM_NCLBUTTONDOWN: {
448  // Fix for 500ms hang after user clicks on the title bar, but before
449  // moving mouse. Reference:
450  // https://gamedev.net/forums/topic/672094-keeping-things-moving-during-win32-moveresize-events/5254386/
451  if (SendMessage(window_handle_, WM_NCHITTEST, wparam, lparam) ==
452  HTCAPTION) {
453  POINT cursorPos;
454  // Get the current cursor position and synthesize WM_MOUSEMOVE to
455  // unblock default window proc implementation for WM_NCLBUTTONDOWN at
456  // HTCAPTION.
457  GetCursorPos(&cursorPos);
458  ScreenToClient(window_handle_, &cursorPos);
459  PostMessage(window_handle_, WM_MOUSEMOVE, 0,
460  MAKELPARAM(cursorPos.x, cursorPos.y));
461  }
462  break;
463  }
464 
465  case WM_DPICHANGED: {
466  auto* const new_scaled_window_rect = reinterpret_cast<RECT*>(lparam);
467  LONG const width =
468  new_scaled_window_rect->right - new_scaled_window_rect->left;
469  LONG const height =
470  new_scaled_window_rect->bottom - new_scaled_window_rect->top;
471  SetWindowPos(hwnd, nullptr, new_scaled_window_rect->left,
472  new_scaled_window_rect->top, width, height,
473  SWP_NOZORDER | SWP_NOACTIVATE);
474  return 0;
475  }
476 
477  case WM_GETMINMAXINFO: {
478  RECT window_rect;
479  GetWindowRect(hwnd, &window_rect);
480  RECT client_rect;
481  GetClientRect(hwnd, &client_rect);
482  LONG const non_client_width = (window_rect.right - window_rect.left) -
483  (client_rect.right - client_rect.left);
484  LONG const non_client_height = (window_rect.bottom - window_rect.top) -
485  (client_rect.bottom - client_rect.top);
486 
487  UINT const dpi = flutter::GetDpiForHWND(hwnd);
488  double const scale_factor =
489  static_cast<double>(dpi) / USER_DEFAULT_SCREEN_DPI;
490 
491  MINMAXINFO* info = reinterpret_cast<MINMAXINFO*>(lparam);
492  Size const min_physical_size = ClampToVirtualScreen(Size(
493  box_constraints_.smallest().width() * scale_factor + non_client_width,
494  box_constraints_.smallest().height() * scale_factor +
495  non_client_height));
496 
497  info->ptMinTrackSize.x = min_physical_size.width();
498  info->ptMinTrackSize.y = min_physical_size.height();
499  Size const max_physical_size = ClampToVirtualScreen(Size(
500  box_constraints_.biggest().width() * scale_factor + non_client_width,
501  box_constraints_.biggest().height() * scale_factor +
502  non_client_height));
503 
504  info->ptMaxTrackSize.x = max_physical_size.width();
505  info->ptMaxTrackSize.y = max_physical_size.height();
506  return 0;
507  }
508 
509  case WM_SIZE: {
510  auto child_content = view_controller_->view()->GetWindowHandle();
511  if (child_content != nullptr) {
512  // Resize and reposition the child content window.
513  RECT client_rect;
514  GetClientRect(hwnd, &client_rect);
515  MoveWindow(child_content, client_rect.left, client_rect.top,
516  client_rect.right - client_rect.left,
517  client_rect.bottom - client_rect.top, TRUE);
518  }
519  return 0;
520  }
521 
522  case WM_ACTIVATE:
523  HandleWindowActivation(hwnd, wparam);
524  return 0;
525 
526  case WM_WINDOWPOSCHANGED: {
527  // Satellites are anchored to this window's client area, which can move
528  // even if the window itself does not (e.g. when a DPI change resizes the
529  // title bar).
530  auto const* const window_pos = reinterpret_cast<WINDOWPOS*>(lparam);
531  UINT const kNoClientChange = SWP_NOMOVE | SWP_NOSIZE;
532  if (window_pos &&
533  ((window_pos->flags & kNoClientChange) != kNoClientChange ||
534  (window_pos->flags & SWP_FRAMECHANGED))) {
535  for (HostWindow* const owned : GetOwnedWindows()) {
536  if (owned->GetArchetype() == WindowArchetype::kSatellite) {
537  static_cast<HostWindowSatellite*>(owned)->OnParentMoved();
538  }
539  }
540  }
541  break;
542  }
543 
544  case WM_DWMCOLORIZATIONCOLORCHANGED:
545  UpdateTheme(hwnd);
546  return 0;
547 
548  default:
549  break;
550  }
551 
552  if (!view_controller_) {
553  return 0;
554  }
555 
556  return DefWindowProc(hwnd, message, wparam, lparam);
557 }
WindowProcDelegateManager * window_proc_delegate_manager()
BoxConstraints box_constraints_
Definition: host_window.h:328
void HandleWindowActivation(HWND hwnd, WPARAM wparam)
Definition: host_window.cc:392
std::optional< LRESULT > OnTopLevelWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) const
Win32Message message

Referenced by flutter::HostWindowDialog::HandleMessage(), flutter::HostWindowSatellite::HandleMessage(), and flutter::HostWindowTooltip::HandleMessage().

◆ HandleWindowActivation()

void flutter::HostWindow::HandleWindowActivation ( HWND  hwnd,
WPARAM  wparam 
)
protected

Definition at line 392 of file host_window.cc.

392  {
393  if (LOWORD(wparam) == WA_INACTIVE) {
394  // The window is being deactivated; leave focus untouched. Focusing this
395  // window's view would call SetFocus on its content, which reactivates the
396  // window (SetFocus activates the parent of the focused window) and pulls it
397  // back to the top of the z-order, taking activation from the window that is
398  // being activated.
399  return;
400  }
401 
402  if (!IsWindowEnabled(hwnd)) {
403  // Prevent a disabled window (e.g. the owner of a modal dialog) from being
404  // activated using the task switcher by redirecting focus and activation to
405  // the first enabled descendant.
406  if (HostWindow* const enabled_descendant = FindFirstEnabledDescendant()) {
407  SetActiveWindow(enabled_descendant->GetWindowHandle());
408  FocusRootViewOf(this);
409  }
410  return;
411  }
412 
413  FocusRootViewOf(this);
414 }
static void FocusRootViewOf(HostWindow *window)
Definition: host_window.cc:385
HostWindow * FindFirstEnabledDescendant() const
Definition: host_window.cc:886

Referenced by flutter::HostWindowDialog::HandleMessage(), and flutter::HostWindowSatellite::HandleMessage().

◆ InitializeFlutterView()

void flutter::HostWindow::InitializeFlutterView ( HostWindowInitializationParams const &  params)
protected

Definition at line 249 of file host_window.cc.

250  {
251  // Set up the view.
252  auto view_window = std::make_unique<FlutterWindow>(
253  params.initial_window_rect.width(), params.initial_window_rect.height(),
255 
256  std::unique_ptr<FlutterWindowsView> view =
257  engine_->CreateView(std::move(view_window), params.is_sized_to_content,
258  params.box_constraints, /*allow_implicit_view=*/false,
259  params.sizing_delegate);
260  FML_CHECK(view != nullptr);
261 
263  std::make_unique<FlutterWindowsViewController>(nullptr, std::move(view));
264  FML_CHECK(engine_->running());
265  // The Windows embedder listens to accessibility updates using the
266  // view's HWND. The embedder's accessibility features may be stale if
267  // the app was in headless mode.
269 
270  // Register the window class.
271  if (!IsClassRegistered(kWindowClassName)) {
272  auto const idi_app_icon = 101;
273  WNDCLASSEX window_class = {};
274  window_class.cbSize = sizeof(WNDCLASSEX);
275  window_class.style = CS_HREDRAW | CS_VREDRAW;
276  window_class.lpfnWndProc = HostWindow::WndProc;
277  window_class.hInstance = GetModuleHandle(nullptr);
278  window_class.hIcon =
279  LoadIcon(window_class.hInstance, MAKEINTRESOURCE(idi_app_icon));
280  if (!window_class.hIcon) {
281  window_class.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
282  }
283  window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
284  window_class.lpszClassName = kWindowClassName;
285 
286  FML_CHECK(RegisterClassEx(&window_class));
287  }
288 
289  // Create the native window.
290  window_handle_ = CreateWindowEx(
291  params.extended_window_style, kWindowClassName, params.title,
292  params.window_style, params.initial_window_rect.left(),
293  params.initial_window_rect.top(), params.initial_window_rect.width(),
294  params.initial_window_rect.height(),
295  params.owner_window ? *params.owner_window : nullptr, nullptr,
296  GetModuleHandle(nullptr), engine_->windows_proc_table().get());
297  FML_CHECK(window_handle_ != nullptr);
298 
299  // Adjust the window position so its origin aligns with the top-left corner
300  // of the window frame, not the window rectangle (which includes the
301  // drop-shadow). This adjustment must be done post-creation since the frame
302  // rectangle is only available after the window has been created.
304 
305  UpdateTheme(window_handle_);
306 
307  SetChildContent(view_controller_->view()->GetWindowHandle(), window_handle_);
308 
309  // Defer showing the window until the first frame is rendered so the user
310  // doesn't see a blank window. Each view has its own first-frame callback,
311  // so this works correctly for multi-window apps.
312  view_controller_->view()->SetFirstFrameCallback(
313  [this, weak_alive = std::weak_ptr<int>(alive_), hwnd = window_handle_,
314  cmd_show = params.nCmdShow]() {
315  if (weak_alive.expired() || !::IsWindow(hwnd)) {
316  return;
317  }
318  OnFirstFrame();
319  ShowWindow(hwnd, cmd_show);
320  });
321  archetype_ = params.archetype;
322  SetWindowLongPtr(window_handle_, GWLP_USERDATA,
323  reinterpret_cast<LONG_PTR>(this));
324 }
std::shared_ptr< WindowsProcTable > windows_proc_table()
std::shared_ptr< DisplayManagerWin32 > display_manager()
std::unique_ptr< FlutterWindowsView > CreateView(std::unique_ptr< WindowBindingHandler > window, bool is_sized_to_content, const BoxConstraints &box_constraints, bool allow_implicit_view, FlutterWindowsViewSizingDelegate *sizing_delegate=nullptr)
virtual void OnFirstFrame()
Definition: host_window.cc:326
static LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:416
std::shared_ptr< int > alive_
Definition: host_window.h:319

References AlignOriginWithFrame(), alive_, flutter::HostWindow::HostWindowInitializationParams::box_constraints, flutter::FlutterWindowsEngine::CreateView(), flutter::FlutterWindowsEngine::display_manager(), engine_, flutter::HostWindow::HostWindowInitializationParams::extended_window_style, flutter::HostWindow::HostWindowInitializationParams::initial_window_rect, flutter::HostWindow::HostWindowInitializationParams::is_sized_to_content, flutter::HostWindow::HostWindowInitializationParams::nCmdShow, OnFirstFrame(), flutter::HostWindow::HostWindowInitializationParams::owner_window, flutter::FlutterWindowsEngine::running(), flutter::HostWindow::HostWindowInitializationParams::sizing_delegate, flutter::HostWindow::HostWindowInitializationParams::title, flutter::FlutterWindowsEngine::UpdateAccessibilityFeatures(), view_controller_, window_handle_, flutter::HostWindow::HostWindowInitializationParams::window_style, flutter::FlutterWindowsEngine::windows_proc_table(), and WndProc().

Referenced by flutter::HostWindowDialog::HostWindowDialog(), and flutter::HostWindowRegular::HostWindowRegular().

◆ OnFirstFrame()

void flutter::HostWindow::OnFirstFrame ( )
protectedvirtual

Reimplemented in flutter::HostWindowSatellite.

Definition at line 326 of file host_window.cc.

326 {}

Referenced by InitializeFlutterView().

◆ SetConstraints()

void flutter::HostWindow::SetConstraints ( const WindowConstraints &  constraints)

Definition at line 601 of file host_window.cc.

601  {
602  box_constraints_ = FromWindowConstraints(constraints);
603 
604  if (GetFullscreen()) {
605  std::optional<Size> const window_size = GetWindowSizeForClientSize(
609  box_constraints_.smallest(), box_constraints_.biggest(),
611  if (!window_size) {
612  return;
613  }
614 
615  saved_window_info_.rect.right =
616  saved_window_info_.rect.left + static_cast<LONG>(window_size->width());
617  saved_window_info_.rect.bottom =
618  saved_window_info_.rect.top + static_cast<LONG>(window_size->height());
619  } else {
620  auto const client_size = GetWindowContentSize(window_handle_);
621  auto const current_size = Size(client_size.width, client_size.height);
622  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
623  GetWindowInfo(window_handle_, &window_info);
624  std::optional<Size> const window_size = GetWindowSizeForClientSize(
625  *engine_->windows_proc_table(), current_size,
626  box_constraints_.smallest(), box_constraints_.biggest(),
627  window_info.dwStyle, window_info.dwExStyle, nullptr);
628 
629  if (window_size && current_size != window_size) {
630  SetWindowPos(window_handle_, NULL, 0, 0, window_size->width(),
631  window_size->height(),
632  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
633  }
634  }
635 }
SavedWindowInfo saved_window_info_
Definition: host_window.h:337
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:792
virtual bool GetFullscreen() const
Definition: host_window.cc:788

Referenced by InternalFlutterWindows_WindowManager_SetWindowConstraints().

◆ SetContentSize()

void flutter::HostWindow::SetContentSize ( const WindowSizeRequest &  size)

Definition at line 559 of file host_window.cc.

559  {
560  if (!size.has_preferred_view_size) {
561  return;
562  }
563 
564  if (GetFullscreen()) {
565  std::optional<Size> const window_size = GetWindowSizeForClientSize(
567  Size(size.preferred_view_width, size.preferred_view_height),
568  box_constraints_.smallest(), box_constraints_.biggest(),
570  if (!window_size) {
571  return;
572  }
573 
575  ActualWindowSize{.width = size.preferred_view_width,
576  .height = size.preferred_view_height};
577  saved_window_info_.rect.right =
578  saved_window_info_.rect.left + static_cast<LONG>(window_size->width());
579  saved_window_info_.rect.bottom =
580  saved_window_info_.rect.top + static_cast<LONG>(window_size->height());
581  } else {
582  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
583  GetWindowInfo(window_handle_, &window_info);
584 
585  std::optional<Size> const window_size = GetWindowSizeForClientSize(
587  Size(size.preferred_view_width, size.preferred_view_height),
588  box_constraints_.smallest(), box_constraints_.biggest(),
589  window_info.dwStyle, window_info.dwExStyle, nullptr);
590 
591  if (!window_size) {
592  return;
593  }
594 
595  SetWindowPos(window_handle_, NULL, 0, 0, window_size->width(),
596  window_size->height(),
597  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
598  }
599 }

References flutter::WindowSizeRequest::has_preferred_view_size.

Referenced by InternalFlutterWindows_WindowManager_SetWindowSize().

◆ SetFullscreen()

void flutter::HostWindow::SetFullscreen ( bool  fullscreen,
std::optional< FlutterEngineDisplayId >  display_id 
)
virtual

Reimplemented in flutter::HostWindowDialog.

Definition at line 642 of file host_window.cc.

644  {
645  if (fullscreen == GetFullscreen()) {
646  return;
647  }
648 
649  if (fullscreen) {
650  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
651  GetWindowInfo(window_handle_, &window_info);
652  saved_window_info_.style = window_info.dwStyle;
653  saved_window_info_.ex_style = window_info.dwExStyle;
654  // Store the original window rect, DPI, and monitor info to detect changes
655  // and more accurately restore window placements when exiting fullscreen.
656  ::GetWindowRect(window_handle_, &saved_window_info_.rect);
660  MonitorFromWindow(window_handle_, MONITOR_DEFAULTTONEAREST);
663  GetMonitorInfo(saved_window_info_.monitor,
665  }
666 
667  if (fullscreen) {
668  // Next, get the raw HMONITOR that we want to be fullscreened on
669  HMONITOR monitor =
670  MonitorFromWindow(window_handle_, MONITOR_DEFAULTTONEAREST);
671  if (display_id) {
672  if (auto const display =
673  engine_->display_manager()->FindById(display_id.value())) {
674  monitor = reinterpret_cast<HMONITOR>(display->display_id);
675  }
676  }
677 
678  MONITORINFO monitor_info;
679  monitor_info.cbSize = sizeof(monitor_info);
680  if (!GetMonitorInfo(monitor, &monitor_info)) {
681  FML_LOG(ERROR) << "Cannot set window fullscreen because the monitor info "
682  "was not found";
683  }
684 
685  auto const width = RectWidth(monitor_info.rcMonitor);
686  auto const height = RectHeight(monitor_info.rcMonitor);
687  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
688  GetWindowInfo(window_handle_, &window_info);
689 
690  // Set new window style and size.
691  SetWindowLong(window_handle_, GWL_STYLE,
692  saved_window_info_.style & ~(WS_CAPTION | WS_THICKFRAME));
693  SetWindowLong(
694  window_handle_, GWL_EXSTYLE,
695  saved_window_info_.ex_style & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE |
696  WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
697 
698  // We call SetWindowPos first to set the window flags immediately. This
699  // makes it so that the WM_GETMINMAXINFO gets called with the correct window
700  // and content sizes.
701  SetWindowPos(window_handle_, NULL, 0, 0, 0, 0,
702  SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
703 
704  SetWindowPos(window_handle_, nullptr, monitor_info.rcMonitor.left,
705  monitor_info.rcMonitor.top, width, height,
706  SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
707  } else {
708  // Restore the window style and bounds saved prior to entering fullscreen.
709  // Use WS_VISIBLE for windows shown after SetFullscreen: crbug.com/1062251.
710  // Making multiple window adjustments here is ugly, but if SetWindowPos()
711  // doesn't redraw, the taskbar won't be repainted.
712  SetWindowLong(window_handle_, GWL_STYLE,
713  saved_window_info_.style | WS_VISIBLE);
714  SetWindowLong(window_handle_, GWL_EXSTYLE, saved_window_info_.ex_style);
715 
716  // We call SetWindowPos first to set the window flags immediately. This
717  // makes it so that the WM_GETMINMAXINFO gets called with the correct window
718  // and content sizes.
719  SetWindowPos(window_handle_, NULL, 0, 0, 0, 0,
720  SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
721 
722  HMONITOR monitor =
723  MonitorFromRect(&saved_window_info_.rect, MONITOR_DEFAULTTONEAREST);
724  MONITORINFO monitor_info;
725  monitor_info.cbSize = sizeof(monitor_info);
726  GetMonitorInfo(monitor, &monitor_info);
727 
728  auto window_rect = saved_window_info_.rect;
729 
730  // Adjust the window bounds to restore, if displays were disconnected,
731  // virtually rearranged, or otherwise changed metrics during fullscreen.
732  if (monitor != saved_window_info_.monitor ||
734  monitor_info.rcWork)) {
735  window_rect = AdjustToFit(monitor_info.rcWork, window_rect);
736  }
737 
738  auto const fullscreen_dpi = GetDpiForHWND(window_handle_);
739  SetWindowPos(window_handle_, nullptr, window_rect.left, window_rect.top,
740  RectWidth(window_rect), RectHeight(window_rect),
741  SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
742  auto const final_dpi = GetDpiForHWND(window_handle_);
743  if (final_dpi != saved_window_info_.dpi || final_dpi != fullscreen_dpi) {
744  // Reissue SetWindowPos if the DPI changed from saved or fullscreen DPIs.
745  // The first call may misinterpret bounds spanning displays, if the
746  // fullscreen display's DPI does not match the target display's DPI.
747  //
748  // Scale and clamp the bounds if the final DPI changed from the saved DPI.
749  // This more accurately matches the original placement, while avoiding
750  // unexpected offscreen placement in a recongifured multi-screen space.
751  if (final_dpi != saved_window_info_.dpi) {
752  auto const scale =
753  final_dpi / static_cast<float>(saved_window_info_.dpi);
754  auto const width = static_cast<LONG>(scale * RectWidth(window_rect));
755  auto const height = static_cast<LONG>(scale * RectHeight(window_rect));
756  window_rect.right = window_rect.left + width;
757  window_rect.bottom = window_rect.top + height;
758  window_rect = AdjustToFit(monitor_info.rcWork, window_rect);
759  }
760 
761  SetWindowPos(window_handle_, nullptr, window_rect.left, window_rect.top,
762  RectWidth(window_rect), RectHeight(window_rect),
763  SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
764  }
765  }
766 
767  if (!task_bar_list_) {
768  HRESULT hr =
769  ::CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER,
770  IID_PPV_ARGS(&task_bar_list_));
771  if (SUCCEEDED(hr) && FAILED(task_bar_list_->HrInit())) {
772  task_bar_list_ = nullptr;
773  }
774  }
775 
776  // As per MSDN marking the window as fullscreen should ensure that the
777  // taskbar is moved to the bottom of the Z-order when the fullscreen window
778  // is activated. If the window is not fullscreen, the Shell falls back to
779  // heuristics to determine how the window should be treated, which means
780  // that it could still consider the window as fullscreen. :(
781  if (task_bar_list_) {
782  task_bar_list_->MarkFullscreenWindow(window_handle_, !!fullscreen);
783  }
784 
785  is_fullscreen_ = fullscreen;
786 }
Microsoft::WRL::ComPtr< ITaskbarList2 > task_bar_list_
Definition: host_window.h:340
LONG RectWidth(const RECT &r)
Definition: rect_helper.h:11
bool AreRectsEqual(const RECT &a, const RECT &b)
Definition: rect_helper.h:19
LONG RectHeight(const RECT &r)
Definition: rect_helper.h:15

References flutter::GetDpiForHWND().

Referenced by InternalFlutterWindows_WindowManager_SetFullscreen().

◆ UpdateModalStateLayer()

void flutter::HostWindow::UpdateModalStateLayer ( )

Definition at line 939 of file host_window.cc.

939  {
940  auto children = GetOwnedWindows();
941 
942  if (children.empty()) {
943  // Leaf window in the active path, enable it.
944  EnableWindow(window_handle_, true);
945  } else {
946  // Non-leaf window in the active path, disable it and process children.
947  EnableWindow(window_handle_, false);
948 
949  // On same level of window hierarchy the most recently created window
950  // will remain enabled.
951  auto latest_child = *std::max_element(
952  children.begin(), children.end(), [](HostWindow* a, HostWindow* b) {
953  return a->view_controller_->view()->view_id() <
954  b->view_controller_->view()->view_id();
955  });
956 
957  for (HostWindow* const child : children) {
958  if (child == latest_child) {
959  child->UpdateModalStateLayer();
960  } else {
961  child->DisableRecursively();
962  }
963  }
964  }
965 }

◆ WndProc()

LRESULT flutter::HostWindow::WndProc ( HWND  hwnd,
UINT  message,
WPARAM  wparam,
LPARAM  lparam 
)
staticprotected

Definition at line 416 of file host_window.cc.

419  {
420  if (message == WM_NCCREATE) {
421  auto* const create_struct = reinterpret_cast<CREATESTRUCT*>(lparam);
422  auto* const windows_proc_table =
423  static_cast<WindowsProcTable*>(create_struct->lpCreateParams);
424  windows_proc_table->EnableNonClientDpiScaling(hwnd);
425  } else if (HostWindow* const window = GetThisFromHandle(hwnd)) {
426  return window->HandleMessage(hwnd, message, wparam, lparam);
427  }
428 
429  return DefWindowProc(hwnd, message, wparam, lparam);
430 }

References flutter::WindowsProcTable::EnableNonClientDpiScaling(), and message.

Referenced by InitializeFlutterView().

Member Data Documentation

◆ alive_

std::shared_ptr<int> flutter::HostWindow::alive_ = std::make_shared<int>(0)
protected

Definition at line 319 of file host_window.h.

Referenced by InitializeFlutterView().

◆ archetype_

WindowArchetype flutter::HostWindow::archetype_ = WindowArchetype::kRegular
protected

Definition at line 322 of file host_window.h.

Referenced by GetArchetype().

◆ box_constraints_

BoxConstraints flutter::HostWindow::box_constraints_
protected

Definition at line 328 of file host_window.h.

Referenced by flutter::HostWindowSized::ApplyContentSize().

◆ engine_

◆ is_being_destroyed_

bool flutter::HostWindow::is_being_destroyed_ = false
protected

Definition at line 331 of file host_window.h.

Referenced by GetOwnedWindows(), and flutter::HostWindowDialog::HandleMessage().

◆ is_fullscreen_

bool flutter::HostWindow::is_fullscreen_ = false
protected

Definition at line 334 of file host_window.h.

◆ saved_window_info_

SavedWindowInfo flutter::HostWindow::saved_window_info_
protected

Definition at line 337 of file host_window.h.

◆ task_bar_list_

Microsoft::WRL::ComPtr<ITaskbarList2> flutter::HostWindow::task_bar_list_
protected

Definition at line 340 of file host_window.h.

◆ view_controller_

◆ window_handle_

◆ window_manager_

WindowManager* const flutter::HostWindow::window_manager_ = nullptr
protected

Definition at line 306 of file host_window.h.

◆ WindowManager

friend flutter::HostWindow::WindowManager
protected

Definition at line 199 of file host_window.h.


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