-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathflutter_window_win32.cc
276 lines (235 loc) · 9.14 KB
/
flutter_window_win32.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/windows/flutter_window_win32.h"
#include <dwmapi.h>
#include <chrono>
#include <map>
namespace flutter {
namespace {
// The Windows DPI system is based on this
// constant for machines running at 100% scaling.
constexpr int base_dpi = 96;
// TODO: See if this can be queried from the OS; this value is chosen
// arbitrarily to get something that feels reasonable.
constexpr int kScrollOffsetMultiplier = 20;
// Maps a Flutter cursor name to an HCURSOR.
//
// Returns the arrow cursor for unknown constants.
//
// This map must be kept in sync with Flutter framework's
// services/mouse_cursor.dart.
static HCURSOR GetCursorByName(const std::string& cursor_name) {
static auto* cursors = new std::map<std::string, const wchar_t*>{
{"allScroll", IDC_SIZEALL},
{"basic", IDC_ARROW},
{"click", IDC_HAND},
{"forbidden", IDC_NO},
{"help", IDC_HELP},
{"move", IDC_SIZEALL},
{"none", nullptr},
{"noDrop", IDC_NO},
{"precise", IDC_CROSS},
{"progress", IDC_APPSTARTING},
{"text", IDC_IBEAM},
{"resizeColumn", IDC_SIZEWE},
{"resizeDown", IDC_SIZENS},
{"resizeDownLeft", IDC_SIZENESW},
{"resizeDownRight", IDC_SIZENWSE},
{"resizeLeft", IDC_SIZEWE},
{"resizeLeftRight", IDC_SIZEWE},
{"resizeRight", IDC_SIZEWE},
{"resizeRow", IDC_SIZENS},
{"resizeUp", IDC_SIZENS},
{"resizeUpDown", IDC_SIZENS},
{"resizeUpLeft", IDC_SIZENWSE},
{"resizeUpRight", IDC_SIZENESW},
{"resizeUpLeftDownRight", IDC_SIZENWSE},
{"resizeUpRightDownLeft", IDC_SIZENESW},
{"wait", IDC_WAIT},
};
const wchar_t* idc_name = IDC_ARROW;
auto it = cursors->find(cursor_name);
if (it != cursors->end()) {
idc_name = it->second;
}
return ::LoadCursor(nullptr, idc_name);
}
} // namespace
FlutterWindowWin32::FlutterWindowWin32(int width, int height)
: binding_handler_delegate_(nullptr) {
WindowWin32::InitializeChild("FLUTTERVIEW", width, height);
current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW);
}
FlutterWindowWin32::~FlutterWindowWin32() {}
void FlutterWindowWin32::SetView(WindowBindingHandlerDelegate* window) {
binding_handler_delegate_ = window;
direct_manipulation_owner_->SetBindingHandlerDelegate(window);
}
WindowsRenderTarget FlutterWindowWin32::GetRenderTarget() {
return WindowsRenderTarget(GetWindowHandle());
}
PlatformWindow FlutterWindowWin32::GetPlatformWindow() {
return GetWindowHandle();
}
float FlutterWindowWin32::GetDpiScale() {
return static_cast<float>(GetCurrentDPI()) / static_cast<float>(base_dpi);
}
bool FlutterWindowWin32::IsVisible() {
return IsWindowVisible(GetWindowHandle());
}
PhysicalWindowBounds FlutterWindowWin32::GetPhysicalWindowBounds() {
return {GetCurrentWidth(), GetCurrentHeight()};
}
void FlutterWindowWin32::UpdateFlutterCursor(const std::string& cursor_name) {
current_cursor_ = GetCursorByName(cursor_name);
}
void FlutterWindowWin32::OnWindowResized() {
// Blocking the raster thread until DWM flushes alleviates glitches where
// previous size surface is stretched over current size view.
DwmFlush();
}
// Translates button codes from Win32 API to FlutterPointerMouseButtons.
static uint64_t ConvertWinButtonToFlutterButton(UINT button) {
switch (button) {
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
return kFlutterPointerButtonMousePrimary;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
return kFlutterPointerButtonMouseSecondary;
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
return kFlutterPointerButtonMouseMiddle;
case XBUTTON1:
return kFlutterPointerButtonMouseBack;
case XBUTTON2:
return kFlutterPointerButtonMouseForward;
}
std::cerr << "Mouse button not recognized: " << button << std::endl;
return 0;
}
void FlutterWindowWin32::OnDpiScale(unsigned int dpi){};
// When DesktopWindow notifies that a WM_Size message has come in
// lets FlutterEngine know about the new size.
void FlutterWindowWin32::OnResize(unsigned int width, unsigned int height) {
if (binding_handler_delegate_ != nullptr) {
binding_handler_delegate_->OnWindowSizeChanged(width, height);
}
}
void FlutterWindowWin32::OnPointerMove(double x,
double y,
FlutterPointerDeviceKind device_kind,
int32_t device_id) {
binding_handler_delegate_->OnPointerMove(x, y, device_kind, device_id);
}
void FlutterWindowWin32::OnPointerDown(double x,
double y,
FlutterPointerDeviceKind device_kind,
int32_t device_id,
UINT button) {
uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
if (flutter_button != 0) {
binding_handler_delegate_->OnPointerDown(
x, y, device_kind, device_id,
static_cast<FlutterPointerMouseButtons>(flutter_button));
}
}
void FlutterWindowWin32::OnPointerUp(double x,
double y,
FlutterPointerDeviceKind device_kind,
int32_t device_id,
UINT button) {
uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
if (flutter_button != 0) {
binding_handler_delegate_->OnPointerUp(
x, y, device_kind, device_id,
static_cast<FlutterPointerMouseButtons>(flutter_button));
}
}
void FlutterWindowWin32::OnPointerLeave(double x,
double y,
FlutterPointerDeviceKind device_kind,
int32_t device_id) {
binding_handler_delegate_->OnPointerLeave(x, y, device_kind, device_id);
}
void FlutterWindowWin32::OnSetCursor() {
::SetCursor(current_cursor_);
}
void FlutterWindowWin32::OnText(const std::u16string& text) {
binding_handler_delegate_->OnText(text);
}
void FlutterWindowWin32::OnKey(int key,
int scancode,
int action,
char32_t character,
bool extended,
bool was_down,
KeyEventCallback callback) {
binding_handler_delegate_->OnKey(key, scancode, action, character, extended,
was_down, std::move(callback));
}
void FlutterWindowWin32::OnComposeBegin() {
binding_handler_delegate_->OnComposeBegin();
}
void FlutterWindowWin32::OnComposeCommit() {
binding_handler_delegate_->OnComposeCommit();
}
void FlutterWindowWin32::OnComposeEnd() {
binding_handler_delegate_->OnComposeEnd();
}
void FlutterWindowWin32::OnComposeChange(const std::u16string& text,
int cursor_pos) {
binding_handler_delegate_->OnComposeChange(text, cursor_pos);
}
void FlutterWindowWin32::OnUpdateSemanticsEnabled(bool enabled) {
binding_handler_delegate_->OnUpdateSemanticsEnabled(enabled);
}
void FlutterWindowWin32::OnScroll(double delta_x,
double delta_y,
FlutterPointerDeviceKind device_kind,
int32_t device_id) {
POINT point;
GetCursorPos(&point);
ScreenToClient(GetWindowHandle(), &point);
binding_handler_delegate_->OnScroll(point.x, point.y, delta_x, delta_y,
kScrollOffsetMultiplier, device_kind,
device_id);
}
void FlutterWindowWin32::OnCursorRectUpdated(const Rect& rect) {
// Convert the rect from Flutter logical coordinates to device coordinates.
auto scale = GetDpiScale();
Point origin(rect.left() * scale, rect.top() * scale);
Size size(rect.width() * scale, rect.height() * scale);
UpdateCursorRect(Rect(origin, size));
}
void FlutterWindowWin32::OnResetImeComposing() {
AbortImeComposing();
}
bool FlutterWindowWin32::OnBitmapSurfaceUpdated(const void* allocation,
size_t row_bytes,
size_t height) {
HDC dc = ::GetDC(std::get<HWND>(GetRenderTarget()));
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = row_bytes / 4;
bmi.bmiHeader.biHeight = -height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
int ret = SetDIBitsToDevice(dc, 0, 0, row_bytes / 4, height, 0, 0, 0, height,
allocation, &bmi, DIB_RGB_COLORS);
return ret != 0;
}
gfx::NativeViewAccessible FlutterWindowWin32::GetNativeViewAccessible() {
return binding_handler_delegate_->GetNativeViewAccessible();
}
PointerLocation FlutterWindowWin32::GetPrimaryPointerLocation() {
POINT point;
GetCursorPos(&point);
ScreenToClient(GetWindowHandle(), &point);
return {(size_t)point.x, (size_t)point.y};
}
} // namespace flutter