Flutter Windows Embedder
host_window_sized.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 #include "flutter/fml/logging.h"
10 
11 namespace flutter {
12 
14  FlutterWindowsEngine* engine,
15  bool resizable)
16  : HostWindow(window_manager, engine),
17  resizable_(resizable),
18  view_alive_(std::make_shared<int>(0)) {}
19 
21  // By the time the base destructor runs, the most-derived class must have
22  // already reset |view_controller_| (and therefore stopped the raster thread
23  // from sizing this object). See the destructor comment in host_window_sized.h
24  // for the rationale. If this fires, a HostWindowSized subclass is missing the
25  // required |view_controller_.reset()| at the start of its destructor.
26  FML_DCHECK(!view_controller_)
27  << "HostWindowSized subclass must reset view_controller_ in its "
28  "destructor.";
29 }
30 
31 void HostWindowSized::DidUpdateViewSize(int32_t width, int32_t height) {
32  // This is called from the raster thread.
33  std::weak_ptr<int> weak_view_alive = view_alive_;
34  engine_->task_runner()->PostTask([this, width, height, weak_view_alive]() {
35  auto const view_alive = weak_view_alive.lock();
36  if (!view_alive) {
37  return;
38  }
39  if (physical_width_ == width && physical_height_ == height) {
40  return;
41  }
42  if (is_being_destroyed_) {
43  return;
44  }
45  physical_width_ = width;
46  physical_height_ = height;
47 
48  ApplyContentSize(width, height);
49  });
50 }
51 
52 void HostWindowSized::ApplyContentSize(int32_t physical_width,
53  int32_t physical_height) {
54  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
55  GetWindowInfo(window_handle_, &window_info);
56 
57  // Convert physical pixels to logical pixels.
58  UINT const dpi = GetDpiForHWND(window_handle_);
59  double const scale = static_cast<double>(dpi > 0 ? dpi : 96) / 96.0;
60  std::optional<Size> const window_size = GetWindowSizeForClientSize(
62  Size(physical_width / scale, physical_height / scale),
63  box_constraints_.smallest(), box_constraints_.biggest(),
64  window_info.dwStyle, window_info.dwExStyle, nullptr);
65 
66  if (!window_size) {
67  return;
68  }
69 
70  SetWindowPos(window_handle_, NULL, 0, 0, window_size->width(),
71  window_size->height(),
72  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
73 
74  if (resizable_) {
75  // For resizable windows, stop tracking content size after the initial
76  // frame so subsequent user-initiated resizes are forwarded to Flutter.
77  view_controller_->view()->SetSizedToContent(false);
78  }
79 }
80 
81 WindowRect HostWindowSized::GetWorkArea() const {
82  constexpr int32_t kDefaultWorkAreaSize = 10000;
83  WindowRect work_area = {0, 0, kDefaultWorkAreaSize, kDefaultWorkAreaSize};
84  HMONITOR const monitor =
85  MonitorFromWindow(window_handle_, MONITOR_DEFAULTTONEAREST);
86  if (monitor) {
87  MONITORINFO monitor_info = {};
88  monitor_info.cbSize = sizeof(monitor_info);
89  if (GetMonitorInfo(monitor, &monitor_info)) {
90  work_area.left = monitor_info.rcWork.left;
91  work_area.top = monitor_info.rcWork.top;
92  work_area.width = monitor_info.rcWork.right - monitor_info.rcWork.left;
93  work_area.height = monitor_info.rcWork.bottom - monitor_info.rcWork.top;
94  }
95  }
96  return work_area;
97 }
98 
99 } // namespace flutter
std::shared_ptr< WindowsProcTable > windows_proc_table()
BoxConstraints box_constraints_
Definition: host_window.h:268
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:259
FlutterWindowsEngine * engine_
Definition: host_window.h:254
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:751
std::shared_ptr< int > view_alive_
HostWindowSized(WindowManager *window_manager, FlutterWindowsEngine *engine, bool resizable)
virtual void ApplyContentSize(int32_t physical_width, int32_t physical_height)
void PostTask(TaskClosure task)
Definition: task_runner.cc:89
UINT GetDpiForHWND(HWND hwnd)
Definition: dpi_utils.cc:128