Flutter Windows Embedder
window_manager.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 <dwmapi.h>
8 #include <optional>
9 #include <vector>
10 
11 #include "embedder.h"
16 #include "fml/logging.h"
23 
24 namespace flutter {
25 
27 
29  on_message_ = request->on_message;
30  isolate_ = Isolate::Current();
31 }
32 
34  const RegularWindowCreationRequest* request) {
35  auto window = HostWindow::CreateRegularWindow(
36  this, engine_, request->preferred_size, request->preferred_constraints,
37  request->title, request->sized_to_content, request->resizable);
38  if (!window || !window->GetWindowHandle()) {
39  FML_LOG(ERROR) << "Failed to create host window";
40  return -1;
41  }
42  FlutterViewId const view_id = window->view_controller_->view()->view_id();
43  active_windows_[window->GetWindowHandle()] = std::move(window);
44  return view_id;
45 }
46 
48  const DialogWindowCreationRequest* request) {
49  auto window = HostWindow::CreateDialogWindow(
50  this, engine_, request->preferred_size, request->preferred_constraints,
51  request->title, request->parent_or_null, request->sized_to_content,
52  request->resizable);
53  if (!window || !window->GetWindowHandle()) {
54  FML_LOG(ERROR) << "Failed to create host window";
55  return -1;
56  }
57  FlutterViewId const view_id = window->view_controller_->view()->view_id();
58  active_windows_[window->GetWindowHandle()] = std::move(window);
59  return view_id;
60 }
61 
63  const TooltipWindowCreationRequest* request) {
64  auto window = HostWindow::CreateTooltipWindow(
65  this, engine_, request->preferred_constraints,
66  request->get_position_callback, request->parent);
67  if (!window || !window->GetWindowHandle()) {
68  FML_LOG(ERROR) << "Failed to create host window";
69  return -1;
70  }
71  FlutterViewId const view_id = window->view_controller_->view()->view_id();
72  active_windows_[window->GetWindowHandle()] = std::move(window);
73  return view_id;
74 }
75 
77  const PopupWindowCreationRequest* request) {
78  auto window = HostWindow::CreatePopupWindow(
79  this, engine_, request->preferred_constraints,
80  request->get_position_callback, request->parent);
81  if (!window || !window->GetWindowHandle()) {
82  FML_LOG(ERROR) << "Failed to create host window";
83  return -1;
84  }
85  FlutterViewId const view_id = window->view_controller_->view()->view_id();
86  active_windows_[window->GetWindowHandle()] = std::move(window);
87  return view_id;
88 }
89 
91  const SatelliteWindowCreationRequest* request) {
93  this, engine_, request->preferred_size, request->preferred_constraints,
94  request->get_position_callback, request->parent, request->title,
95  request->sized_to_content, request->resizable);
96  if (!window || !window->GetWindowHandle()) {
97  FML_LOG(ERROR) << "Failed to create host window";
98  return -1;
99  }
100  FlutterViewId const view_id = window->view_controller_->view()->view_id();
101  active_windows_[window->GetWindowHandle()] = std::move(window);
102  return view_id;
103 }
104 
107  if (!window || window->GetArchetype() != WindowArchetype::kSatellite) {
108  return nullptr;
109  }
110  return static_cast<HostWindowSatellite*>(window);
111 }
112 
114  // The isolate state will be completely lost after a hot restart, so notifying
115  // the isolate about destroying windows does not have any benefits. On the
116  // contrary it can cause the old isolate to shut down the application because
117  // all windows will be destroyed.
118  on_message_ = nullptr;
120 }
121 
123  // Destroy the windows before clearing |on_message_| so the WM_DESTROY
124  // round-trip reaches the isolate. Otherwise per-view Dart controllers
125  // never observe destruction and may issue follow-up FFI calls (e.g.
126  // updatePosition) with stale handles after the engine is torn down.
127  //
128  // Destroying single window may result in removal of child entries from the
129  // map so this loop is safer than iterating over the map.
130  while (!active_windows_.empty()) {
131  auto it = active_windows_.begin();
132  // This will destroy the window, which will in turn remove the
133  // HostWindow from map when handling WM_NCDESTROY inside
134  // HandleMessage.
136  }
137 
138  // Don't send any more messages to isolate.
139  on_message_ = nullptr;
140 }
141 
142 std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
143  UINT message,
144  WPARAM wparam,
145  LPARAM lparam) {
146  if (message == WM_DESTROY) {
147  // Destroying parent HWND will transitively destroy child HWND, including
148  // the FlutterView HWND. That causes a problem because the raster thread
149  // may require the Flutterview HWND to be alive until Engine RemoveView
150  // completes. To ensure that the FlutterView HWND is removed from the
151  // parent window. The FlutterView HWND will be destroyed later inside
152  // FlutterWindow::Destroy.
154  if (window) {
155  auto handle = window->GetFlutterViewWindowHandle();
156  ShowWindow(handle, SW_HIDE);
157  SetParent(handle, nullptr);
158  }
159  }
160 
161  if (message == WM_NCDESTROY) {
162  active_windows_.erase(hwnd);
163  return std::nullopt;
164  }
165 
166  HostWindow* host_window = HostWindow::GetThisFromHandle(hwnd);
167  FlutterWindowsView* view =
168  host_window ? host_window->view_controller_->view() : nullptr;
169 
170  if (!view) {
171  FML_LOG(WARNING) << "Received message for unknown view";
172  return std::nullopt;
173  }
174 
175  WindowsMessage message_struct = {.view_id = view->view_id(),
176  .hwnd = hwnd,
177  .message = message,
178  .wParam = wparam,
179  .lParam = lparam,
180  .result = 0,
181  .handled = false};
182 
183  // Not initialized yet.
184  if (!isolate_ || on_message_ == nullptr) {
185  return std::nullopt;
186  }
187 
188  IsolateScope scope(*isolate_);
189  on_message_(&message_struct);
190  if (message_struct.handled) {
191  return message_struct.result;
192  } else {
193  return std::nullopt;
194  }
195 }
196 
197 } // namespace flutter
198 
200  int64_t engine_id,
201  const flutter::WindowingInitRequest* request) {
204  engine->window_manager()->Initialize(request);
205 }
206 
208  int64_t engine_id,
209  const flutter::RegularWindowCreationRequest* request) {
212  return engine->window_manager()->CreateRegularWindow(request);
213 }
214 
217  int64_t engine_id,
218  const flutter::DialogWindowCreationRequest* request) {
221  return engine->window_manager()->CreateDialogWindow(request);
222 }
223 
226  int64_t engine_id,
227  const flutter::TooltipWindowCreationRequest* request) {
230  return engine->window_manager()->CreateTooltipWindow(request);
231 }
232 
235  int64_t engine_id,
236  const flutter::PopupWindowCreationRequest* request) {
239  return engine->window_manager()->CreatePopupWindow(request);
240 }
241 
243  int64_t engine_id,
244  FlutterViewId view_id) {
247  flutter::FlutterWindowsView* view = engine->view(view_id);
248  if (view == nullptr) {
249  return nullptr;
250  } else {
251  return GetAncestor(view->GetWindowHandle(), GA_ROOT);
252  }
253 }
254 
258 }
259 
261  HWND hwnd,
262  const flutter::WindowSizeRequest* size) {
264  if (window) {
265  window->SetContentSize(*size);
266  }
267 }
268 
270  DestroyWindow(hwnd);
271 }
272 
274  HWND hwnd,
275  const flutter::WindowConstraints* constraints) {
277  if (window) {
278  window->SetConstraints(*constraints);
279  }
280 }
281 
283  HWND hwnd,
284  const flutter::FullscreenRequest* request) {
286  const std::optional<FlutterEngineDisplayId> display_id =
287  request->has_display_id
288  ? std::optional<FlutterEngineDisplayId>(request->display_id)
289  : std::nullopt;
290  if (window) {
291  window->SetFullscreen(request->fullscreen, display_id);
292  }
293 }
294 
297  if (window) {
298  return window->GetFullscreen();
299  }
300 
301  return false;
302 }
303 
307  flutter::HostWindowTooltip* tooltip_window =
308  reinterpret_cast<flutter::HostWindowTooltip*>(window);
309  tooltip_window->UpdatePosition();
310 }
311 
315  flutter::HostWindowPopup* popup_window =
316  reinterpret_cast<flutter::HostWindowPopup*>(window);
317  popup_window->UpdatePosition();
318 }
319 
322  int64_t engine_id,
326  return engine->window_manager()->CreateSatelliteWindow(request);
327 }
328 
331  HWND satellite_hwnd,
332  HWND new_parent) {
333  if (flutter::HostWindowSatellite* satellite =
335  satellite_hwnd)) {
336  satellite->SetSatelliteParent(new_parent);
337  }
338 }
FlutterWindowsView * view(FlutterViewId view_id) const
static FlutterWindowsEngine * GetEngineForId(int64_t engine_id)
virtual HWND GetWindowHandle() const
static std::unique_ptr< HostWindow > CreatePopupWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
Definition: host_window.cc:218
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)
Definition: host_window.cc:192
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:314
static std::unique_ptr< HostWindow > CreateTooltipWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
Definition: host_window.cc:207
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:792
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:559
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)
Definition: host_window.cc:229
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)
Definition: host_window.cc:178
HWND GetFlutterViewWindowHandle() const
Definition: host_window.cc:381
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:362
WindowArchetype GetArchetype() const
Definition: host_window.h:177
virtual bool GetFullscreen() const
Definition: host_window.cc:788
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:601
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:642
static Isolate Current()
Definition: isolate_scope.cc:9
static HostWindowSatellite * GetSatelliteWindowFromHandle(HWND hwnd)
FlutterViewId CreateDialogWindow(const DialogWindowCreationRequest *request)
std::optional< LRESULT > HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
FlutterViewId CreateRegularWindow(const RegularWindowCreationRequest *request)
void Initialize(const WindowingInitRequest *request)
WindowManager(FlutterWindowsEngine *engine)
FlutterViewId CreatePopupWindow(const PopupWindowCreationRequest *request)
FlutterViewId CreateSatelliteWindow(const SatelliteWindowCreationRequest *request)
FlutterViewId CreateTooltipWindow(const TooltipWindowCreationRequest *request)
#define FLUTTER_EXPORT
Win32Message message
int64_t FlutterViewId
FlutterEngineDisplayId display_id
GetWindowPositionCallback get_position_callback
GetWindowPositionCallback get_position_callback
GetWindowPositionCallback get_position_callback
void(* on_message)(WindowsMessage *)
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
void InternalFlutterWindows_WindowManager_SetWindowConstraints(HWND hwnd, const flutter::WindowConstraints *constraints)
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_UpdateTooltipPosition(HWND hwnd)
bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd)
void InternalFlutterWindows_WindowManager_SetWindowSize(HWND hwnd, const flutter::WindowSizeRequest *size)
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::RegularWindowCreationRequest *request)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(int64_t engine_id, const flutter::DialogWindowCreationRequest *request)
void InternalFlutterWindows_WindowManager_SetFullscreen(HWND hwnd, const flutter::FullscreenRequest *request)
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_UpdatePopupPosition(HWND hwnd)
void InternalFlutterWindows_WindowManager_OnDestroyWindow(HWND hwnd)
flutter::ActualWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateSatelliteWindow(int64_t engine_id, const flutter::SatelliteWindowCreationRequest *request)
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_SetSatelliteParent(HWND satellite_hwnd, HWND new_parent)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(int64_t engine_id, const flutter::TooltipWindowCreationRequest *request)
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreatePopupWindow(int64_t engine_id, const flutter::PopupWindowCreationRequest *request)
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)