Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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"
22
23namespace flutter {
24
26
28 on_message_ = request->on_message;
29 isolate_ = Isolate::Current();
30}
31
33 const RegularWindowCreationRequest* request) {
35 this, engine_, request->preferred_size, request->preferred_constraints,
36 request->title);
37 if (!window || !window->GetWindowHandle()) {
38 FML_LOG(ERROR) << "Failed to create host window";
39 return -1;
40 }
41 FlutterViewId const view_id = window->view_controller_->view()->view_id();
42 active_windows_[window->GetWindowHandle()] = std::move(window);
43 return view_id;
44}
45
47 const DialogWindowCreationRequest* request) {
49 this, engine_, request->preferred_size, request->preferred_constraints,
50 request->title, request->parent_or_null);
51 if (!window || !window->GetWindowHandle()) {
52 FML_LOG(ERROR) << "Failed to create host window";
53 return -1;
54 }
55 FlutterViewId const view_id = window->view_controller_->view()->view_id();
56 active_windows_[window->GetWindowHandle()] = std::move(window);
57 return view_id;
58}
59
61 const TooltipWindowCreationRequest* request) {
63 this, engine_, request->preferred_constraints,
64 request->get_position_callback, request->parent);
65 if (!window || !window->GetWindowHandle()) {
66 FML_LOG(ERROR) << "Failed to create host window";
67 return -1;
68 }
69 FlutterViewId const view_id = window->view_controller_->view()->view_id();
70 active_windows_[window->GetWindowHandle()] = std::move(window);
71 return view_id;
72}
73
75 const PopupWindowCreationRequest* request) {
77 this, engine_, request->preferred_constraints,
78 request->get_position_callback, request->parent);
79 if (!window || !window->GetWindowHandle()) {
80 FML_LOG(ERROR) << "Failed to create host window";
81 return -1;
82 }
83 FlutterViewId const view_id = window->view_controller_->view()->view_id();
84 active_windows_[window->GetWindowHandle()] = std::move(window);
85 return view_id;
86}
87
89 // Don't send any more messages to isolate.
90 on_message_ = nullptr;
91 std::vector<HWND> active_handles;
92 active_handles.reserve(active_windows_.size());
93 for (auto& [hwnd, window] : active_windows_) {
94 active_handles.push_back(hwnd);
95 }
96 for (auto hwnd : active_handles) {
97 // This will destroy the window, which will in turn remove the
98 // HostWindow from map when handling WM_NCDESTROY inside
99 // HandleMessage.
101 }
102}
103
104std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
106 WPARAM wparam,
107 LPARAM lparam) {
108 if (message == WM_NCDESTROY) {
109 active_windows_.erase(hwnd);
110 return std::nullopt;
111 }
112
113 HostWindow* host_window = HostWindow::GetThisFromHandle(hwnd);
115 host_window ? host_window->view_controller_->view() : nullptr;
116
117 if (!view) {
118 FML_LOG(WARNING) << "Received message for unknown view";
119 return std::nullopt;
120 }
121
122 WindowsMessage message_struct = {.view_id = view->view_id(),
123 .hwnd = hwnd,
124 .message = message,
125 .wParam = wparam,
126 .lParam = lparam,
127 .result = 0,
128 .handled = false};
129
130 // Not initialized yet.
131 if (!isolate_) {
132 return std::nullopt;
133 }
134
135 IsolateScope scope(*isolate_);
136 on_message_(&message_struct);
137 if (message_struct.handled) {
138 return message_struct.result;
139 } else {
140 return std::nullopt;
141 }
142}
143
144} // namespace flutter
145
147 int64_t engine_id,
148 const flutter::WindowingInitRequest* request) {
151 engine->window_manager()->Initialize(request);
152}
153
155 int64_t engine_id,
159 return engine->window_manager()->CreateRegularWindow(request);
160}
161
164 int64_t engine_id,
168 return engine->window_manager()->CreateDialogWindow(request);
169}
170
173 int64_t engine_id,
177 return engine->window_manager()->CreateTooltipWindow(request);
178}
179
182 int64_t engine_id,
186 return engine->window_manager()->CreatePopupWindow(request);
187}
188
190 int64_t engine_id,
195 if (view == nullptr) {
196 return nullptr;
197 } else {
198 return GetAncestor(view->GetWindowHandle(), GA_ROOT);
199 }
200}
201
206
208 HWND hwnd,
209 const flutter::WindowSizeRequest* size) {
211 if (window) {
212 window->SetContentSize(*size);
213 }
214}
215
218 HWND flutter_view_handle = nullptr;
219 if (window) {
220 // First reparent the FlutterView to null parent. Otherwise destroying
221 // the window HWND will immediately destroy the FlutterView HWND as well,
222 // which will cause crash when raster thread tries to reallocate surface.
223 // The FlutterView may only be destroyed safely when
224 // FlutterWindowsEngine::RemoveView finishes.
225 flutter_view_handle = window->GetFlutterViewWindowHandle();
226 ShowWindow(flutter_view_handle, SW_HIDE);
227 SetParent(flutter_view_handle, nullptr);
228 }
229 DestroyWindow(hwnd);
230 if (flutter_view_handle) {
231 // Now the flutter view HWND can be destroyed safely.
232 DestroyWindow(flutter_view_handle);
233 }
234}
235
237 HWND hwnd,
238 const flutter::WindowConstraints* constraints) {
240 if (window) {
241 window->SetConstraints(*constraints);
242 }
243}
244
246 HWND hwnd,
247 const flutter::FullscreenRequest* request) {
249 const std::optional<FlutterEngineDisplayId> display_id =
250 request->has_display_id
251 ? std::optional<FlutterEngineDisplayId>(request->display_id)
252 : std::nullopt;
253 if (window) {
254 window->SetFullscreen(request->fullscreen, display_id);
255 }
256}
257
260 if (window) {
261 return window->GetFullscreen();
262 }
263
264 return false;
265}
266
274
static FlutterWindowsEngine * GetEngineForId(int64_t engine_id)
static std::unique_ptr< HostWindow > CreatePopupWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
std::unique_ptr< FlutterWindowsViewController > view_controller_
static std::unique_ptr< HostWindow > CreateTooltipWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, GetWindowPositionCallback get_position_callback, HWND parent)
static ActualWindowSize GetWindowContentSize(HWND hwnd)
static HostWindow * GetThisFromHandle(HWND hwnd)
static std::unique_ptr< HostWindow > CreateDialogWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title, HWND parent)
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title)
static Isolate Current()
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 CreateTooltipWindow(const TooltipWindowCreationRequest *request)
int64_t FlutterViewId
Definition embedder.h:393
GLFWwindow * window
Definition main.cc:60
FlutterEngine engine
Definition main.cc:84
FlView * view
const char * message
G_BEGIN_DECLS FlutterViewId view_id
#define FLUTTER_EXPORT
#define FML_LOG(severity)
Definition logging.h:101
int64_t FlutterViewId
FlutterEngineDisplayId display_id
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_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)
unsigned int UINT
LONG_PTR LPARAM
UINT_PTR WPARAM