123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter/gestures.dart';
- import 'package:flutter_hbb/models/platform_model.dart';
- import 'package:flutter_hbb/common.dart';
- import 'package:flutter_hbb/consts.dart';
- import 'package:flutter_hbb/models/model.dart';
- import 'package:flutter_hbb/models/input_model.dart';
- import './gestures.dart';
- class RawKeyFocusScope extends StatelessWidget {
- final FocusNode? focusNode;
- final ValueChanged<bool>? onFocusChange;
- final InputModel inputModel;
- final Widget child;
- RawKeyFocusScope({
- this.focusNode,
- this.onFocusChange,
- required this.inputModel,
- required this.child,
- });
- @override
- Widget build(BuildContext context) {
- // https://github.com/flutter/flutter/issues/154053
- final useRawKeyEvents = isLinux && !isWeb;
- // FIXME: On Windows, `AltGr` will generate `Alt` and `Control` key events,
- // while `Alt` and `Control` are seperated key events for en-US input method.
- return FocusScope(
- autofocus: true,
- child: Focus(
- autofocus: true,
- canRequestFocus: true,
- focusNode: focusNode,
- onFocusChange: onFocusChange,
- onKey: useRawKeyEvents
- ? (FocusNode data, RawKeyEvent event) =>
- inputModel.handleRawKeyEvent(event)
- : null,
- onKeyEvent: useRawKeyEvents
- ? null
- : (FocusNode node, KeyEvent event) =>
- inputModel.handleKeyEvent(event),
- child: child));
- }
- }
- class RawTouchGestureDetectorRegion extends StatefulWidget {
- final Widget child;
- final FFI ffi;
- late final InputModel inputModel = ffi.inputModel;
- late final FfiModel ffiModel = ffi.ffiModel;
- RawTouchGestureDetectorRegion({
- required this.child,
- required this.ffi,
- });
- @override
- State<RawTouchGestureDetectorRegion> createState() =>
- _RawTouchGestureDetectorRegionState();
- }
- /// touchMode only:
- /// LongPress -> right click
- /// OneFingerPan -> start/end -> left down start/end
- /// onDoubleTapDown -> move to
- /// onLongPressDown => move to
- ///
- /// mouseMode only:
- /// DoubleFiner -> right click
- /// HoldDrag -> left drag
- class _RawTouchGestureDetectorRegionState
- extends State<RawTouchGestureDetectorRegion> {
- Offset _cacheLongPressPosition = Offset(0, 0);
- // Timestamp of the last long press event.
- int _cacheLongPressPositionTs = 0;
- double _mouseScrollIntegral = 0; // mouse scroll speed controller
- double _scale = 1;
- // Workaround tap down event when two fingers are used to scale(mobile)
- TapDownDetails? _lastTapDownDetails;
- PointerDeviceKind? lastDeviceKind;
- // For touch mode, onDoubleTap
- // `onDoubleTap()` does not provide the position of the tap event.
- Offset _lastPosOfDoubleTapDown = Offset.zero;
- bool _touchModePanStarted = false;
- Offset _doubleFinerTapPosition = Offset.zero;
- FFI get ffi => widget.ffi;
- FfiModel get ffiModel => widget.ffiModel;
- InputModel get inputModel => widget.inputModel;
- bool get handleTouch => (isDesktop || isWebDesktop) || ffiModel.touchMode;
- SessionID get sessionId => ffi.sessionId;
- @override
- Widget build(BuildContext context) {
- return RawGestureDetector(
- child: widget.child,
- gestures: makeGestures(context),
- );
- }
- onTapDown(TapDownDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- _lastPosOfDoubleTapDown = d.localPosition;
- // Desktop or mobile "Touch mode"
- _lastTapDownDetails = d;
- }
- }
- onTapUp(TapUpDetails d) async {
- final TapDownDetails? lastTapDownDetails = _lastTapDownDetails;
- _lastTapDownDetails = null;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- final isMoved =
- await ffi.cursorModel.move(d.localPosition.dx, d.localPosition.dy);
- if (isMoved) {
- if (lastTapDownDetails != null) {
- await inputModel.tapDown(MouseButtons.left);
- }
- await inputModel.tapUp(MouseButtons.left);
- }
- }
- }
- onTap() async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (!handleTouch) {
- // Mobile, "Mouse mode"
- await inputModel.tap(MouseButtons.left);
- }
- }
- onDoubleTapDown(TapDownDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- _lastPosOfDoubleTapDown = d.localPosition;
- await ffi.cursorModel.move(d.localPosition.dx, d.localPosition.dy);
- }
- }
- onDoubleTap() async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (ffiModel.touchMode && ffi.cursorModel.lastIsBlocked) {
- return;
- }
- if (handleTouch &&
- !ffi.cursorModel.isInRemoteRect(_lastPosOfDoubleTapDown)) {
- return;
- }
- await inputModel.tap(MouseButtons.left);
- await inputModel.tap(MouseButtons.left);
- }
- onLongPressDown(LongPressDownDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- _lastPosOfDoubleTapDown = d.localPosition;
- _cacheLongPressPosition = d.localPosition;
- if (!ffi.cursorModel.isInRemoteRect(d.localPosition)) {
- return;
- }
- _cacheLongPressPositionTs = DateTime.now().millisecondsSinceEpoch;
- }
- }
- onLongPressUp() async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- await inputModel.tapUp(MouseButtons.left);
- }
- }
- // for mobiles
- onLongPress() async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- final isMoved = await ffi.cursorModel
- .move(_cacheLongPressPosition.dx, _cacheLongPressPosition.dy);
- if (!isMoved) {
- return;
- }
- }
- if (!ffi.ffiModel.isPeerMobile) {
- await inputModel.tap(MouseButtons.right);
- }
- }
- onDoubleFinerTapDown(TapDownDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- _doubleFinerTapPosition = d.localPosition;
- // ignore for desktop and mobile
- }
- onDoubleFinerTap(TapDownDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- // mobile mouse mode or desktop touch screen
- final isMobileMouseMode = isMobile && !ffiModel.touchMode;
- // We can't use `d.localPosition` here because it's always (0, 0) on desktop.
- final isDesktopInRemoteRect = (isDesktop || isWebDesktop) &&
- ffi.cursorModel.isInRemoteRect(_doubleFinerTapPosition);
- if (isMobileMouseMode || isDesktopInRemoteRect) {
- await inputModel.tap(MouseButtons.right);
- }
- }
- onHoldDragStart(DragStartDetails d) async {
- lastDeviceKind = d.kind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (!handleTouch) {
- await inputModel.sendMouse('down', MouseButtons.left);
- }
- }
- onHoldDragUpdate(DragUpdateDetails d) async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (!handleTouch) {
- await ffi.cursorModel.updatePan(d.delta, d.localPosition, handleTouch);
- }
- }
- onHoldDragEnd(DragEndDetails d) async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (!handleTouch) {
- await inputModel.sendMouse('up', MouseButtons.left);
- }
- }
- onOneFingerPanStart(BuildContext context, DragStartDetails d) async {
- final TapDownDetails? lastTapDownDetails = _lastTapDownDetails;
- _lastTapDownDetails = null;
- lastDeviceKind = d.kind ?? lastDeviceKind;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (handleTouch) {
- if (lastTapDownDetails != null) {
- await ffi.cursorModel.move(lastTapDownDetails.localPosition.dx,
- lastTapDownDetails.localPosition.dy);
- }
- if (ffi.cursorModel.shouldBlock(d.localPosition.dx, d.localPosition.dy)) {
- return;
- }
- if (!ffi.cursorModel.isInRemoteRect(d.localPosition)) {
- return;
- }
- _touchModePanStarted = true;
- if (isDesktop || isWebDesktop) {
- ffi.cursorModel.trySetRemoteWindowCoords();
- }
- // Workaround for the issue that the first pan event is sent a long time after the start event.
- // If the time interval between the start event and the first pan event is less than 500ms,
- // we consider to use the long press position as the start position.
- //
- // TODO: We should find a better way to send the first pan event as soon as possible.
- if (DateTime.now().millisecondsSinceEpoch - _cacheLongPressPositionTs <
- 500) {
- await ffi.cursorModel
- .move(_cacheLongPressPosition.dx, _cacheLongPressPosition.dy);
- }
- await inputModel.sendMouse('down', MouseButtons.left);
- await ffi.cursorModel.move(d.localPosition.dx, d.localPosition.dy);
- } else {
- final offset = ffi.cursorModel.offset;
- final cursorX = offset.dx;
- final cursorY = offset.dy;
- final visible =
- ffi.cursorModel.getVisibleRect().inflate(1); // extend edges
- final size = MediaQueryData.fromView(View.of(context)).size;
- if (!visible.contains(Offset(cursorX, cursorY))) {
- await ffi.cursorModel.move(size.width / 2, size.height / 2);
- }
- }
- }
- onOneFingerPanUpdate(DragUpdateDetails d) async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (ffi.cursorModel.shouldBlock(d.localPosition.dx, d.localPosition.dy)) {
- return;
- }
- if (handleTouch && !_touchModePanStarted) {
- return;
- }
- await ffi.cursorModel.updatePan(d.delta, d.localPosition, handleTouch);
- }
- onOneFingerPanEnd(DragEndDetails d) async {
- _touchModePanStarted = false;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if (isDesktop || isWebDesktop) {
- ffi.cursorModel.clearRemoteWindowCoords();
- }
- await inputModel.sendMouse('up', MouseButtons.left);
- }
- // scale + pan event
- onTwoFingerScaleStart(ScaleStartDetails d) {
- _lastTapDownDetails = null;
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- }
- onTwoFingerScaleUpdate(ScaleUpdateDetails d) async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if ((isDesktop || isWebDesktop)) {
- final scale = ((d.scale - _scale) * 1000).toInt();
- _scale = d.scale;
- if (scale != 0) {
- await bind.sessionSendPointer(
- sessionId: sessionId,
- msg: json.encode(
- PointerEventToRust(kPointerEventKindTouch, 'scale', scale)
- .toJson()));
- }
- } else {
- // mobile
- ffi.canvasModel.updateScale(d.scale / _scale, d.focalPoint);
- _scale = d.scale;
- ffi.canvasModel.panX(d.focalPointDelta.dx);
- ffi.canvasModel.panY(d.focalPointDelta.dy);
- }
- }
- onTwoFingerScaleEnd(ScaleEndDetails d) async {
- if (lastDeviceKind != PointerDeviceKind.touch) {
- return;
- }
- if ((isDesktop || isWebDesktop)) {
- await bind.sessionSendPointer(
- sessionId: sessionId,
- msg: json.encode(
- PointerEventToRust(kPointerEventKindTouch, 'scale', 0).toJson()));
- } else {
- // mobile
- _scale = 1;
- // No idea why we need to set the view style to "" here.
- // bind.sessionSetViewStyle(sessionId: sessionId, value: "");
- }
- await inputModel.sendMouse('up', MouseButtons.left);
- }
- get onHoldDragCancel => null;
- get onThreeFingerVerticalDragUpdate => ffi.ffiModel.isPeerAndroid
- ? null
- : (d) {
- _mouseScrollIntegral += d.delta.dy / 4;
- if (_mouseScrollIntegral > 1) {
- inputModel.scroll(1);
- _mouseScrollIntegral = 0;
- } else if (_mouseScrollIntegral < -1) {
- inputModel.scroll(-1);
- _mouseScrollIntegral = 0;
- }
- };
- makeGestures(BuildContext context) {
- return <Type, GestureRecognizerFactory>{
- // Official
- TapGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<TapGestureRecognizer>(
- () => TapGestureRecognizer(), (instance) {
- instance
- ..onTapDown = onTapDown
- ..onTapUp = onTapUp
- ..onTap = onTap;
- }),
- DoubleTapGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<DoubleTapGestureRecognizer>(
- () => DoubleTapGestureRecognizer(), (instance) {
- instance
- ..onDoubleTapDown = onDoubleTapDown
- ..onDoubleTap = onDoubleTap;
- }),
- LongPressGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
- () => LongPressGestureRecognizer(), (instance) {
- instance
- ..onLongPressDown = onLongPressDown
- ..onLongPressUp = onLongPressUp
- ..onLongPress = onLongPress;
- }),
- // Customized
- HoldTapMoveGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<HoldTapMoveGestureRecognizer>(
- () => HoldTapMoveGestureRecognizer(),
- (instance) => instance
- ..onHoldDragStart = onHoldDragStart
- ..onHoldDragUpdate = onHoldDragUpdate
- ..onHoldDragCancel = onHoldDragCancel
- ..onHoldDragEnd = onHoldDragEnd),
- DoubleFinerTapGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<DoubleFinerTapGestureRecognizer>(
- () => DoubleFinerTapGestureRecognizer(), (instance) {
- instance
- ..onDoubleFinerTap = onDoubleFinerTap
- ..onDoubleFinerTapDown = onDoubleFinerTapDown;
- }),
- CustomTouchGestureRecognizer:
- GestureRecognizerFactoryWithHandlers<CustomTouchGestureRecognizer>(
- () => CustomTouchGestureRecognizer(), (instance) {
- instance.onOneFingerPanStart =
- (DragStartDetails d) => onOneFingerPanStart(context, d);
- instance
- ..onOneFingerPanUpdate = onOneFingerPanUpdate
- ..onOneFingerPanEnd = onOneFingerPanEnd
- ..onTwoFingerScaleStart = onTwoFingerScaleStart
- ..onTwoFingerScaleUpdate = onTwoFingerScaleUpdate
- ..onTwoFingerScaleEnd = onTwoFingerScaleEnd
- ..onThreeFingerVerticalDragUpdate = onThreeFingerVerticalDragUpdate;
- }),
- };
- }
- }
- class RawPointerMouseRegion extends StatelessWidget {
- final InputModel inputModel;
- final Widget child;
- final MouseCursor? cursor;
- final PointerEnterEventListener? onEnter;
- final PointerExitEventListener? onExit;
- final PointerDownEventListener? onPointerDown;
- final PointerUpEventListener? onPointerUp;
- RawPointerMouseRegion({
- this.onEnter,
- this.onExit,
- this.cursor,
- this.onPointerDown,
- this.onPointerUp,
- required this.inputModel,
- required this.child,
- });
- @override
- Widget build(BuildContext context) {
- return Listener(
- onPointerHover: inputModel.onPointHoverImage,
- onPointerDown: (evt) {
- onPointerDown?.call(evt);
- inputModel.onPointDownImage(evt);
- },
- onPointerUp: (evt) {
- onPointerUp?.call(evt);
- inputModel.onPointUpImage(evt);
- },
- onPointerMove: inputModel.onPointMoveImage,
- onPointerSignal: inputModel.onPointerSignalImage,
- onPointerPanZoomStart: inputModel.onPointerPanZoomStart,
- onPointerPanZoomUpdate: inputModel.onPointerPanZoomUpdate,
- onPointerPanZoomEnd: inputModel.onPointerPanZoomEnd,
- child: MouseRegion(
- cursor: inputModel.isViewOnly
- ? MouseCursor.defer
- : (cursor ?? MouseCursor.defer),
- onEnter: onEnter,
- onExit: onExit,
- child: child,
- ),
- );
- }
- }
|