| 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->get_position_callback, request->parent);
64 if (!window || !window->GetWindowHandle()) {
65 FML_LOG(ERROR) << "Failed to create host window";
66 return -1;
67 }
68 FlutterViewId const view_id = window->view_controller_->view()->view_id();
69 active_windows_[window->GetWindowHandle()] = std::move(window);
70 return view_id;
71 }
72
73 void WindowManager::OnEngineShutdown() {
74 // Don't send any more messages to isolate.
75 on_message_ = nullptr;
76 std::vector<HWND> active_handles;
77 active_handles.reserve(active_windows_.size());
78 for (auto& [hwnd, window] : active_windows_) {
79 active_handles.push_back(hwnd);
80 }
81 for (auto hwnd : active_handles) {
82 // This will destroy the window, which will in turn remove the
83 // HostWindow from map when handling WM_NCDESTROY inside
84 // HandleMessage.
85 InternalFlutterWindows_WindowManager_OnDestroyWindow(hwnd);
86 }
87 }
88
89 std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
90 UINT message,
91 WPARAM wparam,
92 LPARAM lparam) {
93 if (message == WM_NCDESTROY) {
94 active_windows_.erase(hwnd);
95 return std::nullopt;
96 }
97
98 HostWindow* host_window = HostWindow::GetThisFromHandle(hwnd);
99 FlutterWindowsView* view =
100 host_window ? host_window->view_controller_->view() : nullptr;
101
102 if (!view) {
103 FML_LOG(WARNING) << "Received message for unknown view";
104 return std::nullopt;
105 }
106
107 WindowsMessage message_struct = {.view_id = view->view_id(),
108 .hwnd = hwnd,
109 .message = message,
110 .wParam = wparam,
111 .lParam = lparam,
112 .result = 0,
113 .handled = false};
114
115 // Not initialized yet.
116 if (!isolate_) {
117 return std::nullopt;
118 }
119
120 IsolateScope scope(*isolate_);
121 on_message_(&message_struct);
122 if (message_struct.handled) {
123 return message_struct.result;
124 } else {
125 return std::nullopt;
126 }
127 }
128
129 } // namespace flutter
130
131 void InternalFlutterWindows_WindowManager_Initialize(
132 int64_t engine_id,
133 const flutter::WindowingInitRequest* request) {
134 flutter::FlutterWindowsEngine* engine =
135 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
136 engine->window_manager()->Initialize(request);
137 }
138
139 FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(
140 int64_t engine_id,
141 const flutter::RegularWindowCreationRequest* request) {
142 flutter::FlutterWindowsEngine* engine =
143 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
144 return engine->window_manager()->CreateRegularWindow(request);
145 }
146
147 FLUTTER_EXPORT
148 FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(
149 int64_t engine_id,
150 const flutter::DialogWindowCreationRequest* request) {
151 flutter::FlutterWindowsEngine* engine =
152 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
153 return engine->window_manager()->CreateDialogWindow(request);
154 }
155
156 FLUTTER_EXPORT
157 FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(
158 int64_t engine_id,
159 const flutter::TooltipWindowCreationRequest* request) {
160 flutter::FlutterWindowsEngine* engine =
161 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
162 return engine->window_manager()->CreateTooltipWindow(request);
163 }
164
165 HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(
166 int64_t engine_id,
167 FlutterViewId view_id) {
168 flutter::FlutterWindowsEngine* engine =
169 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
170 flutter::FlutterWindowsView* view = engine->view(view_id);
171 if (view == nullptr) {
172 return nullptr;
173 } else {
174 return GetAncestor(view->GetWindowHandle(), GA_ROOT);
175 }
176 }
177
178 flutter::ActualWindowSize
179 InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd) {
180 return flutter::HostWindow::GetWindowContentSize(hwnd);
181 }
182
183 void InternalFlutterWindows_WindowManager_SetWindowSize(
184 HWND hwnd,
185 const flutter::WindowSizeRequest* size) {
186 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
187 if (window) {
188 window->SetContentSize(*size);
189 }
190 }
191
192 void InternalFlutterWindows_WindowManager_OnDestroyWindow(HWND hwnd) {
193 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
194 HWND flutter_view_handle = nullptr;
195 if (window) {
196 // First reparent the FlutterView to null parent. Otherwise destroying
197 // the window HWND will immediately destroy the FlutterView HWND as well,
198 // which will cause crash when raster thread tries to reallocate surface.
199 // The FlutterView may only be destroyed safely when
200 // FlutterWindowsEngine::RemoveView finishes.
201 flutter_view_handle = window->GetFlutterViewWindowHandle();
202 ShowWindow(flutter_view_handle, SW_HIDE);
203 SetParent(flutter_view_handle, nullptr);
204 }
205 DestroyWindow(hwnd);
206 if (flutter_view_handle) {
207 // Now the flutter view HWND can be destroyed safely.
208 DestroyWindow(flutter_view_handle);
209 }
210 }
211
212 void InternalFlutterWindows_WindowManager_SetWindowConstraints(
213 HWND hwnd,
214 const flutter::WindowConstraints* constraints) {
215 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
216 if (window) {
217 window->SetConstraints(*constraints);
218 }
219 }
220
221 void InternalFlutterWindows_WindowManager_SetFullscreen(
222 HWND hwnd,
223 const flutter::FullscreenRequest* request) {
224 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
225 const std::optional<FlutterEngineDisplayId> display_id =
226 request->has_display_id
227 ? std::optional<FlutterEngineDisplayId>(request->display_id)
228 : std::nullopt;
229 if (window) {
230 window->SetFullscreen(request->fullscreen, display_id);
231 }
232 }
233
234 bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd) {
235 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
236 if (window) {
237 return window->GetFullscreen();
238 }
239
240 return false;
241 }
242
243 FLUTTER_EXPORT
244 void InternalFlutterWindows_WindowManager_UpdateTooltipPosition(HWND hwnd) {
245 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
246 flutter::HostWindowTooltip* tooltip_window =
247 reinterpret_cast<flutter::HostWindowTooltip*>(window);
248 tooltip_window->UpdatePosition();
249 }
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:335
flutter::FlutterWindowsView
Definition: flutter_windows_view.h:47
flutter::FlutterWindowsView::view_id
FlutterViewId view_id() const
Definition: flutter_windows_view.cc:795
flutter::FlutterWindowsView::GetWindowHandle
virtual HWND GetWindowHandle() const
Definition: flutter_windows_view.cc:864
flutter::HostWindow
Definition: host_window.h:29
flutter::HostWindow::view_controller_
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:216
flutter::HostWindow::CreateTooltipWindow
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:229
flutter::HostWindow::GetWindowContentSize
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:726
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:493
flutter::HostWindow::GetFlutterViewWindowHandle
HWND GetFlutterViewWindowHandle() const
Definition: host_window.cc:356
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:337
flutter::HostWindow::GetFullscreen
virtual bool GetFullscreen() const
Definition: host_window.cc:722
flutter::HostWindow::SetConstraints
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:535
flutter::HostWindow::SetFullscreen
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:576
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:83
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:73
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:89
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:95
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:100
flutter::FullscreenRequest::has_display_id
bool has_display_id
Definition: window_manager.h:102
flutter::FullscreenRequest::fullscreen
bool fullscreen
Definition: window_manager.h:101
flutter::FullscreenRequest::display_id
FlutterEngineDisplayId display_id
Definition: window_manager.h:103
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::parent
HWND parent
Definition: window_manager.h:75
flutter::TooltipWindowCreationRequest::get_position_callback
GetWindowPositionCallback get_position_callback
Definition: window_manager.h:76
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:89
flutter::WindowingInitRequest::on_message
void(* on_message)(WindowsMessage *)
Definition: window_manager.h:90
flutter::WindowsMessage
Definition: window_manager.h:79
flutter::WindowsMessage::result
LRESULT result
Definition: window_manager.h:85
flutter::WindowsMessage::view_id
FlutterViewId view_id
Definition: window_manager.h:80
flutter::WindowsMessage::handled
bool handled
Definition: window_manager.h:86
InternalFlutterWindows_WindowManager_Initialize
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
Definition: window_manager.cc:131
InternalFlutterWindows_WindowManager_SetWindowConstraints
void InternalFlutterWindows_WindowManager_SetWindowConstraints(HWND hwnd, const flutter::WindowConstraints *constraints)
Definition: window_manager.cc:212
InternalFlutterWindows_WindowManager_UpdateTooltipPosition
FLUTTER_EXPORT void InternalFlutterWindows_WindowManager_UpdateTooltipPosition(HWND hwnd)
Definition: window_manager.cc:244
InternalFlutterWindows_WindowManager_GetFullscreen
bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd)
Definition: window_manager.cc:234
InternalFlutterWindows_WindowManager_SetWindowSize
void InternalFlutterWindows_WindowManager_SetWindowSize(HWND hwnd, const flutter::WindowSizeRequest *size)
Definition: window_manager.cc:183
InternalFlutterWindows_WindowManager_CreateRegularWindow
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::RegularWindowCreationRequest *request)
Definition: window_manager.cc:139
InternalFlutterWindows_WindowManager_CreateDialogWindow
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(int64_t engine_id, const flutter::DialogWindowCreationRequest *request)
Definition: window_manager.cc:148
InternalFlutterWindows_WindowManager_SetFullscreen
void InternalFlutterWindows_WindowManager_SetFullscreen(HWND hwnd, const flutter::FullscreenRequest *request)
Definition: window_manager.cc:221
InternalFlutterWindows_WindowManager_OnDestroyWindow
void InternalFlutterWindows_WindowManager_OnDestroyWindow(HWND hwnd)
Definition: window_manager.cc:192
InternalFlutterWindows_WindowManager_GetWindowContentSize
flutter::ActualWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
Definition: window_manager.cc:179
InternalFlutterWindows_WindowManager_CreateTooltipWindow
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateTooltipWindow(int64_t engine_id, const flutter::TooltipWindowCreationRequest *request)
Definition: window_manager.cc:157
InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
Definition: window_manager.cc:165
window_manager.h
windowing.h