From cd66e9dcfa432a1c56b739d9e3cadb96eda65adc Mon Sep 17 00:00:00 2001 From: Jon Salmon <26483285+Jon-Salmon@users.noreply.github.com> Date: Tue, 12 Jul 2022 23:01:17 +0100 Subject: [PATCH] [Windows] Respect OS's scroll speed setting at startup (flutter/engine#32094) Updates scrolling in Windows to respect the 'lines to scroll per scroll wheel tick' setting at app startup. The scrolling amount per tick is also increased to match Chrome (at 100px with default windows settings). This is also similar to what UWP does natively (approximately 96px based on observation). Resolves https://github.com/flutter/flutter/issues/97924 --- .../platform/windows/flutter_window_win32.cc | 6 +---- .../windows/flutter_window_win32_unittests.cc | 23 ++++++++++++++++-- .../shell/platform/windows/window_win32.cc | 24 +++++++++++++++++++ .../shell/platform/windows/window_win32.h | 9 +++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc index 1815a6c0dd..a6477cbbd0 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc @@ -16,10 +16,6 @@ namespace { // 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. @@ -228,7 +224,7 @@ void FlutterWindowWin32::OnScroll(double delta_x, ScreenToClient(GetWindowHandle(), &point); binding_handler_delegate_->OnScroll(point.x, point.y, delta_x, delta_y, - kScrollOffsetMultiplier, device_kind, + GetScrollOffsetMultiplier(), device_kind, device_id); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc index 063f51a77e..523379834f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32_unittests.cc @@ -120,8 +120,7 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32 { MOCK_METHOD4(OnPointerLeave, void(double, double, FlutterPointerDeviceKind, int32_t)); MOCK_METHOD0(OnSetCursor, void()); - MOCK_METHOD4(OnScroll, - void(double, double, FlutterPointerDeviceKind, int32_t)); + MOCK_METHOD0(GetScrollOffsetMultiplier, float()); MOCK_METHOD0(GetDpiScale, float()); MOCK_METHOD0(IsVisible, bool()); MOCK_METHOD1(UpdateCursorRect, void(const Rect&)); @@ -327,5 +326,25 @@ TEST(FlutterWindowWin32Test, OnPointerStarSendsDeviceType) { kDefaultPointerDeviceId); } +// Tests that calls to OnScroll in turn calls GetScrollOffsetMultiplier +// for mapping scroll ticks to pixels. +TEST(FlutterWindowWin32Test, OnScrollCallsGetScrollOffsetMultiplier) { + MockFlutterWindowWin32 win32window; + MockWindowBindingHandlerDelegate delegate; + win32window.SetView(&delegate); + + ON_CALL(win32window, GetScrollOffsetMultiplier()) + .WillByDefault(Return(120.0f)); + EXPECT_CALL(win32window, GetScrollOffsetMultiplier()).Times(1); + + EXPECT_CALL(delegate, + OnScroll(_, _, 0, 0, 120.0f, kFlutterPointerDeviceKindMouse, + kDefaultPointerDeviceId)) + .Times(1); + + win32window.OnScroll(0.0f, 0.0f, kFlutterPointerDeviceKindMouse, + kDefaultPointerDeviceId); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/window_win32.cc b/engine/src/flutter/shell/platform/windows/window_win32.cc index e050633461..555d9f5611 100644 --- a/engine/src/flutter/shell/platform/windows/window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/window_win32.cc @@ -49,6 +49,8 @@ char32_t CodePointFromSurrogatePair(wchar_t high, wchar_t low) { static const int kMinTouchDeviceId = 0; static const int kMaxTouchDeviceId = 128; +static const int kLinesPerScrollWindowsDefault = 3; + } // namespace WindowWin32::WindowWin32() : WindowWin32(nullptr) {} @@ -61,6 +63,12 @@ WindowWin32::WindowWin32( // supported, |current_dpi_| should be updated in the // kWmDpiChangedBeforeParent message. current_dpi_ = GetDpiForHWND(nullptr); + + // Get initial value for wheel scroll lines + // TODO: Listen to changes for this value + // https://github.com/flutter/flutter/issues/107248 + UpdateScrollOffsetMultiplier(); + if (text_input_manager_ == nullptr) { text_input_manager_ = std::make_unique(); } @@ -556,6 +564,22 @@ HWND WindowWin32::GetWindowHandle() { return window_handle_; } +float WindowWin32::GetScrollOffsetMultiplier() { + return scroll_offset_multiplier_; +} + +void WindowWin32::UpdateScrollOffsetMultiplier() { + UINT lines_per_scroll = kLinesPerScrollWindowsDefault; + + // Get lines per scroll wheel value from Windows + SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &lines_per_scroll, 0); + + // This logic is based off Chromium's implementation + // https://source.chromium.org/chromium/chromium/src/+/main:ui/events/blink/web_input_event_builders_win.cc;l=319-331 + scroll_offset_multiplier_ = + static_cast(lines_per_scroll) * 100.0 / 3.0; +} + void WindowWin32::Destroy() { if (window_handle_) { text_input_manager_->SetWindowHandle(nullptr); diff --git a/engine/src/flutter/shell/platform/windows/window_win32.h b/engine/src/flutter/shell/platform/windows/window_win32.h index cc142e6994..4e0a9f83f6 100644 --- a/engine/src/flutter/shell/platform/windows/window_win32.h +++ b/engine/src/flutter/shell/platform/windows/window_win32.h @@ -199,6 +199,9 @@ class WindowWin32 : public KeyboardManagerWin32::WindowDelegate { UINT GetCurrentHeight(); + // Returns the current pixel per scroll tick value. + virtual float GetScrollOffsetMultiplier(); + protected: // Win32's DefWindowProc. // @@ -229,10 +232,16 @@ class WindowWin32 : public KeyboardManagerWin32::WindowDelegate { // Retrieves a class instance pointer for |window| static WindowWin32* GetThisFromHandle(HWND const window) noexcept; + // Updates the cached scroll_offset_multiplier_ value based off OS settings. + void UpdateScrollOffsetMultiplier(); + int current_dpi_ = 0; int current_width_ = 0; int current_height_ = 0; + // Holds the conversion factor from lines scrolled to pixels scrolled. + float scroll_offset_multiplier_; + // WM_DPICHANGED_BEFOREPARENT defined in more recent Windows // SDK const static long kWmDpiChangedBeforeParent = 0x02E2;