Flutter Windows Embedder
host_window_satellite.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 
7 #include <cmath>
8 #include <utility>
9 
10 #include "flutter/fml/logging.h"
15 
16 namespace flutter {
17 
18 DWORD HostWindowSatellite::GetWindowStyleForSatellite(bool resizable) {
19  // Satellites are decorated and activatable like a regular window, but they
20  // are never minimizable.
21  DWORD window_style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
22  if (resizable) {
23  window_style |= WS_THICKFRAME | WS_MAXIMIZEBOX;
24  }
25  return window_style;
26 }
27 
29  WindowManager* window_manager,
30  FlutterWindowsEngine* engine,
31  const WindowSizeRequest& preferred_size,
32  const BoxConstraints& constraints,
33  GetWindowPositionCallback get_position_callback,
34  HWND parent,
35  LPCWSTR title,
36  bool sized_to_content,
37  bool resizable)
38  : HostWindowSized(window_manager, engine, resizable),
39  get_position_callback_(get_position_callback),
40  parent_(parent),
41  isolate_(Isolate::Current()) {
42  FML_CHECK(sized_to_content || preferred_size.has_preferred_view_size);
43 
46  .window_style = GetWindowStyleForSatellite(resizable),
47  .extended_window_style = 0,
48  .box_constraints = constraints,
49  .initial_window_rect =
50  GetInitialRect(engine, preferred_size, constraints, parent,
51  sized_to_content, resizable),
52  .title = title ? title : L"",
53  .owner_window = parent,
54  .sizing_delegate = sized_to_content ? AsSizingDelegate() : nullptr,
55  .is_sized_to_content = sized_to_content,
56  });
57 
58  UpdateParentOffset();
59 }
60 
62  // Reset the view while this most-derived object is still fully alive, to stop
63  // the raster thread from sizing it (via the overridden ApplyContentSize /
64  // GetWorkArea) before any subobject is torn down. See the destructor comment
65  // in host_window_sized.h for the rationale.
66  view_controller_.reset();
67 }
68 
69 Rect HostWindowSatellite::GetInitialRect(
70  FlutterWindowsEngine* engine,
71  const WindowSizeRequest& preferred_size,
72  const BoxConstraints& constraints,
73  HWND parent,
74  bool sized_to_content,
75  bool resizable) {
76  std::optional<Size> const window_size = GetInitialWindowSize(
77  engine, preferred_size, constraints,
78  GetWindowStyleForSatellite(resizable), /*extended_window_style=*/0,
79  parent, sized_to_content);
80 
81  // The placement is resolved after the first frame, so the window is created
82  // at a default position and moved before it is shown for the first time.
83  return {{CW_USEDEFAULT, CW_USEDEFAULT},
84  window_size ? *window_size : Size{CW_USEDEFAULT, CW_USEDEFAULT}};
85 }
86 
87 WindowRect HostWindowSatellite::GetWorkArea() const {
88  return GetWorkAreaForWindow(parent_);
89 }
90 
91 std::optional<WindowRect> HostWindowSatellite::ComputePosition(
92  const WindowSize& window_size) const {
93  if (!get_position_callback_) {
94  return std::nullopt;
95  }
96 
97  RECT parent_client_rect;
98  GetClientRect(parent_, &parent_client_rect);
99 
100  // Convert top-left and bottom-right points to screen coordinates.
101  POINT parent_top_left = {parent_client_rect.left, parent_client_rect.top};
102  POINT parent_bottom_right = {parent_client_rect.right,
103  parent_client_rect.bottom};
104  ClientToScreen(parent_, &parent_top_left);
105  ClientToScreen(parent_, &parent_bottom_right);
106 
107  IsolateScope scope(isolate_);
108 
109  WindowRect rect = {};
110  get_position_callback_(window_size,
111  WindowRect{parent_top_left.x, parent_top_left.y,
112  parent_bottom_right.x - parent_top_left.x,
113  parent_bottom_right.y - parent_top_left.y},
114  GetWorkAreaForWindow(parent_), rect);
115  return rect;
116 }
117 
118 void HostWindowSatellite::ApplyInitialPosition() {
119  if (initial_position_applied_) {
120  return;
121  }
122 
123  RECT window_rect;
124  if (!GetWindowRect(window_handle_, &window_rect)) {
125  return;
126  }
127  WindowSize const window_size = {window_rect.right - window_rect.left,
128  window_rect.bottom - window_rect.top};
129 
130  std::optional<WindowRect> const rect = ComputePosition(window_size);
131  if (!rect) {
132  return;
133  }
134 
135  SetWindowPos(window_handle_, nullptr, rect->left, rect->top, rect->width,
136  rect->height, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
137 
138  initial_position_applied_ = true;
139 
140  // The positioner constrained the dimensions more than the current size, so
141  // let the framework know about the size it actually got.
142  if (rect->width != window_size.width || rect->height != window_size.height) {
143  auto metrics_event = view_controller_->view()->CreateWindowMetricsEvent();
144  view_controller_->engine()->SendWindowMetricsEvent(metrics_event);
145  }
146 }
147 
149  // A satellite that is sized to content has already been placed from
150  // |ApplyContentSize|, which runs once the first frame has been generated.
151  ApplyInitialPosition();
152 }
153 
154 void HostWindowSatellite::ApplyContentSize(int32_t physical_width,
155  int32_t physical_height) {
156  // Resize the window to fit its content. For resizable satellites this also
157  // stops content-size tracking after the first frame, so the user's subsequent
158  // manual resizes are forwarded to Flutter instead of being overridden.
159  HostWindowSized::ApplyContentSize(physical_width, physical_height);
160 
161  // Run the positioner only for the initial placement. Afterwards the satellite
162  // is positioned solely by following its parent.
163  ApplyInitialPosition();
164 }
165 
166 std::optional<POINT> HostWindowSatellite::GetParentClientOrigin() const {
167  // A minimized window's client area has no meaningful position.
168  if (IsIconic(parent_)) {
169  return std::nullopt;
170  }
171 
172  POINT origin = {0, 0};
173  if (!ClientToScreen(parent_, &origin)) {
174  return std::nullopt;
175  }
176 
177  return origin;
178 }
179 
180 double HostWindowSatellite::GetParentScaleFactor() const {
181  return static_cast<double>(GetDpiForHWND(parent_)) / kDefaultDpi;
182 }
183 
184 void HostWindowSatellite::UpdateParentOffset() {
185  std::optional<POINT> const parent_origin = GetParentClientOrigin();
186  RECT window_rect;
187  if (!parent_origin || !GetWindowRect(window_handle_, &window_rect)) {
188  return;
189  }
190 
191  double const scale_factor = GetParentScaleFactor();
192  parent_offset_x_ = (window_rect.left - parent_origin->x) / scale_factor;
193  parent_offset_y_ = (window_rect.top - parent_origin->y) / scale_factor;
194 }
195 
197  bool const was_following_parent = std::exchange(is_following_parent_, true);
198 
199  // Moving the satellite may carry it onto a monitor with a different DPI. Its
200  // WM_DPICHANGED handler then resizes it to the system-suggested rectangle,
201  // which may also move it. A second pass corrects for that.
202  for (int pass = 0; pass < 2; ++pass) {
203  std::optional<POINT> const parent_origin = GetParentClientOrigin();
204  RECT window_rect;
205  if (!parent_origin || !GetWindowRect(window_handle_, &window_rect)) {
206  break;
207  }
208 
209  double const scale_factor = GetParentScaleFactor();
210  LONG const target_x =
211  parent_origin->x + std::lround(parent_offset_x_ * scale_factor);
212  LONG const target_y =
213  parent_origin->y + std::lround(parent_offset_y_ * scale_factor);
214  if (target_x == window_rect.left && target_y == window_rect.top) {
215  break;
216  }
217 
218  SetWindowPos(window_handle_, nullptr, target_x, target_y, 0, 0,
219  SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
220  }
221 
222  is_following_parent_ = was_following_parent;
223 }
224 
226  if (new_parent == parent_ || new_parent == nullptr) {
227  return;
228  }
229 
230  // Reject a reparent that would make this satellite its own ancestor.
231  for (HWND ancestor = new_parent; ancestor != nullptr;
232  ancestor = GetWindow(ancestor, GW_OWNER)) {
233  if (ancestor == window_handle_) {
234  FML_LOG(ERROR) << "Refusing to anchor a satellite window to itself or to "
235  "one of its own satellites.";
236  return;
237  }
238  }
239 
240  parent_ = new_parent;
241  // Transfer ownership to the new parent, so that the satellite is destroyed
242  // alongside it.
243  SetWindowLongPtr(window_handle_, GWLP_HWNDPARENT,
244  reinterpret_cast<LONG_PTR>(new_parent));
245  SetWindowPos(window_handle_, nullptr, 0, 0, 0, 0,
246  SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE |
247  SWP_FRAMECHANGED);
248 
249  // Measure the offset against the new parent so that reparenting does not
250  // move the satellite.
251  UpdateParentOffset();
252 }
253 
255  UINT message,
256  WPARAM wparam,
257  LPARAM lparam) {
258  switch (message) {
259  case WM_SYSCOMMAND:
260  // Disallow minimization.
261  if ((wparam & 0xFFF0) == SC_MINIMIZE) {
262  return 0;
263  }
264  break;
265 
266  case WM_ENTERSIZEMOVE:
267  is_in_move_size_loop_ = true;
268  break;
269 
270  case WM_EXITSIZEMOVE:
271  is_in_move_size_loop_ = false;
272  break;
273 
274  case WM_DPICHANGED: {
275  // While the user drags the satellite, or while it is already following
276  // its parent (whose loop corrects the position), use the default
277  // handling, which applies the system-suggested rectangle.
278  if (is_in_move_size_loop_ || is_following_parent_) {
279  break;
280  }
281 
282  // Otherwise the DPI change was not caused by the user moving the
283  // satellite (e.g. it followed its parent onto another monitor). Apply the
284  // suggested rectangle for its new size, without treating the
285  // accompanying move as a new offset from the parent, and then re-anchor
286  // the satellite to its parent.
287  bool const was_following_parent =
288  std::exchange(is_following_parent_, true);
289  LRESULT const result =
290  HostWindow::HandleMessage(hwnd, message, wparam, lparam);
291  is_following_parent_ = was_following_parent;
292  OnParentMoved();
293  return result;
294  }
295 
296  case WM_WINDOWPOSCHANGED: {
297  // When the satellite is moved other than by following its parent (e.g.
298  // dragged by the user), keep the new offset from the parent.
299  auto const* const window_pos = reinterpret_cast<WINDOWPOS*>(lparam);
300  if (!is_following_parent_ && window_pos &&
301  !(window_pos->flags & SWP_NOMOVE)) {
302  UpdateParentOffset();
303  }
304  break;
305  }
306 
307  case WM_ACTIVATE:
308  // Forward the message to Dart before handling it on the C++ side, so
309  // that Dart-side handlers (e.g. popup dismiss logic) can observe
310  // activation changes caused by satellite windows.
311  if (auto const result =
313  window_handle_, message, wparam, lparam)) {
314  return *result;
315  }
316 
317  HandleWindowActivation(hwnd, wparam);
318  return 0;
319  }
320 
321  return HostWindow::HandleMessage(hwnd, message, wparam, lparam);
322 }
323 
324 } // namespace flutter
WindowProcDelegateManager * window_proc_delegate_manager()
void InitializeFlutterView(HostWindowInitializationParams const &params)
Definition: host_window.cc:249
void HandleWindowActivation(HWND hwnd, WPARAM wparam)
Definition: host_window.cc:392
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:314
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)
Definition: host_window.cc:852
FlutterWindowsEngine * engine_
Definition: host_window.h:309
virtual LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:432
HostWindowSatellite(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const BoxConstraints &constraints, GetWindowPositionCallback get_position_callback, HWND parent, LPCWSTR title, bool sized_to_content, bool resizable)
LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) override
void ApplyContentSize(int32_t physical_width, int32_t physical_height) override
void SetSatelliteParent(HWND new_parent)
static WindowRect GetWorkAreaForWindow(HWND hwnd)
FlutterWindowsViewSizingDelegate * AsSizingDelegate()
virtual void ApplyContentSize(int32_t physical_width, int32_t physical_height)
std::optional< LRESULT > OnTopLevelWindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) const
Win32Message message
UINT GetDpiForHWND(HWND hwnd)
Definition: dpi_utils.cc:128
void(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &display_rect, WindowRect &output_rect)
constexpr UINT kDefaultDpi
Definition: dpi_utils.h:12