From 697dd8738346cb3c72fbc6976e9fb45dfb826a58 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:11:00 +0800 Subject: [PATCH] Refact/mobile remove adjust 4 soft keyabord (#9787) * refact: remove adjust for soft keyboard Signed-off-by: fufesou * mobile, do not set the view style after scale end Signed-off-by: fufesou --------- Signed-off-by: fufesou --- flutter/lib/common/widgets/remote_input.dart | 3 +- flutter/lib/mobile/pages/remote_page.dart | 22 ++++++++---- flutter/lib/models/model.dart | 37 ++++++-------------- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/flutter/lib/common/widgets/remote_input.dart b/flutter/lib/common/widgets/remote_input.dart index 4a82dfae326..bea1490fe5c 100644 --- a/flutter/lib/common/widgets/remote_input.dart +++ b/flutter/lib/common/widgets/remote_input.dart @@ -336,7 +336,8 @@ class _RawTouchGestureDetectorRegionState } else { // mobile _scale = 1; - bind.sessionSetViewStyle(sessionId: sessionId, value: ""); + // No idea why we need to set the view style to "" here. + // bind.sessionSetViewStyle(sessionId: sessionId, value: ""); } inputModel.sendMouse('up', MouseButtons.left); } diff --git a/flutter/lib/mobile/pages/remote_page.dart b/flutter/lib/mobile/pages/remote_page.dart index 70ebb987797..2e6004faa87 100644 --- a/flutter/lib/mobile/pages/remote_page.dart +++ b/flutter/lib/mobile/pages/remote_page.dart @@ -37,13 +37,15 @@ class RemotePage extends StatefulWidget { State createState() => _RemotePageState(id); } -class _RemotePageState extends State { +class _RemotePageState extends State with WidgetsBindingObserver { Timer? _timer; bool _showBar = !isWebDesktop; bool _showGestureHelp = false; String _value = ''; Orientation? _currentOrientation; + Timer? _timerDidChangeMetrics; + final _blockableOverlayState = BlockableOverlayState(); final keyboardVisibilityController = KeyboardVisibilityController(); @@ -100,10 +102,12 @@ class _RemotePageState extends State { if (isAndroid) { _textController.addListener(textAndroidListener); } + WidgetsBinding.instance.addObserver(this); } @override Future dispose() async { + WidgetsBinding.instance.removeObserver(this); // https://github.com/flutter/flutter/issues/64935 super.dispose(); gFFI.dialogManager.hideMobileActionsOverlay(store: false); @@ -115,6 +119,7 @@ class _RemotePageState extends State { _physicalFocusNode.dispose(); await gFFI.close(); _timer?.cancel(); + _timerDidChangeMetrics?.cancel(); gFFI.dialogManager.dismissAll(); await SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: SystemUiOverlay.values); @@ -132,6 +137,14 @@ class _RemotePageState extends State { } } + @override + void didChangeMetrics() { + _timerDidChangeMetrics?.cancel(); + _timerDidChangeMetrics = Timer(Duration(milliseconds: 100), () { + gFFI.canvasModel.updateViewStyle(refreshMousePos: false); + }); + } + // This listener is used to handle the composing region changes for Android soft keyboard input. void textAndroidListener() { if (_lastComposingChangeValid) { @@ -968,11 +981,9 @@ class ImagePaint extends StatelessWidget { Widget build(BuildContext context) { final m = Provider.of(context); final c = Provider.of(context); - final adjust = gFFI.cursorModel.adjustForKeyboard(); var s = c.scale; return CustomPaint( - painter: ImagePainter( - image: m.image, x: c.x / s, y: (c.y - adjust) / s, scale: s), + painter: ImagePainter(image: m.image, x: c.x / s, y: c.y / s, scale: s), ); } } @@ -986,7 +997,6 @@ class CursorPaint extends StatelessWidget { final m = Provider.of(context); final c = Provider.of(context); final ffiModel = Provider.of(context); - final adjust = gFFI.cursorModel.adjustForKeyboard(); final s = c.scale; double hotx = m.hotx; double hoty = m.hoty; @@ -1022,7 +1032,7 @@ class CursorPaint extends StatelessWidget { painter: ImagePainter( image: image, x: (m.x - hotx) * factor + c.x / s2, - y: (m.y - hoty) * factor + (c.y - adjust) / s2, + y: (m.y - hoty) * factor + c.y / s2, scale: s2), ); } diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index 1e7baf6739d..e55a5063606 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -1466,10 +1466,14 @@ class CanvasModel with ChangeNotifier { updateViewStyle({refreshMousePos = true}) async { Size getSize() { - final size = MediaQueryData.fromWindow(ui.window).size; + final mediaData = MediaQueryData.fromView(ui.window); + final size = mediaData.size; // If minimized, w or h may be negative here. double w = size.width - leftToEdge - rightToEdge; double h = size.height - topToEdge - bottomToEdge; + if (isMobile) { + h -= (mediaData.padding.top + mediaData.viewInsets.bottom); + } return Size(w < 0 ? 0 : w, h < 0 ? 0 : h); } @@ -1643,13 +1647,9 @@ class CanvasModel with ChangeNotifier { // (focalPoint.dx - _x_1) / s1 + displayOriginX = (focalPoint.dx - _x_2) / s2 + displayOriginX // _x_2 = focalPoint.dx - (focalPoint.dx - _x_1) / s1 * s2 _x = focalPoint.dx - (focalPoint.dx - _x) / s * _scale; - final adjustForKeyboard = - parent.target?.cursorModel.adjustForKeyboard() ?? 0.0; - // (focalPoint.dy - _y_1 + adjust) / s1 + displayOriginY = (focalPoint.dy - _y_2 + adjust) / s2 + displayOriginY - // _y_2 = focalPoint.dy + adjust - (focalPoint.dy - _y_1 + adjust) / s1 * s2 - _y = focalPoint.dy + - adjustForKeyboard - - (focalPoint.dy - _y + adjustForKeyboard) / s * _scale; + // (focalPoint.dy - _y_1) / s1 + displayOriginY = (focalPoint.dy - _y_2) / s2 + displayOriginY + // _y_2 = focalPoint.dy - (focalPoint.dy - _y_1) / s1 * s2 + _y = focalPoint.dy - (focalPoint.dy - _y) / s * _scale; notifyListeners(); } @@ -1883,7 +1883,6 @@ class CursorModel with ChangeNotifier { // `lastIsBlocked` is only used in common/widgets/remote_input.dart -> _RawTouchGestureDetectorRegionState -> onDoubleTap() // Because onDoubleTap() doesn't have the `event` parameter, we can't get the touch event's position. bool _lastIsBlocked = false; - double _yForKeyboardAdjust = 0; keyHelpToolsVisibilityChanged(Rect? r) { _keyHelpToolsRect = r; @@ -1895,7 +1894,6 @@ class CursorModel with ChangeNotifier { // `lastIsBlocked` will be set when the cursor is moving or touch somewhere else. _lastIsBlocked = true; } - _yForKeyboardAdjust = _y; } get lastIsBlocked => _lastIsBlocked; @@ -1947,19 +1945,6 @@ class CursorModel with ChangeNotifier { get keyboardHeight => MediaQueryData.fromWindow(ui.window).viewInsets.bottom; get scale => parent.target?.canvasModel.scale ?? 1.0; - double adjustForKeyboard() { - if (keyboardHeight < 100) { - return 0.0; - } - - final m = MediaQueryData.fromWindow(ui.window); - final size = m.size; - final thresh = (size.height - keyboardHeight) / 2; - final h = (_yForKeyboardAdjust - getVisibleRect().top) * - scale; // local physical display height - return h - thresh; - } - // mobile Soft keyboard, block touch event from the KeyHelpTools shouldBlock(double x, double y) { if (!(parent.target?.ffiModel.touchMode ?? false)) { @@ -1980,16 +1965,16 @@ class CursorModel with ChangeNotifier { return false; } _lastIsBlocked = false; - moveLocal(x, y, adjust: adjustForKeyboard()); + moveLocal(x, y); parent.target?.inputModel.moveMouse(_x, _y); return true; } - moveLocal(double x, double y, {double adjust = 0}) { + moveLocal(double x, double y) { final xoffset = parent.target?.canvasModel.x ?? 0; final yoffset = parent.target?.canvasModel.y ?? 0; _x = (x - xoffset) / scale + _displayOriginX; - _y = (y - yoffset + adjust) / scale + _displayOriginY; + _y = (y - yoffset) / scale + _displayOriginY; notifyListeners(); }