| 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
21 namespace flutter {
22
23 WindowManager::WindowManager(FlutterWindowsEngine* engine) : engine_(engine) {}
24
25 void WindowManager::Initialize(const WindowingInitRequest* request) {
26 on_message_ = request->on_message;
27 isolate_ = Isolate::Current();
28 }
29
30 FlutterViewId WindowManager::CreateRegularWindow(
31 const RegularWindowCreationRequest* request) {
32 auto window = HostWindow::CreateRegularWindow(
33 this, engine_, request->preferred_size, request->preferred_constraints,
34 request->title);
35 if (!window || !window->GetWindowHandle()) {
36 FML_LOG(ERROR) << "Failed to create host window";
37 return -1;
38 }
39 FlutterViewId const view_id = window->view_controller_->view()->view_id();
40 active_windows_[window->GetWindowHandle()] = std::move(window);
41 return view_id;
42 }
43
44 FlutterViewId WindowManager::CreateDialogWindow(
45 const DialogWindowCreationRequest* request) {
46 auto window = HostWindow::CreateDialogWindow(
47 this, engine_, request->preferred_size, request->preferred_constraints,
48 request->title, request->parent_or_null);
49 if (!window || !window->GetWindowHandle()) {
50 FML_LOG(ERROR) << "Failed to create host window";
51 return -1;
52 }
53 FlutterViewId const view_id = window->view_controller_->view()->view_id();
54 active_windows_[window->GetWindowHandle()] = std::move(window);
55 return view_id;
56 }
57
58 void WindowManager::OnEngineShutdown() {
59 // Don't send any more messages to isolate.
60 on_message_ = nullptr;
61 std::vector<HWND> active_handles;
62 active_handles.reserve(active_windows_.size());
63 for (auto& [hwnd, window] : active_windows_) {
64 active_handles.push_back(hwnd);
65 }
66 for (auto hwnd : active_handles) {
67 // This will destroy the window, which will in turn remove the
68 // HostWindow from map when handling WM_NCDESTROY inside
69 // HandleMessage.
70 DestroyWindow(hwnd);
71 }
72 }
73
74 std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
75 UINT message,
76 WPARAM wparam,
77 LPARAM lparam) {
78 if (message == WM_NCDESTROY) {
79 active_windows_.erase(hwnd);
80 }
81
82 FlutterWindowsView* view = engine_->GetViewFromTopLevelWindow(hwnd);
83 if (!view) {
84 FML_LOG(WARNING) << "Received message for unknown view";
85 return std::nullopt;
86 }
87
88 WindowsMessage message_struct = {.view_id = view->view_id(),
89 .hwnd = hwnd,
90 .message = message,
91 .wParam = wparam,
92 .lParam = lparam,
93 .result = 0,
94 .handled = false};
95
96 // Not initialized yet.
97 if (!isolate_) {
98 return std::nullopt;
99 }
100
101 IsolateScope scope(*isolate_);
102 on_message_(&message_struct);
103 if (message_struct.handled) {
104 return message_struct.result;
105 } else {
106 return std::nullopt;
107 }
108 }
109
110 } // namespace flutter
111
112 void InternalFlutterWindows_WindowManager_Initialize(
113 int64_t engine_id,
114 const flutter::WindowingInitRequest* request) {
115 flutter::FlutterWindowsEngine* engine =
116 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
117 engine->window_manager()->Initialize(request);
118 }
119
120 FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(
121 int64_t engine_id,
122 const flutter::RegularWindowCreationRequest* request) {
123 flutter::FlutterWindowsEngine* engine =
124 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
125 return engine->window_manager()->CreateRegularWindow(request);
126 }
127
128 FLUTTER_EXPORT
129 FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(
130 int64_t engine_id,
131 const flutter::DialogWindowCreationRequest* request) {
132 flutter::FlutterWindowsEngine* engine =
133 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
134 return engine->window_manager()->CreateDialogWindow(request);
135 }
136
137 HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(
138 int64_t engine_id,
139 FlutterViewId view_id) {
140 flutter::FlutterWindowsEngine* engine =
141 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
142 flutter::FlutterWindowsView* view = engine->view(view_id);
143 if (view == nullptr) {
144 return nullptr;
145 } else {
146 return GetAncestor(view->GetWindowHandle(), GA_ROOT);
147 }
148 }
149
150 flutter::ActualWindowSize
151 InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd) {
152 return flutter::HostWindow::GetWindowContentSize(hwnd);
153 }
154
155 void InternalFlutterWindows_WindowManager_SetWindowSize(
156 HWND hwnd,
157 const flutter::WindowSizeRequest* size) {
158 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
159 if (window) {
160 window->SetContentSize(*size);
161 }
162 }
163
164 void InternalFlutterWindows_WindowManager_SetWindowConstraints(
165 HWND hwnd,
166 const flutter::WindowConstraints* constraints) {
167 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
168 if (window) {
169 window->SetConstraints(*constraints);
170 }
171 }
172
173 void InternalFlutterWindows_WindowManager_SetFullscreen(
174 HWND hwnd,
175 const flutter::FullscreenRequest* request) {
176 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
177 const std::optional<FlutterEngineDisplayId> display_id =
178 request->has_display_id
179 ? std::optional<FlutterEngineDisplayId>(request->display_id)
180 : std::nullopt;
181 if (window) {
182 window->SetFullscreen(request->fullscreen, display_id);
183 }
184 }
185
186 bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd) {
187 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
188 if (window) {
189 return window->GetFullscreen();
190 }
191
192 return false;
193 }
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:92
flutter::FlutterWindowsEngine::view
FlutterWindowsView * view(FlutterViewId view_id) const
Definition: flutter_windows_engine.cc:688
flutter::FlutterWindowsEngine::GetEngineForId
static FlutterWindowsEngine * GetEngineForId(int64_t engine_id)
Definition: flutter_windows_engine.cc:259
flutter::FlutterWindowsEngine::GetViewFromTopLevelWindow
FlutterWindowsView * GetViewFromTopLevelWindow(HWND hwnd) const
Definition: flutter_windows_engine.cc:864
flutter::FlutterWindowsEngine::window_manager
WindowManager * window_manager()
Definition: flutter_windows_engine.h:327
flutter::FlutterWindowsView
Definition: flutter_windows_view.h:34
flutter::FlutterWindowsView::view_id
FlutterViewId view_id() const
Definition: flutter_windows_view.cc:713
flutter::FlutterWindowsView::GetWindowHandle
virtual HWND GetWindowHandle() const
Definition: flutter_windows_view.cc:782
flutter::HostWindow
Definition: host_window.h:27
flutter::HostWindow::GetWindowContentSize
static ActualWindowSize GetWindowContentSize(HWND hwnd)
Definition: host_window.cc:712
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizeRequest &size)
Definition: host_window.cc:479
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:327
flutter::HostWindow::GetFullscreen
virtual bool GetFullscreen() const
Definition: host_window.cc:708
flutter::HostWindow::SetConstraints
void SetConstraints(const WindowConstraints &constraints)
Definition: host_window.cc:521
flutter::HostWindow::SetFullscreen
virtual void SetFullscreen(bool fullscreen, std::optional< FlutterEngineDisplayId > display_id)
Definition: host_window.cc:562
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:215
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:204
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:58
flutter::WindowManager::CreateDialogWindow
FlutterViewId CreateDialogWindow(const DialogWindowCreationRequest *request)
Definition: window_manager.cc:44
flutter::WindowManager::HandleMessage
std::optional< LRESULT > HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: window_manager.cc:74
flutter::WindowManager::CreateRegularWindow
FlutterViewId CreateRegularWindow(const RegularWindowCreationRequest *request)
Definition: window_manager.cc:30
flutter::WindowManager::Initialize
void Initialize(const WindowingInitRequest *request)
Definition: window_manager.cc:25
flutter::WindowManager::WindowManager
WindowManager(FlutterWindowsEngine *engine)
Definition: window_manager.cc:23
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
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:71
flutter::DialogWindowCreationRequest
Definition: window_manager.h:48
flutter::DialogWindowCreationRequest::title
LPCWSTR title
Definition: window_manager.h:51
flutter::DialogWindowCreationRequest::preferred_size
WindowSizeRequest preferred_size
Definition: window_manager.h:49
flutter::DialogWindowCreationRequest::parent_or_null
HWND parent_or_null
Definition: window_manager.h:52
flutter::DialogWindowCreationRequest::preferred_constraints
WindowConstraints preferred_constraints
Definition: window_manager.h:50
flutter::FullscreenRequest
Definition: window_manager.h:76
flutter::FullscreenRequest::has_display_id
bool has_display_id
Definition: window_manager.h:78
flutter::FullscreenRequest::fullscreen
bool fullscreen
Definition: window_manager.h:77
flutter::FullscreenRequest::display_id
FlutterEngineDisplayId display_id
Definition: window_manager.h:79
flutter::RegularWindowCreationRequest
Definition: window_manager.h:42
flutter::RegularWindowCreationRequest::preferred_size
WindowSizeRequest preferred_size
Definition: window_manager.h:43
flutter::RegularWindowCreationRequest::preferred_constraints
WindowConstraints preferred_constraints
Definition: window_manager.h:44
flutter::RegularWindowCreationRequest::title
LPCWSTR title
Definition: window_manager.h:45
flutter::WindowConstraints
Definition: window_manager.h:33
flutter::WindowSizeRequest
Definition: window_manager.h:26
flutter::WindowingInitRequest
Definition: window_manager.h:65
flutter::WindowingInitRequest::on_message
void(* on_message)(WindowsMessage *)
Definition: window_manager.h:66
flutter::WindowsMessage
Definition: window_manager.h:55
flutter::WindowsMessage::result
LRESULT result
Definition: window_manager.h:61
flutter::WindowsMessage::view_id
FlutterViewId view_id
Definition: window_manager.h:56
flutter::WindowsMessage::handled
bool handled
Definition: window_manager.h:62
InternalFlutterWindows_WindowManager_Initialize
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
Definition: window_manager.cc:112
InternalFlutterWindows_WindowManager_SetWindowConstraints
void InternalFlutterWindows_WindowManager_SetWindowConstraints(HWND hwnd, const flutter::WindowConstraints *constraints)
Definition: window_manager.cc:164
InternalFlutterWindows_WindowManager_GetFullscreen
bool InternalFlutterWindows_WindowManager_GetFullscreen(HWND hwnd)
Definition: window_manager.cc:186
InternalFlutterWindows_WindowManager_SetWindowSize
void InternalFlutterWindows_WindowManager_SetWindowSize(HWND hwnd, const flutter::WindowSizeRequest *size)
Definition: window_manager.cc:155
InternalFlutterWindows_WindowManager_CreateRegularWindow
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::RegularWindowCreationRequest *request)
Definition: window_manager.cc:120
InternalFlutterWindows_WindowManager_CreateDialogWindow
FLUTTER_EXPORT FlutterViewId InternalFlutterWindows_WindowManager_CreateDialogWindow(int64_t engine_id, const flutter::DialogWindowCreationRequest *request)
Definition: window_manager.cc:129
InternalFlutterWindows_WindowManager_SetFullscreen
void InternalFlutterWindows_WindowManager_SetFullscreen(HWND hwnd, const flutter::FullscreenRequest *request)
Definition: window_manager.cc:173
InternalFlutterWindows_WindowManager_GetWindowContentSize
flutter::ActualWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
Definition: window_manager.cc:151
InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
Definition: window_manager.cc:137
window_manager.h
windowing.h