Flutter Engine
win32_flutter_window.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 
5 #include "flutter/shell/platform/windows/win32_flutter_window.h"
6 
7 #include <chrono>
8 #include <map>
9 
10 namespace flutter {
11 
12 namespace {
13 
14 // The Windows DPI system is based on this
15 // constant for machines running at 100% scaling.
16 constexpr int base_dpi = 96;
17 
18 // TODO: See if this can be queried from the OS; this value is chosen
19 // arbitrarily to get something that feels reasonable.
20 constexpr int kScrollOffsetMultiplier = 20;
21 
22 // Maps a Flutter cursor name to an HCURSOR.
23 //
24 // Returns the arrow cursor for unknown constants.
25 //
26 // This map must be kept in sync with Flutter framework's
27 // rendering/mouse_cursor.dart.
28 static HCURSOR GetCursorByName(const std::string& cursor_name) {
29  static auto* cursors = new std::map<std::string, const wchar_t*>{
30  {"allScroll", IDC_SIZEALL},
31  {"basic", IDC_ARROW},
32  {"click", IDC_HAND},
33  {"forbidden", IDC_NO},
34  {"help", IDC_HELP},
35  {"move", IDC_SIZEALL},
36  {"none", nullptr},
37  {"noDrop", IDC_NO},
38  {"precise", IDC_CROSS},
39  {"progress", IDC_APPSTARTING},
40  {"text", IDC_IBEAM},
41  {"resizeColumn", IDC_SIZEWE},
42  {"resizeDown", IDC_SIZENS},
43  {"resizeDownLeft", IDC_SIZENESW},
44  {"resizeDownRight", IDC_SIZENWSE},
45  {"resizeLeft", IDC_SIZEWE},
46  {"resizeLeftRight", IDC_SIZEWE},
47  {"resizeRight", IDC_SIZEWE},
48  {"resizeRow", IDC_SIZENS},
49  {"resizeUp", IDC_SIZENS},
50  {"resizeUpDown", IDC_SIZENS},
51  {"resizeUpLeft", IDC_SIZENWSE},
52  {"resizeUpRight", IDC_SIZENESW},
53  {"resizeUpLeftDownRight", IDC_SIZENWSE},
54  {"resizeUpRightDownLeft", IDC_SIZENESW},
55  {"wait", IDC_WAIT},
56  };
57  const wchar_t* idc_name = IDC_ARROW;
58  auto it = cursors->find(cursor_name);
59  if (it != cursors->end()) {
60  idc_name = it->second;
61  }
62  return ::LoadCursor(nullptr, idc_name);
63 }
64 
65 } // namespace
66 
68  : binding_handler_delegate_(nullptr) {
69  Win32Window::InitializeChild("FLUTTERVIEW", width, height);
70  current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW);
71 }
72 
74 
76  binding_handler_delegate_ = window;
77 }
78 
81 }
82 
84  return static_cast<float>(GetCurrentDPI()) / static_cast<float>(base_dpi);
85 }
86 
88  return {GetCurrentWidth(), GetCurrentHeight()};
89 }
90 
91 void Win32FlutterWindow::UpdateFlutterCursor(const std::string& cursor_name) {
92  current_cursor_ = GetCursorByName(cursor_name);
93 }
94 
95 // Translates button codes from Win32 API to FlutterPointerMouseButtons.
96 static uint64_t ConvertWinButtonToFlutterButton(UINT button) {
97  switch (button) {
98  case WM_LBUTTONDOWN:
99  case WM_LBUTTONUP:
101  case WM_RBUTTONDOWN:
102  case WM_RBUTTONUP:
104  case WM_MBUTTONDOWN:
105  case WM_MBUTTONUP:
107  case XBUTTON1:
109  case XBUTTON2:
111  }
112  std::cerr << "Mouse button not recognized: " << button << std::endl;
113  return 0;
114 }
115 
116 void Win32FlutterWindow::OnDpiScale(unsigned int dpi){};
117 
118 // When DesktopWindow notifies that a WM_Size message has come in
119 // lets FlutterEngine know about the new size.
120 void Win32FlutterWindow::OnResize(unsigned int width, unsigned int height) {
121  if (binding_handler_delegate_ != nullptr) {
122  binding_handler_delegate_->OnWindowSizeChanged(width, height);
123  }
124 }
125 
126 void Win32FlutterWindow::OnPointerMove(double x, double y) {
127  binding_handler_delegate_->OnPointerMove(x, y);
128 }
129 
130 void Win32FlutterWindow::OnPointerDown(double x, double y, UINT button) {
131  uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
132  if (flutter_button != 0) {
133  binding_handler_delegate_->OnPointerDown(
134  x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
135  }
136 }
137 
138 void Win32FlutterWindow::OnPointerUp(double x, double y, UINT button) {
139  uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
140  if (flutter_button != 0) {
141  binding_handler_delegate_->OnPointerUp(
142  x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
143  }
144 }
145 
147  binding_handler_delegate_->OnPointerLeave();
148 }
149 
151  ::SetCursor(current_cursor_);
152 }
153 
154 void Win32FlutterWindow::OnText(const std::u16string& text) {
155  binding_handler_delegate_->OnText(text);
156 }
157 
159  int scancode,
160  int action,
161  char32_t character) {
162  binding_handler_delegate_->OnKey(key, scancode, action, character);
163 }
164 
165 void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) {
166  POINT point;
167  GetCursorPos(&point);
168 
169  ScreenToClient(GetWindowHandle(), &point);
170  binding_handler_delegate_->OnScroll(point.x, point.y, delta_x, delta_y,
171  kScrollOffsetMultiplier);
172 }
173 
174 } // namespace flutter
void UpdateFlutterCursor(const std::string &cursor_name) override
virtual void OnPointerUp(double x, double y, FlutterPointerMouseButtons button)=0
PhysicalWindowBounds GetPhysicalWindowBounds() override
std::variant< HWND > WindowsRenderTarget
virtual void OnText(const std::u16string &)=0
virtual void OnPointerMove(double x, double y)=0
void OnPointerDown(double x, double y, UINT button) override
void OnScroll(double delta_x, double delta_y) override
void InitializeChild(const char *title, unsigned int width, unsigned int height)
Definition: win32_window.cc:31
void OnKey(int key, int scancode, int action, char32_t character) override
virtual void OnWindowSizeChanged(size_t width, size_t height) const =0
void OnText(const std::u16string &text) override
virtual void OnKey(int key, int scancode, int action, char32_t character)=0
WindowsRenderTarget GetRenderTarget() override
void OnPointerMove(double x, double y) override
virtual void OnPointerDown(double x, double y, FlutterPointerMouseButtons button)=0
SemanticsAction action
int32_t height
int32_t width
static uint64_t ConvertWinButtonToFlutterButton(UINT button)
void SetView(WindowBindingHandlerDelegate *view) override
void OnDpiScale(unsigned int dpi) override
Win32FlutterWindow(int width, int height)
void OnPointerUp(double x, double y, UINT button) override
virtual void OnScroll(double x, double y, double delta_x, double delta_y, int scroll_offset_multiplier)=0
void OnResize(unsigned int width, unsigned int height) override