Skip to content

Commit b62b39f

Browse files
Gustl22mdebbarflutter-zl
authored
fix: Apply reversed axis flip by macOS on Web when scroll with modifier keys (flutter#189506)
MacOS is flipping the vertical scroll (y) to a horizontal scroll (x), when the "Shift" modifier key is pressed. To conform with other platforms, a reverse flip was already applied for native macOS, see: flutter@be9ae47 The same must be done for web platforms, as the event is received by the browser, but the flipping by OSX happens before. Fixes flutter#159868 See also flutter#185146 ## 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: Mouad Debbar <[email protected]> Co-authored-by: zhongliugo <[email protected]>
1 parent b138043 commit b62b39f

4 files changed

Lines changed: 117 additions & 4 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/browser_detection.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ bool get isWasm => ui_web.browser.isWasm;
3737
// Whether the detected `operatingSystem` is `OperatingSystem.iOs`.
3838
bool get _isIOS => ui_web.browser.operatingSystem == ui_web.OperatingSystem.iOs;
3939

40+
/// Returns true if the browser is on macOS, false otherwise.
41+
bool get isMacOS => ui_web.browser.operatingSystem == ui_web.OperatingSystem.macOs;
42+
4043
/// Whether the browser is running on macOS or iOS.
4144
///
4245
/// - See [operatingSystem].
4346
/// - See [OperatingSystem].
44-
bool get isMacOrIOS => _isIOS || ui_web.browser.operatingSystem == ui_web.OperatingSystem.macOs;
47+
bool get isMacOrIOS => _isIOS || isMacOS;
4548

4649
/// Detect iOS 15.
4750
bool get isIOS15 => debugIsIOS15 ?? _isIOS && ui_web.browser.userAgent.contains('OS 15_');

‎engine/src/flutter/lib/web_ui/lib/src/engine/pointer_binding.dart‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import 'package:ui/ui.dart' as ui;
1313
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
1414

1515
import '../engine.dart' show registerHotRestartListener;
16-
import 'browser_detection.dart' show isIosSafari;
16+
import 'browser_detection.dart' show isIosSafari, isMacOS;
1717
import 'dom.dart';
1818
import 'platform_dispatcher.dart';
1919
import 'pointer_binding/event_position_helper.dart';
@@ -743,6 +743,24 @@ mixin _WheelEventListenerMixin on _BaseAdapter {
743743
// to pixels.
744744
double deltaX = event.deltaX;
745745
double deltaY = event.deltaY;
746+
747+
// When mouse input is received while shift is pressed (regardless of
748+
// any other pressed keys), Mac automatically flips the axis. Other
749+
// platforms do not do this, so we flip it back to normalize the input
750+
// received by the framework. The keyboard+mouse-scroll mechanism is exposed
751+
// in the ScrollBehavior of the framework so developers can customize the
752+
// behavior.
753+
// At time of change, Apple does not expose any other type of API or signal
754+
// that the X/Y axes have been flipped.
755+
//
756+
// The same conversion happens on the native side, see:
757+
// https://github.com/flutter/flutter/commit/be9ae4793d1e6aff6ad108200c4319fb1c82db96
758+
if (isMacOS && kind == ui.PointerDeviceKind.mouse && event.getModifierState('Shift')) {
759+
final temp = deltaX;
760+
deltaX = deltaY;
761+
deltaY = temp;
762+
}
763+
746764
switch (event.deltaMode.toInt()) {
747765
case domDeltaLine:
748766
_defaultScrollLineHeight ??= _computeDefaultScrollLineHeight();
@@ -752,7 +770,7 @@ mixin _WheelEventListenerMixin on _BaseAdapter {
752770
deltaX *= _view.physicalSize.width;
753771
deltaY *= _view.physicalSize.height;
754772
case domDeltaPixel:
755-
if (ui_web.browser.operatingSystem == ui_web.OperatingSystem.macOs) {
773+
if (isMacOS) {
756774
// Safari and Firefox seem to report delta in logical pixels while
757775
// Chrome uses physical pixels.
758776
deltaX *= _view.devicePixelRatio;
@@ -765,7 +783,7 @@ mixin _WheelEventListenerMixin on _BaseAdapter {
765783
final data = <ui.PointerData>[];
766784
final ui.Offset offset = computeEventOffsetToTarget(event, _view);
767785
var ignoreCtrlKey = false;
768-
if (ui_web.browser.operatingSystem == ui_web.OperatingSystem.macOs) {
786+
if (isMacOS) {
769787
ignoreCtrlKey =
770788
(_keyboardConverter?.keyIsPressed(kPhysicalControlLeft) ?? false) ||
771789
(_keyboardConverter?.keyIsPressed(kPhysicalControlRight) ?? false);

‎engine/src/flutter/lib/web_ui/test/engine/pointer_binding_test.dart‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,64 @@ void testMain() {
13591359
ui_web.browser.debugOperatingSystemOverride = null;
13601360
});
13611361

1362+
test('does reverse flip of scroll axis when "shift" key is pressed (macOS)', () {
1363+
final _ButtonedEventMixin context = _PointerEventContext();
1364+
final packets = <ui.PointerDataPacket>[];
1365+
ui.PlatformDispatcher.instance.onPointerDataPacket = (ui.PointerDataPacket packet) {
1366+
packets.add(packet);
1367+
};
1368+
1369+
ui_web.browser.debugOperatingSystemOverride = ui_web.OperatingSystem.macOs;
1370+
1371+
// Without Shift: axes are not flipped.
1372+
rootElement.dispatchEvent(
1373+
context.wheel(buttons: 0, clientX: 10, clientY: 10, deltaX: 200, deltaY: 100),
1374+
);
1375+
1376+
// With Shift pressed: macOS flips the axis, so we flip it back to normalize.
1377+
rootElement.dispatchEvent(
1378+
context.wheel(buttons: 0, clientX: 10, clientY: 10, deltaX: 210, deltaY: 110, shiftKey: true),
1379+
);
1380+
1381+
// After Shift is released: axes are not flipped again.
1382+
rootElement.dispatchEvent(
1383+
context.wheel(buttons: 0, clientX: 10, clientY: 10, deltaX: 220, deltaY: 120),
1384+
);
1385+
1386+
expect(packets, hasLength(3));
1387+
1388+
// An add will be synthesized.
1389+
expect(packets[0].data, hasLength(2));
1390+
expect(packets[0].data[0].change, equals(ui.PointerChange.add));
1391+
expect(packets[0].data[0].synthesized, isTrue);
1392+
// No Shift: scroll axes are not flipped.
1393+
expect(packets[0].data[1].change, equals(ui.PointerChange.hover));
1394+
expect(packets[0].data[1].signalKind, equals(ui.PointerSignalKind.scroll));
1395+
expect(packets[0].data[1].scrollDeltaX, equals(200.0));
1396+
expect(packets[0].data[1].scrollDeltaY, equals(100.0));
1397+
1398+
// Shift held: deltaX and deltaY are swapped.
1399+
expect(packets[1].data, hasLength(1));
1400+
expect(packets[1].data[0].change, equals(ui.PointerChange.hover));
1401+
expect(packets[1].data[0].signalKind, equals(ui.PointerSignalKind.scroll));
1402+
expect(packets[1].data[0].scrollDeltaX, equals(110.0));
1403+
// Actually "scrollDeltaY" is the important thing to test:
1404+
// - User scrolls on the y axis while pressing "Shift".
1405+
// - MacOS converts / flips it to "deltaX", when pressing "Shift".
1406+
// - The Web engine reverts this flip to "scrollDeltaY".
1407+
// - Flutter framework flips it again (for all platforms) depending on the modifier key (default: "Shift") to "deltaX" again.
1408+
expect(packets[1].data[0].scrollDeltaY, equals(210.0));
1409+
1410+
// Shift released: axes are not flipped.
1411+
expect(packets[2].data, hasLength(1));
1412+
expect(packets[2].data[0].change, equals(ui.PointerChange.hover));
1413+
expect(packets[2].data[0].signalKind, equals(ui.PointerSignalKind.scroll));
1414+
expect(packets[2].data[0].scrollDeltaX, equals(220.0));
1415+
expect(packets[2].data[0].scrollDeltaY, equals(120.0));
1416+
1417+
ui_web.browser.debugOperatingSystemOverride = null;
1418+
});
1419+
13621420
test('does calculate delta and pointer identifier correctly', () {
13631421
final _ButtonedEventMixin context = _PointerEventContext();
13641422
final packets = <ui.PointerDataPacket>[];
@@ -3623,6 +3681,35 @@ mixin _ButtonedEventMixin on _BasicEventContext {
36233681
return mouseUp(button: 0, clientX: clientX, clientY: clientY);
36243682
}
36253683

3684+
/// Creates a synthetic DOM `WheelEvent` for testing. It inherits from DOM
3685+
/// `MouseEvent` and `Event` classes.
3686+
/// See MDN: https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
3687+
///
3688+
/// [buttons] is a bitmask of pressed mouse buttons (0 = none, 1 = primary,
3689+
/// 2 = secondary). See MDN: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
3690+
///
3691+
/// [clientX] and [clientY] are the pointer position in CSS pixels relative
3692+
/// to the viewport. See MDN: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/clientX
3693+
///
3694+
/// [deltaX] and [deltaY] are the horizontal and vertical scroll distances in
3695+
/// pixels (positive = right/down). See MDN: https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent/deltaX
3696+
///
3697+
/// [wheelDeltaX] and [wheelDeltaY] are the legacy equivalents of [deltaX]
3698+
/// and [deltaY] with opposite sign, only available in Chromium-based browsers
3699+
/// (absent in Firefox).
3700+
///
3701+
/// [timeStamp] overrides the browser-assigned event time in milliseconds.
3702+
/// It must be set via a property descriptor because it cannot be passed
3703+
/// through the constructor. See MDN: https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp
3704+
///
3705+
/// [ctrlKey] signals a pinch-to-zoom gesture. The browser sets this even when
3706+
/// "Ctrl" is not physically held. Flutter uses it to emit
3707+
/// [ui.PointerSignalKind.scale] instead of [ui.PointerSignalKind.scroll],
3708+
/// unless the physical "Ctrl" key is also down.
3709+
/// See MDN: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/ctrlKey
3710+
///
3711+
/// [shiftKey] emulates a left or right "Shift" key press.
3712+
/// See MDN: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/shiftKey
36263713
DomEvent wheel({
36273714
required int? buttons,
36283715
required double? clientX,
@@ -3633,6 +3720,7 @@ mixin _ButtonedEventMixin on _BasicEventContext {
36333720
double? wheelDeltaY,
36343721
int? timeStamp,
36353722
bool ctrlKey = false,
3723+
bool shiftKey = false,
36363724
}) {
36373725
final DomEvent event = createDomWheelEvent('wheel', <String, Object>{
36383726
'buttons': ?buttons,
@@ -3643,6 +3731,7 @@ mixin _ButtonedEventMixin on _BasicEventContext {
36433731
'wheelDeltaX': ?wheelDeltaX,
36443732
'wheelDeltaY': ?wheelDeltaY,
36453733
'ctrlKey': ctrlKey,
3734+
'shiftKey': shiftKey,
36463735
'cancelable': true,
36473736
'bubbles': true,
36483737
'composed': true,

‎engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ - (void)dispatchMouseEvent:(NSEvent*)event phase:(FlutterPointerPhase)phase {
750750
// behavior.
751751
// At time of change, Apple does not expose any other type of API or signal
752752
// that the X/Y axes have been flipped.
753+
//
754+
// The same conversion happens on Web for macOS, see:
755+
// https://github.com/flutter/flutter/issues/159868
753756
double scaledDeltaX = -event.scrollingDeltaX * pixelsPerLine * scaleFactor;
754757
double scaledDeltaY = -event.scrollingDeltaY * pixelsPerLine * scaleFactor;
755758
if (event.modifierFlags & NSShiftKeyMask) {

0 commit comments

Comments
 (0)