Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
host_window_popup.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>
7#include <memory>
10
11namespace flutter {
13 WindowManager* window_manager,
15 const BoxConstraints& constraints,
16 GetWindowPositionCallback get_position_callback,
17 HWND parent)
18 : HostWindow(window_manager, engine),
19 get_position_callback_(get_position_callback),
20 parent_(parent),
21 isolate_(Isolate::Current()),
22 view_alive_(std::make_shared<int>(0)) {
23 // Use minimum constraints as initial size to ensure the view can be created
24 // with valid metrics. The size will be updated when content is rendered.
25 auto const initial_width =
26 static_cast<double>(constraints.smallest().width());
27 auto const initial_height =
28 static_cast<double>(constraints.smallest().height());
29
32 .window_style = WS_POPUP,
33 .extended_window_style = WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
34 .box_constraints = constraints,
35 .initial_window_rect = {{0, 0}, {initial_width, initial_height}},
36 .title = L"",
37 .owner_window = parent,
38 .nCmdShow = SW_SHOWNOACTIVATE,
39 .sizing_delegate = this,
40 .is_sized_to_content = true});
41}
42
43void HostWindowPopup::DidUpdateViewSize(int32_t width, int32_t height) {
44 // This is called from the raster thread.
45 std::weak_ptr<int> weak_view_alive = view_alive_;
46 engine_->task_runner()->PostTask([this, width, height, weak_view_alive]() {
47 auto const view_alive = weak_view_alive.lock();
48 if (!view_alive) {
49 return;
50 }
51 if (width_ == width && height_ == height) {
52 return;
53 }
54
56 return;
57 }
58
59 width_ = width;
60 height_ = height;
62 });
63}
64
65WindowRect HostWindowPopup::GetWorkArea() const {
66 constexpr int32_t kDefaultWorkAreaSize = 10000;
67 WindowRect work_area = {0, 0, kDefaultWorkAreaSize, kDefaultWorkAreaSize};
68 HMONITOR monitor = MonitorFromWindow(parent_, MONITOR_DEFAULTTONEAREST);
69 if (monitor) {
70 MONITORINFO monitor_info = {0};
71 monitor_info.cbSize = sizeof(monitor_info);
72 if (GetMonitorInfo(monitor, &monitor_info)) {
73 work_area.left = monitor_info.rcWork.left;
74 work_area.top = monitor_info.rcWork.top;
75 work_area.width = monitor_info.rcWork.right - monitor_info.rcWork.left;
76 work_area.height = monitor_info.rcWork.bottom - monitor_info.rcWork.top;
77 }
78 }
79 return work_area;
80}
81
83 RECT parent_client_rect;
84 GetClientRect(parent_, &parent_client_rect);
85
86 // Convert top-left and bottom-right points to screen coordinates.
87 POINT parent_top_left = {parent_client_rect.left, parent_client_rect.top};
88 POINT parent_bottom_right = {parent_client_rect.right,
89 parent_client_rect.bottom};
90
91 ClientToScreen(parent_, &parent_top_left);
92 ClientToScreen(parent_, &parent_bottom_right);
93
94 // Get monitor from HWND and usable work area.
95 HMONITOR monitor = MonitorFromWindow(parent_, MONITOR_DEFAULTTONEAREST);
96 WindowRect work_area = GetWorkArea();
97
98 IsolateScope scope(isolate_);
99
100 // Frees the memory allocated by the positioner callback.
101 // Even if the callback throws an exception, the memory will be freed when
102 // rect goes out of scope.
103 std::unique_ptr<WindowRect, decltype(&free)> rect(
104 get_position_callback_(
105 WindowSize{width_, height_},
106 WindowRect{parent_top_left.x, parent_top_left.y,
107 parent_bottom_right.x - parent_top_left.x,
108 parent_bottom_right.y - parent_top_left.y},
109 work_area),
110 free);
111 SetWindowPos(window_handle_, HWND_TOP, rect->left, rect->top, rect->width,
112 rect->height, SWP_NOACTIVATE | SWP_NOOWNERZORDER);
113
114 // The positioner constrained the dimensions more than current size, apply
115 // positioner constraints.
116 if (rect->width < width_ || rect->height < height_) {
117 auto metrics_event = view_controller_->view()->CreateWindowMetricsEvent();
118 view_controller_->engine()->SendWindowMetricsEvent(metrics_event);
119 }
120}
121
122} // namespace flutter
Size smallest() const
Definition geometry.h:94
void InitializeFlutterView(HostWindowInitializationParams const &params)
std::unique_ptr< FlutterWindowsViewController > view_controller_
FlutterWindowsEngine * engine_
HostWindowPopup(WindowManager *window_manager, FlutterWindowsEngine *engine, const BoxConstraints &constraints, GetWindowPositionCallback get_position_callback, HWND parent)
double height() const
Definition geometry.h:45
double width() const
Definition geometry.h:44
void PostTask(TaskClosure task)
FlutterEngine engine
Definition main.cc:84
WindowRect *(* GetWindowPositionCallback)(const WindowSize &child_size, const WindowRect &parent_rect, const WindowRect &output_rect)
Definition ref_ptr.h:261
int32_t height
int32_t width