win32_flutter_window.cc 5.4 KB
Newer Older
1 2 3 4
// 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.

5 6 7
#include "flutter/shell/platform/windows/win32_flutter_window.h"

#include <chrono>
8
#include <map>
9 10 11

namespace flutter {

12 13
namespace {

14
// The Windows DPI system is based on this
15 16 17
// constant for machines running at 100% scaling.
constexpr int base_dpi = 96;

18 19 20
// 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;
21

22 23 24
// Maps a Flutter cursor name to an HCURSOR.
//
// Returns the arrow cursor for unknown constants.
25 26 27
//
// This map must be kept in sync with Flutter framework's
// rendering/mouse_cursor.dart.
28 29
static HCURSOR GetCursorByName(const std::string& cursor_name) {
  static auto* cursors = new std::map<std::string, const wchar_t*>{
30
      {"allScroll", IDC_SIZEALL},
31 32 33
      {"basic", IDC_ARROW},
      {"click", IDC_HAND},
      {"forbidden", IDC_NO},
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      {"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},
56 57 58 59 60 61 62 63 64 65 66
  };
  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

67 68 69
Win32FlutterWindow::Win32FlutterWindow(int width, int height)
    : binding_handler_delegate_(nullptr) {
  Win32Window::InitializeChild("FLUTTERVIEW", width, height);
70
  current_cursor_ = ::LoadCursor(nullptr, IDC_ARROW);
71 72
}

73
Win32FlutterWindow::~Win32FlutterWindow() {}
74

75 76
void Win32FlutterWindow::SetView(WindowBindingHandlerDelegate* window) {
  binding_handler_delegate_ = window;
77 78
}

79 80
WindowsRenderTarget Win32FlutterWindow::GetRenderTarget() {
  return WindowsRenderTarget(GetWindowHandle());
81 82
}

83 84
float Win32FlutterWindow::GetDpiScale() {
  return static_cast<float>(GetCurrentDPI()) / static_cast<float>(base_dpi);
85 86
}

87 88
PhysicalWindowBounds Win32FlutterWindow::GetPhysicalWindowBounds() {
  return {GetCurrentWidth(), GetCurrentHeight()};
89 90
}

91 92 93 94
void Win32FlutterWindow::UpdateFlutterCursor(const std::string& cursor_name) {
  current_cursor_ = GetCursorByName(cursor_name);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
// 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;
}

116 117 118 119 120
void Win32FlutterWindow::OnDpiScale(unsigned int dpi){};

// When DesktopWindow notifies that a WM_Size message has come in
// lets FlutterEngine know about the new size.
void Win32FlutterWindow::OnResize(unsigned int width, unsigned int height) {
121 122 123
  if (binding_handler_delegate_ != nullptr) {
    binding_handler_delegate_->OnWindowSizeChanged(width, height);
  }
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 132 133 134
  uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
  if (flutter_button != 0) {
    binding_handler_delegate_->OnPointerDown(
        x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
135 136 137
  }
}

138
void Win32FlutterWindow::OnPointerUp(double x, double y, UINT button) {
139 140 141 142
  uint64_t flutter_button = ConvertWinButtonToFlutterButton(button);
  if (flutter_button != 0) {
    binding_handler_delegate_->OnPointerUp(
        x, y, static_cast<FlutterPointerMouseButtons>(flutter_button));
143 144 145
  }
}

146
void Win32FlutterWindow::OnPointerLeave() {
147
  binding_handler_delegate_->OnPointerLeave();
148 149
}

150 151 152 153
void Win32FlutterWindow::OnSetCursor() {
  ::SetCursor(current_cursor_);
}

154
void Win32FlutterWindow::OnText(const std::u16string& text) {
155
  binding_handler_delegate_->OnText(text);
156 157
}

158 159 160 161
void Win32FlutterWindow::OnKey(int key,
                               int scancode,
                               int action,
                               char32_t character) {
162
  binding_handler_delegate_->OnKey(key, scancode, action, character);
163 164 165 166 167 168 169
}

void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) {
  POINT point;
  GetCursorPos(&point);

  ScreenToClient(GetWindowHandle(), &point);
170 171
  binding_handler_delegate_->OnScroll(point.x, point.y, delta_x, delta_y,
                                      kScrollOffsetMultiplier);
172 173
}

174 175
void Win32FlutterWindow::OnFontChange() {
  binding_handler_delegate_->OnFontChange();
176 177
}

178
}  // namespace flutter