mouse works

This commit is contained in:
open-trade 2022-02-03 17:19:25 +08:00
parent 47ca02e239
commit 73da1583da
3 changed files with 81 additions and 15 deletions

View File

@ -367,7 +367,6 @@ class CursorModel with ChangeNotifier {
void touch(double x, double y, bool right) {
final scale = FFI.canvasModel.scale;
print(scale);
final xoffset = FFI.canvasModel.x;
final yoffset = FFI.canvasModel.y;
_x = (x - xoffset) / scale + _displayOriginX;
@ -633,8 +632,10 @@ class FFI {
}
static void close() {
savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);
if (FFI.imageModel.image != null && !isDesktop) {
savePreference(id, cursorModel.x, cursorModel.y, canvasModel.x,
canvasModel.y, canvasModel.scale, ffiModel.pi.currentDisplay);
}
id = "";
setByName('close', '');
imageModel.update(null);
@ -657,7 +658,54 @@ class FFI {
}
static handleMouse(Map<String, dynamic> evt) {
//
var type = '';
switch (evt['type']) {
case 'mousedown':
type = 'down';
break;
case 'mouseup':
type = 'up';
break;
case 'mousemove':
break;
default:
return;
}
evt['type'] = type;
var x = evt['x'];
var y = evt['y'];
final d = FFI.ffiModel.display;
x -= FFI.canvasModel.x;
y -= FFI.canvasModel.y;
if (x < 0 || x > d.width || y < 0 || y > d.height) {
return;
}
x += d.x;
y += d.y;
if (type != '') {
x = 0;
y = 0;
}
evt['x'] = '$x';
evt['y'] = '$y';
var buttons = '';
switch (evt['buttons']) {
case 1:
buttons = 'left';
break;
case 2:
buttons = 'right';
break;
case 4:
buttons = 'wheel';
break;
}
evt['buttons'] = buttons;
if (evt['ctrl'] != true) evt.remove('ctrl');
if (evt['shift'] != true) evt.remove('shift');
if (evt['alt'] != true) evt.remove('alt');
if (evt['command'] != true) evt.remove('command');
setByName('send_mouse', json.encode(evt));
}
static listenToMouse(bool yesOrNo) {
@ -713,6 +761,7 @@ void savePreference(String id, double xCursor, double yCursor, double xCanvas,
}
Future<Map<String, dynamic>> getPreference(String id) async {
if (!isDesktop) return null;
SharedPreferences prefs = await SharedPreferences.getInstance();
var p = prefs.getString('peer' + id);
if (p == null) return null;

View File

@ -438,7 +438,6 @@ class _RemotePageState extends State<RemotePage> {
}
Widget getBodyForDesktopWithListener() {
print(FFI.ffiModel.display.width);
return MouseRegion(
onEnter: (event) {
print('enter');

View File

@ -4,7 +4,8 @@ import 'common.dart';
import 'dart:html';
import 'dart:async';
final List<StreamSubscription<MouseEvent>> mouselisteners = [];
final List<StreamSubscription<MouseEvent>> mouseListeners = [];
int lastMouseDownButtons = 0;
class PlatformFFI {
static void clearRgbaFrame() {}
@ -26,7 +27,6 @@ class PlatformFFI {
}
static Future<Null> init() async {
window.document.onContextMenu.listen((evt) => evt.preventDefault());
isWeb = true;
isDesktop = !js.context.callMethod('isMobile');
js.context.callMethod('init');
@ -35,37 +35,55 @@ class PlatformFFI {
// MouseRegion onHover not work for mouse move when right button down
static void startDesktopWebListener(
Function(Map<String, dynamic>) handleMouse) {
lastMouseDownButtons = 0;
// document.body.getElementsByTagName('flt-glass-pane')[0].style.cursor = 'none';
mouselisteners.add(window.document.onMouseMove
mouseListeners.add(window.document.onMouseMove
.listen((evt) => handleMouse(getEvent(evt))));
mouselisteners.add(window.document.onMouseDown
mouseListeners.add(window.document.onMouseDown
.listen((evt) => handleMouse(getEvent(evt))));
mouselisteners.add(
mouseListeners.add(
window.document.onMouseUp.listen((evt) => handleMouse(getEvent(evt))));
mouselisteners.add(window.document.onMouseWheel.listen((evt) => {}));
mouseListeners.add(window.document.onMouseWheel.listen((evt) {
var dx = evt.deltaX;
var dy = evt.deltaY;
if (dx > 0)
dx = -1;
else if (dx < 0) dx = 1;
if (dy > 0)
dy = -1;
else if (dy < 0) dy = 1;
setByName('send_mouse', '{"type": "wheel", "x": "$dx", "y": "$dy"}');
}));
mouseListeners.add(
window.document.onContextMenu.listen((evt) => evt.preventDefault()));
}
static void stopDesktopWebListener() {
mouselisteners.forEach((l) {
mouseListeners.forEach((l) {
l.cancel();
});
mouselisteners.clear();
mouseListeners.clear();
}
}
Map<String, dynamic> getEvent(MouseEvent evt) {
// https://github.com/novnc/noVNC/blob/679b45fa3b453c7cf32f4b4455f4814818ecf161/core/rfb.js
// https://developer.mozilla.org/zh-CN/docs/Web/API/Element/mousedown_event
final out = {};
final Map<String, dynamic> out = {};
out['type'] = evt.type;
out['x'] = evt.client.x;
out['y'] = evt.client.y;
out['ctrl'] = evt.ctrlKey;
out['shift'] = evt.shiftKey;
out['alt'] = evt.altKey;
out['meta'] = evt.metaKey;
out['command'] = evt.metaKey;
out['buttons'] = evt
.buttons; // left button: 1, right button: 2, middle button: 4, 1 | 2 = 3 (left + right)
if (evt.buttons != 0) {
lastMouseDownButtons = evt.buttons;
} else {
out['buttons'] = lastMouseDownButtons;
}
return out;
}