fix cursor lost control sometime && refactor some Camel-Case flutter

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2022-11-14 15:05:44 +08:00
parent 60a30042c0
commit 98bb47a81d
3 changed files with 34 additions and 22 deletions

View File

@ -42,7 +42,7 @@ class InputModel {
// mouse
final isPhysicalMouse = false.obs;
int _lastMouseDownButtons = 0;
Offset last_mouse_pos = Offset.zero;
Offset lastMousePos = Offset.zero;
get id => parent.target?.id ?? "";
@ -308,23 +308,23 @@ class InputModel {
double y = max(0.0, evt['y']);
final cursorModel = parent.target!.cursorModel;
if (cursorModel.is_peer_control_protected) {
last_mouse_pos = ui.Offset(x, y);
if (cursorModel.isPeerControlProtected) {
lastMousePos = ui.Offset(x, y);
return;
}
if (!cursorModel.got_mouse_control) {
bool self_get_control =
(x - last_mouse_pos.dx).abs() > kMouseControlDistance ||
(y - last_mouse_pos.dy).abs() > kMouseControlDistance;
if (self_get_control) {
cursorModel.got_mouse_control = true;
if (!cursorModel.gotMouseControl) {
bool selfGetControl =
(x - lastMousePos.dx).abs() > kMouseControlDistance ||
(y - lastMousePos.dy).abs() > kMouseControlDistance;
if (selfGetControl) {
cursorModel.gotMouseControl = true;
} else {
last_mouse_pos = ui.Offset(x, y);
lastMousePos = ui.Offset(x, y);
return;
}
}
last_mouse_pos = ui.Offset(x, y);
lastMousePos = ui.Offset(x, y);
var type = '';
var isMove = false;

View File

@ -753,8 +753,8 @@ class CursorModel with ChangeNotifier {
double _hoty = 0;
double _displayOriginX = 0;
double _displayOriginY = 0;
bool got_mouse_control = true;
DateTime _last_peer_mouse = DateTime.now()
bool gotMouseControl = true;
DateTime _lastPeerMouse = DateTime.now()
.subtract(Duration(milliseconds: 2 * kMouseControlTimeoutMSec));
String id = '';
WeakReference<FFI> parent;
@ -772,8 +772,8 @@ class CursorModel with ChangeNotifier {
double get hotx => _hotx;
double get hoty => _hoty;
bool get is_peer_control_protected =>
DateTime.now().difference(_last_peer_mouse).inMilliseconds <
bool get isPeerControlProtected =>
DateTime.now().difference(_lastPeerMouse).inMilliseconds <
kMouseControlTimeoutMSec;
CursorModel(this.parent) {
@ -1012,8 +1012,8 @@ class CursorModel with ChangeNotifier {
/// Update the cursor position.
updateCursorPosition(Map<String, dynamic> evt, String id) async {
got_mouse_control = false;
_last_peer_mouse = DateTime.now();
gotMouseControl = false;
_lastPeerMouse = DateTime.now();
_x = double.parse(evt['x']);
_y = double.parse(evt['y']);
try {

View File

@ -209,6 +209,7 @@ lazy_static::lazy_static! {
static EXITING: AtomicBool = AtomicBool::new(false);
const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000);
// Actual diff of (x,y) is (1,1) here. But 5 may be tolerant.
const MOUSE_ACTIVE_DISTANCE: i32 = 5;
// mac key input must be run in main thread, otherwise crash on >= osx 10.15
@ -406,14 +407,25 @@ fn active_mouse_(conn: i32) -> bool {
return false;
}
let in_actived_dist = |a: i32, b: i32| -> bool { (a - b).abs() < MOUSE_ACTIVE_DISTANCE };
// check if input is in valid range
match crate::get_cursor_pos() {
Some((x, y)) => {
let can_active = (last_input.x - x).abs() < MOUSE_ACTIVE_DISTANCE
&& (last_input.y - y).abs() < MOUSE_ACTIVE_DISTANCE;
let mut can_active =
in_actived_dist(last_input.x, x) && in_actived_dist(last_input.y, y);
if !can_active {
last_input.x = -MOUSE_ACTIVE_DISTANCE * 2;
last_input.y = -MOUSE_ACTIVE_DISTANCE * 2;
// Try agin
// No need to care about sleep here. It's not a common case.
std::thread::sleep(std::time::Duration::from_micros(10));
if let Some((x2, y2)) = crate::get_cursor_pos() {
can_active =
in_actived_dist(last_input.x, x2) && in_actived_dist(last_input.y, y2);
}
}
if !can_active {
last_input.x = INVALID_CURSOR_POS / 2;
last_input.y = INVALID_CURSOR_POS / 2;
}
can_active
}