| 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 =
37 HostWindow::CreateRegularWindow(this, engine_, request->content_size);
38 if (!window || !window->GetWindowHandle()) {
39 FML_LOG(ERROR) << "Failed to create host window";
40 return -1;
41 }
42 FlutterViewId const view_id = window->view_controller_->view()->view_id();
43 active_windows_[window->GetWindowHandle()] = std::move(window);
44 return view_id;
45 }
46
47 void WindowManager::OnEngineShutdown() {
48 // Don't send any more messages to isolate.
49 on_message_ = nullptr;
50 std::vector<HWND> active_handles;
51 active_handles.reserve(active_windows_.size());
52 for (auto& [hwnd, window] : active_windows_) {
53 active_handles.push_back(hwnd);
54 }
55 for (auto hwnd : active_handles) {
56 // This will destroy the window, which will in turn remove the
57 // HostWindow from map when handling WM_NCDESTROY inside
58 // HandleMessage.
59 DestroyWindow(hwnd);
60 }
61 }
62
63 std::optional<LRESULT> WindowManager::HandleMessage(HWND hwnd,
64 UINT message,
65 WPARAM wparam,
66 LPARAM lparam) {
67 if (message == WM_NCDESTROY) {
68 active_windows_.erase(hwnd);
69 }
70
71 FlutterWindowsView* view = engine_->GetViewFromTopLevelWindow(hwnd);
72 if (!view) {
73 FML_LOG(WARNING) << "Received message for unknown view";
74 return std::nullopt;
75 }
76
77 WindowsMessage message_struct = {.view_id = view->view_id(),
78 .hwnd = hwnd,
79 .message = message,
80 .wParam = wparam,
81 .lParam = lparam,
82 .result = 0,
83 .handled = false};
84
85 // Not initialized yet.
86 if (!isolate_) {
87 return std::nullopt;
88 }
89
90 IsolateScope scope(*isolate_);
91 on_message_(&message_struct);
92 if (message_struct.handled) {
93 return message_struct.result;
94 } else {
95 return std::nullopt;
96 }
97 }
98
99 } // namespace flutter
100
101 void InternalFlutterWindows_WindowManager_Initialize(
102 int64_t engine_id,
103 const flutter::WindowingInitRequest* request) {
104 flutter::FlutterWindowsEngine* engine =
105 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
106 engine->window_manager()->Initialize(request);
107 }
108
109 bool InternalFlutterWindows_WindowManager_HasTopLevelWindows(
110 int64_t engine_id) {
111 flutter::FlutterWindowsEngine* engine =
112 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
113 return engine->window_manager()->HasTopLevelWindows();
114 }
115
116 FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(
117 int64_t engine_id,
118 const flutter::WindowCreationRequest* request) {
119 flutter::FlutterWindowsEngine* engine =
120 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
121 return engine->window_manager()->CreateRegularWindow(request);
122 }
123
124 HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(
125 int64_t engine_id,
126 FlutterViewId view_id) {
127 flutter::FlutterWindowsEngine* engine =
128 flutter::FlutterWindowsEngine::GetEngineForId(engine_id);
129 flutter::FlutterWindowsView* view = engine->view(view_id);
130 if (view == nullptr) {
131 return nullptr;
132 } else {
133 return GetAncestor(view->GetWindowHandle(), GA_ROOT);
134 }
135 }
136
137 FlutterWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(
138 HWND hwnd) {
139 RECT rect;
140 GetClientRect(hwnd, &rect);
141 double const dpr = FlutterDesktopGetDpiForHWND(hwnd) /
142 static_cast<double>(USER_DEFAULT_SCREEN_DPI);
143 double const width = rect.right / dpr;
144 double const height = rect.bottom / dpr;
145 return {
146 .width = rect.right / dpr,
147 .height = rect.bottom / dpr,
148 };
149 }
150
151 void InternalFlutterWindows_WindowManager_SetWindowContentSize(
152 HWND hwnd,
153 const flutter::WindowSizing* size) {
154 flutter::HostWindow* window = flutter::HostWindow::GetThisFromHandle(hwnd);
155 if (window) {
156 window->SetContentSize(*size);
157 }
158 }
flutter::FlutterWindowsEngine
Definition: flutter_windows_engine.h:91
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:248
flutter::FlutterWindowsEngine::GetViewFromTopLevelWindow
FlutterWindowsView * GetViewFromTopLevelWindow(HWND hwnd) const
Definition: flutter_windows_engine.cc:855
flutter::FlutterWindowsEngine::window_manager
WindowManager * window_manager()
Definition: flutter_windows_engine.h:319
flutter::FlutterWindowsView
Definition: flutter_windows_view.h:34
flutter::FlutterWindowsView::view_id
FlutterViewId view_id() const
Definition: flutter_windows_view.cc:709
flutter::FlutterWindowsView::GetWindowHandle
virtual HWND GetWindowHandle() const
Definition: flutter_windows_view.cc:778
flutter::HostWindow
Definition: host_window.h:24
flutter::HostWindow::SetContentSize
void SetContentSize(const WindowSizing &size)
Definition: host_window.cc:466
flutter::HostWindow::CreateRegularWindow
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *controller, FlutterWindowsEngine *engine, const WindowSizing &content_size)
Definition: host_window.cc:200
flutter::HostWindow::GetThisFromHandle
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:343
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:47
flutter::WindowManager::HandleMessage
std::optional< LRESULT > HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: window_manager.cc:63
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
FlutterDesktopGetDpiForHWND
UINT FlutterDesktopGetDpiForHWND(HWND hwnd)
Definition: flutter_windows.cc:322
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
FlutterWindowSize
Definition: window_manager.h:120
flutter::WindowCreationRequest
Definition: window_manager.h:51
flutter::WindowCreationRequest::content_size
WindowSizing content_size
Definition: window_manager.h:52
flutter::WindowSizing
Definition: window_manager.h:36
flutter::WindowingInitRequest
Definition: window_manager.h:47
flutter::WindowingInitRequest::on_message
void(* on_message)(WindowsMessage *)
Definition: window_manager.h:48
flutter::WindowsMessage
Definition: window_manager.h:26
flutter::WindowsMessage::result
LRESULT result
Definition: window_manager.h:32
flutter::WindowsMessage::view_id
FlutterViewId view_id
Definition: window_manager.h:27
flutter::WindowsMessage::handled
bool handled
Definition: window_manager.h:33
InternalFlutterWindows_WindowManager_Initialize
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
Definition: window_manager.cc:101
InternalFlutterWindows_WindowManager_HasTopLevelWindows
bool InternalFlutterWindows_WindowManager_HasTopLevelWindows(int64_t engine_id)
Definition: window_manager.cc:109
InternalFlutterWindows_WindowManager_CreateRegularWindow
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::WindowCreationRequest *request)
Definition: window_manager.cc:116
InternalFlutterWindows_WindowManager_GetWindowContentSize
FlutterWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
Definition: window_manager.cc:137
InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
Definition: window_manager.cc:124
InternalFlutterWindows_WindowManager_SetWindowContentSize
void InternalFlutterWindows_WindowManager_SetWindowContentSize(HWND hwnd, const flutter::WindowSizing *size)
Definition: window_manager.cc:151
window_manager.h
windowing.h