Fix non primary buttons not being captured on windows (#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 #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 <matt.kosarek@canonical.com>
diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.cc b/engine/src/flutter/shell/platform/windows/flutter_window.cc
index 2b778dd..24398ee 100644
--- a/engine/src/flutter/shell/platform/windows/flutter_window.cc
+++ b/engine/src/flutter/shell/platform/windows/flutter_window.cc
@@ -735,14 +735,11 @@
         break;
       }
 
-      if (message == WM_LBUTTONDOWN) {
-        // Capture the pointer in case the user drags outside the client area.
-        // In this case, the "mouse leave" event is delayed until the user
-        // releases the button. It's only activated on left click given that
-        // it's more common for apps to handle dragging with only the left
-        // button.
-        SetCapture(window_handle_);
-      }
+      // Capture the pointer in case the user drags outside the client area.
+      // In this case, the "mouse leave" event is delayed until the user
+      // releases the button.
+      SetCapture(window_handle_);
+
       button_pressed = message;
       if (message == WM_XBUTTONDOWN) {
         button_pressed = GET_XBUTTON_WPARAM(wparam);
@@ -764,9 +761,6 @@
         break;
       }
 
-      if (message == WM_LBUTTONUP) {
-        ReleaseCapture();
-      }
       button_pressed = message;
       if (message == WM_XBUTTONUP) {
         button_pressed = GET_XBUTTON_WPARAM(wparam);
@@ -775,6 +769,17 @@
       y_pos = GET_Y_LPARAM(lparam);
       flutter_button = ConvertWinButtonToFlutterButton(button_pressed);
 
+      // WM_*BUTTONUP messages use wparam to report which buttons remain
+      // pressed after this event; release capture only after the last mouse
+      // button is released. WM_*BUTTONDOWN messages already identify the newly
+      // pressed button via the message itself.
+      // See:
+      // https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-lbuttonup
+      if ((wparam & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON | MK_XBUTTON1 |
+                     MK_XBUTTON2)) == 0) {
+        ReleaseCapture();
+      }
+
       OnPointerUp(static_cast<double>(x_pos), static_cast<double>(y_pos),
                   device_kind, kDefaultPointerDeviceId, flutter_button);
       break;
diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc
index e1fbcfe..3c3a3f0 100644
--- a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc
+++ b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc
@@ -853,6 +853,80 @@
                                       50);
 }
 
+TEST_F(FlutterWindowTest, NonPrimaryMouseButtonCapturesPointer) {
+  MockFlutterWindow win32window(100, 100);
+  MockWindowBindingHandlerDelegate delegate;
+  EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
+  EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
+  win32window.SetView(&delegate);
+
+  HWND window_handle = win32window.FlutterWindow::GetWindowHandle();
+  ASSERT_NE(window_handle, nullptr);
+  ReleaseCapture();
+  ASSERT_EQ(GetCapture(), nullptr);
+
+  EXPECT_CALL(delegate,
+              OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                            kDefaultPointerDeviceId,
+                            kFlutterPointerButtonMouseSecondary, 0, 0))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_RBUTTONDOWN, MK_RBUTTON,
+                                  MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), window_handle);
+
+  EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                                    kDefaultPointerDeviceId,
+                                    kFlutterPointerButtonMouseSecondary))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_RBUTTONUP, 0, MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), nullptr);
+}
+
+TEST_F(FlutterWindowTest, MouseButtonCaptureReleasedAfterAllButtonsReleased) {
+  MockFlutterWindow win32window(100, 100);
+  MockWindowBindingHandlerDelegate delegate;
+  EXPECT_CALL(win32window, OnWindowStateEvent).Times(AnyNumber());
+  EXPECT_CALL(delegate, OnWindowStateEvent).Times(AnyNumber());
+  win32window.SetView(&delegate);
+
+  HWND window_handle = win32window.FlutterWindow::GetWindowHandle();
+  ASSERT_NE(window_handle, nullptr);
+  ReleaseCapture();
+  ASSERT_EQ(GetCapture(), nullptr);
+
+  EXPECT_CALL(delegate,
+              OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                            kDefaultPointerDeviceId,
+                            kFlutterPointerButtonMouseSecondary, 0, 0))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_RBUTTONDOWN, MK_RBUTTON,
+                                  MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), window_handle);
+
+  EXPECT_CALL(delegate,
+              OnPointerDown(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                            kDefaultPointerDeviceId,
+                            kFlutterPointerButtonMousePrimary, 0, 0))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_LBUTTONDOWN, MK_LBUTTON | MK_RBUTTON,
+                                  MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), window_handle);
+
+  EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                                    kDefaultPointerDeviceId,
+                                    kFlutterPointerButtonMouseSecondary))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_RBUTTONUP, MK_LBUTTON, MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), window_handle);
+
+  EXPECT_CALL(delegate, OnPointerUp(10.0, 10.0, kFlutterPointerDeviceKindMouse,
+                                    kDefaultPointerDeviceId,
+                                    kFlutterPointerButtonMousePrimary))
+      .Times(1);
+  win32window.InjectWindowMessage(WM_LBUTTONUP, 0, MAKELPARAM(10, 10));
+  EXPECT_EQ(GetCapture(), nullptr);
+}
+
 TEST_F(FlutterWindowTest, OnTouchPointerDown) {
   auto mock_proc_table = std::make_shared<MockWindowsProcTable>();