Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 7830179

Browse files
Track mouse leave
1 parent 70f21b0 commit 7830179

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

shell/platform/windows/win32_flutter_window.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ void Win32FlutterWindow::OnPointerUp(double x, double y) {
125125
}
126126
}
127127

128+
void Win32FlutterWindow::OnPointerLeave() {
129+
if (process_events_) {
130+
SendPointerLeave();
131+
}
132+
}
133+
128134
void Win32FlutterWindow::OnChar(char32_t code_point) {
129135
if (process_events_) {
130136
SendChar(code_point);
@@ -207,6 +213,12 @@ void Win32FlutterWindow::SendPointerUp(double x, double y) {
207213
SendPointerEventWithData(event);
208214
}
209215

216+
void Win32FlutterWindow::SendPointerLeave() {
217+
FlutterPointerEvent event = {};
218+
event.phase = FlutterPointerPhase::kRemove;
219+
SendPointerEventWithData(event);
220+
}
221+
210222
void Win32FlutterWindow::SendChar(char32_t code_point) {
211223
for (const auto& handler : keyboard_hook_handlers_) {
212224
handler->CharHook(this, code_point);

shell/platform/windows/win32_flutter_window.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ class Win32FlutterWindow : public Win32Window {
5353
// |Win32Window|
5454
void OnPointerUp(double x, double y) override;
5555

56+
// |Win32Window|
57+
void OnPointerLeave() override;
58+
5659
// |Win32Window|
5760
void OnChar(char32_t code_point) override;
5861

@@ -102,6 +105,9 @@ class Win32FlutterWindow : public Win32Window {
102105
// Reports mouse release to Flutter engine.
103106
void SendPointerUp(double x, double y);
104107

108+
// Reports mouse left the window client area.
109+
void SendPointerLeave();
110+
105111
// Reports a keyboard character to Flutter engine.
106112
void SendChar(char32_t code_point);
107113

shell/platform/windows/win32_window.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Win32Window::MessageHandler(HWND hwnd,
107107
UINT width = 0, height = 0;
108108
auto window =
109109
reinterpret_cast<Win32Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
110+
static bool tracking_mouse_events = false;
110111

111112
if (window != nullptr) {
112113
switch (message) {
@@ -131,12 +132,28 @@ Win32Window::MessageHandler(HWND hwnd,
131132
break;
132133

133134
case WM_MOUSEMOVE:
135+
if (!tracking_mouse_events) {
136+
TRACKMOUSEEVENT tme;
137+
tme.cbSize = sizeof(tme);
138+
tme.hwndTrack = hwnd;
139+
// Not tracking Hover since the engine handles that logic.
140+
tme.dwFlags = TME_LEAVE;
141+
TrackMouseEvent(&tme);
142+
tracking_mouse_events = true;
143+
}
134144
xPos = GET_X_LPARAM(lparam);
135145
yPos = GET_Y_LPARAM(lparam);
136146

137147
window->OnPointerMove(static_cast<double>(xPos),
138148
static_cast<double>(yPos));
139149
break;
150+
case WM_MOUSELEAVE:;
151+
window->OnPointerLeave();
152+
// Once the tracked event is received, the TrackMouseEvent function
153+
// resets. Set to false to make sure it's called once mouse movement is
154+
// detected again.
155+
tracking_mouse_events = false;
156+
break;
140157
case WM_LBUTTONDOWN:
141158
xPos = GET_X_LPARAM(lparam);
142159
yPos = GET_Y_LPARAM(lparam);

shell/platform/windows/win32_window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class Win32Window {
8888
// down to up
8989
virtual void OnPointerUp(double x, double y) = 0;
9090

91+
// Called when the mouse leaves the window.
92+
virtual void OnPointerLeave() = 0;
93+
9194
// Called when character input occurs.
9295
virtual void OnChar(char32_t code_point) = 0;
9396

0 commit comments

Comments
 (0)