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 
6 
7 #include <dwmapi.h>
8 #include <optional>
9 #include <vector>
10 
11 #include "embedder.h"
16 #include "fml/logging.h"
20 
21 namespace flutter {
22 
24 
26  on_message_ = request->on_message;
27  isolate_ = Isolate::Current();
28 }
29 
31  return !active_windows_.empty();
32 }
33 
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 
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 
102  int64_t engine_id,
103  const flutter::WindowingInitRequest* request) {
106  engine->window_manager()->Initialize(request);
107 }
108 
110  int64_t engine_id) {
113  return engine->window_manager()->HasTopLevelWindows();
114 }
115 
117  int64_t engine_id,
118  const flutter::WindowCreationRequest* request) {
121  return engine->window_manager()->CreateRegularWindow(request);
122 }
123 
125  int64_t engine_id,
126  FlutterViewId view_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 
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 
152  HWND hwnd,
153  const flutter::WindowSizing* size) {
155  if (window) {
156  window->SetContentSize(*size);
157  }
158 }
FlutterWindowsView * view(FlutterViewId view_id) const
static FlutterWindowsEngine * GetEngineForId(int64_t engine_id)
FlutterWindowsView * GetViewFromTopLevelWindow(HWND hwnd) const
virtual HWND GetWindowHandle() const
void SetContentSize(const WindowSizing &size)
Definition: host_window.cc:466
static std::unique_ptr< HostWindow > CreateRegularWindow(WindowManager *controller, FlutterWindowsEngine *engine, const WindowSizing &content_size)
Definition: host_window.cc:200
static HostWindow * GetThisFromHandle(HWND hwnd)
Definition: host_window.cc:343
static Isolate Current()
Definition: isolate_scope.cc:9
std::optional< LRESULT > HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
void Initialize(const WindowingInitRequest *request)
WindowManager(FlutterWindowsEngine *engine)
FlutterViewId CreateRegularWindow(const WindowCreationRequest *request)
bool HasTopLevelWindows() const
UINT FlutterDesktopGetDpiForHWND(HWND hwnd)
Win32Message message
int64_t FlutterViewId
Definition: flutter_view.h:13
void(* on_message)(WindowsMessage *)
void InternalFlutterWindows_WindowManager_Initialize(int64_t engine_id, const flutter::WindowingInitRequest *request)
bool InternalFlutterWindows_WindowManager_HasTopLevelWindows(int64_t engine_id)
FlutterViewId InternalFlutterWindows_WindowManager_CreateRegularWindow(int64_t engine_id, const flutter::WindowCreationRequest *request)
FlutterWindowSize InternalFlutterWindows_WindowManager_GetWindowContentSize(HWND hwnd)
HWND InternalFlutterWindows_WindowManager_GetTopLevelWindowHandle(int64_t engine_id, FlutterViewId view_id)
void InternalFlutterWindows_WindowManager_SetWindowContentSize(HWND hwnd, const flutter::WindowSizing *size)