| 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
5 #include "flutter/shell/platform/windows/window_manager.h"
6
7 #include <dwmapi.h>
8 #include <optional>
9 #include <vector>
10
11 #include "embedder.h"
12 #include "flutter/shell/platform/common/windowing.h"
13 #include "flutter/shell/platform/windows/flutter_windows_engine.h"
14 #include "flutter/shell/platform/windows/flutter_windows_view_controller.h"
15 #include "flutter/shell/platform/windows/host_window.h"
16 #include "fml/logging.h"
17 #include "shell/platform/windows/client_wrapper/include/flutter/flutter_view.h"
18 #include "shell/platform/windows/flutter_windows_view.h"
19 #include "shell/platform/windows/host_window.h"
20 #include "shell/platform/windows/host_window_tooltip.h"
21
22 namespace flutter {
23
24 WindowManager::WindowManager(FlutterWindowsEngine* engine) : engine_(engine) {}
25
26 void WindowManager::Initialize(const WindowingInitRequest* request) {
27 on_message_ = request->on_message;
28 isolate_ = Isolate::Current();
29 }
30
31 FlutterViewId WindowManager::CreateRegularWindow(
32 const RegularWindowCreationRequest* request) {
33 auto window = HostWindow::CreateRegularWindow(
34 this, engine_, request->preferred_size, request->preferred_constraints,
35 request->title);
36 if (!window || !window->GetWindowHandle()) {
37 FML_LOG(ERROR) << "Failed to create host window";
38 return -1;
39 }
40 FlutterViewId const view_id = window->view_controller_->view()->view_id();
41 active_windows_[window->GetWindowHandle()] = std::move(window);
42 return view_id;
43 }
44
45 FlutterViewId WindowManager::CreateDialogWindow(
46 const DialogWindowCreationRequest* request) {
47 auto window = HostWindow::CreateDialogWindow(
48 this, engine_, request->preferred_size, request->preferred_constraints,
49 request->title, request->parent_or_null);
50 if (!window || !window->GetWindowHandle()) {
51 FML_LOG(ERROR) << "Failed to create host window";
52 return -1;
53 }
54 FlutterViewId const view_id = window->view_controller_->view()->view_id();
55 active_windows_[window->GetWindowHandle()] = std::move(window);
56 return view_id;
57 }
58
59 FlutterViewId WindowManager::CreateTooltipWindow(
60 const TooltipWindowCreationRequest* request) {
61 auto window = HostWindow::CreateTooltipWindow(
62 this, engine_, request->preferred_constraints,
63 request->is_sized_to_content, request->get_position_callback,
64 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
74 void WindowManager::OnEngineShutdown() {
75 // Don't send any more messages to isolate.
76 on_message_ = nullptr;
77 std::vector<HWND> active_handles;
78 active_handles.reserve(active_windows_.size());
79 for (auto& [hwnd, window] : active_windows_) {
80 active_handles.push_back(hwnd);
81 }
82 for (auto hwnd : active_handles) {
83 // This will destroy the window, which will in turn remove the
84 // HostWindow from map when handling WM_NCDESTROY inside
85 // HandleMessage.
86 InternalFlutterWindows_WindowManager_OnDestroyWindow(hwnd);
87 }
88 }
89
90 std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
91 UINT message,
92 WPARAM wparam,
93 LPARAM lparam) {
94 if (message == WM_NCDESTROY) {
95 active_windows_.erase(hwnd);
96 return std::nullopt;
97 }
98
99 HostWindow* host_window = HostWindow::GetThisFromHandle(hwnd);
100 FlutterWindowsView* view =
101 host_window ? host_window->view_controller_->view() : nullptr;
102
103 if (!view) {
104 FML_LOG(WARNING) << "Received message for unknown view";
105 return std::nullopt;
106 }
107
108 WindowsMessage message_struct = {.view_id = view->view_id(),
109 .hwnd = hwnd,
110 .message = message,
111 .wParam = wparam,
112 .lParam = lparam,
113 .result = 0,
114 .handled = false};
115
116 // Not initialized yet.
117 if (!isolate_) {
118 return std::nullopt;
119 }
120
121 IsolateScope scope(*isolate_);
122 on_message_(&message_struct);
123 if (message_struct.handled) {
124 return message_struct.result;
125 } else {
126 return std::nullopt;
127 }
128 }
129
130 } // namespace flutter
131
132 void InternalFlutterWindows_WindowManager_Initialize(
133 int64_t engine_id,
134 const flutter::WindowingInitRequest* request) {
135 flutter::FlutterWindowsEngine* engine =
136 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
137 engine->window_manager()->Initialize(request);
138 }
139
140 FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(
141 int64_t engine_id,
142 const flutter::RegularWindowCreationRequest* request) {
143 flutter::FlutterWindowsEngine* engine =
144 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
145 return engine->window_manager()->CreateRegularWindow(request);
146 }
147
148 FLUTTER_EXPORT
149 FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(
150 int64_t engine_id,
151 const flutter::DialogWindowCreationRequest* request) {
152 flutter::FlutterWindowsEngine* engine =
153 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
154 return engine->window_manager()->CreateDialogWindow(request);
155 }
156
157 FLUTTER_EXPORT
158 FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(
159 int64_t engine_id,
160 const flutter::TooltipWindowCreationRequest* request) {
161 flutter::FlutterWindowsEngine* engine =
162 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
163 return engine->window_manager()->CreateTooltipWindow(request);
164 }
165
166 HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(
167 int64_t engine_id,
168 FlutterViewId view_id) {
169 flutter::FlutterWindowsEngine* engine =
170 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
171 flutter::FlutterWindowsView* view = engine->view(view_id);
172 if (view == nullptr) {
173 return nullptr;
174 } else {
175 return GetAncestor(view->GetWindowHandle(), GA_ROOT);
176 }
177 }
178
179 flutter::ActualWindowSize
180 InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd) {
181 return flutter::HostWindow::GetWindowContentSize(hwnd);
182 }
183
184 void InternalFlutterWindows_WindowManager_SetWindowSize(
185 HWND hwnd,
186 const flutter::WindowSizeRequest* size) {
187 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
188 if (window) {
189 window->SetContentSize(*size);
190 }
191 }
192
193 void InternalFlutterWindows_WindowManager_OnDestroyWindow(HWND hwnd) {
194 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
195 HWND flutter_view_handle = nullptr;
196 if (window) {
197 // First reparent the FlutterView to null parent. Otherwise destroying
198 // the window HWND will immediately destroy the FlutterView HWND as well,
199 // which will cause crash when raster thread tries to reallocate surface.
200 // The FlutterView may only be destroyed safely when
201 // FlutterWindowsEngine::RemoveView finishes.
202 flutter_view_handle = window->GetFlutterViewWindowHandle();
203 ShowWindow(flutter_view_handle, SW_HIDE);
204 SetParent(flutter_view_handle, nullptr);
205 }
206 DestroyWindow(hwnd);
207 if (flutter_view_handle) {
208 // Now the flutter view HWND can be destroyed safely.
209 DestroyWindow(flutter_view_handle);
210 }
211 }
212
213 void InternalFlutterWindows_WindowManager_SetWindowConstraints(
214 HWND hwnd,
215 const flutter::WindowConstraints* constraints) {
216 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
217 if (window) {
218 window->SetConstraints(*constraints);
219 }
220 }
221
222 void InternalFlutterWindows_WindowManager_SetFullscreen(
223 HWND hwnd,
224 const flutter::FullscreenRequest* request) {
225 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
226 const std::optional<FlutterEngineDisplayId> display_id =
227 request->has_display_id
228 ? std::optional<FlutterEngineDisplayId>(request->display_id)
229 : std::nullopt;
230 if (window) {
231 window->SetFullscreen(request->fullscreen, display_id);
232 }
233 }
234
235 bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd) {
236 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
237 if (window) {
238 return window->GetFullscreen();
239 }
240
241 return false;
242 }
243
244 FLUTTER_EXPORT
245 void InternalFlutterWindows_WindowManager_UpdateTooltipPosition(HWND hwnd) {
246 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
247 flutter::HostWindowTooltip* tooltip_window =
248 reinterpret_cast<flutter::HostWindowTooltip*>(window);
249 tooltip_window->UpdatePosition();
250 }
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:92
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view(FlutterViewId view_id) const
Definition: flutter_windows_engine.cc:692
flutter::FlutterWindowsEngine::GetEngineForId
static FlutterWindowsEngine * GetEngineForId(int64_t engine_id)
Definition: flutter_windows_engine.cc:259
flutter::FlutterWindowsEngine::window_manager
WindowManager * window_manager()
Definition: flutter_windows_engine.h:330
flutter::FlutterWindowsView
Definition: flutter_windows_view.h:46
flutter::FlutterWindowsView::view_id
FlutterViewId view_id() const
Definition: flutter_windows_view.cc:753
flutter::FlutterWindowsView::GetWindowHandle
virtual HWND GetWindowHandle() const
Definition: flutter_windows_view.cc:822
flutter::HostWindow
Definition: host_window.h:29
flutter::HostWindow::view_controller_
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:217
flutter::HostWindow::GetWindowContentSize
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:724
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:491
flutter::HostWindow::GetFlutterViewWindowHandle
HWND GetFlutterViewWindowHandle() const
Definition: host_window.cc:354
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:335
flutter::HostWindow::GetFullscreen
virtual bool GetFullscreen() const
Definition: host_window.cc:720
flutter::HostWindow::SetConstraints
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:533
flutter::HostWindow::CreateTooltipWindow
static std::unique_ptr< HostWindow > CreateTooltipWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowConstraints &preferred_constraints, bool is_sized_to_content, GetWindowPositionCallback get_position_callback, HWND parent)
Definition: host_window.cc:229
flutter::HostWindow::SetFullscreen
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:574
flutter::HostWindow::CreateDialogWindow
static std::unique_ptr< HostWindow > CreateDialogWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title, HWND parent)
Definition: host_window.cc:216
flutter::HostWindow::CreateRegularWindow
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *window_manager, FlutterWindowsEngine *engine, const WindowSizeRequest &preferred_size, const WindowConstraints &preferred_constraints, LPCWSTR title)
Definition: host_window.cc:205
flutter::HostWindowTooltip
Definition: host_window_tooltip.h:15
flutter::HostWindowTooltip::UpdatePosition
void UpdatePosition()
Definition: host_window_tooltip.cc:85
flutter::Isolate::Current
static Isolate Current()
Definition: isolate_scope.cc:9
flutter::IsolateScope
Definition: isolate_scope.h:33
flutter::WindowManager::OnEngineShutdown
void OnEngineShutdown()
Definition: window_manager.cc:74
flutter::WindowManager::CreateDialogWindow
FlutterViewId CreateDialogWindow(const DialogWindowCreationRequest *request)
Definition: window_manager.cc:45
flutter::WindowManager::HandleMessage
std::optional< LRESULT > HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: window_manager.cc:90
flutter::WindowManager::CreateRegularWindow
FlutterViewId CreateRegularWindow(const RegularWindowCreationRequest *request)
Definition: window_manager.cc:31
flutter::WindowManager::Initialize
void Initialize(const WindowingInitRequest *request)
Definition: window_manager.cc:26
flutter::WindowManager::WindowManager
WindowManager(FlutterWindowsEngine *engine)
Definition: window_manager.cc:24
flutter::WindowManager::CreateTooltipWindow
FlutterViewId CreateTooltipWindow(const TooltipWindowCreationRequest *request)
Definition: window_manager.cc:59
FLUTTER_EXPORT
#define FLUTTER_EXPORT
Definition: flutter_export.h:23
flutter_view.h
flutter_windows_engine.h
flutter_windows_view.h
flutter_windows_view_controller.h
host_window.h
host_window_tooltip.h
message
Win32Message message
Definition: keyboard_unittests.cc:137
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::FlutterViewId
int64_t FlutterViewId
Definition: accessibility_plugin.h:15
flutter::ActualWindowSize
Definition: window_manager.h:96
flutter::DialogWindowCreationRequest
Definition: window_manager.h:62
flutter::DialogWindowCreationRequest::title
LPCWSTR title
Definition: window_manager.h:65
flutter::DialogWindowCreationRequest::preferred_size
WindowSizeRequest preferred_size
Definition: window_manager.h:63
flutter::DialogWindowCreationRequest::parent_or_null
HWND parent_or_null
Definition: window_manager.h:66
flutter::DialogWindowCreationRequest::preferred_constraints
WindowConstraints preferred_constraints
Definition: window_manager.h:64
flutter::FullscreenRequest
Definition: window_manager.h:101
flutter::FullscreenRequest::has_display_id
bool has_display_id
Definition: window_manager.h:103
flutter::FullscreenRequest::fullscreen
bool fullscreen
Definition: window_manager.h:102
flutter::FullscreenRequest::display_id
FlutterEngineDisplayId display_id
Definition: window_manager.h:104
flutter::RegularWindowCreationRequest
Definition: window_manager.h:56
flutter::RegularWindowCreationRequest::preferred_size
WindowSizeRequest preferred_size
Definition: window_manager.h:57
flutter::RegularWindowCreationRequest::preferred_constraints
WindowConstraints preferred_constraints
Definition: window_manager.h:58
flutter::RegularWindowCreationRequest::title
LPCWSTR title
Definition: window_manager.h:59
flutter::TooltipWindowCreationRequest
Definition: window_manager.h:73
flutter::TooltipWindowCreationRequest::is_sized_to_content
bool is_sized_to_content
Definition: window_manager.h:75
flutter::TooltipWindowCreationRequest::parent
HWND parent
Definition: window_manager.h:76
flutter::TooltipWindowCreationRequest::get_position_callback
GetWindowPositionCallback get_position_callback
Definition: window_manager.h:77
flutter::TooltipWindowCreationRequest::preferred_constraints
WindowConstraints preferred_constraints
Definition: window_manager.h:74
flutter::WindowConstraints
Definition: window_manager.h:33
flutter::WindowSizeRequest
Definition: window_manager.h:26
flutter::WindowingInitRequest
Definition: window_manager.h:90
flutter::WindowingInitRequest::on_message
void(* on_message)(WindowsMessage *)
Definition: window_manager.h:91
flutter::WindowsMessage
Definition: window_manager.h:80
flutter::WindowsMessage::result
LRESULT result
Definition: window_manager.h:86
flutter::WindowsMessage::view_id
FlutterViewId view_id
Definition: window_manager.h:81
flutter::WindowsMessage::handled
bool handled
Definition: window_manager.h:87
InternalFlutterWindows_WindowManager_Initialize
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
Definition: window_manager.cc:132
InternalFlutterWindows_WindowManager_SetWindowConstraints
void InternalFlutterWindows_WindowManager_SetWindowConstraints(HWND hwnd, const flutter::WindowConstraints *constraints)
Definition: window_manager.cc:213
InternalFlutterWindows_WindowManager_UpdateTooltipPosition
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_UpdateTooltipPosition(HWND hwnd)
Definition: window_manager.cc:245
InternalFlutterWindows_WindowManager_GetFullscreen
bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd)
Definition: window_manager.cc:235
InternalFlutterWindows_WindowManager_SetWindowSize
void InternalFlutterWindows_WindowManager_SetWindowSize(HWND hwnd, const flutter::WindowSizeRequest *size)
Definition: window_manager.cc:184
InternalFlutterWindows_WindowManager_CreateRegularWindow
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::RegularWindowCreationRequest *request)
Definition: window_manager.cc:140
InternalFlutterWindows_WindowManager_CreateDialogWindow
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(int64_t engine_id, const flutter::DialogWindowCreationRequest *request)
Definition: window_manager.cc:149
InternalFlutterWindows_WindowManager_SetFullscreen
void InternalFlutterWindows_WindowManager_SetFullscreen(HWND hwnd, const flutter::FullscreenRequest *request)
Definition: window_manager.cc:222
InternalFlutterWindows_WindowManager_OnDestroyWindow
void InternalFlutterWindows_WindowManager_OnDestroyWindow(HWND hwnd)
Definition: window_manager.cc:193
InternalFlutterWindows_WindowManager_GetWindowContentSize
flutter::ActualWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
Definition: window_manager.cc:180
InternalFlutterWindows_WindowManager_CreateTooltipWindow
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(int64_t engine_id, const flutter::TooltipWindowCreationRequest *request)
Definition: window_manager.cc:158
InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
Definition: window_manager.cc:166
window_manager.h
windowing.h