Skip to content

Commit 5a238f5

Browse files
authored
[Win32] Ignore mouse move events without capture when dragging (flutter#190029)
Fixes flutter#189583 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 89d96ef commit 5a238f5

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎engine/src/flutter/shell/platform/windows/flutter_window.cc‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,22 @@ FlutterWindow::HandleMessage(UINT const message,
692692
if (wparam & MK_SHIFT) {
693693
mods |= kShift;
694694
}
695+
696+
// Mouse move with mouse buttons pressed but current HWND does not have
697+
// capture. This can happen when switching windows mid drag - windows
698+
// will stop honoring current capture and starts sending WM_MOUSEMOVE
699+
// to the HWND below cursor, but without sending a WM_(LRMX)BUTTONDOWN
700+
// first. This would confuse pointer tracking in Flutter so it is better
701+
// to ignore these events. This also matches behavior of other
702+
// applications where drag like this is ignored despite the mouse
703+
// capture being lost. https://github.com/flutter/flutter/issues/189583
704+
auto buttons = ConvertWinMouseStateToFlutterButtons(wparam);
705+
if (buttons != 0 && GetCapture() != window_handle_) {
706+
break;
707+
}
708+
695709
OnPointerMove(mouse_x_, mouse_y_, device_kind, kDefaultPointerDeviceId,
696-
ConvertWinMouseStateToFlutterButtons(wparam),
697-
/*rotation=*/0, /*pressure=*/0, mods);
710+
buttons, /*rotation=*/0, /*pressure=*/0, mods);
698711
}
699712
break;
700713
case WM_MOUSELEAVE:

‎engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,42 @@ TEST_F(FlutterWindowTest, MouseButtonCaptureReleasedAfterAllButtonsReleased) {
927927
EXPECT_EQ(GetCapture(), nullptr);
928928
}
929929

930+
TEST_F(FlutterWindowTest, MouseMoveWithButtonIgnoredWithoutCapture) {
931+
MockFlutterWindow win32window(100, 100);
932+
MockWindowBindingHandlerDelegate delegate;
933+
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
934+
EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
935+
win32window.SetView(&delegate);
936+
937+
ReleaseCapture();
938+
ASSERT_EQ(GetCapture(), nullptr);
939+
940+
EXPECT_CALL(delegate, OnPointerMove).Times(0);
941+
win32window.InjectWindowMessage(WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(10, 10));
942+
}
943+
944+
TEST_F(FlutterWindowTest, MouseMoveWithButtonDispatchedWithCapture) {
945+
MockFlutterWindow win32window(100, 100);
946+
MockWindowBindingHandlerDelegate delegate;
947+
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
948+
EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
949+
win32window.SetView(&delegate);
950+
951+
HWND window_handle = win32window.FlutterWindow::GetWindowHandle();
952+
ASSERT_NE(window_handle, nullptr);
953+
SetCapture(window_handle);
954+
ASSERT_EQ(GetCapture(), window_handle);
955+
956+
EXPECT_CALL(delegate,
957+
OnPointerMove(10.0, 10.0, kFlutterPointerDeviceKindMouse,
958+
kDefaultPointerDeviceId,
959+
kFlutterPointerButtonMousePrimary, 0, 0, 0))
960+
.Times(1);
961+
win32window.InjectWindowMessage(WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(10, 10));
962+
963+
ReleaseCapture();
964+
}
965+
930966
TEST_F(FlutterWindowTest, OnTouchPointerDown) {
931967
auto mock_proc_table = std::make_shared<MockWindowsProcTable>();
932968

0 commit comments

Comments
 (0)