Flutter Windows Embedder
host_window_tooltip.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 <cstdio>
9 
10 namespace flutter {
12  WindowManager* window_manager,
13  FlutterWindowsEngine* engine,
14  const BoxConstraints& constraints,
15  GetWindowPositionCallback get_position_callback,
16  HWND parent)
17  : HostWindow(window_manager, engine),
18  get_position_callback_(get_position_callback),
19  parent_(parent),
20  isolate_(Isolate::Current()),
21  view_alive_(std::make_shared<int>(0)) {
22  // Use minimum constraints as initial size to ensure the view can be created
23  // with valid metrics.
24  auto const initial_width =
25  static_cast<double>(constraints.smallest().width());
26  auto const initial_height =
27  static_cast<double>(constraints.smallest().height());
28 
31  .window_style = WS_POPUP,
32  .extended_window_style = WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
33  .box_constraints = constraints,
34  .initial_window_rect = {{0, 0}, {initial_width, initial_height}},
35  .title = L"",
36  .owner_window = parent,
37  .nCmdShow = SW_SHOWNOACTIVATE,
38  .sizing_delegate = this,
39  .is_sized_to_content = true});
40  SetWindowLongPtr(window_handle_, GWLP_HWNDPARENT,
41  reinterpret_cast<LONG_PTR>(parent_));
42 }
43 
44 void HostWindowTooltip::DidUpdateViewSize(int32_t width, int32_t height) {
45  // This is called from the raster thread.
46  std::weak_ptr<int> weak_view_alive = view_alive_;
47  engine_->task_runner()->PostTask([this, width, height, weak_view_alive]() {
48  auto const view_alive = weak_view_alive.lock();
49  if (!view_alive) {
50  return;
51  }
52  if (width_ == width && height_ == height) {
53  return;
54  }
55 
56  if (is_being_destroyed_) {
57  return;
58  }
59 
60  width_ = width;
61  height_ = height;
63  });
64 }
65 
66 WindowRect HostWindowTooltip::GetWorkArea() const {
67  constexpr int32_t kDefaultWorkAreaSize = 10000;
68  WindowRect work_area = {0, 0, kDefaultWorkAreaSize, kDefaultWorkAreaSize};
69  HMONITOR monitor = MonitorFromWindow(parent_, MONITOR_DEFAULTTONEAREST);
70  if (monitor) {
71  MONITORINFO monitor_info = {0};
72  monitor_info.cbSize = sizeof(monitor_info);
73  if (GetMonitorInfo(monitor, &monitor_info)) {
74  work_area.left = monitor_info.rcWork.left;
75  work_area.top = monitor_info.rcWork.top;
76  work_area.width = monitor_info.rcWork.right - monitor_info.rcWork.left;
77  work_area.height = monitor_info.rcWork.bottom - monitor_info.rcWork.top;
78  }
79  }
80  return work_area;
81 }
82 
84  RECT parent_client_rect;
85  GetClientRect(parent_, &parent_client_rect);
86 
87  // Convert top-left and bottom-right points to screen coordinates.
88  POINT parent_top_left = {parent_client_rect.left, parent_client_rect.top};
89  POINT parent_bottom_right = {parent_client_rect.right,
90  parent_client_rect.bottom};
91 
92  ClientToScreen(parent_, &parent_top_left);
93  ClientToScreen(parent_, &parent_bottom_right);
94 
95  // Get monitor from HWND and usable work area.
96  HMONITOR monitor = MonitorFromWindow(parent_, MONITOR_DEFAULTTONEAREST);
97  WindowRect work_area = GetWorkArea();
98 
99  IsolateScope scope(isolate_);
100  auto rect = get_position_callback_(
101  WindowSize{width_, height_},
102  WindowRect{parent_top_left.x, parent_top_left.y,
103  parent_bottom_right.x - parent_top_left.x,
104  parent_bottom_right.y - parent_top_left.y},
105  work_area);
106  SetWindowPos(window_handle_, nullptr, rect->left, rect->top, rect->width,
107  rect->height, SWP_NOACTIVATE | SWP_NOOWNERZORDER);
108  free(rect);
109 
110  // The positioner constrained the dimensions more than current size, apply
111  // positioner constraints.
112  if (rect->width < width_ || rect->height < height_) {
113  auto metrics_event = view_controller_->view()->CreateWindowMetricsEvent();
114  view_controller_->engine()->SendWindowMetricsEvent(metrics_event);
115  }
116 }
117 
119  UINT message,
120  WPARAM wparam,
121  LPARAM lparam) {
122  switch (message) {
123  case WM_MOUSEACTIVATE:
124  // Prevent activation when clicked
125  return MA_NOACTIVATE;
126 
127  case WM_NCACTIVATE:
128  // Return TRUE to prevent visual activation changes
129  return TRUE;
130 
131  case WM_ACTIVATE:
132  // Immediately deactivate if somehow activated
133  if (LOWORD(wparam) != WA_INACTIVE) {
134  HWND hFocus = GetFocus();
135  SetFocus(nullptr);
136  }
137  break;
138  }
139 
140  return HostWindow::HandleMessage(hwnd, message, wparam, lparam);
141 }
142 
143 } // namespace flutter
void InitializeFlutterView(HostWindowInitializationParams const &params)
Definition: host_window.cc:244
std::unique_ptr< FlutterWindowsViewController > view_controller_
Definition: host_window.h:216
FlutterWindowsEngine * engine_
Definition: host_window.h:211
virtual LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
Definition: host_window.cc:384
LRESULT HandleMessage(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) override
HostWindowTooltip(WindowManager *window_manager, FlutterWindowsEngine *engine, const BoxConstraints &constraints, GetWindowPositionCallback get_position_callback, HWND parent)
void PostTask(TaskClosure task)
Definition: task_runner.cc:89
Win32Message message
WindowRect *(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &output_rect)