Skip to content

Commit 2b9bbc8

Browse files
Fix non primary buttons not being captured on windows (flutter#188394)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Closes flutter#166309. Windows only captured mouse input for primary-button drags. As a result, drag gestures using middle, secondary, or X buttons received a pointer remove when leaving the client area. Capture is now enabled for every supported mouse button and released only after the last pressed mouse button is released. Also tested on my universal flutter input demo: https://github.com/CodeDoctorDE/flutter-input-demo/tree/e448c2cf42981d9c8b5186a7e43d1b82a0239619. ~~Currently in draft since I cannot have 2 pull requests open at the same time.~~ ## 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 --------- Co-authored-by: Matthew Kosarek <[email protected]>
1 parent c72bd32 commit 2b9bbc8

2 files changed

Lines changed: 90 additions & 11 deletions

File tree

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,11 @@ FlutterWindow::HandleMessage(UINT const message,
735735
break;
736736
}
737737

738-
if (message == WM_LBUTTONDOWN) {
739-
// Capture the pointer in case the user drags outside the client area.
740-
// In this case, the "mouse leave" event is delayed until the user
741-
// releases the button. It's only activated on left click given that
742-
// it's more common for apps to handle dragging with only the left
743-
// button.
744-
SetCapture(window_handle_);
745-
}
738+
// Capture the pointer in case the user drags outside the client area.
739+
// In this case, the "mouse leave" event is delayed until the user
740+
// releases the button.
741+
SetCapture(window_handle_);
742+
746743
button_pressed = message;
747744
if (message == WM_XBUTTONDOWN) {
748745
button_pressed = GET_XBUTTON_WPARAM(wparam);
@@ -764,9 +761,6 @@ FlutterWindow::HandleMessage(UINT const message,
764761
break;
765762
}
766763

767-
if (message == WM_LBUTTONUP) {
768-
ReleaseCapture();
769-
}
770764
button_pressed = message;
771765
if (message == WM_XBUTTONUP) {
772766
button_pressed = GET_XBUTTON_WPARAM(wparam);
@@ -775,6 +769,17 @@ FlutterWindow::HandleMessage(UINT const message,
775769
y_pos = GET_Y_LPARAM(lparam);
776770
flutter_button = ConvertWinButtonToFlutterButton(button_pressed);
777771

772+
// WM_*BUTTONUP messages use wparam to report which buttons remain
773+
// pressed after this event; release capture only after the last mouse
774+
// button is released. WM_*BUTTONDOWN messages already identify the newly
775+
// pressed button via the message itself.
776+
// See:
777+
// https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-lbuttonup
778+
if ((wparam & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON | MK_XBUTTON1 |
779+
MK_XBUTTON2)) == 0) {
780+
ReleaseCapture();
781+
}
782+
778783
OnPointerUp(static_cast<double>(x_pos), static_cast<double>(y_pos),
779784
device_kind, kDefaultPointerDeviceId, flutter_button);
780785
break;

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,80 @@ TEST_F(FlutterWindowTest, OnMousePointerDown) {
853853
50);
854854
}
855855

856+
TEST_F(FlutterWindowTest, NonPrimaryMouseButtonCapturesPointer) {
857+
MockFlutterWindow win32window(100, 100);
858+
MockWindowBindingHandlerDelegate delegate;
859+
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
860+
EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
861+
win32window.SetView(&delegate);
862+
863+
HWND window_handle = win32window.FlutterWindow::GetWindowHandle();
864+
ASSERT_NE(window_handle, nullptr);
865+
ReleaseCapture();
866+
ASSERT_EQ(GetCapture(), nullptr);
867+
868+
EXPECT_CALL(delegate,
869+
OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
870+
kDefaultPointerDeviceId,
871+
kFlutterPointerButtonMouseSecondary, 0, 0))
872+
.Times(1);
873+
win32window.InjectWindowMessage(WM_RBUTTONDOWN, MK_RBUTTON,
874+
MAKELPARAM(10, 10));
875+
EXPECT_EQ(GetCapture(), window_handle);
876+
877+
EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
878+
kDefaultPointerDeviceId,
879+
kFlutterPointerButtonMouseSecondary))
880+
.Times(1);
881+
win32window.InjectWindowMessage(WM_RBUTTONUP, 0, MAKELPARAM(10, 10));
882+
EXPECT_EQ(GetCapture(), nullptr);
883+
}
884+
885+
TEST_F(FlutterWindowTest, MouseButtonCaptureReleasedAfterAllButtonsReleased) {
886+
MockFlutterWindow win32window(100, 100);
887+
MockWindowBindingHandlerDelegate delegate;
888+
EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
889+
EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
890+
win32window.SetView(&delegate);
891+
892+
HWND window_handle = win32window.FlutterWindow::GetWindowHandle();
893+
ASSERT_NE(window_handle, nullptr);
894+
ReleaseCapture();
895+
ASSERT_EQ(GetCapture(), nullptr);
896+
897+
EXPECT_CALL(delegate,
898+
OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
899+
kDefaultPointerDeviceId,
900+
kFlutterPointerButtonMouseSecondary, 0, 0))
901+
.Times(1);
902+
win32window.InjectWindowMessage(WM_RBUTTONDOWN, MK_RBUTTON,
903+
MAKELPARAM(10, 10));
904+
EXPECT_EQ(GetCapture(), window_handle);
905+
906+
EXPECT_CALL(delegate,
907+
OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
908+
kDefaultPointerDeviceId,
909+
kFlutterPointerButtonMousePrimary, 0, 0))
910+
.Times(1);
911+
win32window.InjectWindowMessage(WM_LBUTTONDOWN, MK_LBUTTON | MK_RBUTTON,
912+
MAKELPARAM(10, 10));
913+
EXPECT_EQ(GetCapture(), window_handle);
914+
915+
EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
916+
kDefaultPointerDeviceId,
917+
kFlutterPointerButtonMouseSecondary))
918+
.Times(1);
919+
win32window.InjectWindowMessage(WM_RBUTTONUP, MK_LBUTTON, MAKELPARAM(10, 10));
920+
EXPECT_EQ(GetCapture(), window_handle);
921+
922+
EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
923+
kDefaultPointerDeviceId,
924+
kFlutterPointerButtonMousePrimary))
925+
.Times(1);
926+
win32window.InjectWindowMessage(WM_LBUTTONUP, 0, MAKELPARAM(10, 10));
927+
EXPECT_EQ(GetCapture(), nullptr);
928+
}
929+
856930
TEST_F(FlutterWindowTest, OnTouchPointerDown) {
857931
auto mock_proc_table = std::make_shared<MockWindowsProcTable>();
858932

0 commit comments

Comments
 (0)