Skip to content

Commit

Permalink
Refact/mobile remove adjust 4 soft keyabord (rustdesk#9787)
Browse files Browse the repository at this point in the history
* refact: remove adjust for soft keyboard

Signed-off-by: fufesou <[email protected]>

* mobile, do not set the view style after scale end

Signed-off-by: fufesou <[email protected]>

---------

Signed-off-by: fufesou <[email protected]>
  • Loading branch information
fufesou authored Oct 31, 2024
1 parent 0b8cccd commit 697dd87
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
3 changes: 2 additions & 1 deletion flutter/lib/common/widgets/remote_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
22 changes: 16 additions & 6 deletions flutter/lib/mobile/pages/remote_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ class RemotePage extends StatefulWidget {
State<RemotePage> createState() => _RemotePageState(id);
}

class _RemotePageState extends State<RemotePage> {
class _RemotePageState extends State<RemotePage> with WidgetsBindingObserver {
Timer? _timer;
bool _showBar = !isWebDesktop;
bool _showGestureHelp = false;
String _value = '';
Orientation? _currentOrientation;

Timer? _timerDidChangeMetrics;

final _blockableOverlayState = BlockableOverlayState();

final keyboardVisibilityController = KeyboardVisibilityController();
Expand Down Expand Up @@ -100,10 +102,12 @@ class _RemotePageState extends State<RemotePage> {
if (isAndroid) {
_textController.addListener(textAndroidListener);
}
WidgetsBinding.instance.addObserver(this);
}

@override
Future<void> dispose() async {
WidgetsBinding.instance.removeObserver(this);
// https://github.com/flutter/flutter/issues/64935
super.dispose();
gFFI.dialogManager.hideMobileActionsOverlay(store: false);
Expand All @@ -115,6 +119,7 @@ class _RemotePageState extends State<RemotePage> {
_physicalFocusNode.dispose();
await gFFI.close();
_timer?.cancel();
_timerDidChangeMetrics?.cancel();
gFFI.dialogManager.dismissAll();
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
overlays: SystemUiOverlay.values);
Expand All @@ -132,6 +137,14 @@ class _RemotePageState extends State<RemotePage> {
}
}

@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) {
Expand Down Expand Up @@ -968,11 +981,9 @@ class ImagePaint extends StatelessWidget {
Widget build(BuildContext context) {
final m = Provider.of<ImageModel>(context);
final c = Provider.of<CanvasModel>(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),
);
}
}
Expand All @@ -986,7 +997,6 @@ class CursorPaint extends StatelessWidget {
final m = Provider.of<CursorModel>(context);
final c = Provider.of<CanvasModel>(context);
final ffiModel = Provider.of<FfiModel>(context);
final adjust = gFFI.cursorModel.adjustForKeyboard();
final s = c.scale;
double hotx = m.hotx;
double hoty = m.hoty;
Expand Down Expand Up @@ -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),
);
}
Expand Down
37 changes: 11 additions & 26 deletions flutter/lib/models/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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();
}

Expand Down

0 comments on commit 697dd87

Please sign in to comment.