Flutter Windows Embedder
host_window_sized.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 #include "flutter/fml/logging.h"
10 
11 namespace flutter {
12 
14  FlutterWindowsEngine* engine,
15  bool resizable)
16  : HostWindow(window_manager, engine), resizable_(resizable) {}
17 
19  // By the time the base destructor runs, the most-derived class must have
20  // already reset |view_controller_| (and therefore stopped the raster thread
21  // from sizing this object). See the destructor comment in host_window_sized.h
22  // for the rationale. If this fires, a HostWindowSized subclass is missing the
23  // required |view_controller_.reset()| at the start of its destructor.
24  FML_DCHECK(!view_controller_)
25  << "HostWindowSized subclass must reset view_controller_ in its "
26  "destructor.";
27 }
28 
29 void HostWindowSized::DidUpdateViewSize(int32_t width, int32_t height) {
30  // This is called from the raster thread.
31  std::weak_ptr<int> weak_alive = alive_;
32  engine_->task_runner()->PostTask([this, width, height, weak_alive]() {
33  if (weak_alive.expired()) {
34  return;
35  }
36  if (physical_width_ == width && physical_height_ == height) {
37  return;
38  }
39  if (is_being_destroyed_) {
40  return;
41  }
42  physical_width_ = width;
43  physical_height_ = height;
44 
45  ApplyContentSize(width, height);
46  });
47 }
48 
49 void HostWindowSized::ApplyContentSize(int32_t physical_width,
50  int32_t physical_height) {
51  WINDOWINFO window_info = {.cbSize = sizeof(WINDOWINFO)};
52  GetWindowInfo(window_handle_, &window_info);
53 
54  // Convert physical pixels to logical pixels.
55  UINT const dpi = GetDpiForHWND(window_handle_);
56  double const scale = static_cast<double>(dpi > 0 ? dpi : 96) / 96.0;
57  std::optional<Size> const window_size = GetWindowSizeForClientSize(
59  Size(physical_width / scale, physical_height / scale),
60  box_constraints_.smallest(), box_constraints_.biggest(),
61  window_info.dwStyle, window_info.dwExStyle, nullptr);
62 
63  if (!window_size) {
64  return;
65  }
66 
67  SetWindowPos(window_handle_, NULL, 0, 0, window_size->width(),
68  window_size->height(),
69  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
70 
71  if (resizable_) {
72  // For resizable windows, stop tracking content size after the initial
73  // frame so subsequent user-initiated resizes are forwarded to Flutter.
74  view_controller_->view()->SetSizedToContent(false);
75  }
76 }
77 
78 WindowRect HostWindowSized::GetWorkArea() const {
80 }
81 
83  constexpr int32_t kDefaultWorkAreaSize = 10000;
84  WindowRect work_area = {0, 0, kDefaultWorkAreaSize, kDefaultWorkAreaSize};
85  HMONITOR const monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
86  if (monitor) {
87  MONITORINFO monitor_info = {};
88  monitor_info.cbSize = sizeof(monitor_info);
89  if (GetMonitorInfo(monitor, &monitor_info)) {
90  work_area.left = monitor_info.rcWork.left;
91  work_area.top = monitor_info.rcWork.top;
92  work_area.width = monitor_info.rcWork.right - monitor_info.rcWork.left;
93  work_area.height = monitor_info.rcWork.bottom - monitor_info.rcWork.top;
94  }
95  }
96  return work_area;
97 }
98 
99 } // namespace flutter
std::shared_ptr< WindowsProcTable > windows_proc_table()
BoxConstraints box_constraints_
Definition: host_window.h:328
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:314
FlutterWindowsEngine * engine_
Definition: host_window.h:309
std::shared_ptr< int > alive_
Definition: host_window.h:319
static std::optional< Size > GetWindowSizeForClientSize(WindowsProcTable const &win32, Size const &client_size, std::optional< Size > smallest, std::optional< Size > biggest, DWORD window_style, DWORD extended_window_style, std::optional< HWND > const &owner_hwnd)
Definition: host_window.cc:805
static WindowRect GetWorkAreaForWindow(HWND hwnd)
HostWindowSized(WindowManager *window_manager, FlutterWindowsEngine *engine, bool resizable)
virtual void ApplyContentSize(int32_t physical_width, int32_t physical_height)
void PostTask(TaskClosure task)
Definition: task_runner.cc:89
UINT GetDpiForHWND(HWND hwnd)
Definition: dpi_utils.cc:128