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